Search in sources :

Example 26 with InstanceConfig

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) TreeMap(java.util.TreeMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) InstanceConfig(org.apache.helix.model.InstanceConfig) StringRepresentation(org.restlet.representation.StringRepresentation) List(java.util.List) LinkedList(java.util.LinkedList) ZNRecord(org.apache.helix.ZNRecord)

Example 27 with InstanceConfig

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"));
}
Also used : Entity(javax.ws.rs.client.Entity) InstanceConfig(org.apache.helix.model.InstanceConfig) Test(org.testng.annotations.Test)

Example 28 with InstanceConfig

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());
}
Also used : Entity(javax.ws.rs.client.Entity) InstanceConfig(org.apache.helix.model.InstanceConfig) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 29 with InstanceConfig

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);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) ArrayList(java.util.ArrayList) Map(java.util.Map) IdealState(org.apache.helix.model.IdealState) ClusterConfig(org.apache.helix.model.ClusterConfig) Test(org.testng.annotations.Test)

Example 30 with InstanceConfig

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();
}
Also used : HelixDataAccessor(org.apache.helix.HelixDataAccessor) InstanceConfig(org.apache.helix.model.InstanceConfig) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

InstanceConfig (org.apache.helix.model.InstanceConfig)149 ArrayList (java.util.ArrayList)40 Test (org.testng.annotations.Test)35 HashMap (java.util.HashMap)32 HashSet (java.util.HashSet)28 ZNRecord (org.apache.helix.ZNRecord)26 IdealState (org.apache.helix.model.IdealState)24 ExternalView (org.apache.helix.model.ExternalView)23 Map (java.util.Map)21 HelixException (org.apache.helix.HelixException)21 HelixAdmin (org.apache.helix.HelixAdmin)20 List (java.util.List)19 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)19 HelixDataAccessor (org.apache.helix.HelixDataAccessor)17 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)15 Test (org.junit.Test)15 Set (java.util.Set)13 VerifiableProperties (com.github.ambry.config.VerifiableProperties)12 IOException (java.io.IOException)12 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)12