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

Commit 286b143

Browse files
authored
Merge pull request #484 from marklogic/feature/565-replicas-cma
DEVEXP-565 Using CMA to create forest replicas
2 parents 610ef34 + feb16cb commit 286b143

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.marklogic.appdeployer.command.CommandContext;
2020
import com.marklogic.appdeployer.command.SortOrderConstants;
2121
import com.marklogic.mgmt.api.API;
22+
import com.marklogic.mgmt.api.configuration.Configuration;
23+
import com.marklogic.mgmt.api.configuration.Configurations;
2224
import com.marklogic.mgmt.api.forest.Forest;
2325
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
2426
import com.marklogic.mgmt.mapper.ResourceMapper;
@@ -28,7 +30,11 @@
2830
import com.marklogic.mgmt.resource.groups.GroupManager;
2931
import com.marklogic.mgmt.resource.hosts.HostManager;
3032

31-
import java.util.*;
33+
import java.util.ArrayList;
34+
import java.util.HashMap;
35+
import java.util.List;
36+
import java.util.Map;
37+
import java.util.stream.Collectors;
3238

3339
/**
3440
* Command for configuring - i.e. creating and setting - replica forests for existing databases.
@@ -131,19 +137,37 @@ protected void configureDatabaseReplicaForests(String databaseName, int replicaC
131137
List<String> dataDirectories = forestBuilder.determineDataDirectories(databaseName, context.getAppConfig());
132138
forestBuilder.addReplicasToForests(forestsNeedingReplicas, forestPlan, context.getAppConfig(), dataDirectories);
133139

134-
// TODO Use CMA here in the future? Need to test to see if a forest name + replicas are allowable
135-
ForestManager forestManager = new ForestManager(context.getManageClient());
136-
for (Forest forest : forestsNeedingReplicas) {
137-
final String forestName = forest.getForestName();
138-
140+
List<Forest> forestsWithOnlyReplicas = forestsNeedingReplicas.stream().map(forest -> {
139141
Forest forestWithOnlyReplicas = new Forest();
142+
forestWithOnlyReplicas.setForestName(forest.getForestName());
140143
forestWithOnlyReplicas.setForestReplica(forest.getForestReplica());
141-
String json = forestWithOnlyReplicas.getJson();
144+
return forestWithOnlyReplicas;
145+
}).collect(Collectors.toList());
146+
147+
// As of 4.5.3, try CMA first so that this can be done in a single request instead of one request per forest.
148+
if (context.getAppConfig().getCmaConfig().isDeployForests()) {
149+
try {
150+
Configuration config = new Configuration();
151+
forestsWithOnlyReplicas.forEach(forest -> {
152+
config.addForest(forest.toObjectNode());
153+
});
154+
new Configurations(config).submit(context.getManageClient());
155+
return;
156+
} catch (Exception ex) {
157+
logger.warn("Unable to create forest replicas via CMA; cause: " + ex.getMessage() + "; will " +
158+
"fall back to using /manage/v2.");
159+
}
160+
}
142161

162+
// If we get here, either CMA usage is disabled or an error occurred with CMA. Just use /manage/v2 to submit
163+
// each forest one-by-one.
164+
ForestManager forestManager = new ForestManager(context.getManageClient());
165+
forestsWithOnlyReplicas.forEach(forest -> {
166+
String forestName = forest.getForestName();
143167
logger.info(format("Creating forest replicas for primary forest %s", forestName));
144-
context.getManageClient().putJson(forestManager.getPropertiesPath(forestName), json);
168+
context.getManageClient().putJson(forestManager.getPropertiesPath(forestName), forest.getJson());
145169
logger.info(format("Finished creating forest replicas for primary forest %s", forestName));
146-
}
170+
});
147171
}
148172

149173
/**

0 commit comments

Comments
 (0)