// 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(); }
@Test public void testAsyncZkBaseDataAccessor() { System.out.println( "START TestZkBaseDataAccessor.async at " + new Date(System.currentTimeMillis())); String root = "TestZkBaseDataAccessor_asyn"; ZkClient zkClient = new ZkClient(ZK_ADDR); zkClient.setZkSerializer(new ZNRecordSerializer()); zkClient.deleteRecursive("/" + root); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(zkClient); // test async createChildren String parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); List<ZNRecord> records = new ArrayList<ZNRecord>(); List<String> paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); records.add(new ZNRecord(msgId)); } boolean[] success = accessor.createChildren(paths, records, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertTrue(success[i], "Should succeed in create " + msgId); } // test get what we created for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; String path = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId); ZNRecord record = zkClient.readData(path); Assert.assertEquals(record.getId(), msgId, "Should get what we created"); } // test async setChildren parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); records = new ArrayList<ZNRecord>(); paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); ZNRecord newRecord = new ZNRecord(msgId); newRecord.setSimpleField("key1", "value1"); records.add(newRecord); } success = accessor.setChildren(paths, records, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertTrue(success[i], "Should succeed in set " + msgId); } // test get what we set for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; String path = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId); ZNRecord record = zkClient.readData(path); Assert.assertEquals(record.getSimpleFields().size(), 1, "Should have 1 simple field set"); Assert.assertEquals(record.getSimpleField("key1"), "value1", "Should have value1 set"); } // test async updateChildren parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); // records = new ArrayList<ZNRecord>(); List<DataUpdater<ZNRecord>> znrecordUpdaters = new ArrayList<DataUpdater<ZNRecord>>(); paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); ZNRecord newRecord = new ZNRecord(msgId); newRecord.setSimpleField("key2", "value2"); // records.add(newRecord); znrecordUpdaters.add(new ZNRecordUpdater(newRecord)); } success = accessor.updateChildren(paths, znrecordUpdaters, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertTrue(success[i], "Should succeed in update " + msgId); } // test get what we updated for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; String path = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId); ZNRecord record = zkClient.readData(path); Assert.assertEquals(record.getSimpleFields().size(), 2, "Should have 2 simple fields set"); Assert.assertEquals(record.getSimpleField("key2"), "value2", "Should have value2 set"); } // test async getChildren parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); records = accessor.getChildren(parentPath, null, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; ZNRecord record = records.get(i); Assert.assertEquals(record.getId(), msgId, "Should get what we updated"); Assert.assertEquals(record.getSimpleFields().size(), 2, "Should have 2 simple fields set"); Assert.assertEquals(record.getSimpleField("key2"), "value2", "Should have value2 set"); } // test async exists parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); } boolean[] exists = accessor.exists(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertTrue(exists[i], "Should exist " + msgId); } // test async getStats parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); } Stat[] stats = accessor.getStats(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertNotNull(stats[i], "Stat should exist for " + msgId); Assert.assertEquals( stats[i].getVersion(), 2, "DataVersion should be 2, since we set 1 and update 1 for " + msgId); } // test async remove parentPath = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1"); paths = new ArrayList<String>(); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; paths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId)); } success = accessor.remove(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Assert.assertTrue(success[i], "Should succeed in remove " + msgId); } // test get what we removed for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; String path = PropertyPathConfig.getPath(PropertyType.MESSAGES, root, "host_1", msgId); boolean pathExists = zkClient.exists(path); Assert.assertFalse(pathExists, "Should be removed " + msgId); } zkClient.close(); System.out.println( "END TestZkBaseDataAccessor.async at " + new Date(System.currentTimeMillis())); }