use of org.apache.helix.model.LiveInstance in project helix by apache.
the class RoutingTable method refresh.
private void refresh(Map<String, Map<String, Map<String, CurrentState>>> currentStateMap) {
Map<String, InstanceConfig> instanceConfigMap = new HashMap<>();
for (InstanceConfig config : _instanceConfigs) {
instanceConfigMap.put(config.getId(), config);
}
for (LiveInstance liveInstance : _liveInstances) {
String instanceName = liveInstance.getInstanceName();
String sessionId = liveInstance.getSessionId();
InstanceConfig instanceConfig = instanceConfigMap.get(instanceName);
if (instanceConfig == null) {
logger.error("Invalid instance name. " + instanceName + " .Not found in /cluster/configs/. instanceName: ");
}
Map<String, CurrentState> currentStates = Collections.emptyMap();
if (currentStateMap.containsKey(instanceName) && currentStateMap.get(instanceName).containsKey(sessionId)) {
currentStates = currentStateMap.get(instanceName).get(sessionId);
}
for (CurrentState currentState : currentStates.values()) {
String resourceName = currentState.getResourceName();
Map<String, String> stateMap = currentState.getPartitionStateMap();
for (String partitionName : stateMap.keySet()) {
String state = stateMap.get(partitionName);
addEntry(resourceName, partitionName, state, instanceConfig);
}
}
}
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestClusterSetup method testDropInstance.
@Test
public void testDropInstance() throws Exception {
// drop without stop, should throw exception
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
10, // number of nodes
5, // replicas
3, "MasterSlave", // do rebalance
true);
// add fake liveInstance
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = new Builder(clusterName);
LiveInstance liveInstance = new LiveInstance("localhost_12918");
liveInstance.setSessionId("session_0");
liveInstance.setHelixVersion("version_0");
accessor.setProperty(keyBuilder.liveInstance("localhost_12918"), liveInstance);
// drop without stop the process, should throw exception
try {
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.fail("Should throw exception since localhost_12918 is still in LIVEINSTANCES/");
} catch (Exception e) {
// OK
}
accessor.removeProperty(keyBuilder.liveInstance("localhost_12918"));
// drop without disable, should throw exception
try {
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.fail("Should throw exception since localhost_12918 is enabled");
} catch (Exception e) {
// e.printStackTrace();
// OK
}
// drop it
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableInstance", clusterName, "localhost_12918", "false" });
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.assertNull(accessor.getProperty(keyBuilder.instanceConfig("localhost_12918")), "Instance config should be dropped");
Assert.assertFalse(_gZkClient.exists(PropertyPathBuilder.instance(clusterName, "localhost_12918")), "Instance/host should be dropped");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class InstanceAccessor method getInstance.
@GET
@Path("{instanceName}")
public Response getInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
Map<String, Object> instanceMap = new HashMap<>();
instanceMap.put(Properties.id.name(), JsonNodeFactory.instance.textNode(instanceName));
instanceMap.put(InstanceProperties.liveInstance.name(), null);
InstanceConfig instanceConfig = accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName));
LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
if (instanceConfig != null) {
instanceMap.put(InstanceProperties.config.name(), instanceConfig.getRecord());
} else {
return notFound();
}
if (liveInstance != null) {
instanceMap.put(InstanceProperties.liveInstance.name(), liveInstance.getRecord());
}
return JSONRepresentation(instanceMap);
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class ClusterAccessor method getClusterController.
@GET
@Path("{clusterId}/controller")
public Response getClusterController(@PathParam("clusterId") String clusterId) {
HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
Map<String, Object> controllerInfo = new HashMap<>();
controllerInfo.put(Properties.id.name(), clusterId);
LiveInstance leader = dataAccessor.getProperty(dataAccessor.keyBuilder().controllerLeader());
if (leader != null) {
controllerInfo.put(ClusterProperties.controller.name(), leader.getInstanceName());
controllerInfo.putAll(leader.getRecord().getSimpleFields());
} else {
controllerInfo.put(ClusterProperties.controller.name(), "No Lead Controller!");
}
return JSONRepresentation(controllerInfo);
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class ClusterResource method getClusterRepresentation.
StringRepresentation getClusterRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
ZNRecord clusterSummayRecord = new ZNRecord("Cluster Summary");
clusterSummayRecord.setListField("participants", instances);
List<String> resources = setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
clusterSummayRecord.setListField("resources", resources);
List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
clusterSummayRecord.setListField("stateModelDefs", models);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
Builder keyBuilder = accessor.keyBuilder();
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
if (leader != null) {
clusterSummayRecord.setSimpleField("LEADER", leader.getInstanceName());
} else {
clusterSummayRecord.setSimpleField("LEADER", "");
}
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clusterSummayRecord), MediaType.APPLICATION_JSON);
return representation;
}
Aggregations