|
19 | 19 | import com.marklogic.appdeployer.command.CommandContext; |
20 | 20 | import com.marklogic.appdeployer.command.SortOrderConstants; |
21 | 21 | import com.marklogic.mgmt.api.API; |
| 22 | +import com.marklogic.mgmt.api.configuration.Configuration; |
| 23 | +import com.marklogic.mgmt.api.configuration.Configurations; |
22 | 24 | import com.marklogic.mgmt.api.forest.Forest; |
23 | 25 | import com.marklogic.mgmt.mapper.DefaultResourceMapper; |
24 | 26 | import com.marklogic.mgmt.mapper.ResourceMapper; |
|
28 | 30 | import com.marklogic.mgmt.resource.groups.GroupManager; |
29 | 31 | import com.marklogic.mgmt.resource.hosts.HostManager; |
30 | 32 |
|
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; |
32 | 38 |
|
33 | 39 | /** |
34 | 40 | * Command for configuring - i.e. creating and setting - replica forests for existing databases. |
@@ -131,19 +137,37 @@ protected void configureDatabaseReplicaForests(String databaseName, int replicaC |
131 | 137 | List<String> dataDirectories = forestBuilder.determineDataDirectories(databaseName, context.getAppConfig()); |
132 | 138 | forestBuilder.addReplicasToForests(forestsNeedingReplicas, forestPlan, context.getAppConfig(), dataDirectories); |
133 | 139 |
|
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 -> { |
139 | 141 | Forest forestWithOnlyReplicas = new Forest(); |
| 142 | + forestWithOnlyReplicas.setForestName(forest.getForestName()); |
140 | 143 | 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 | + } |
142 | 161 |
|
| 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(); |
143 | 167 | 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()); |
145 | 169 | logger.info(format("Finished creating forest replicas for primary forest %s", forestName)); |
146 | | - } |
| 170 | + }); |
147 | 171 | } |
148 | 172 |
|
149 | 173 | /** |
|
0 commit comments