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;
}
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;
}
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;
}
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));
}
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"));
}
Aggregations