Search in sources :

Example 16 with DataUpdater

use of org.apache.helix.zookeeper.zkclient.DataUpdater in project helix by apache.

the class TestAutoFallbackPropertyStore method testMultiUpdateUsingFallbackPath.

@Test
public void testMultiUpdateUsingFallbackPath() {
    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<>(_gZkClient);
    // create 0-9 under fallbackRoot
    for (int i = 0; i < 10; i++) {
        String path = String.format("%s/%d", fallbackRoot, i);
        baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT);
    }
    AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<>(baseAccessor, root, fallbackRoot);
    List<String> paths = new ArrayList<>();
    List<DataUpdater<ZNRecord>> updaters = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String path = String.format("/%d", i);
        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");
        paths.add(path);
        updaters.add(new MyDataUpdater("new" + i));
    }
    boolean[] succeed = store.updateChildren(paths, updaters, AccessOption.PERSISTENT);
    for (int i = 0; i < 10; i++) {
        Assert.assertTrue(succeed[i]);
        String path = paths.get(i);
        // fallback path should remain unchanged
        ZNRecord record = baseAccessor.get(String.format("%s%s", fallbackRoot, path), null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "" + i);
        Assert.assertNull(record.getSimpleField("key"));
        // new path should have simple field set
        record = baseAccessor.get(String.format("%s%s", root, path), null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "" + i);
        Assert.assertNotNull(record.getSimpleField("key"));
        Assert.assertEquals(record.getSimpleField("key"), "value");
    }
    deleteCluster(clusterName);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) ArrayList(java.util.ArrayList) Date(java.util.Date) DataUpdater(org.apache.helix.zookeeper.zkclient.DataUpdater) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) Test(org.testng.annotations.Test)

Example 17 with DataUpdater

use of org.apache.helix.zookeeper.zkclient.DataUpdater in project helix by apache.

the class TestAutoFallbackPropertyStore method testMultiUpdateUsingNewath.

@Test
public void testMultiUpdateUsingNewath() {
    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<>(_gZkClient);
    // create 0-9 under both fallbackRoot and new root
    for (int i = 0; i < 10; i++) {
        String path = String.format("%s/%d", fallbackRoot, i);
        baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT);
        path = String.format("%s/%d", root, i);
        baseAccessor.create(path, new ZNRecord("new" + i), AccessOption.PERSISTENT);
    }
    AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<>(baseAccessor, root, fallbackRoot);
    List<String> paths = new ArrayList<>();
    List<DataUpdater<ZNRecord>> updaters = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        String path = String.format("/%d", i);
        if (i < 10) {
            Assert.assertTrue(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should exist under new location");
            Assert.assertTrue(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should exist under fallback location");
        } else {
            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");
        }
        paths.add(path);
        updaters.add(new MyDataUpdater("new" + i));
    }
    boolean[] succeed = store.updateChildren(paths, updaters, AccessOption.PERSISTENT);
    for (int i = 0; i < 10; i++) {
        Assert.assertTrue(succeed[i]);
        String path = paths.get(i);
        // fallback path should remain unchanged
        {
            ZNRecord record = baseAccessor.get(String.format("%s%s", fallbackRoot, path), null, 0);
            Assert.assertNotNull(record);
            Assert.assertEquals(record.getId(), "" + i);
            Assert.assertNull(record.getSimpleField("key"));
        }
        // new path should have simple field set
        ZNRecord record = baseAccessor.get(String.format("%s%s", root, path), null, 0);
        Assert.assertNotNull(record);
        Assert.assertEquals(record.getId(), "new" + i);
        Assert.assertNotNull(record.getSimpleField("key"));
        Assert.assertEquals(record.getSimpleField("key"), "value");
    }
    deleteCluster(clusterName);
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) ArrayList(java.util.ArrayList) Date(java.util.Date) DataUpdater(org.apache.helix.zookeeper.zkclient.DataUpdater) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) Test(org.testng.annotations.Test)

Aggregations

DataUpdater (org.apache.helix.zookeeper.zkclient.DataUpdater)17 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)14 ArrayList (java.util.ArrayList)11 Date (java.util.Date)7 Test (org.testng.annotations.Test)7 HelixException (org.apache.helix.HelixException)6 HashMap (java.util.HashMap)5 PropertyKey (org.apache.helix.PropertyKey)5 ZNRecordUpdater (org.apache.helix.zookeeper.datamodel.ZNRecordUpdater)5 Map (java.util.Map)4 Set (java.util.Set)4 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)4 HashSet (java.util.HashSet)3 List (java.util.List)3 AccessOption (org.apache.helix.AccessOption)3 HelixManager (org.apache.helix.HelixManager)3 InvalidRoutingDataException (org.apache.helix.msdcommon.exception.InvalidRoutingDataException)3 CreateMode (org.apache.zookeeper.CreateMode)3 Arrays (java.util.Arrays)2