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

Commit 34d4f3c

Browse files
committed
#207 Now deleting content/modules replicas when deleting a REST API server
1 parent f532cae commit 34d4f3c

File tree

6 files changed

+187
-18
lines changed

6 files changed

+187
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void undo(CommandContext context) {
108108
for (String databaseName : databaseNamesAndReplicaCounts.keySet()) {
109109
logger.info(format("Deleting forest replicas for database %s", databaseName));
110110
if (!dbMgr.exists(databaseName)) {
111-
logger.warn(format("Database %s does not exist, so not able to delete forest replica for it; perhaps a previous command deleted the database?"));
111+
logger.warn(format("Database %s does not exist, so not able to delete forest replica for it; perhaps a previous command deleted the database?", databaseName));
112112
}
113113
else {
114114
List<String> forestNames = dbMgr.getForestNames(databaseName);

src/main/java/com/marklogic/appdeployer/command/restapis/DeployRestApiServersCommand.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.marklogic.mgmt.ManageClient;
1010
import com.marklogic.mgmt.admin.ActionRequiringRestart;
1111
import com.marklogic.mgmt.resource.appservers.ServerManager;
12+
import com.marklogic.mgmt.resource.restapis.RestApiDeletionRequest;
1213
import com.marklogic.mgmt.resource.restapis.RestApiManager;
1314

1415
import java.io.File;
@@ -23,6 +24,10 @@ public class DeployRestApiServersCommand extends AbstractCommand implements Undo
2324
private boolean deleteModulesDatabase = true;
2425
private boolean deleteContentDatabase = false;
2526

27+
// Controls whether this command first deletes replica forests if it's supposed to delete the modules or content databases
28+
private boolean deleteModulesReplicaForests = true;
29+
private boolean deleteContentReplicaForests = true;
30+
2631
private String restApiFilename;
2732

2833
public DeployRestApiServersCommand() {
@@ -94,7 +99,7 @@ public void undo(CommandContext context) {
9499

95100
/**
96101
* If we have a test REST API, we first modify it to point at Documents for the modules database so we can safely
97-
* delete each REST API
102+
* delete each REST API.
98103
*
99104
* @param context
100105
*/
@@ -142,9 +147,24 @@ protected String getDefaultRestApiPayload() {
142147
return RestApiUtil.buildDefaultRestApiJson();
143148
}
144149

150+
/**
151+
* Delete the REST API server with the given name.
152+
*
153+
* @param serverName
154+
* @param groupName
155+
* @param manageClient
156+
* @param includeModules
157+
* @param includeContent
158+
* @return
159+
*/
145160
protected boolean deleteRestApi(String serverName, String groupName, ManageClient manageClient,
146161
boolean includeModules, boolean includeContent) {
147-
return new RestApiManager(manageClient).deleteRestApi(serverName, groupName, includeModules, includeContent);
162+
RestApiDeletionRequest request = new RestApiDeletionRequest(serverName, groupName);
163+
request.setIncludeContent(includeContent);
164+
request.setIncludeModules(includeModules);
165+
request.setDeleteContentReplicaForests(isDeleteContentReplicaForests());
166+
request.setDeleteModulesReplicaForests(isDeleteModulesReplicaForests());
167+
return new RestApiManager(manageClient).deleteRestApi(request);
148168
}
149169

150170
public boolean isDeleteModulesDatabase() {
@@ -170,4 +190,20 @@ public String getRestApiFilename() {
170190
public void setRestApiFilename(String restApiFilename) {
171191
this.restApiFilename = restApiFilename;
172192
}
193+
194+
public boolean isDeleteModulesReplicaForests() {
195+
return deleteModulesReplicaForests;
196+
}
197+
198+
public void setDeleteModulesReplicaForests(boolean deleteModulesReplicaForests) {
199+
this.deleteModulesReplicaForests = deleteModulesReplicaForests;
200+
}
201+
202+
public boolean isDeleteContentReplicaForests() {
203+
return deleteContentReplicaForests;
204+
}
205+
206+
public void setDeleteContentReplicaForests(boolean deleteContentReplicaForests) {
207+
this.deleteContentReplicaForests = deleteContentReplicaForests;
208+
}
173209
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.marklogic.mgmt.resource.restapis;
2+
3+
/**
4+
* Encapsulates a call for deleting a REST API server. Defaults to deleting the modules database but not the
5+
* content database. For any database that should be deleted, defaults to deleting any replica forests that exist for
6+
* that database as well, as the call to DELETE /v1/rest-apis will not delete replica forests itself.
7+
*/
8+
public class RestApiDeletionRequest {
9+
10+
private String serverName;
11+
private String groupName;
12+
13+
private boolean includeContent = false;
14+
private boolean includeModules = true;
15+
16+
private boolean deleteContentReplicaForests = true;
17+
private boolean deleteModulesReplicaForests = true;
18+
19+
public RestApiDeletionRequest(String serverName, String groupName) {
20+
this.serverName = serverName;
21+
this.groupName = groupName;
22+
}
23+
24+
public String getServerName() {
25+
return serverName;
26+
}
27+
28+
public String getGroupName() {
29+
return groupName;
30+
}
31+
32+
public boolean isIncludeContent() {
33+
return includeContent;
34+
}
35+
36+
public void setIncludeContent(boolean includeContent) {
37+
this.includeContent = includeContent;
38+
}
39+
40+
public boolean isIncludeModules() {
41+
return includeModules;
42+
}
43+
44+
public void setIncludeModules(boolean includeModules) {
45+
this.includeModules = includeModules;
46+
}
47+
48+
public boolean isDeleteContentReplicaForests() {
49+
return deleteContentReplicaForests;
50+
}
51+
52+
public void setDeleteContentReplicaForests(boolean deleteContentReplicaForests) {
53+
this.deleteContentReplicaForests = deleteContentReplicaForests;
54+
}
55+
56+
public boolean isDeleteModulesReplicaForests() {
57+
return deleteModulesReplicaForests;
58+
}
59+
60+
public void setDeleteModulesReplicaForests(boolean deleteModulesReplicaForests) {
61+
this.deleteModulesReplicaForests = deleteModulesReplicaForests;
62+
}
63+
}

src/main/java/com/marklogic/mgmt/resource/restapis/RestApiManager.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.marklogic.mgmt.ManageClient;
66
import com.marklogic.mgmt.PayloadParser;
77
import com.marklogic.mgmt.resource.appservers.ServerManager;
8+
import com.marklogic.mgmt.resource.databases.DatabaseManager;
89
import com.marklogic.rest.util.Fragment;
910
import org.springframework.http.HttpMethod;
1011
import org.springframework.http.ResponseEntity;
@@ -51,21 +52,43 @@ public boolean restApiServerExists(String name) {
5152
/**
5253
* Will need to wait for MarkLogic to restart, so consider using AdminManager with this.
5354
*
54-
* @param serverName
55-
* @param groupName
56-
* @param includeModules
57-
* @param includeContent
55+
* @param request
5856
* @return
5957
*/
60-
public boolean deleteRestApi(String serverName, String groupName, boolean includeModules, boolean includeContent) {
61-
if (new ServerManager(client, groupName).exists(serverName)) {
62-
String path = format("/v1/rest-apis/%s?", serverName);
63-
if (includeModules) {
64-
path += "include=modules&";
65-
}
66-
if (includeContent) {
67-
path += "include=content";
58+
public boolean deleteRestApi(RestApiDeletionRequest request) {
59+
ServerManager serverManager = new ServerManager(client, request.getGroupName());
60+
final String serverName = request.getServerName();
61+
if (serverManager.exists(serverName)) {
62+
String path = format("/v1/rest-apis/%s", serverName);
63+
64+
if (request.isIncludeContent() || request.isIncludeContent()) {
65+
path += "?";
66+
67+
DatabaseManager databaseManager = new DatabaseManager(client);
68+
String payload = serverManager.getPropertiesAsJson(serverName);
69+
PayloadParser parser = new PayloadParser();
70+
71+
if (request.isIncludeModules()) {
72+
if (request.isDeleteModulesReplicaForests()) {
73+
String modulesDatabase = parser.getPayloadFieldValue(payload, "modules-database");
74+
if (databaseManager.exists(modulesDatabase)) {
75+
databaseManager.deleteReplicaForests(modulesDatabase);
76+
}
77+
}
78+
path += "include=modules&";
79+
}
80+
81+
if (request.isIncludeContent()) {
82+
if (request.isDeleteContentReplicaForests()) {
83+
String contentDatabase = parser.getPayloadFieldValue(payload, "content-database");
84+
if (databaseManager.exists(contentDatabase)) {
85+
databaseManager.deleteReplicaForests(contentDatabase);
86+
}
87+
}
88+
path += "include=content";
89+
}
6890
}
91+
6992
logger.info("Deleting REST API, path: " + path);
7093
client.getRestTemplate().exchange(client.buildUri(path), HttpMethod.DELETE, null, String.class);
7194
logger.info("Deleted REST API");

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public static void main(String[] args) {
2020
ManageClient manageClient = new ManageClient(config);
2121
AppConfig appConfig = new AppConfig();
2222
appConfig.setDatabaseNamesAndReplicaCounts("testdb,1");
23-
appConfig.setReplicaForestDataDirectory("/var/opt/MarkLogic/Replica");
24-
appConfig.setReplicaForestLargeDataDirectory("/var/opt/MarkLogic/Large");
25-
appConfig.setReplicaForestFastDataDirectory("/var/opt/MarkLogic/Fast");
2623
CommandContext context = new CommandContext(appConfig, manageClient, null);
2724

2825
DeployDatabaseCommand ddc = new DeployDatabaseCommand();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.marklogic.appdeployer.command.forests;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
5+
import com.marklogic.appdeployer.impl.SimpleAppDeployer;
6+
import com.marklogic.mgmt.ManageClient;
7+
import com.marklogic.mgmt.ManageConfig;
8+
import com.marklogic.mgmt.admin.AdminConfig;
9+
import com.marklogic.mgmt.admin.AdminManager;
10+
11+
/**
12+
* This program is used to verify that when a REST API server is deleted, the replica forests for both the content
13+
* and modules databases are deleted first if the command is configured to delete the content/modules databases. It
14+
* relies on replicas being created, so it's currently just a debug program instead of a test, as the test environment
15+
* is assumed to be a single host.
16+
*/
17+
public class UndeployModuleReplicaForestsDebug {
18+
19+
public static void main(String[] args) {
20+
final String host = args[0];
21+
final String password = args[1];
22+
23+
ManageConfig config = new ManageConfig(host, 8002, "admin", password);
24+
ManageClient manageClient = new ManageClient(config);
25+
26+
AdminConfig adminConfig = new AdminConfig(host, 8001, "admin", password);
27+
AdminManager adminManager = new AdminManager(adminConfig);
28+
29+
AppConfig appConfig = new AppConfig();
30+
appConfig.setName("testapp");
31+
appConfig.setDatabaseNamesAndReplicaCounts("testapp-modules,1,testapp-content,1");
32+
33+
DeployRestApiServersCommand restApiCommand = new DeployRestApiServersCommand();
34+
// Command is assumed to delete the modules database by default
35+
restApiCommand.setDeleteContentDatabase(true);
36+
37+
ConfigureForestReplicasCommand replicasCommand = new ConfigureForestReplicasCommand();
38+
39+
SimpleAppDeployer deployer = new SimpleAppDeployer(manageClient, adminManager, restApiCommand, replicasCommand);
40+
41+
deployer.deploy(appConfig);
42+
43+
/**
44+
* This should delete all of the replica forests for the content/modules databases first, and then the DELETE
45+
* call to /v1/rest-apis will delete the content and modules databases.
46+
*/
47+
deployer.undeploy(appConfig);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)