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

Commit 5d7b88b

Browse files
committed
#268 Can now customize the naming of forests
1 parent 0eb6936 commit 5d7b88b

File tree

7 files changed

+182
-32
lines changed

7 files changed

+182
-32
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marklogic.appdeployer;
22

3+
import com.marklogic.appdeployer.command.forests.ForestNamingStrategy;
34
import com.marklogic.client.DatabaseClient;
45
import com.marklogic.client.DatabaseClientFactory;
56
import com.marklogic.client.DatabaseClientFactory.Authentication;
@@ -175,6 +176,8 @@ public class AppConfig {
175176
private String replicaForestLargeDataDirectory;
176177
private String replicaForestFastDataDirectory;
177178

179+
private Map<String, ForestNamingStrategy> forestNamingStrategies = new HashMap<>();
180+
178181
// Path to use for DeployFlexrepCommand
179182
private String flexrepPath;
180183

@@ -1193,4 +1196,12 @@ public Map<String, Integer> getDatabaseNamesAndReplicaCounts() {
11931196
public void setDatabaseNamesAndReplicaCounts(Map<String, Integer> databaseNamesAndReplicaCounts) {
11941197
this.databaseNamesAndReplicaCounts = databaseNamesAndReplicaCounts;
11951198
}
1199+
1200+
public Map<String, ForestNamingStrategy> getForestNamingStrategies() {
1201+
return forestNamingStrategies;
1202+
}
1203+
1204+
public void setForestNamingStrategies(Map<String, ForestNamingStrategy> forestNamingStrategies) {
1205+
this.forestNamingStrategies = forestNamingStrategies;
1206+
}
11961207
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.marklogic.appdeployer.command.forests;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
5+
public class DefaultForestNamingStrategy implements ForestNamingStrategy {
6+
7+
@Override
8+
public String getForestName(String databaseName, int forestNumber, AppConfig appConfig) {
9+
return databaseName + "-" + forestNumber;
10+
}
11+
12+
@Override
13+
public String getReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig) {
14+
return forestName + "-replica-" + forestReplicaNumber;
15+
}
16+
}

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
*/
1919
public class ForestBuilder extends LoggingObject {
2020

21+
private ForestNamingStrategy forestNamingStrategy;
22+
23+
public ForestBuilder() {
24+
this(new DefaultForestNamingStrategy());
25+
}
26+
27+
public ForestBuilder(ForestNamingStrategy forestNamingStrategy) {
28+
this.forestNamingStrategy = forestNamingStrategy;
29+
}
30+
2131
/**
2232
* Builds a list of Forest objects based on the given ForestPlan and AppConfig. If replicaCount on the ForestPlan
2333
* is greater than zero, then ForestReplica objects are added to each Forest as well.
@@ -52,7 +62,7 @@ public List<Forest> buildForests(ForestPlan forestPlan, AppConfig appConfig) {
5262
for (int i = 0; i < forestsPerDataDirectory; i++) {
5363
if (forestCounter <= numberToBuild) {
5464
Forest forest = newForest(forestPlan);
55-
forest.setForestName(getForestName(databaseName, appConfig, forestCounter));
65+
forest.setForestName(getForestName(databaseName, forestCounter, appConfig));
5666
forest.setHost(hostName);
5767
forest.setDatabase(databaseName);
5868

@@ -130,7 +140,7 @@ public void addReplicasToForests(List<Forest> forests, ForestPlan forestPlan, Ap
130140
int dataDirectoryPointer = dataDirectories.indexOf(f.getDataDirectory());
131141
for (int i = 1; i <= replicaCount; i++) {
132142
ForestReplica replica = new ForestReplica();
133-
replica.setReplicaName(f.getForestName() + "-replica-" + i);
143+
replica.setReplicaName(getForestReplicaName(databaseName, f.getForestName(), i, appConfig));
134144
replicas.add(replica);
135145

136146
hostPointer++;
@@ -268,15 +278,40 @@ protected int determineForestsPerDataDirectory(ForestPlan forestPlan, AppConfig
268278
}
269279

270280
/**
271-
* Construct the name of a forest. Protected so it can be overridden. Could eventually have a strategy interface here
272-
* for allowing for easy customization.
273281
*
274282
* @param databaseName
275-
* @param appConfig
276283
* @param forestNumber
284+
* @param appConfig
285+
* @return
286+
*/
287+
protected String getForestName(String databaseName, int forestNumber, AppConfig appConfig) {
288+
return determineForestNamingStrategy(databaseName, appConfig).getForestName(databaseName, forestNumber, appConfig);
289+
}
290+
291+
/**
292+
*
293+
* @param databaseName
294+
* @param forestName
295+
* @param forestReplicaNumber
296+
* @param appConfig
297+
* @return
298+
*/
299+
protected String getForestReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig) {
300+
return determineForestNamingStrategy(databaseName, appConfig).getReplicaName(databaseName, forestName, forestReplicaNumber, appConfig);
301+
}
302+
303+
/**
304+
*
305+
* @param databaseName
306+
* @param appConfig
277307
* @return
278308
*/
279-
protected String getForestName(String databaseName, AppConfig appConfig, int forestNumber) {
280-
return databaseName + "-" + forestNumber;
309+
protected ForestNamingStrategy determineForestNamingStrategy(String databaseName, AppConfig appConfig) {
310+
ForestNamingStrategy fns = null;
311+
Map<String, ForestNamingStrategy> map = appConfig.getForestNamingStrategies();
312+
if (map != null) {
313+
fns = map.get(databaseName);
314+
}
315+
return fns != null ? fns : this.forestNamingStrategy;
281316
}
282317
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.marklogic.appdeployer.command.forests;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.mgmt.api.forest.Forest;
5+
6+
public interface ForestNamingStrategy {
7+
8+
/**
9+
* @param databaseName
10+
* @param forestNumber Keeps track of the total number of primary forests being created for the given database
11+
* @param appConfig
12+
* @return
13+
*/
14+
String getForestName(String databaseName, int forestNumber, AppConfig appConfig);
15+
16+
/**
17+
* @param databaseName
18+
* @param forestName
19+
* @param forestReplicaNumber Keeps track of the total number of replica forests being created for the given
20+
* forest
21+
* @param appConfig
22+
* @return
23+
*/
24+
String getReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig);
25+
26+
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.marklogic.appdeployer.command.cpf;
22

3-
import org.junit.After;
4-
import org.junit.Test;
5-
63
import com.marklogic.appdeployer.AbstractAppDeployerTest;
74
import com.marklogic.appdeployer.command.databases.DeployContentDatabasesCommand;
85
import com.marklogic.appdeployer.command.databases.DeploySchemasDatabaseCommand;
@@ -11,26 +8,28 @@
118
import com.marklogic.mgmt.resource.cpf.CpfConfigManager;
129
import com.marklogic.mgmt.resource.cpf.DomainManager;
1310
import com.marklogic.mgmt.resource.cpf.PipelineManager;
11+
import org.junit.After;
12+
import org.junit.Test;
1413

1514
public class ManageCpfTest extends AbstractAppDeployerTest {
1615

17-
@After
18-
public void teardown() {
19-
undeploySampleApp();
20-
}
16+
@After
17+
public void teardown() {
18+
undeploySampleApp();
19+
}
2120

22-
@Test
23-
public void test() {
24-
initializeAppDeployer(new DeployRestApiServersCommand(), new DeployContentDatabasesCommand(),
25-
new DeploySchemasDatabaseCommand(), new DeployTriggersDatabaseCommand(), new DeployDomainsCommand(),
26-
new DeployCpfConfigsCommand(), new DeployPipelinesCommand());
21+
@Test
22+
public void test() {
23+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployRestApiServersCommand(),
24+
new DeploySchemasDatabaseCommand(), new DeployTriggersDatabaseCommand(), new DeployDomainsCommand(),
25+
new DeployCpfConfigsCommand(), new DeployPipelinesCommand());
2726

28-
appDeployer.deploy(appConfig);
27+
appDeployer.deploy(appConfig);
2928

30-
String dbName = appConfig.getTriggersDatabaseName();
31-
assertEquals(2, new DomainManager(manageClient, dbName).getAsXml().getResourceCount());
32-
assertEquals(1, new CpfConfigManager(manageClient, dbName).getAsXml().getResourceCount());
33-
assertEquals(1, new PipelineManager(manageClient, dbName).getAsXml().getResourceCount());
34-
}
29+
String dbName = appConfig.getTriggersDatabaseName();
30+
assertEquals(2, new DomainManager(manageClient, dbName).getAsXml().getResourceCount());
31+
assertEquals(1, new CpfConfigManager(manageClient, dbName).getAsXml().getResourceCount());
32+
assertEquals(1, new PipelineManager(manageClient, dbName).getAsXml().getResourceCount());
33+
}
3534

3635
}

src/test/java/com/marklogic/appdeployer/command/forests/BuildForestReplicaTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,47 @@
88
import org.junit.Assert;
99
import org.junit.Test;
1010

11+
import java.util.List;
12+
1113
public class BuildForestReplicaTest extends Assert {
1214

15+
@Test
16+
public void customNamingStrategy() {
17+
AppConfig appConfig = newAppConfig();
18+
appConfig.getForestNamingStrategies().put("my-database", new ForestNamingStrategy() {
19+
@Override
20+
public String getForestName(String databaseName, int forestNumber, AppConfig appConfig) {
21+
return "forest-" + forestNumber;
22+
}
23+
24+
@Override
25+
public String getReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig) {
26+
return "my-replica-" + forestName + "-" + forestReplicaNumber;
27+
}
28+
});
29+
30+
List<Forest> forests = new ForestBuilder().buildForests(
31+
new ForestPlan("my-database", "host1", "host2", "host3").withReplicaCount(2), appConfig);
32+
33+
Forest f1 = forests.get(0);
34+
assertEquals("forest-1", f1.getForestName());
35+
assertEquals("host1", f1.getHost());
36+
assertEquals("my-replica-forest-1-1", f1.getForestReplica().get(0).getReplicaName());
37+
assertEquals("my-replica-forest-1-2", f1.getForestReplica().get(1).getReplicaName());
38+
39+
Forest f2 = forests.get(1);
40+
assertEquals("forest-2", f2.getForestName());
41+
assertEquals("host2", f2.getHost());
42+
assertEquals("my-replica-forest-2-1", f2.getForestReplica().get(0).getReplicaName());
43+
assertEquals("my-replica-forest-2-2", f2.getForestReplica().get(1).getReplicaName());
44+
45+
Forest f3 = forests.get(2);
46+
assertEquals("forest-3", f3.getForestName());
47+
assertEquals("host3", f3.getHost());
48+
assertEquals("my-replica-forest-3-1", f3.getForestReplica().get(0).getReplicaName());
49+
assertEquals("my-replica-forest-3-2", f3.getForestReplica().get(1).getReplicaName());
50+
}
51+
1352
@Test
1453
public void databaseAgnosticDirectories() {
1554
ForestReplica replica = buildForestReplica(
@@ -81,9 +120,7 @@ public void databaseSpecificReplicaDirectories() {
81120
}
82121

83122
private ForestReplica buildForestReplica(String... propertyNamesAndValues) {
84-
SimplePropertySource source = new SimplePropertySource(propertyNamesAndValues);
85-
DefaultAppConfigFactory f = new DefaultAppConfigFactory(source);
86-
AppConfig config = f.newAppConfig();
123+
AppConfig config = newAppConfig(propertyNamesAndValues);
87124

88125
Forest forest = new ForestBuilder().buildForests(
89126
new ForestPlan("my-database", "host1", "host2").withReplicaCount(1), config).get(0);
@@ -92,4 +129,10 @@ private ForestReplica buildForestReplica(String... propertyNamesAndValues) {
92129
assertEquals("host2", replica.getHost());
93130
return replica;
94131
}
132+
133+
protected AppConfig newAppConfig(String... propertyNamesAndValues) {
134+
SimplePropertySource source = new SimplePropertySource(propertyNamesAndValues);
135+
DefaultAppConfigFactory f = new DefaultAppConfigFactory(source);
136+
return f.newAppConfig();
137+
}
95138
}

src/test/java/com/marklogic/appdeployer/command/forests/BuildForestTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,39 @@ public class BuildForestTest extends Assert {
1414

1515
private ForestBuilder builder = new ForestBuilder();
1616

17+
@Test
18+
public void customNamingStrategy() {
19+
AppConfig appConfig = new AppConfig();
20+
appConfig.getForestNamingStrategies().put("testdb", new ForestNamingStrategy() {
21+
@Override
22+
public String getForestName(String databaseName, int forestNumber, AppConfig appConfig) {
23+
return "custom-" + databaseName + "-" + forestNumber;
24+
}
25+
26+
@Override
27+
public String getReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig) {
28+
return null;
29+
}
30+
});
31+
appConfig.getForestNamingStrategies().put("shouldn't be used", new DefaultForestNamingStrategy());
32+
33+
ForestPlan plan = new ForestPlan("testdb", "host1", "host2");
34+
List<Forest> forests = builder.buildForests(plan, appConfig);
35+
verifyNameAndHost(forests.get(0), "custom-testdb-1", "host1");
36+
verifyNameAndHost(forests.get(1), "custom-testdb-2", "host2");
37+
}
38+
1739
@Test
1840
public void withTemplate() {
1941
ForestPlan plan = new ForestPlan("testdb", "host1", "host2");
2042
plan.withTemplate("{\"rebalancer-enable\":false, \"failover-enable\":true}");
2143

2244
List<Forest> forests = builder.buildForests(plan, new AppConfig());
23-
assertEquals("testdb-1", forests.get(0).getForestName());
24-
assertEquals("host1", forests.get(0).getHost());
45+
verifyNameAndHost(forests.get(0), "testdb-1", "host1");
2546
assertFalse(forests.get(0).isRebalancerEnable());
2647
assertTrue(forests.get(0).getFailoverEnable());
2748

28-
assertEquals("testdb-2", forests.get(1).getForestName());
29-
assertEquals("host2", forests.get(1).getHost());
49+
verifyNameAndHost(forests.get(1), "testdb-2", "host2");
3050
assertFalse(forests.get(1).isRebalancerEnable());
3151
assertTrue(forests.get(1).getFailoverEnable());
3252
}

0 commit comments

Comments
 (0)