@@ -108,11 +108,45 @@ protected void deleteReplicas(String forestName, ForestManager forestMgr) {
108108 * @param context
109109 */
110110 protected void configureDatabaseReplicaForests (String databaseName , int replicaCount , List <String > hostNames , CommandContext context ) {
111+ List <Forest > forestsNeedingReplicas = determineForestsNeedingReplicas (databaseName , context );
112+
113+ ForestBuilder forestBuilder = new ForestBuilder ();
114+ List <String > selectedHostNames = getHostNamesForDatabaseForests (databaseName , hostNames , context );
115+ ForestPlan forestPlan = new ForestPlan (databaseName , selectedHostNames ).withReplicaCount (replicaCount );
116+ List <String > dataDirectories = forestBuilder .determineDataDirectories (databaseName , context .getAppConfig ());
117+ forestBuilder .addReplicasToForests (forestsNeedingReplicas , forestPlan , context .getAppConfig (), dataDirectories );
118+
119+ // TODO Use CMA here in the future? Need to test to see if a forest name + replicas are allowable
120+ ForestManager forestManager = new ForestManager (context .getManageClient ());
121+ for (Forest forest : forestsNeedingReplicas ) {
122+ final String forestName = forest .getForestName ();
123+
124+ Forest forestWithOnlyReplicas = new Forest ();
125+ forestWithOnlyReplicas .setForestReplica (forest .getForestReplica ());
126+ String json = forestWithOnlyReplicas .getJson ();
127+
128+ logger .info (format ("Creating forest replicas for primary forest %s" , forestName ));
129+ context .getManageClient ().putJson (forestManager .getPropertiesPath (forestName ), json );
130+ logger .info (format ("Finished creating forest replicas for primary forest %s" , forestName ));
131+ }
132+ }
133+
134+ /**
135+ * Per #389, the list of replicas needs to be calculated for all forests at once so that ForestBuilder produces the
136+ * correct results.
137+ *
138+ * @param databaseName
139+ * @param context
140+ * @return
141+ */
142+ protected List <Forest > determineForestsNeedingReplicas (String databaseName , CommandContext context ) {
111143 ForestManager forestManager = new ForestManager (context .getManageClient ());
112144 DatabaseManager dbMgr = new DatabaseManager (context .getManageClient ());
113145 API api = new API (context .getManageClient ());
114146 ResourceMapper resourceMapper = new DefaultResourceMapper (api );
115147
148+ List <Forest > forestsNeedingReplicas = new ArrayList <>();
149+
116150 for (String forestName : dbMgr .getForestNames (databaseName )) {
117151 logger .info (format ("Checking the status of forest %s to determine if it is a primary forest and whether or not it has replicas already." , forestName ));
118152 ForestStatus status = forestManager .getForestStatus (forestName );
@@ -125,25 +159,12 @@ protected void configureDatabaseReplicaForests(String databaseName, int replicaC
125159 continue ;
126160 }
127161
128- logger .info (format ("Creating forest replicas for primary forest %s" , forestName ));
129-
130162 String forestJson = forestManager .getPropertiesAsJson (forestName );
131163 Forest forest = resourceMapper .readResource (forestJson , Forest .class );
132-
133- List <String > selectedHostNames = getHostNamesForDatabaseForests (databaseName , hostNames , context );
134-
135- ForestBuilder forestBuilder = new ForestBuilder ();
136- ForestPlan forestPlan = new ForestPlan (databaseName , selectedHostNames ).withReplicaCount (replicaCount );
137- List <String > dataDirectories = forestBuilder .determineDataDirectories (databaseName , context .getAppConfig ());
138- forestBuilder .addReplicasToForests (Arrays .asList (forest ), forestPlan , context .getAppConfig (), dataDirectories );
139-
140- Forest forestWithOnlyReplicas = new Forest ();
141- forestWithOnlyReplicas .setForestReplica (forest .getForestReplica ());
142-
143- String json = forestWithOnlyReplicas .getJson ();
144- context .getManageClient ().putJson (forestManager .getPropertiesPath (forestName ), json );
145- logger .info (format ("Finished creating forest replicas for primary forest %s" , forestName ));
164+ forestsNeedingReplicas .add (forest );
146165 }
166+
167+ return forestsNeedingReplicas ;
147168 }
148169
149170 /**
0 commit comments