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

Commit 9522405

Browse files
committed
#357 Defaulting to using CMA except for servers
Moved prepareCommand into a new listener class
1 parent 6323a9e commit 9522405

File tree

12 files changed

+148
-61
lines changed

12 files changed

+148
-61
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
33
version=3.15.develop
4-
mlJavaclientUtilVersion=3.12.0
4+
mlJavaclientUtilVersion=3.13.develop
55
mlJunitVersion=3.1.0
66

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class AppConfig {
7373
private boolean catchDeployExceptions = false;
7474
private boolean catchUndeployExceptions = false;
7575

76-
private CmaConfig cmaConfig = new CmaConfig();
76+
private CmaConfig cmaConfig;
7777
private boolean mergeResources = true;
7878

7979
private boolean addHostNameTokens = false;
@@ -251,6 +251,11 @@ public AppConfig(File projectDir) {
251251
dataConfig = new DataConfig(projectDir);
252252
pluginConfig = new PluginConfig(projectDir);
253253

254+
// As of 3.15.0, defaulting everything except servers to use CMA. Changes to servers, such as changing group or
255+
// port number, cause conflicts with CMA.
256+
cmaConfig = new CmaConfig(true);
257+
cmaConfig.setDeployServers(false);
258+
254259
modulePaths = new ArrayList<>();
255260
String path = projectDir != null ? new File(projectDir, DEFAULT_MODULES_PATH).getAbsolutePath() : DEFAULT_MODULES_PATH;
256261
modulePaths.add(path);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public class CmaConfig {
1313
private boolean deployServers;
1414
private boolean deployUsers;
1515

16+
public CmaConfig() {
17+
}
18+
19+
public CmaConfig(boolean enableAll) {
20+
if (enableAll) {
21+
enableAll();
22+
}
23+
}
24+
1625
public void enableAll() {
1726
setCombineRequests(true);
1827
setDeployAmps(true);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ public void initialize() {
6363
config.setMergeResources(Boolean.parseBoolean(prop));
6464
});
6565

66-
propertyConsumerMap.put("mlOptimizeWithCma", (config, prop) -> {
67-
logger.info("mlOptimizeWithCma is DEPRECATED; please use a property specific to the resource that you want to deploy with CMA");
68-
// mlOptimizeWithCma was deprecated in 3.11; it was only used for deploying forests, so if the
69-
// property is still used, the client in theory expects forests to still be deployed with CMA
70-
config.getCmaConfig().setDeployForests(true);
71-
});
72-
7366
final String cmaMessage = " with the Configuration Management API (CMA): ";
7467

7568
propertyConsumerMap.put("mlDeployWithCma", (config, prop) -> {
7669
logger.info("Deploy all supported resources and combine requests" + cmaMessage + prop);
7770
if (Boolean.parseBoolean(prop)) {
7871
config.getCmaConfig().enableAll();
72+
} else {
73+
config.setCmaConfig(new CmaConfig());
7974
}
8075
});
8176

77+
propertyConsumerMap.put("mlOptimizeWithCma", (config, prop) -> {
78+
logger.info("mlOptimizeWithCma is DEPRECATED; please use a property specific to the resource that you want to deploy with CMA");
79+
// mlOptimizeWithCma was deprecated in 3.11; it was only used for deploying forests, so if the
80+
// property is still used, the client in theory expects forests to still be deployed with CMA
81+
config.getCmaConfig().setDeployForests(true);
82+
});
83+
8284
propertyConsumerMap.put("mlCombineCmaRequests", (config, prop) -> {
8385
logger.info("Combine requests" + cmaMessage + prop);
8486
config.getCmaConfig().setCombineRequests(Boolean.parseBoolean(prop));

src/main/java/com/marklogic/appdeployer/command/databases/DeployDatabaseCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class DeployDatabaseCommand extends AbstractCommand implements UndoableCo
9696
public DeployDatabaseCommand() {
9797
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
9898
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_DATABASES);
99+
setResourceClassType(Database.class);
99100
}
100101

101102
public DeployDatabaseCommand(File databaseFile) {

src/main/java/com/marklogic/appdeployer/command/databases/DeployOtherDatabasesCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public DeployOtherDatabasesCommand() {
5151
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
5252
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_DATABASES);
5353
initializeDefaultDatabasesToNotUndeploy();
54+
setResourceClassType(Database.class);
5455
}
5556

5657
public DeployOtherDatabasesCommand(int forestsPerHost) {

src/main/java/com/marklogic/appdeployer/impl/AbstractAppDeployer.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.marklogic.appdeployer.AppConfig;
44
import com.marklogic.appdeployer.AppDeployer;
55
import com.marklogic.appdeployer.ConfigDir;
6-
import com.marklogic.appdeployer.command.AbstractCommand;
76
import com.marklogic.appdeployer.command.Command;
87
import com.marklogic.appdeployer.command.CommandContext;
98
import com.marklogic.appdeployer.command.UndoableCommand;
@@ -15,7 +14,6 @@
1514
import java.util.Collections;
1615
import java.util.Comparator;
1716
import java.util.List;
18-
import java.util.regex.Pattern;
1917

2018
/**
2119
* Abstract base class that just needs the subclass to define the list of Command instances to use. Handles executing
@@ -41,6 +39,7 @@ public AbstractAppDeployer(ManageClient manageClient, AdminManager adminManager)
4139

4240
this.deployerListeners = new ArrayList<>();
4341
this.deployerListeners.add(new AddHostNameTokensDeployerListener());
42+
this.deployerListeners.add(new PrepareCommandListener());
4443
this.deployerListeners.add(new CmaDeployerListener());
4544
}
4645

@@ -72,50 +71,25 @@ public void deploy(AppConfig appConfig) {
7271

7372
deployerListeners.forEach(listener -> listener.beforeCommandsExecuted(deploymentContext));
7473

74+
boolean catchExceptions = appConfig.isCatchDeployExceptions();
75+
7576
int commandCount = commands.size();
7677
for (int i = 0; i < commandCount; i++) {
7778
Command command = commands.get(i);
7879
final List<Command> remainingCommands = commands.subList(i + 1, commandCount);
7980
String name = command.getClass().getName();
8081

8182
logger.info(format("Executing command [%s] with sort order [%d]", name, command.getExecuteSortOrder()));
82-
prepareCommand(command, context);
83-
deployerListeners.forEach(listener -> listener.beforeCommandExecuted(command, deploymentContext, remainingCommands));
83+
invokeListenersBeforeCommandExecuted(context, command, deploymentContext, remainingCommands, catchExceptions);
8484
long start = System.currentTimeMillis();
8585
executeCommand(command, context);
8686
logger.info(format("Finished executing command [%s] in %dms\n", name, (System.currentTimeMillis() - start)));
87-
deployerListeners.forEach(listener -> listener.afterCommandExecuted(command, deploymentContext, remainingCommands));
87+
invokeListenersAfterCommandExecuted(context, command, deploymentContext, remainingCommands, catchExceptions);
8888
}
8989

9090
logger.info(format("Deployed app %s", appConfig.getName()));
9191
}
9292

93-
/**
94-
* Prepare the given command before either execute or undo is called on it.
95-
*
96-
* @param command
97-
* @param context
98-
*/
99-
protected void prepareCommand(Command command, CommandContext context) {
100-
if (command instanceof AbstractCommand) {
101-
AppConfig appConfig = context.getAppConfig();
102-
String[] filenamesToIgnore = appConfig.getResourceFilenamesToIgnore();
103-
Pattern excludePattern = appConfig.getResourceFilenamesExcludePattern();
104-
Pattern includePattern = appConfig.getResourceFilenamesIncludePattern();
105-
106-
AbstractCommand abstractCommand = (AbstractCommand) command;
107-
if (filenamesToIgnore != null) {
108-
abstractCommand.setFilenamesToIgnore(filenamesToIgnore);
109-
}
110-
if (excludePattern != null) {
111-
abstractCommand.setResourceFilenamesExcludePattern(excludePattern);
112-
}
113-
if (includePattern != null) {
114-
abstractCommand.setResourceFilenamesIncludePattern(includePattern);
115-
}
116-
}
117-
}
118-
11993
/**
12094
* Executes the command, catching an exception if desired.
12195
*
@@ -158,12 +132,22 @@ public void undeploy(AppConfig appConfig) {
158132
Collections.sort(undoableCommands, new UndoComparator());
159133
CommandContext context = new CommandContext(appConfig, manageClient, adminManager);
160134

161-
for (UndoableCommand command : undoableCommands) {
135+
final DeploymentContext deploymentContext = new DeploymentContext(context, appConfig, commands);
136+
deployerListeners.forEach(listener -> listener.beforeCommandsExecuted(deploymentContext));
137+
138+
boolean catchExceptions = appConfig.isCatchUndeployExceptions();
139+
140+
int commandCount = undoableCommands.size();
141+
for (int i = 0; i < commandCount; i++) {
142+
UndoableCommand command = undoableCommands.get(i);
143+
final List<Command> remainingCommands = commands.subList(i + 1, commandCount);
144+
162145
String name = command.getClass().getName();
163146
logger.info(format("Undoing command [%s] with sort order [%d]", name, command.getUndoSortOrder()));
164-
prepareCommand(command, context);
147+
invokeListenersBeforeCommandExecuted(context, command, deploymentContext, remainingCommands, catchExceptions);
165148
undoCommand(command, context);
166149
logger.info(format("Finished undoing command [%s]\n", name));
150+
invokeListenersAfterCommandExecuted(context, command, deploymentContext, remainingCommands, catchExceptions);
167151
}
168152

169153
logger.info(format("Undeployed app %s", appConfig.getName()));
@@ -187,6 +171,36 @@ protected void undoCommand(UndoableCommand command, CommandContext context) {
187171
}
188172
}
189173

174+
protected void invokeListenersBeforeCommandExecuted(CommandContext context, Command command, DeploymentContext deploymentContext,
175+
List<Command> remainingCommands, boolean catchExceptions) {
176+
deployerListeners.forEach(listener -> {
177+
try {
178+
listener.beforeCommandExecuted(command, deploymentContext, remainingCommands);
179+
} catch (Exception ex) {
180+
if (catchExceptions) {
181+
logger.error(format("Listener threw exception that was caught; cause: %s", ex.getMessage()), ex);
182+
} else {
183+
throw ex;
184+
}
185+
}
186+
});
187+
}
188+
189+
protected void invokeListenersAfterCommandExecuted(CommandContext context, Command command, DeploymentContext deploymentContext,
190+
List<Command> remainingCommands, boolean catchExceptions) {
191+
deployerListeners.forEach(listener -> {
192+
try {
193+
listener.afterCommandExecuted(command, deploymentContext, remainingCommands);
194+
} catch (Exception ex) {
195+
if (catchExceptions) {
196+
logger.error(format("Listener threw exception that was caught; cause: %s", ex.getMessage()), ex);
197+
} else {
198+
throw ex;
199+
}
200+
}
201+
});
202+
}
203+
190204
public List<DeployerListener> getDeployerListeners() {
191205
return deployerListeners;
192206
}

src/main/java/com/marklogic/appdeployer/impl/CmaDeployerListener.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.marklogic.appdeployer.command.Command;
44
import com.marklogic.appdeployer.command.CommandContext;
5+
import com.marklogic.appdeployer.command.SortOrderConstants;
56
import com.marklogic.appdeployer.command.databases.DatabasePlan;
67
import com.marklogic.appdeployer.command.security.DeployAmpsCommand;
78
import com.marklogic.appdeployer.command.security.DeployUsersCommand;
@@ -29,11 +30,7 @@ public class CmaDeployerListener extends DeployerListenerSupport {
2930

3031
@Override
3132
public void afterCommandExecuted(Command command, DeploymentContext context, List<Command> remainingCommands) {
32-
if (
33-
command instanceof DeployUsersCommand ||
34-
command instanceof DeployAmpsCommand ||
35-
remainingCommands.isEmpty()
36-
) {
33+
if (combinedRequestBeSubmitted(command, remainingCommands)) {
3734
CommandContext commandContext = context.getCommandContext();
3835

3936
Configurations configs = commandContext.getCombinedCmaRequest();
@@ -57,4 +54,23 @@ public void afterCommandExecuted(Command command, DeploymentContext context, Lis
5754
}
5855
}
5956
}
57+
58+
protected boolean combinedRequestBeSubmitted(Command command, List<Command> remainingCommands) {
59+
if (command instanceof DeployUsersCommand || command instanceof DeployAmpsCommand) {
60+
return true;
61+
}
62+
63+
if (remainingCommands.isEmpty()) {
64+
return true;
65+
}
66+
67+
/**
68+
* At least for many ml-app-deployer tests, a small subset of commands are used - for example, perhaps only the
69+
* commands for deploying databases and triggers are used. To ensure that databases are still deployed, we look
70+
* at the next command to see if it's being executed after amps are deployed, as amps are the last resource
71+
* we include in a combined request. If so, then we know we need to submit the combined request.
72+
*/
73+
Command nextCommand = remainingCommands.get(0);
74+
return nextCommand.getExecuteSortOrder() >= SortOrderConstants.DEPLOY_AMPS;
75+
}
6076
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.marklogic.appdeployer.impl;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.command.AbstractCommand;
5+
import com.marklogic.appdeployer.command.Command;
6+
7+
import java.util.List;
8+
import java.util.regex.Pattern;
9+
10+
public class PrepareCommandListener extends DeployerListenerSupport {
11+
12+
@Override
13+
public void beforeCommandExecuted(Command command, DeploymentContext context, List<Command> remainingCommands) {
14+
if (command instanceof AbstractCommand) {
15+
AppConfig appConfig = context.getAppConfig();
16+
String[] filenamesToIgnore = appConfig.getResourceFilenamesToIgnore();
17+
Pattern excludePattern = appConfig.getResourceFilenamesExcludePattern();
18+
Pattern includePattern = appConfig.getResourceFilenamesIncludePattern();
19+
20+
AbstractCommand abstractCommand = (AbstractCommand) command;
21+
if (filenamesToIgnore != null) {
22+
abstractCommand.setFilenamesToIgnore(filenamesToIgnore);
23+
}
24+
if (excludePattern != null) {
25+
abstractCommand.setResourceFilenamesExcludePattern(excludePattern);
26+
}
27+
if (includePattern != null) {
28+
abstractCommand.setResourceFilenamesIncludePattern(includePattern);
29+
}
30+
}
31+
}
32+
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,27 +213,27 @@ public void deployWithCma() {
213213
Properties p = new Properties();
214214

215215
CmaConfig cmaConfig = new DefaultAppConfigFactory(new SimplePropertySource(p)).newAppConfig().getCmaConfig();
216+
assertTrue(cmaConfig.isCombineRequests());
217+
assertTrue(cmaConfig.isDeployAmps());
218+
assertTrue(cmaConfig.isDeployDatabases());
219+
assertTrue(cmaConfig.isDeployForests());
220+
assertTrue(cmaConfig.isDeployPrivileges());
221+
assertTrue(cmaConfig.isDeployRoles());
222+
assertFalse(cmaConfig.isDeployServers());
223+
assertTrue(cmaConfig.isDeployUsers());
224+
225+
p.setProperty("mlDeployWithCma", "false");
226+
cmaConfig = new DefaultAppConfigFactory(new SimplePropertySource(p)).newAppConfig().getCmaConfig();
216227
assertFalse(cmaConfig.isCombineRequests());
217228
assertFalse(cmaConfig.isDeployAmps());
218229
assertFalse(cmaConfig.isDeployDatabases());
219230
assertFalse(cmaConfig.isDeployForests());
220231
assertFalse(cmaConfig.isDeployPrivileges());
232+
assertFalse(cmaConfig.isDeployProtectedPaths());
233+
assertFalse(cmaConfig.isDeployQueryRolesets());
221234
assertFalse(cmaConfig.isDeployRoles());
222235
assertFalse(cmaConfig.isDeployServers());
223236
assertFalse(cmaConfig.isDeployUsers());
224-
225-
p.setProperty("mlDeployWithCma", "true");
226-
cmaConfig = new DefaultAppConfigFactory(new SimplePropertySource(p)).newAppConfig().getCmaConfig();
227-
assertTrue(cmaConfig.isCombineRequests());
228-
assertTrue(cmaConfig.isDeployAmps());
229-
assertTrue(cmaConfig.isDeployDatabases());
230-
assertTrue(cmaConfig.isDeployForests());
231-
assertTrue(cmaConfig.isDeployPrivileges());
232-
assertTrue(cmaConfig.isDeployProtectedPaths());
233-
assertTrue(cmaConfig.isDeployQueryRolesets());
234-
assertTrue(cmaConfig.isDeployRoles());
235-
assertTrue(cmaConfig.isDeployServers());
236-
assertTrue(cmaConfig.isDeployUsers());
237237
}
238238

239239
@Test

0 commit comments

Comments
 (0)