Search in sources :

Example 1 with AddNodeWork

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);
        }
    }
}
Also used : Node(org.apache.asterix.event.schema.cluster.Node) IClusterManagementWork(org.apache.asterix.common.api.IClusterManagementWork) ArrayList(java.util.ArrayList) AsterixException(org.apache.asterix.common.exceptions.AsterixException) RemoveNodeWorkResponse(org.apache.asterix.metadata.cluster.RemoveNodeWorkResponse) AddNodeWork(org.apache.asterix.metadata.cluster.AddNodeWork) AddNodeWorkResponse(org.apache.asterix.metadata.cluster.AddNodeWorkResponse) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 Node (org.apache.asterix.event.schema.cluster.Node)1 AddNodeWork (org.apache.asterix.metadata.cluster.AddNodeWork)1 AddNodeWorkResponse (org.apache.asterix.metadata.cluster.AddNodeWorkResponse)1 RemoveNodeWorkResponse (org.apache.asterix.metadata.cluster.RemoveNodeWorkResponse)1