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();
}
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;
}
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);
}
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();
}
}
}
}
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();
}
Aggregations