Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit d019a7c

Browse files
committed
#89 Fixing bug where forests weren't created properly on a multi-node cluster when the desired count per host was increased
1 parent 7cadcac commit d019a7c

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/main/java/com/marklogic/appdeployer/command/forests/DeployForestsCommand.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.marklogic.appdeployer.command.forests;
22

33
import java.io.File;
4+
import java.util.ArrayList;
45
import java.util.List;
56

7+
import com.marklogic.appdeployer.command.Command;
8+
import com.marklogic.mgmt.databases.DatabaseManager;
69
import org.springframework.util.StringUtils;
710

811
import com.marklogic.appdeployer.AppConfig;
@@ -68,23 +71,33 @@ public void execute(CommandContext context) {
6871
protected void createForests(String originalPayload, CommandContext context) {
6972
ForestManager mgr = new ForestManager(context.getManageClient());
7073
AppConfig appConfig = context.getAppConfig();
74+
75+
// Find out which hosts to create forests on
7176
List<String> hostNames = new HostManager(context.getManageClient()).getHostNames();
72-
int size = hostNames.size();
7377
if (!createForestsOnEachHost) {
74-
logger.info(format("Only creating forests on the first host: " + hostNames.get(0)));
75-
size = 1;
78+
String first = hostNames.get(0);
79+
logger.info(format("Only creating forests on the first host: " + first));
80+
hostNames = new ArrayList<>();
81+
hostNames.add(first);
7682
}
77-
for (int i = 0; i < size; i++) {
78-
String hostName = hostNames.get(i);
79-
logger.info(format("Creating forests on host %s", hostName));
80-
int startNumber = (i * forestsPerHost) + 1;
81-
int endNumber = (i + 1) * forestsPerHost;
82-
for (int j = startNumber; j <= endNumber; j++) {
83-
String payload = tokenReplacer.replaceTokens(originalPayload, appConfig, false);
84-
payload = payload.replace("%%FOREST_HOST%%", hostName);
85-
payload = payload.replace("%%FOREST_NAME%%", getForestName(appConfig, j));
86-
payload = payload.replace("%%FOREST_DATABASE%%", getForestDatabaseName(appConfig));
87-
mgr.save(payload);
83+
84+
// Find out how many forests exist already
85+
int countOfExistingForests = new DatabaseManager(context.getManageClient()).getPrimaryForestIds(getForestDatabaseName(appConfig)).size();
86+
int desiredNumberOfForests = hostNames.size() * forestsPerHost;
87+
88+
// Loop over the number of forests to create, starting with count + 1, and iterating over the hosts
89+
for (int i = countOfExistingForests + 1; i <= desiredNumberOfForests;) {
90+
for (String hostName : hostNames) {
91+
if (i <= desiredNumberOfForests) {
92+
String payload = tokenReplacer.replaceTokens(originalPayload, appConfig, false);
93+
payload = payload.replace("%%FOREST_HOST%%", hostName);
94+
String forestName = getForestName(appConfig, i);
95+
payload = payload.replace("%%FOREST_NAME%%", forestName);
96+
payload = payload.replace("%%FOREST_DATABASE%%", getForestDatabaseName(appConfig));
97+
logger.info(format("Creating forest %s on host %s", forestName, hostName));
98+
mgr.save(payload);
99+
}
100+
i++;
88101
}
89102
}
90103
}

src/main/java/com/marklogic/mgmt/databases/DatabaseManager.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,11 @@ public List<String> getForestNames(String databaseNameOrId) {
5252

5353
/**
5454
* @param databaseNameOrId
55-
* @return the IDs of all primary forests related to the database. A primary forest is any forest that has replica
56-
* forests configured for it.
55+
* @return the IDs of all primary forests related to the database. The properties endpoint for a database lists
56+
* primary forest IDs, but not replica forest IDs.
5757
*/
5858
public List<String> getPrimaryForestIds(String databaseNameOrId) {
59-
List<String> primaryForestIds = new ArrayList<String>();
60-
ForestManager mgr = new ForestManager(getManageClient());
61-
for (String forestId : getForestIds(databaseNameOrId)) {
62-
if (!mgr.getReplicaIds(forestId).isEmpty()) {
63-
primaryForestIds.add(forestId);
64-
}
65-
}
66-
return primaryForestIds;
59+
return getPropertiesAsXml(databaseNameOrId).getElementValues("/node()/m:forests/m:forest");
6760
}
6861

6962
public void deleteReplicaForests(String databaseNameOrId) {

0 commit comments

Comments
 (0)