void setPropertyStore(String clusterName) { _propertyStore = new ZkHelixPropertyStore<>(new ZkBaseDataAccessor<ZNRecord>(_zkclient), "/" + clusterName + "/PROPERTYSTORE", null); }
LOGGER.info("Updating IdealState for table {}", tableName); if (zkBaseDataAccessor .set(idealStateKey.getPath(), nextIdealState.getRecord(), currentIdealState.getRecord().getVersion(), AccessOption.PERSISTENT)) {
String root = String.format("/%s/%s", clusterName, PropertyType.PROPERTYSTORE.name()); String fallbackRoot = String.format("/%s/%s", clusterName, "HELIX_PROPERTYSTORE"); ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT); Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location"); Assert.assertTrue(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should exist under fallback location"); ZNRecord record = new ZNRecord("new0"); Assert.assertTrue(succeed); record = baseAccessor.get(String.format("%s%s", fallbackRoot, path), null, 0); Assert.assertNotNull(record); Assert.assertEquals(record.getId(), "0"); record = baseAccessor.get(String.format("%s%s", root, path), null, 0); Assert.assertNotNull(record); Assert.assertEquals(record.getId(), "new0");
@Test public void testSyncExist() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", _rootPath, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); boolean success = accessor.exists(path, 0); Assert.assertFalse(success); success = accessor.create(path, record, AccessOption.EPHEMERAL); Assert.assertTrue(success); success = accessor.exists(path, 0); Assert.assertTrue(success); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<>(_gZkClient); boolean[] success = accessor.createChildren(paths, records, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; success = accessor.setChildren(paths, records, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; success = accessor.updateChildren(paths, znrecordUpdaters, AccessOption.PERSISTENT); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; records = accessor.getChildren(parentPath, null, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; boolean[] exists = accessor.exists(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; Stat[] stats = accessor.getStats(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i; success = accessor.remove(paths, 0); for (int i = 0; i < 10; i++) { String msgId = "msg_" + i;
@Test public void testSyncCreate() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", _rootPath, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<>(_gZkClient); boolean success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertTrue(success); ZNRecord getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getId(), "msg_0"); record.setSimpleField("key0", "value0"); success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertFalse(success, "Should fail since node already exists"); getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getSimpleFields().size(), 0); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
public static void main(String[] args) { ZkClient zkclient = new ZkClient("localhost:2191"); zkclient.setZkSerializer(new ZNRecordSerializer()); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(zkclient); 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); 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)); accessor.update(updatePaths, updaters, pathsCreated, null, AccessOption.PERSISTENT); for (int i = 0; i < updatePaths.size(); i++) { success[i] = updateRecords.get(i) != null;
.buildZkClient(new HelixZkClient.ZkConnectionConfig(ZK_ADDR)); extZkclient.setZkSerializer(new ZNRecordSerializer()); ZkBaseDataAccessor<ZNRecord> extBaseAccessor = new ZkBaseDataAccessor<ZNRecord>(extZkclient); String extViewPath = PropertyPathBuilder.externalView(clusterName); ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); extBaseAccessor.create(curStatePath, null, AccessOption.PERSISTENT); 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)); updaters.add(updater); success = extBaseAccessor.updateChildren(paths, updaters, AccessOption.PERSISTENT); 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)); success = extBaseAccessor.remove(paths, 0); for (int i = 0; i < 10; i++) { Assert.assertTrue(success[i], "Should succeed in remove: " + paths.get(i));
@Test public void testSyncGetStat() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", _rootPath, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); Stat stat = accessor.getStat(path, 0); Assert.assertNull(stat); boolean success = accessor.create(path, record, AccessOption.EPHEMERAL); Assert.assertTrue(success); stat = accessor.getStat(path, 0); Assert.assertNotNull(stat); Assert.assertEquals(stat.getVersion(), 0); Assert.assertNotSame(stat.getEphemeralOwner(), 0); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testSyncRemove() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", _rootPath, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); // Base data accessor shall not fail when remove a non-exist path boolean success = accessor.remove(path, 0); Assert.assertTrue(success); success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertTrue(success); ZNRecord getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getId(), "msg_0"); success = accessor.remove(path, 0); Assert.assertTrue(success); Assert.assertFalse(_gZkClient.exists(path)); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); String path = keyBuilder.liveInstance("localhost_12918").getPath(); Stat stat = new Stat(); ZNRecord data = accessor.get(path, stat, 2); data.getSimpleFields().put("SESSION_ID", "invalid-id"); accessor.set(path, data, 2); Thread.sleep(2000);
@Test public void testSyncDoSet() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s/%s", _rootPath, "msg_0", "submsg_0"); ZNRecord record = new ZNRecord("submsg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); AccessResult result = accessor.doSet(path, record, -1, AccessOption.PERSISTENT); Assert.assertEquals(result._retCode, RetCode.OK); Assert.assertEquals(result._pathCreated.size(), 3); Assert.assertTrue(result._pathCreated.contains(String.format("/%s/%s", _rootPath, "msg_0"))); Assert.assertTrue(result._pathCreated.contains(path)); Assert.assertTrue(_gZkClient.exists(String.format("/%s/%s", _rootPath, "msg_0"))); ZNRecord getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getId(), "submsg_0"); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testFailOnSingleGet() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String clusterName = className + "_" + methodName; System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis())); String root = String.format("/%s/%s", clusterName, PropertyType.PROPERTYSTORE.name()); String fallbackRoot = String.format("/%s/%s", clusterName, "HELIX_PROPERTYSTORE"); ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<ZNRecord>(baseAccessor, root, fallbackRoot); String path = String.format("/%d", 0); Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location"); Assert.assertFalse(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should not exist under fallback location"); // test single exist boolean exist = store.exists(path, 0); Assert.assertFalse(exist); // test single getStat Stat stat = store.getStat(path, 0); Assert.assertNull(stat); // test single get ZNRecord record = store.get(path, null, 0); Assert.assertNull(record); System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis())); }
@Override public boolean[] setChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); List<Stat> setStats = new ArrayList<Stat>(); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>> nCopies(size, null)); boolean[] success = _baseAccessor.set(serverPaths, records, pathsCreatedList, setStats, options); for (int i = 0; i < size; i++) { updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), setStats.get(i)); } return success; } finally { cache.unlockWrite(); } } return _baseAccessor.setChildren(serverPaths, records, options); }
@Override public boolean set(String path, T data, int expectVersion, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); boolean success = false; try { if (cache != null) { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doSet(serverPath, data, expectVersion, options); success = result._retCode == RetCode.OK; updateCache(cache, result._pathCreated, success, serverPath, data, result._stat); } else { // no cache success = _baseAccessor.set(serverPath, data, expectVersion, options); } } catch (Exception e) { } finally { if (cache != null) { cache.unlockWrite(); } } return success; }
@Override public List<T> get(List<String> paths, List<Stat> stats, int options, boolean throwException) throws HelixException { boolean[] needRead = new boolean[paths.size()]; Arrays.fill(needRead, true); return get(paths, stats, needRead, throwException); }
@Override public Stat getStat(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { Stat stat = new Stat(); ZNode znode = cache.get(serverPath); if (znode != null) { return znode.getStat(); } else { // if cache miss, fall back to zk and update cache try { cache.lockWrite(); T data = _baseAccessor.get(serverPath, stat, options); cache.update(serverPath, data, stat); } catch (ZkNoNodeException e) { return null; } finally { cache.unlockWrite(); } return stat; } } // no cache return _baseAccessor.getStat(serverPath, options); }
@Override public boolean[] createChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); boolean[] needCreate = new boolean[size]; Arrays.fill(needCreate, true); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>> nCopies(size, null)); CreateCallbackHandler[] createCbList = _baseAccessor.create(serverPaths, records, needCreate, pathsCreatedList, options); boolean[] success = new boolean[size]; for (int i = 0; i < size; i++) { CreateCallbackHandler cb = createCbList[i]; success[i] = (Code.get(cb.getRc()) == Code.OK); updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), ZNode.ZERO_STAT); } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.createChildren(serverPaths, records, options); }