1
1
package com .marklogic .appdeployer .command .forests ;
2
2
3
+ import com .marklogic .appdeployer .AppConfig ;
3
4
import com .marklogic .appdeployer .command .AbstractUndoableCommand ;
4
5
import com .marklogic .appdeployer .command .CommandContext ;
5
6
import 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 ;
6
10
import com .marklogic .mgmt .databases .DatabaseManager ;
7
11
import com .marklogic .mgmt .forests .ForestManager ;
8
12
import com .marklogic .mgmt .forests .ForestStatus ;
9
13
import com .marklogic .mgmt .hosts .HostManager ;
10
14
15
+ import java .util .ArrayList ;
11
16
import java .util .HashMap ;
12
17
import java .util .List ;
13
18
import java .util .Map ;
@@ -84,7 +89,7 @@ public void execute(CommandContext context) {
84
89
for (String forestName : forestNamesAndReplicaCounts .keySet ()) {
85
90
int replicaCount = forestNamesAndReplicaCounts .get (forestName );
86
91
if (replicaCount > 0 ) {
87
- configureReplicaForests (forestName , replicaCount , hostIds , forestMgr );
92
+ configureReplicaForests (forestName , replicaCount , hostIds , context , forestMgr );
88
93
}
89
94
}
90
95
}
@@ -130,21 +135,20 @@ protected void deleteReplicas(String forestName, ForestManager forestMgr) {
130
135
131
136
/**
132
137
* 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,
134
139
* then don't do anything.
135
140
*
136
141
* @param databaseName
137
142
* @param replicaCount
138
143
* @param hostIds
139
144
*/
140
145
protected void configureDatabaseReplicaForests (String databaseName , int replicaCount , List <String > hostIds ,
141
- CommandContext context ) {
146
+ CommandContext context ) {
142
147
ForestManager forestMgr = new ForestManager (context .getManageClient ());
143
148
DatabaseManager dbMgr = new DatabaseManager (context .getManageClient ());
144
149
List <String > forestNames = dbMgr .getForestNames (databaseName );
145
- logger .info ("Forests: " + forestNames );
146
150
for (String name : forestNames ) {
147
- configureReplicaForests (name , replicaCount , hostIds , forestMgr );
151
+ configureReplicaForests (name , replicaCount , hostIds , context , forestMgr );
148
152
}
149
153
}
150
154
@@ -155,10 +159,11 @@ protected void configureDatabaseReplicaForests(String databaseName, int replicaC
155
159
* @param forestIdOrName
156
160
* @param replicaCount
157
161
* @param hostIds
162
+ * @param context
158
163
* @param forestMgr
159
164
*/
160
165
protected void configureReplicaForests (String forestIdOrName , int replicaCount , List <String > hostIds ,
161
- ForestManager forestMgr ) {
166
+ CommandContext context , ForestManager forestMgr ) {
162
167
ForestStatus status = forestMgr .getForestStatus (forestIdOrName );
163
168
if (!status .isPrimary ()) {
164
169
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,
169
174
return ;
170
175
}
171
176
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 ));
178
180
}
179
181
180
182
/**
@@ -184,12 +186,20 @@ protected void configureReplicaForests(String forestIdOrName, int replicaCount,
184
186
* @param forestIdOrName
185
187
* @param replicaCount
186
188
* @param hostIds
189
+ * @param context
187
190
* @param forestMgr
188
191
* @return a map where the keys are replica forest names, and the value of each key is the ID of the host that
189
192
* the replica was created on
190
193
*/
191
194
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
+
193
203
String primaryForestHostId = forestMgr .getHostId (forestIdOrName );
194
204
Map <String , String > replicaNamesAndHostIds = new HashMap <>();
195
205
int size = hostIds .size ();
@@ -203,15 +213,28 @@ protected Map<String, String> createReplicaForests(String forestIdOrName, int re
203
213
}
204
214
String replicaHostId = hostIds .get (nextReplicaHostIndex );
205
215
String name = forestIdOrName + "-replica-" + j ;
206
- forestMgr . createJsonForestWithName ( name , replicaHostId );
216
+ replicas . add ( buildForestReplica ( name , replicaHostId , context . getAppConfig ()) );
207
217
replicaNamesAndHostIds .put (name , replicaHostId );
208
218
nextReplicaHostIndex ++;
209
219
}
210
220
}
211
221
}
222
+
223
+ String json = forest .getJson ();
224
+ context .getManageClient ().putJson (forestMgr .getPropertiesPath (forestIdOrName ), json );
212
225
return replicaNamesAndHostIds ;
213
226
}
214
227
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
+
215
238
public Map <String , Integer > getForestNamesAndReplicaCounts () {
216
239
return forestNamesAndReplicaCounts ;
217
240
}
0 commit comments