use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class JobResource method getHostedEntitiesRepresentation.
StringRepresentation getHostedEntitiesRepresentation(String clusterName, String jobQueueName, String jobName) throws Exception {
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
// Get job queue config
String namespacedJobName = TaskUtil.getNamespacedJobName(jobQueueName, jobName);
HelixProperty jobConfig = accessor.getProperty(keyBuilder.resourceConfig(namespacedJobName));
TaskDriver taskDriver = new TaskDriver(zkClient, clusterName);
// Get job queue context
JobContext ctx = taskDriver.getJobContext(namespacedJobName);
// Create the result
ZNRecord hostedEntitiesRecord = new ZNRecord(namespacedJobName);
if (jobConfig != null) {
hostedEntitiesRecord.merge(jobConfig.getRecord());
}
if (ctx != null) {
hostedEntitiesRecord.merge(ctx.getRecord());
}
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class SchedulerTasksResource method getSchedulerTasksRepresentation.
StringRepresentation getSchedulerTasksRepresentation() throws JsonGenerationException, JsonMappingException, IOException {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
String instanceName = (String) getRequest().getAttributes().get("instanceName");
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
String sessionId = liveInstance.getSessionId();
// (ClusterRepresentationUtil.ObjectToJson(instanceConfigs),
StringRepresentation representation = new StringRepresentation("");
return representation;
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class StateModelsResource method post.
@Override
public Representation post(Representation entity) {
try {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
;
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
if (command.equalsIgnoreCase(ClusterSetup.addStateModelDef)) {
ZNRecord newStateModel = jsonParameters.getExtraParameter(JsonParameters.NEW_STATE_MODEL_DEF);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
accessor.setProperty(accessor.keyBuilder().stateModelDef(newStateModel.getId()), new StateModelDefinition(newStateModel));
getResponse().setEntity(getStateModelsRepresentation());
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addStateModelDef + "]");
}
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("Error in posting " + entity, e);
}
return null;
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class InstancesResource method getInstancesRepresentation.
StringRepresentation getInstancesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
Map<String, LiveInstance> liveInstancesMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
Map<String, InstanceConfig> instanceConfigsMap = accessor.getChildValuesMap(accessor.keyBuilder().instanceConfigs());
Map<String, List<String>> tagInstanceLists = new TreeMap<String, List<String>>();
for (String instanceName : instanceConfigsMap.keySet()) {
boolean isAlive = liveInstancesMap.containsKey(instanceName);
instanceConfigsMap.get(instanceName).getRecord().setSimpleField("Alive", isAlive + "");
InstanceConfig config = instanceConfigsMap.get(instanceName);
for (String tag : config.getTags()) {
if (!tagInstanceLists.containsKey(tag)) {
tagInstanceLists.put(tag, new LinkedList<String>());
}
if (!tagInstanceLists.get(tag).contains(instanceName)) {
tagInstanceLists.get(tag).add(instanceName);
}
}
}
// Wrap raw data into an object, then serialize it
List<ZNRecord> recordList = Lists.newArrayList();
for (InstanceConfig instanceConfig : instanceConfigsMap.values()) {
recordList.add(instanceConfig.getRecord());
}
ListInstancesWrapper wrapper = new ListInstancesWrapper();
wrapper.instanceInfo = recordList;
wrapper.tagInfo = tagInstanceLists;
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ObjectToJson(wrapper), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class TestClusterAccessor method testEnableDisableMaintenanceMode.
@Test(dependsOnMethods = "testGetClusterConfig")
public void testEnableDisableMaintenanceMode() {
System.out.println("Start test :" + TestHelper.getTestMethodName());
String cluster = _clusters.iterator().next();
String reason = "Test reason";
HelixDataAccessor accessor = new ZKHelixDataAccessor(cluster, _baseAccessor);
post("clusters/" + cluster, ImmutableMap.of("command", "enableMaintenanceMode"), Entity.entity(reason, MediaType.APPLICATION_JSON_TYPE), Response.Status.OK.getStatusCode());
MaintenanceSignal signal = accessor.getProperty(accessor.keyBuilder().maintenance());
Assert.assertNotNull(signal);
Assert.assertEquals(reason, signal.getReason());
post("clusters/" + cluster, ImmutableMap.of("command", "disableMaintenanceMode"), Entity.entity(new String(), MediaType.APPLICATION_JSON_TYPE), Response.Status.OK.getStatusCode());
Assert.assertNull(accessor.getProperty(accessor.keyBuilder().maintenance()));
}
Aggregations