Search in sources :

Example 51 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ZkChildResource method readZkChild.

private ZNRecord readZkChild(String zkPath, ZkClient zkClient) {
    ZNRecord result = null;
    // read data and stat
    Stat stat = new Stat();
    ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true);
    if (data != null) {
        result = data;
    } else {
        result = new ZNRecord("");
    }
    // read childrenList
    List<String> children = zkClient.getChildren(zkPath);
    if (children != null && children.size() > 0) {
        result.setSimpleField("numChildren", "" + children.size());
        result.setListField("childrenList", children);
    } else {
        result.setSimpleField("numChildren", "" + 0);
    }
    return result;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZNRecord(org.apache.helix.ZNRecord)

Example 52 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ZkPathResource method get.

@Override
public Representation get() {
    StringRepresentation presentation = null;
    String zkPath = getZKPath();
    try {
        ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
        ZNRecord result = readZkDataStatAndChild(zkPath, zkClient);
        presentation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(result), MediaType.APPLICATION_JSON);
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("Error in read zkPath: " + zkPath, e);
    }
    return presentation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException)

Example 53 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ZkPathResource method readZkDataStatAndChild.

private ZNRecord readZkDataStatAndChild(String zkPath, ZkClient zkClient) {
    ZNRecord result = null;
    // read data and stat
    Stat stat = new Stat();
    ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true);
    if (data != null) {
        result = data;
    } else {
        result = new ZNRecord("");
    }
    result.setSimpleField("zkPath", zkPath);
    result.setSimpleField("stat", stat.toString());
    result.setSimpleField("numChildren", "" + stat.getNumChildren());
    result.setSimpleField("ctime", "" + new Date(stat.getCtime()));
    result.setSimpleField("mtime", "" + new Date(stat.getMtime()));
    result.setSimpleField("dataLength", "" + stat.getDataLength());
    // read childrenList
    List<String> children = zkClient.getChildren(zkPath);
    if (children != null && children.size() > 0) {
        result.setListField("children", children);
    }
    return result;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZNRecord(org.apache.helix.ZNRecord) Date(java.util.Date)

Example 54 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class TestClusterManagementWebapp method verifyEnableInstance.

void verifyEnableInstance() throws JsonGenerationException, JsonMappingException, IOException {
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances/" + instance1 + "_" + instancePort;
    Map<String, String> paraMap = new HashMap<String, String>();
    // Add 1 instance
    paraMap.put(JsonParameters.ENABLED, "" + false);
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableInstance);
    Reference resourceRef = new Reference(httpUrlBase);
    Request request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    ObjectMapper mapper = new ObjectMapper();
    ZNRecord r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + false));
    // Then enable it
    paraMap.put(JsonParameters.ENABLED, "" + true);
    request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    response = _gClient.handle(request);
    result = response.getEntity();
    sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    mapper = new ObjectMapper();
    r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + true));
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) StringReader(java.io.StringReader) Representation(org.restlet.representation.Representation) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Example 55 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class TestClusterManagementWebapp method verifyConfigAccessor.

// verify get/post configs in different scopes
void verifyConfigAccessor() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    // set/get cluster scope configs
    String url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/cluster/" + clusterName;
    postConfig(_gClient, url, mapper, ClusterSetup.setConfig, "key1=value1,key2=value2");
    ZNRecord record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getSimpleFields().size(), 2);
    Assert.assertEquals(record.getSimpleField("key1"), "value1");
    Assert.assertEquals(record.getSimpleField("key2"), "value2");
    // set/get participant scope configs
    String participantName = "test2_9999";
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/participant/" + participantName;
    postConfig(_gClient, url, mapper, ClusterSetup.setConfig, "key3=value3,key4=value4");
    record = get(_gClient, url, mapper);
    Assert.assertTrue(record.getSimpleFields().size() >= 2, "Should at least contains 2 keys");
    Assert.assertEquals(record.getSimpleField("key3"), "value3");
    Assert.assertEquals(record.getSimpleField("key4"), "value4");
    // set/get resource scope configs
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/resource/testResource";
    postConfig(_gClient, url, mapper, ClusterSetup.setConfig, "key5=value5,key6=value6");
    record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getSimpleFields().size(), 2);
    Assert.assertEquals(record.getSimpleField("key5"), "value5");
    Assert.assertEquals(record.getSimpleField("key6"), "value6");
    // set/get partition scope configs
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/partition/testResource/testPartition";
    postConfig(_gClient, url, mapper, ClusterSetup.setConfig, "key7=value7,key8=value8");
    record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getSimpleFields().size(), 2);
    Assert.assertEquals(record.getSimpleField("key7"), "value7");
    Assert.assertEquals(record.getSimpleField("key8"), "value8");
    // list keys
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs";
    record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getListFields().size(), 1);
    Assert.assertTrue(record.getListFields().containsKey("scopes"));
    Assert.assertTrue(contains(record.getListField("scopes"), "CLUSTER", "PARTICIPANT", "RESOURCE", "PARTITION"));
    // url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/cluster";
    // record = get(client, url, mapper);
    // Assert.assertEquals(record.getListFields().size(), 1);
    // Assert.assertTrue(record.getListFields().containsKey("CLUSTER"));
    // Assert.assertTrue(contains(record.getListField("CLUSTER"), clusterName), "record: " +
    // record);
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/participant";
    record = get(_gClient, url, mapper);
    Assert.assertTrue(record.getListFields().containsKey("PARTICIPANT"));
    Assert.assertTrue(contains(record.getListField("PARTICIPANT"), participantName));
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/resource";
    record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getListFields().size(), 1);
    Assert.assertTrue(record.getListFields().containsKey("RESOURCE"));
    Assert.assertTrue(contains(record.getListField("RESOURCE"), "testResource"));
    url = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/configs/partition/testResource";
    record = get(_gClient, url, mapper);
    Assert.assertEquals(record.getListFields().size(), 1);
    Assert.assertTrue(record.getListFields().containsKey("PARTITION"));
    Assert.assertTrue(contains(record.getListField("PARTITION"), "testPartition"));
}
Also used : ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ZNRecord (org.apache.helix.ZNRecord)448 Test (org.testng.annotations.Test)186 ArrayList (java.util.ArrayList)117 Date (java.util.Date)111 HelixDataAccessor (org.apache.helix.HelixDataAccessor)91 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)78 Builder (org.apache.helix.PropertyKey.Builder)75 HashMap (java.util.HashMap)72 IdealState (org.apache.helix.model.IdealState)69 PropertyKey (org.apache.helix.PropertyKey)61 HelixException (org.apache.helix.HelixException)47 Map (java.util.Map)41 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)40 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)40 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)33 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)30 List (java.util.List)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 HelixAdmin (org.apache.helix.HelixAdmin)28 LiveInstance (org.apache.helix.model.LiveInstance)28