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

Commit

Permalink
#56 ConfigureForestReplicasCommand now has an undo method
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Sep 22, 2015
1 parent 1c48d7a commit 69a5902
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ TODO Reconcile this with what's in gradle.properties

## 2.0b9

* [#55](https://github.com/rjrudin/ml-app-deployer/issues/56) ForestManager now has methods for simplifying the
deletion of replica forests.
* [#56](https://github.com/rjrudin/ml-app-deployer/issues/56) ConfigureForestReplicasCommand now supports "undo", using
new methods in ForestManager to delete replica forests. Also extracted AbstractUndoableCommand.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@
* Provides a basic implementation for creating/updating a resource while an app is being deployed and then deleting it
* while the app is being undeployed.
*/
public abstract class AbstractResourceCommand extends AbstractCommand implements UndoableCommand {
public abstract class AbstractResourceCommand extends AbstractUndoableCommand {

private boolean deleteResourcesOnUndo = true;
private boolean restartAfterDelete = false;
private int undoSortOrder = Integer.MAX_VALUE;

protected abstract File[] getResourceDirs(CommandContext context);

protected abstract ResourceManager getResourceManager(CommandContext context);

@Override
public Integer getUndoSortOrder() {
return undoSortOrder;
}

@Override
public void execute(CommandContext context) {
for (File resourceDir : getResourceDirs(context)) {
Expand Down Expand Up @@ -94,8 +88,4 @@ public boolean isDeleteResourcesOnUndo() {
public boolean isRestartAfterDelete() {
return restartAfterDelete;
}

public void setUndoSortOrder(int undoSortOrder) {
this.undoSortOrder = undoSortOrder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rjrudin.marklogic.appdeployer.command;

/**
* Useful base class for implementing UndoableCommand, allowing you to configure the undo sort order easily.
*/
public abstract class AbstractUndoableCommand extends AbstractCommand implements UndoableCommand {

private int undoSortOrder = Integer.MAX_VALUE;

@Override
public Integer getUndoSortOrder() {
return undoSortOrder;
}

public void setUndoSortOrder(int undoSortOrder) {
this.undoSortOrder = undoSortOrder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.Map;

import com.rjrudin.marklogic.appdeployer.command.AbstractCommand;
import com.rjrudin.marklogic.appdeployer.command.AbstractUndoableCommand;
import com.rjrudin.marklogic.appdeployer.command.CommandContext;
import com.rjrudin.marklogic.mgmt.forests.ForestManager;
import com.rjrudin.marklogic.mgmt.hosts.HostManager;
Expand All @@ -14,9 +14,10 @@
* for the out-of-the-box forests such as Security, Schemas, App-Services, and Meters, which normally need replicas for
* failover in a cluster.
*/
public class ConfigureForestReplicasCommand extends AbstractCommand {
public class ConfigureForestReplicasCommand extends AbstractUndoableCommand {

private Map<String, Integer> forestNamesAndReplicaCounts = new HashMap<>();
private boolean deleteReplicasOnUndo = true;

/**
* By default, the execute sort order is Integer.MAX_VALUE as a way of guaranteeing that the referenced primary
Expand Down Expand Up @@ -59,9 +60,7 @@ public void execute(CommandContext context) {
*/
protected void configureReplicaForests(String primaryForestName, int replicaCount, List<String> hostIds,
ForestManager forestMgr) {
if (logger.isInfoEnabled()) {
logger.info("Configuring forest replicas for primary forest: " + primaryForestName);
}
logger.info(format("Configuring forest replicas for primary forest %s", primaryForestName));

String primaryForestHostId = forestMgr.getHostId(primaryForestName);

Expand All @@ -81,6 +80,22 @@ protected void configureReplicaForests(String primaryForestName, int replicaCoun
if (!replicaNamesAndHostIds.isEmpty()) {
forestMgr.setReplicas(primaryForestName, replicaNamesAndHostIds);
}

logger.info(format("Finished configuring forest replicas for primary forest %s", primaryForestName));
}

@Override
public void undo(CommandContext context) {
if (deleteReplicasOnUndo) {
ForestManager mgr = new ForestManager(context.getManageClient());
for (String forestName : forestNamesAndReplicaCounts.keySet()) {
logger.info(format("Deleting forest replicas for primary forest %s", forestName));
mgr.deleteReplicas(forestName);
logger.info(format("Finished deleting forest replicas for primary forest %s", forestName));
}
} else {
logger.info("deleteReplicasOnUndo is set to false, so not deleting any replicas");
}
}

public void setForestNamesAndReplicaCounts(Map<String, Integer> forestNamesAndReplicaCounts) {
Expand All @@ -90,4 +105,8 @@ public void setForestNamesAndReplicaCounts(Map<String, Integer> forestNamesAndRe
public Map<String, Integer> getForestNamesAndReplicaCounts() {
return forestNamesAndReplicaCounts;
}

public void setDeleteReplicasOnUndo(boolean deleteReplicasOnUndo) {
this.deleteReplicasOnUndo = deleteReplicasOnUndo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.rjrudin.marklogic.appdeployer.command.CommandContext;
import com.rjrudin.marklogic.mgmt.ManageClient;
import com.rjrudin.marklogic.mgmt.ManageConfig;
import com.rjrudin.marklogic.mgmt.databases.DatabaseManager;

/**
* Not an actual test, as this depends on an environment with multiple hosts, which is normally not the case on a
Expand All @@ -21,13 +20,13 @@ public static void main(String[] args) {
AppConfig appConfig = new AppConfig();
CommandContext context = new CommandContext(appConfig, manageClient, null);

// Configure replicas
ConfigureForestReplicasCommand command = new ConfigureForestReplicasCommand();
command.getForestNamesAndReplicaCounts().put("Security", 1);
command.getForestNamesAndReplicaCounts().put("Schemas", 2);
command.execute(context);

DatabaseManager dbMgr = new DatabaseManager(manageClient);
dbMgr.deleteReplicaForests("Security");
dbMgr.deleteReplicaForests("Schemas");
// And then delete those replicas
command.undo(context);
}
}

0 comments on commit 69a5902

Please sign in to comment.