use of org.I0Itec.zkclient.DataUpdater in project helix by apache.
the class TestZkCacheAsyncOpSingleThread method testHappyPathExtOpZkCacheBaseDataAccessor.
@Test
public void testHappyPathExtOpZkCacheBaseDataAccessor() 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 external base data accessor
ZkClient extZkclient = new ZkClient(ZK_ADDR);
extZkclient.setZkSerializer(new ZNRecordSerializer());
ZkBaseDataAccessor<ZNRecord> extBaseAccessor = new ZkBaseDataAccessor<ZNRecord>(extZkclient);
// init zkCacheBaseDataAccessor
String curStatePath = PropertyPathBuilder.instanceCurrentState(clusterName, "localhost_8901");
String extViewPath = PropertyPathBuilder.externalView(clusterName);
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
extBaseAccessor.create(curStatePath, null, AccessOption.PERSISTENT);
List<String> zkCacheInitPaths = Arrays.asList(curStatePath, extViewPath);
ZkCacheBaseDataAccessor<ZNRecord> accessor = new ZkCacheBaseDataAccessor<ZNRecord>(baseAccessor, null, null, zkCacheInitPaths);
// TestHelper.printCache(accessor._zkCache);
boolean ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// create 10 current states using external base accessor
List<String> paths = new ArrayList<String>();
List<ZNRecord> records = new ArrayList<ZNRecord>();
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.instanceCurrentState(clusterName, "localhost_8901", "session_0", "TestDB" + i);
ZNRecord record = new ZNRecord("TestDB" + i);
paths.add(path);
records.add(record);
}
boolean[] success = extBaseAccessor.createChildren(paths, records, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in create: " + paths.get(i));
}
// verify wtCache
for (int i = 0; i < 10; i++) {
// TestHelper.printCache(accessor._zkCache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
if (ret == true)
break;
Thread.sleep(100);
}
// System.out.println("ret: " + ret);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// update each current state 10 times by external base accessor
List<DataUpdater<ZNRecord>> updaters = new ArrayList<DataUpdater<ZNRecord>>();
for (int j = 0; j < 10; j++) {
paths.clear();
updaters.clear();
for (int i = 0; i < 10; i++) {
String path = curStatePath + "/session_0/TestDB" + i;
ZNRecord newRecord = new ZNRecord("TestDB" + i);
newRecord.setSimpleField("" + j, "" + j);
DataUpdater<ZNRecord> updater = new ZNRecordUpdater(newRecord);
paths.add(path);
updaters.add(updater);
}
success = extBaseAccessor.updateChildren(paths, updaters, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in update: " + paths.get(i));
}
}
// wait zkEventThread update zkCache
Thread.sleep(100);
// verify cache
// TestHelper.printCache(accessor._zkCache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// set 10 external views by external accessor
paths.clear();
records.clear();
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.externalView(clusterName, "TestDB" + i);
ZNRecord record = new ZNRecord("TestDB" + i);
paths.add(path);
records.add(record);
}
success = extBaseAccessor.setChildren(paths, records, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in set: " + paths.get(i));
}
// wait zkEventThread update zkCache
Thread.sleep(100);
// verify cache
// TestHelper.printCache(accessor._zkCache._cache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// remove 10 external views by external accessor
paths.clear();
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.externalView(clusterName, "TestDB" + i);
paths.add(path);
}
success = extBaseAccessor.remove(paths, 0);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in remove: " + paths.get(i));
}
// wait zkEventThread update zkCache
Thread.sleep(100);
// verify cache
// TestHelper.printCache(accessor._zkCache._cache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// clean up
extZkclient.close();
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.I0Itec.zkclient.DataUpdater in project helix by apache.
the class TestZkCacheAsyncOpSingleThread method testHappyPathSelfOpZkCacheBaseDataAccessor.
@Test
public void testHappyPathSelfOpZkCacheBaseDataAccessor() 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> zkCacheInitPaths = Arrays.asList(curStatePath, extViewPath);
ZkCacheBaseDataAccessor<ZNRecord> accessor = new ZkCacheBaseDataAccessor<ZNRecord>(baseAccessor, null, null, zkCacheInitPaths);
// TestHelper.printCache(accessor._zkCache._cache);
boolean ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// create 10 current states using this accessor
List<String> paths = new ArrayList<String>();
List<ZNRecord> records = new ArrayList<ZNRecord>();
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.instanceCurrentState(clusterName, "localhost_8901", "session_0", "TestDB" + i);
ZNRecord record = new ZNRecord("TestDB" + i);
paths.add(path);
records.add(record);
}
boolean[] success = accessor.createChildren(paths, records, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in create: " + paths.get(i));
}
// verify cache
// TestHelper.printCache(accessor._zkCache._cache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, false);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// update each current state 10 times by this accessor
List<DataUpdater<ZNRecord>> updaters = new ArrayList<DataUpdater<ZNRecord>>();
for (int j = 0; j < 10; j++) {
paths.clear();
updaters.clear();
for (int i = 0; i < 10; i++) {
String path = curStatePath + "/session_0/TestDB" + i;
ZNRecord newRecord = new ZNRecord("TestDB" + i);
newRecord.setSimpleField("" + j, "" + j);
DataUpdater<ZNRecord> updater = new ZNRecordUpdater(newRecord);
paths.add(path);
updaters.add(updater);
}
success = accessor.updateChildren(paths, updaters, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in update: " + paths.get(i));
}
}
// verify cache
// TestHelper.printCache(accessor._zkCache._cache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
// ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor, _gZkClient, true);
// System.out.println("ret: " + ret);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// set 10 external views 10 times by this accessor
paths.clear();
records.clear();
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
String path = PropertyPathBuilder.externalView(clusterName, "TestDB" + i);
ZNRecord record = new ZNRecord("TestDB" + i);
record.setSimpleField("setKey", "" + j);
paths.add(path);
records.add(record);
}
success = accessor.setChildren(paths, records, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should succeed in set: " + paths.get(i));
}
}
// verify cache
// TestHelper.printCache(accessor._zkCache._cache);
ret = TestHelper.verifyZkCache(zkCacheInitPaths, accessor._zkCache._cache, _gZkClient, true);
// System.out.println("ret: " + ret);
Assert.assertTrue(ret, "zkCache doesn't match data on Zk");
// get 10 external views
paths.clear();
records.clear();
for (int i = 0; i < 10; i++) {
String path = extViewPath + "/TestDB" + i;
paths.add(path);
}
records = accessor.get(paths, null, 0);
for (int i = 0; i < 10; i++) {
Assert.assertEquals(records.get(i).getId(), "TestDB" + i);
}
// getChildren
records.clear();
records = accessor.getChildren(extViewPath, null, 0);
for (int i = 0; i < 10; i++) {
Assert.assertEquals(records.get(i).getId(), "TestDB" + i);
}
// // exists
paths.clear();
for (int i = 0; i < 10; i++) {
String path = curStatePath + "/session_0/TestDB" + i;
// // PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
// // clusterName,
// // "localhost_8901",
// // "session_0",
// // "TestDB" + i);
paths.add(path);
}
success = accessor.exists(paths, 0);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(success[i], "Should exits: " + paths.get(i));
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.I0Itec.zkclient.DataUpdater in project helix by apache.
the class ZKUtil method createOrUpdate.
public static void createOrUpdate(ZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
int retryCount = 0;
while (retryCount < RETRYLIMIT) {
try {
if (client.exists(path)) {
DataUpdater<ZNRecord> updater = new DataUpdater<ZNRecord>() {
@Override
public ZNRecord update(ZNRecord currentData) {
if (currentData != null && mergeOnUpdate) {
currentData.update(record);
return currentData;
}
return record;
}
};
client.updateDataSerialized(path, updater);
} else {
CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
client.create(path, record, mode);
}
break;
} catch (Exception e) {
retryCount = retryCount + 1;
logger.warn("Exception trying to update " + path + " Exception:" + e.getMessage() + ". Will retry.");
}
}
}
use of org.I0Itec.zkclient.DataUpdater in project helix by apache.
the class ZKUtil method createOrMerge.
public static void createOrMerge(ZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
int retryCount = 0;
while (retryCount < RETRYLIMIT) {
try {
if (client.exists(path)) {
DataUpdater<ZNRecord> updater = new DataUpdater<ZNRecord>() {
@Override
public ZNRecord update(ZNRecord currentData) {
if (currentData != null && mergeOnUpdate) {
currentData.merge(record);
return currentData;
}
return record;
}
};
client.updateDataSerialized(path, updater);
} else {
CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
if (record.getDeltaList().size() > 0) {
ZNRecord value = new ZNRecord(record.getId());
value.merge(record);
client.create(path, value, mode);
} else {
client.create(path, record, mode);
}
}
break;
} catch (Exception e) {
retryCount = retryCount + 1;
logger.warn("Exception trying to update " + path + " Exception:" + e.getMessage() + ". Will retry.");
}
}
}
use of org.I0Itec.zkclient.DataUpdater in project helix by apache.
the class ZkBaseDataAccessor method main.
// simple test
public static void main(String[] args) {
ZkClient zkclient = new ZkClient("localhost:2191");
zkclient.setZkSerializer(new ZNRecordSerializer());
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(zkclient);
// test async create
List<String> createPaths = Arrays.asList("/test/child1/child1", "/test/child2/child2");
List<ZNRecord> createRecords = Arrays.asList(new ZNRecord("child1"), new ZNRecord("child2"));
boolean[] needCreate = new boolean[createPaths.size()];
Arrays.fill(needCreate, true);
List<List<String>> pathsCreated = new ArrayList<List<String>>(Collections.<List<String>>nCopies(createPaths.size(), null));
accessor.create(createPaths, createRecords, needCreate, pathsCreated, AccessOption.PERSISTENT);
System.out.println("pathsCreated: " + pathsCreated);
// test async set
List<String> setPaths = Arrays.asList("/test/setChild1/setChild1", "/test/setChild2/setChild2");
List<ZNRecord> setRecords = Arrays.asList(new ZNRecord("setChild1"), new ZNRecord("setChild2"));
pathsCreated = new ArrayList<List<String>>(Collections.<List<String>>nCopies(setPaths.size(), null));
boolean[] success = accessor.set(setPaths, setRecords, pathsCreated, null, AccessOption.PERSISTENT);
System.out.println("pathsCreated: " + pathsCreated);
System.out.println("setSuccess: " + Arrays.toString(success));
// test async update
List<String> updatePaths = Arrays.asList("/test/updateChild1/updateChild1", "/test/setChild2/setChild2");
class TestUpdater implements DataUpdater<ZNRecord> {
final ZNRecord _newData;
public TestUpdater(ZNRecord newData) {
_newData = newData;
}
@Override
public ZNRecord update(ZNRecord currentData) {
return _newData;
}
}
List<DataUpdater<ZNRecord>> updaters = Arrays.asList((DataUpdater<ZNRecord>) new TestUpdater(new ZNRecord("updateChild1")), (DataUpdater<ZNRecord>) new TestUpdater(new ZNRecord("updateChild2")));
pathsCreated = new ArrayList<List<String>>(Collections.<List<String>>nCopies(updatePaths.size(), null));
List<ZNRecord> updateRecords = accessor.update(updatePaths, updaters, pathsCreated, null, AccessOption.PERSISTENT);
for (int i = 0; i < updatePaths.size(); i++) {
success[i] = updateRecords.get(i) != null;
}
System.out.println("pathsCreated: " + pathsCreated);
System.out.println("updateSuccess: " + Arrays.toString(success));
System.out.println("CLOSING");
zkclient.close();
}
Aggregations