use of org.apache.helix.ZNRecord in project helix by apache.
the class ConfigResource method getConfigs.
StringRepresentation getConfigs(ConfigScopeProperty scopeProperty, String... keys) throws Exception {
StringRepresentation representation = null;
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
HelixAdmin admin = setupTool.getClusterManagementTool();
ZNRecord record = new ZNRecord(scopeProperty + " Config");
HelixConfigScope scope = new HelixConfigScopeBuilder(scopeProperty, keys).build();
List<String> configKeys = admin.getConfigKeys(scope);
Map<String, String> configs = admin.getConfig(scope, configKeys);
record.setSimpleFields(configs);
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class ConstraintResource method get.
/**
* List all constraints
* <p>
* Usage: <code>curl http://{host:port}/clusters/{clusterName}/constraints/MESSAGE_CONSTRAINT
*/
@Override
public Representation get() {
StringRepresentation representation = null;
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String constraintTypeStr = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_TYPE);
String constraintId = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_ID);
try {
ConstraintType constraintType = ConstraintType.valueOf(constraintTypeStr);
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
HelixAdmin admin = new ZKHelixAdmin(zkClient);
ZNRecord record = admin.getConstraints(clusterName, constraintType).getRecord();
if (constraintId == null) {
// get all message constraints
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
} else {
// get a specific constraint
Map<String, String> constraint = record.getMapField(constraintId);
if (constraint == null) {
representation = new StringRepresentation("No constraint of type: " + constraintType + " associated with id: " + constraintId, MediaType.APPLICATION_JSON);
} else {
ZNRecord subRecord = new ZNRecord(record.getId());
subRecord.setMapField(constraintId, constraint);
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(subRecord), MediaType.APPLICATION_JSON);
}
}
} catch (IllegalArgumentException e) {
representation = new StringRepresentation("constraint-type: " + constraintTypeStr + " not recognized.", MediaType.APPLICATION_JSON);
} catch (Exception e) {
String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
representation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
LOG.error("Exception get constraints", e);
}
return representation;
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class ControllerResource method getControllerRepresentation.
StringRepresentation getControllerRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
ZNRecord record = null;
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
if (leader != null) {
record = leader.getRecord();
} else {
record = new ZNRecord("");
DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss.SSSSSS");
String time = formatter.format(new Date());
Map<String, String> contentMap = new TreeMap<String, String>();
contentMap.put("AdditionalInfo", "No leader exists");
record.setMapField(Level.HELIX_INFO + "-" + time, contentMap);
}
boolean paused = (accessor.getProperty(keyBuilder.pause()) == null ? false : true);
record.setSimpleField(PropertyType.PAUSE.toString(), "" + paused);
String retVal = ClusterRepresentationUtil.ZNRecordToJson(record);
StringRepresentation representation = new StringRepresentation(retVal, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.ZNRecord 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.ZNRecord 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;
}
Aggregations