11package com .marklogic .appdeployer .command .forests ;
22
3+ import com .marklogic .appdeployer .AppConfig ;
34import com .marklogic .appdeployer .command .AbstractUndoableCommand ;
45import com .marklogic .appdeployer .command .CommandContext ;
56import com .marklogic .appdeployer .command .SortOrderConstants ;
7+ import com .marklogic .mgmt .api .API ;
8+ import com .marklogic .mgmt .api .forest .Forest ;
9+ import com .marklogic .mgmt .api .forest .ForestReplica ;
610import com .marklogic .mgmt .databases .DatabaseManager ;
711import com .marklogic .mgmt .forests .ForestManager ;
812import com .marklogic .mgmt .forests .ForestStatus ;
913import com .marklogic .mgmt .hosts .HostManager ;
1014
15+ import java .util .ArrayList ;
1116import java .util .HashMap ;
1217import java .util .List ;
1318import java .util .Map ;
@@ -84,7 +89,7 @@ public void execute(CommandContext context) {
8489 for (String forestName : forestNamesAndReplicaCounts .keySet ()) {
8590 int replicaCount = forestNamesAndReplicaCounts .get (forestName );
8691 if (replicaCount > 0 ) {
87- configureReplicaForests (forestName , replicaCount , hostIds , forestMgr );
92+ configureReplicaForests (forestName , replicaCount , hostIds , context , forestMgr );
8893 }
8994 }
9095 }
@@ -130,21 +135,20 @@ protected void deleteReplicas(String forestName, ForestManager forestMgr) {
130135
131136 /**
132137 * For the given database, find all of its primary forests. Then for each primary forest, just call
133- * configureReplicaForests? And that should be smart enough to say - if the primary forest already has replicas,
138+ * configureReplicaForests. And that should be smart enough to say - if the primary forest already has replicas,
134139 * then don't do anything.
135140 *
136141 * @param databaseName
137142 * @param replicaCount
138143 * @param hostIds
139144 */
140145 protected void configureDatabaseReplicaForests (String databaseName , int replicaCount , List <String > hostIds ,
141- CommandContext context ) {
146+ CommandContext context ) {
142147 ForestManager forestMgr = new ForestManager (context .getManageClient ());
143148 DatabaseManager dbMgr = new DatabaseManager (context .getManageClient ());
144149 List <String > forestNames = dbMgr .getForestNames (databaseName );
145- logger .info ("Forests: " + forestNames );
146150 for (String name : forestNames ) {
147- configureReplicaForests (name , replicaCount , hostIds , forestMgr );
151+ configureReplicaForests (name , replicaCount , hostIds , context , forestMgr );
148152 }
149153 }
150154
@@ -155,10 +159,11 @@ protected void configureDatabaseReplicaForests(String databaseName, int replicaC
155159 * @param forestIdOrName
156160 * @param replicaCount
157161 * @param hostIds
162+ * @param context
158163 * @param forestMgr
159164 */
160165 protected void configureReplicaForests (String forestIdOrName , int replicaCount , List <String > hostIds ,
161- ForestManager forestMgr ) {
166+ CommandContext context , ForestManager forestMgr ) {
162167 ForestStatus status = forestMgr .getForestStatus (forestIdOrName );
163168 if (!status .isPrimary ()) {
164169 logger .info (format ("Forest %s is not a primary forest, so not configuring replica forests" , forestIdOrName ));
@@ -169,12 +174,9 @@ protected void configureReplicaForests(String forestIdOrName, int replicaCount,
169174 return ;
170175 }
171176
172- Map <String , String > replicaNamesAndHostIds = createReplicaForests (forestIdOrName , replicaCount , hostIds , forestMgr );
173- logger .info (format ("Configuring forest replicas for primary forest %s" , forestIdOrName ));
174- if (!replicaNamesAndHostIds .isEmpty ()) {
175- forestMgr .setReplicas (forestIdOrName , replicaNamesAndHostIds );
176- }
177- logger .info (format ("Finished configuring forest replicas for primary forest %s" , forestIdOrName ));
177+ logger .info (format ("Creating forest replicas for primary forest %s" , forestIdOrName ));
178+ createReplicaForests (forestIdOrName , replicaCount , hostIds , context , forestMgr );
179+ logger .info (format ("Finished creating forest replicas for primary forest %s" , forestIdOrName ));
178180 }
179181
180182 /**
@@ -184,12 +186,20 @@ protected void configureReplicaForests(String forestIdOrName, int replicaCount,
184186 * @param forestIdOrName
185187 * @param replicaCount
186188 * @param hostIds
189+ * @param context
187190 * @param forestMgr
188191 * @return a map where the keys are replica forest names, and the value of each key is the ID of the host that
189192 * the replica was created on
190193 */
191194 protected Map <String , String > createReplicaForests (String forestIdOrName , int replicaCount , List <String > hostIds ,
192- ForestManager forestMgr ) {
195+ CommandContext context , ForestManager forestMgr ) {
196+
197+ // Using the Forest class to generate JSON
198+ API api = new API (context .getManageClient ());
199+ Forest forest = new Forest (api , null );
200+ List <ForestReplica > replicas = new ArrayList <>();
201+ forest .setForestReplica (replicas );
202+
193203 String primaryForestHostId = forestMgr .getHostId (forestIdOrName );
194204 Map <String , String > replicaNamesAndHostIds = new HashMap <>();
195205 int size = hostIds .size ();
@@ -203,15 +213,28 @@ protected Map<String, String> createReplicaForests(String forestIdOrName, int re
203213 }
204214 String replicaHostId = hostIds .get (nextReplicaHostIndex );
205215 String name = forestIdOrName + "-replica-" + j ;
206- forestMgr . createJsonForestWithName ( name , replicaHostId );
216+ replicas . add ( buildForestReplica ( name , replicaHostId , context . getAppConfig ()) );
207217 replicaNamesAndHostIds .put (name , replicaHostId );
208218 nextReplicaHostIndex ++;
209219 }
210220 }
211221 }
222+
223+ String json = forest .getJson ();
224+ context .getManageClient ().putJson (forestMgr .getPropertiesPath (forestIdOrName ), json );
212225 return replicaNamesAndHostIds ;
213226 }
214227
228+ protected ForestReplica buildForestReplica (String name , String replicaHostId , AppConfig appConfig ) {
229+ ForestReplica replica = new ForestReplica ();
230+ replica .setHost (replicaHostId );
231+ replica .setReplicaName (name );
232+ replica .setDataDirectory (appConfig .getReplicaForestDataDirectory ());
233+ replica .setLargeDataDirectory (appConfig .getReplicaForestLargeDataDirectory ());
234+ replica .setFastDataDirectory (appConfig .getReplicaForestFastDataDirectory ());
235+ return replica ;
236+ }
237+
215238 public Map <String , Integer > getForestNamesAndReplicaCounts () {
216239 return forestNamesAndReplicaCounts ;
217240 }
0 commit comments