use of org.apache.asterix.metadata.cluster.AddNodeWork in project asterixdb by apache.
the class ClusterLifecycleListener method executeWorkSet.
private void executeWorkSet(Set<IClusterManagementWork> workSet) {
int nodesToAdd = 0;
Set<String> nodesToRemove = new HashSet<>();
Set<AddNodeWork> nodeAdditionRequests = new HashSet<>();
Set<IClusterManagementWork> nodeRemovalRequests = new HashSet<>();
for (IClusterManagementWork w : workSet) {
switch(w.getClusterManagementWorkType()) {
case ADD_NODE:
if (nodesToAdd < ((AddNodeWork) w).getNumberOfNodesRequested()) {
nodesToAdd = ((AddNodeWork) w).getNumberOfNodesRequested();
}
nodeAdditionRequests.add((AddNodeWork) w);
break;
case REMOVE_NODE:
nodesToRemove.addAll(((RemoveNodeWork) w).getNodesToBeRemoved());
nodeRemovalRequests.add(w);
RemoveNodeWorkResponse response = new RemoveNodeWorkResponse((RemoveNodeWork) w, Status.IN_PROGRESS);
pendingWorkResponses.add(response);
break;
}
}
List<String> addedNodes = new ArrayList<>();
String asterixInstanceName = ClusterProperties.INSTANCE.getCluster().getInstanceName();
for (int i = 0; i < nodesToAdd; i++) {
Node node = ClusterStateManager.INSTANCE.getAvailableSubstitutionNode();
if (node != null) {
try {
ClusterManagerProvider.getClusterManager().addNode(appCtx, node);
addedNodes.add(asterixInstanceName + "_" + node.getId());
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Added NC at:" + node.getId());
}
} catch (AsterixException e) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("Unable to add NC at:" + node.getId());
}
e.printStackTrace();
}
} else {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("Unable to add NC: no more available nodes");
}
}
}
for (AddNodeWork w : nodeAdditionRequests) {
int n = w.getNumberOfNodesRequested();
List<String> nodesToBeAddedForWork = new ArrayList<>();
for (int i = 0; i < n && i < addedNodes.size(); i++) {
nodesToBeAddedForWork.add(addedNodes.get(i));
}
if (nodesToBeAddedForWork.isEmpty()) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Unable to satisfy request by " + w);
}
AddNodeWorkResponse response = new AddNodeWorkResponse(w, nodesToBeAddedForWork);
response.setStatus(Status.FAILURE);
w.getSourceSubscriber().notifyRequestCompletion(response);
} else {
AddNodeWorkResponse response = new AddNodeWorkResponse(w, nodesToBeAddedForWork);
pendingWorkResponses.add(response);
}
}
}
Aggregations