use of org.apache.helix.model.InstanceConfig 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.model.InstanceConfig in project helix by apache.
the class TestInstanceAccessor method testAddInstance.
@Test(dependsOnMethods = "updateInstance")
public void testAddInstance() throws IOException {
System.out.println("Start test :" + TestHelper.getTestMethodName());
InstanceConfig instanceConfig = new InstanceConfig(INSTANCE_NAME + "TEST");
Entity entity = Entity.entity(OBJECT_MAPPER.writeValueAsString(instanceConfig.getRecord()), MediaType.APPLICATION_JSON_TYPE);
put("clusters/" + CLUSTER_NAME + "/instances/" + INSTANCE_NAME, null, entity, Response.Status.OK.getStatusCode());
Assert.assertEquals(instanceConfig, _configAccessor.getInstanceConfig(CLUSTER_NAME, INSTANCE_NAME + "TEST"));
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class TestInstanceAccessor method updateInstanceConfig.
@Test(dependsOnMethods = "updateInstance")
public void updateInstanceConfig() throws IOException {
System.out.println("Start test :" + TestHelper.getTestMethodName());
String instanceName = CLUSTER_NAME + "localhost_12918";
InstanceConfig instanceConfig = _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName);
ZNRecord record = instanceConfig.getRecord();
record.getSimpleFields().put("TestSimple", "value");
record.getMapFields().put("TestMap", ImmutableMap.of("key", "value"));
record.getListFields().put("TestList", Arrays.asList("e1", "e2", "e3"));
Entity entity = Entity.entity(OBJECT_MAPPER.writeValueAsString(record), MediaType.APPLICATION_JSON_TYPE);
put("clusters/" + CLUSTER_NAME + "/instances/" + instanceName + "/configs", null, entity, Response.Status.OK.getStatusCode());
Assert.assertEquals(record.getSimpleFields(), _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord().getSimpleFields());
Assert.assertEquals(record.getListFields(), _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord().getListFields());
Assert.assertEquals(record.getMapFields(), _configAccessor.getInstanceConfig(CLUSTER_NAME, instanceName).getRecord().getMapFields());
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class TestIdealStateAssignment method testIdealStateAssignment.
@Test(dataProvider = "IdealStateInput")
public void testIdealStateAssignment(String clusterName, List<String> instances, List<String> partitions, String numReplicas, String stateModeDef, String strategyName, Map<String, Map<String, String>> expectedMapping) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
ClusterConfig clusterConfig = new ClusterConfig(clusterName);
List<InstanceConfig> instanceConfigs = new ArrayList<>();
for (String instance : instances) {
instanceConfigs.add(new InstanceConfig(instance));
}
IdealState idealState = new IdealState("TestResource");
idealState.setStateModelDefRef(stateModeDef);
idealState.setNumPartitions(partitions.size());
idealState.setReplicas(numReplicas);
Map<String, Map<String, String>> idealStateMapping = HelixUtil.getIdealAssignmentForFullAuto(clusterConfig, instanceConfigs, instances, idealState, partitions, strategyName);
Assert.assertEquals(idealStateMapping, expectedMapping);
}
use of org.apache.helix.model.InstanceConfig in project helix by apache.
the class InstanceAccessor method getInstanceConfig.
@GET
@Path("{instanceName}/configs")
public Response getInstanceConfig(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName) throws IOException {
HelixDataAccessor accessor = getDataAccssor(clusterId);
InstanceConfig instanceConfig = accessor.getProperty(accessor.keyBuilder().instanceConfig(instanceName));
if (instanceConfig != null) {
return JSONRepresentation(instanceConfig.getRecord());
}
return notFound();
}
Aggregations