Search in sources :

Example 91 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ResourceAccessor method getResourceExternalView.

@GET
@Path("{resourceName}/externalView")
public Response getResourceExternalView(@PathParam("clusterId") String clusterId, @PathParam("resourceName") String resourceName) {
    HelixAdmin admin = getHelixAdmin();
    ExternalView externalView = admin.getResourceExternalView(clusterId, resourceName);
    if (externalView != null) {
        return JSONRepresentation(externalView.getRecord());
    }
    return notFound();
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixAdmin(org.apache.helix.HelixAdmin) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 92 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ResourceAccessor method computePartitionHealth.

private Map<String, String> computePartitionHealth(String clusterId, String resourceName) {
    HelixAdmin admin = getHelixAdmin();
    IdealState idealState = admin.getResourceIdealState(clusterId, resourceName);
    ExternalView externalView = admin.getResourceExternalView(clusterId, resourceName);
    StateModelDefinition stateModelDef = admin.getStateModelDef(clusterId, idealState.getStateModelDefRef());
    String initialState = stateModelDef.getInitialState();
    List<String> statesPriorityList = stateModelDef.getStatesPriorityList();
    // Trim stateList to initialState and above
    statesPriorityList = statesPriorityList.subList(0, statesPriorityList.indexOf(initialState));
    int minActiveReplicas = idealState.getMinActiveReplicas();
    // Start the logic that determines the health status of each partition
    Map<String, String> partitionHealthResult = new HashMap<>();
    Set<String> allPartitionNames = idealState.getPartitionSet();
    if (!allPartitionNames.isEmpty()) {
        for (String partitionName : allPartitionNames) {
            int replicaCount = idealState.getReplicaCount(idealState.getPreferenceList(partitionName).size());
            // Simplify expectedStateCountMap by assuming that all instances are available to reduce computation load on this REST endpoint
            LinkedHashMap<String, Integer> expectedStateCountMap = stateModelDef.getStateCountMap(replicaCount, replicaCount);
            // Extract all states into Collections from ExternalView
            Map<String, String> stateMapInExternalView = externalView.getStateMap(partitionName);
            Collection<String> allReplicaStatesInExternalView = (stateMapInExternalView != null && !stateMapInExternalView.isEmpty()) ? stateMapInExternalView.values() : Collections.<String>emptyList();
            int numActiveReplicasInExternalView = 0;
            HealthStatus status = HealthStatus.HEALTHY;
            // Go through all states that are "active" states (higher priority than InitialState)
            for (int statePriorityIndex = 0; statePriorityIndex < statesPriorityList.size(); statePriorityIndex++) {
                String currentState = statesPriorityList.get(statePriorityIndex);
                int currentStateCountInIdealState = expectedStateCountMap.get(currentState);
                int currentStateCountInExternalView = Collections.frequency(allReplicaStatesInExternalView, currentState);
                numActiveReplicasInExternalView += currentStateCountInExternalView;
                // Top state counts must match, if not, unhealthy
                if (statePriorityIndex == 0 && currentStateCountInExternalView != currentStateCountInIdealState) {
                    status = HealthStatus.UNHEALTHY;
                    break;
                } else if (currentStateCountInExternalView < currentStateCountInIdealState) {
                    // For non-top states, if count in ExternalView is less than count in IdealState, partially healthy
                    status = HealthStatus.PARTIAL_HEALTHY;
                }
            }
            if (numActiveReplicasInExternalView < minActiveReplicas) {
                // If this partition does not satisfy the number of minimum active replicas, unhealthy
                status = HealthStatus.UNHEALTHY;
            }
            partitionHealthResult.put(partitionName, status.name());
        }
    }
    return partitionHealthResult;
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) StateModelDefinition(org.apache.helix.model.StateModelDefinition)

Example 93 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ResourceAccessor method getResource.

@GET
@Path("{resourceName}")
public Response getResource(@PathParam("clusterId") String clusterId, @PathParam("resourceName") String resourceName) {
    ConfigAccessor accessor = getConfigAccessor();
    HelixAdmin admin = getHelixAdmin();
    ResourceConfig resourceConfig = accessor.getResourceConfig(clusterId, resourceName);
    IdealState idealState = admin.getResourceIdealState(clusterId, resourceName);
    ExternalView externalView = admin.getResourceExternalView(clusterId, resourceName);
    Map<String, ZNRecord> resourceMap = new HashMap<>();
    if (idealState != null) {
        resourceMap.put(ResourceProperties.idealState.name(), idealState.getRecord());
    } else {
        return notFound();
    }
    resourceMap.put(ResourceProperties.resourceConfig.name(), null);
    resourceMap.put(ResourceProperties.externalView.name(), null);
    if (resourceConfig != null) {
        resourceMap.put(ResourceProperties.resourceConfig.name(), resourceConfig.getRecord());
    }
    if (externalView != null) {
        resourceMap.put(ResourceProperties.externalView.name(), externalView.getRecord());
    }
    return JSONRepresentation(resourceMap);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConfigAccessor(org.apache.helix.ConfigAccessor) ResourceConfig(org.apache.helix.model.ResourceConfig) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) ZNRecord(org.apache.helix.ZNRecord) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 94 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class LockManagerDemo method main.

/**
 * LockManagerDemo clusterName, numInstances, lockGroupName, numLocks
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    final String zkAddress = "localhost:2199";
    final String clusterName = "lock-manager-demo";
    final String lockGroupName = "lock-group";
    final int numInstances = 3;
    final int numPartitions = 12;
    final boolean startController = false;
    HelixManager controllerManager = null;
    Thread[] processArray;
    processArray = new Thread[numInstances];
    try {
        startLocalZookeeper(2199);
        HelixAdmin admin = new ZKHelixAdmin(zkAddress);
        admin.addCluster(clusterName, true);
        StateModelConfigGenerator generator = new StateModelConfigGenerator();
        admin.addStateModelDef(clusterName, "OnlineOffline", new StateModelDefinition(generator.generateConfigForOnlineOffline()));
        admin.addResource(clusterName, lockGroupName, numPartitions, "OnlineOffline", RebalanceMode.FULL_AUTO.toString());
        admin.rebalance(clusterName, lockGroupName, 1);
        for (int i = 0; i < numInstances; i++) {
            final String instanceName = "localhost_" + (12000 + i);
            processArray[i] = new Thread(new Runnable() {

                @Override
                public void run() {
                    LockProcess lockProcess = null;
                    try {
                        lockProcess = new LockProcess(clusterName, zkAddress, instanceName, startController);
                        lockProcess.start();
                        Thread.currentThread().join();
                    } catch (InterruptedException e) {
                        System.out.println(instanceName + "Interrupted");
                        if (lockProcess != null) {
                            lockProcess.stop();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            processArray[i].start();
        }
        Thread.sleep(3000);
        controllerManager = HelixControllerMain.startHelixController(zkAddress, clusterName, "controller", HelixControllerMain.STANDALONE);
        Thread.sleep(5000);
        printStatus(admin, clusterName, lockGroupName);
        System.out.println("Stopping localhost_12000");
        processArray[0].interrupt();
        Thread.sleep(3000);
        printStatus(admin, clusterName, lockGroupName);
        Thread.currentThread().join();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (controllerManager != null) {
            controllerManager.disconnect();
        }
        for (Thread process : processArray) {
            if (process != null) {
                process.interrupt();
            }
        }
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) StateModelConfigGenerator(org.apache.helix.tools.StateModelConfigGenerator)

Example 95 with HelixAdmin

use of org.apache.helix.HelixAdmin in project helix by apache.

the class ClusterAccessor method updateCluster.

@POST
@Path("{clusterId}")
public Response updateCluster(@PathParam("clusterId") String clusterId, @QueryParam("command") String commandStr, @QueryParam("superCluster") String superCluster, String content) {
    Command command;
    try {
        command = getCommand(commandStr);
    } catch (HelixException ex) {
        return badRequest(ex.getMessage());
    }
    ClusterSetup clusterSetup = getClusterSetup();
    HelixAdmin helixAdmin = getHelixAdmin();
    switch(command) {
        case activate:
            if (superCluster == null) {
                return badRequest("Super Cluster name is missing!");
            }
            try {
                clusterSetup.activateCluster(clusterId, superCluster, true);
            } catch (Exception ex) {
                _logger.error("Failed to add cluster " + clusterId + " to super cluster " + superCluster);
                return serverError(ex);
            }
            break;
        case expand:
            try {
                clusterSetup.expandCluster(clusterId);
            } catch (Exception ex) {
                _logger.error("Failed to expand cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case enable:
            try {
                helixAdmin.enableCluster(clusterId, true);
            } catch (Exception ex) {
                _logger.error("Failed to enable cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case disable:
            try {
                helixAdmin.enableCluster(clusterId, false);
            } catch (Exception ex) {
                _logger.error("Failed to disable cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case enableMaintenanceMode:
            try {
                helixAdmin.enableMaintenanceMode(clusterId, true, content);
            } catch (Exception ex) {
                _logger.error("Failed to enable maintenance mode " + clusterId);
                return serverError(ex);
            }
            break;
        case disableMaintenanceMode:
            try {
                helixAdmin.enableMaintenanceMode(clusterId, false);
            } catch (Exception ex) {
                _logger.error("Failed to disable maintenance mode " + clusterId);
                return serverError(ex);
            }
            break;
        default:
            return badRequest("Unsupported command " + command);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

HelixAdmin (org.apache.helix.HelixAdmin)104 IdealState (org.apache.helix.model.IdealState)44 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)43 Test (org.testng.annotations.Test)40 HashMap (java.util.HashMap)30 ZNRecord (org.apache.helix.ZNRecord)28 Date (java.util.Date)22 InstanceConfig (org.apache.helix.model.InstanceConfig)22 ArrayList (java.util.ArrayList)18 Map (java.util.Map)17 HashSet (java.util.HashSet)16 ExternalView (org.apache.helix.model.ExternalView)16 StateModelDefinition (org.apache.helix.model.StateModelDefinition)16 IOException (java.io.IOException)13 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)13 HelixDataAccessor (org.apache.helix.HelixDataAccessor)12 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)12 TreeMap (java.util.TreeMap)11 PropertyKey (org.apache.helix.PropertyKey)11 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)11