use of org.apache.helix.ZNRecord in project helix by apache.
the class TestWtCacheSyncOpSingleThread method testHappyPathZkCacheBaseDataAccessor.
// TODO: add TestZkCacheSyncOpSingleThread
// TODO: add TestZkCacheAsyncOpMultiThread
@Test
public void testHappyPathZkCacheBaseDataAccessor() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// init zkCacheDataAccessor
String curStatePath = PropertyPathBuilder.instanceCurrentState(clusterName, "localhost_8901");
String extViewPath = PropertyPathBuilder.externalView(clusterName);
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
baseAccessor.create(curStatePath, null, AccessOption.PERSISTENT);
List<String> cachePaths = Arrays.asList(curStatePath, extViewPath);
ZkCacheBaseDataAccessor<ZNRecord> accessor = new ZkCacheBaseDataAccessor<ZNRecord>(baseAccessor, null, cachePaths, null);
boolean ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
// create 10 current states
for (int i = 0; i < 10; i++) {
String path = curStatePath + "/session_0/TestDB" + i;
boolean success = accessor.create(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
Assert.assertTrue(success, "Should succeed in create: " + path);
}
// verify wtCache
// TestHelper.printCache(accessor._wtCache);
ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
// update each current state 10 times, single thread
for (int i = 0; i < 10; i++) {
String path = curStatePath + "/session_0/TestDB" + i;
for (int j = 0; j < 10; j++) {
ZNRecord newRecord = new ZNRecord("TestDB" + i);
newRecord.setSimpleField("" + j, "" + j);
boolean success = accessor.update(path, new ZNRecordUpdater(newRecord), AccessOption.PERSISTENT);
Assert.assertTrue(success, "Should succeed in update: " + path);
}
}
// verify cache
// TestHelper.printCache(accessor._wtCache._cache);
ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
// set 10 external views
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.externalView(clusterName, "TestDB" + i);
boolean success = accessor.set(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
Assert.assertTrue(success, "Should succeed in set: " + path);
}
// verify wtCache
// accessor.printWtCache();
ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
// get 10 external views
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.externalView(clusterName, "TestDB" + i);
ZNRecord record = accessor.get(path, null, 0);
Assert.assertEquals(record.getId(), "TestDB" + i);
}
// getChildNames
List<String> childNames = accessor.getChildNames(extViewPath, 0);
// System.out.println(childNames);
Assert.assertEquals(childNames.size(), 10, "Should contain only: TestDB0-9");
for (int i = 0; i < 10; i++) {
Assert.assertTrue(childNames.contains("TestDB" + i));
}
// exists
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.instanceCurrentState(clusterName, "localhost_8901", "session_0", "TestDB" + i);
Assert.assertTrue(accessor.exists(path, 0));
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZKUtil method testCreateOrUpdate.
@Test()
public void testCreateOrUpdate() {
String path = PropertyPathBuilder.instanceConfig(clusterName, "id7");
ZNRecord record = new ZNRecord("id7");
ZKUtil.createOrMerge(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals("id7", record.getId());
record = new ZNRecord("id7");
List<String> list = Arrays.asList("value1", "value2");
record.setListField("list", list);
ZKUtil.createOrUpdate(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(list, record.getListField("list"));
record = new ZNRecord("id7");
List<String> list2 = Arrays.asList("value3", "value4");
record.setListField("list", list2);
ZKUtil.createOrUpdate(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(list2, record.getListField("list"));
Map<String, String> map = new HashMap<String, String>() {
{
put("k1", "v1");
}
};
record.setMapField("map", map);
ZKUtil.createOrUpdate(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(map, record.getMapField("map"));
record = new ZNRecord("id7");
Map<String, String> map2 = new HashMap<String, String>() {
{
put("k2", "v2");
}
};
record.setMapField("map", map2);
ZKUtil.createOrUpdate(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(new HashMap<String, String>() {
{
put("k2", "v2");
}
}, record.getMapField("map"));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZKUtil method testUpdateIfExists.
@Test()
public void testUpdateIfExists() {
String path = PropertyPathBuilder.instanceConfig(clusterName, "id3");
ZNRecord record = new ZNRecord("id4");
ZKUtil.updateIfExists(_zkClient, path, record, false);
AssertJUnit.assertFalse(_zkClient.exists(path));
_zkClient.createPersistent(path);
ZKUtil.updateIfExists(_zkClient, path, record, false);
AssertJUnit.assertTrue(_zkClient.exists(path));
record = _zkClient.<ZNRecord>readData(path);
AssertJUnit.assertEquals("id4", record.getId());
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZKUtil method testCreateOrMerge.
@Test()
public void testCreateOrMerge() {
String path = PropertyPathBuilder.instanceConfig(clusterName, "id7");
ZNRecord record = new ZNRecord("id7");
List<String> list = Arrays.asList("value1");
record.setListField("list", list);
ZKUtil.createOrMerge(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(list, record.getListField("list"));
record = new ZNRecord("id7");
List<String> list2 = Arrays.asList("value2");
record.setListField("list", list2);
ZKUtil.createOrMerge(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(Arrays.asList("value1", "value2"), record.getListField("list"));
Map<String, String> map = new HashMap<String, String>() {
{
put("k1", "v1");
}
};
record.setMapField("map", map);
ZKUtil.createOrMerge(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(map, record.getMapField("map"));
record = new ZNRecord("id7");
Map<String, String> map2 = new HashMap<String, String>() {
{
put("k2", "v2");
}
};
record.setMapField("map", map2);
ZKUtil.createOrMerge(_zkClient, path, record, true, true);
record = _zkClient.readData(path);
AssertJUnit.assertEquals(new HashMap<String, String>() {
{
put("k1", "v1");
put("k2", "v2");
}
}, record.getMapField("map"));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZKUtil method testCreateOrReplace.
@Test()
public void testCreateOrReplace() {
String path = PropertyPathBuilder.instanceConfig(clusterName, "id8");
ZNRecord record = new ZNRecord("id8");
ZKUtil.createOrReplace(_zkClient, path, record, true);
record = _zkClient.<ZNRecord>readData(path);
AssertJUnit.assertEquals("id8", record.getId());
record = new ZNRecord("id9");
ZKUtil.createOrReplace(_zkClient, path, record, true);
record = _zkClient.<ZNRecord>readData(path);
AssertJUnit.assertEquals("id9", record.getId());
}
Aggregations