Search in sources :

Example 6 with ZNRecordUpdater

use of org.apache.helix.ZNRecordUpdater in project helix by apache.

the class TestWtCacheAsyncOpSingleThread method testHappyPathZkCacheBaseDataAccessor.

@Test
public void testHappyPathZkCacheBaseDataAccessor() {
    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, false);
    Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
    // create 10 current states
    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 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
    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._wtCache);
    ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
    Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
    // set 10 external views
    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 = 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 wtCache
    // TestHelper.printCache(accessor._wtCache);
    ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
    Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
    // get 10 external views
    paths.clear();
    records.clear();
    for (int i = 0; i < 10; i++) {
        String path = PropertyPathBuilder.externalView(clusterName, "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 = PropertyPathBuilder.instanceCurrentState(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: TestDB" + i);
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) DataUpdater(org.I0Itec.zkclient.DataUpdater) ZNRecordUpdater(org.apache.helix.ZNRecordUpdater) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 7 with ZNRecordUpdater

use of org.apache.helix.ZNRecordUpdater in project helix by apache.

the class TestZkBaseDataAccessor method testAsyncZkBaseDataAccessor.

@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.deleteRecursively("/" + root);
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(zkClient);
    // test async createChildren
    String parentPath = PropertyPathBuilder.instanceMessage(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(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(root, "host_1", msgId);
        ZNRecord record = zkClient.readData(path);
        Assert.assertEquals(record.getId(), msgId, "Should get what we created");
    }
    // test async setChildren
    parentPath = PropertyPathBuilder.instanceMessage(root, "host_1");
    records = new ArrayList<ZNRecord>();
    paths = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        String msgId = "msg_" + i;
        paths.add(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(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(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(root, "host_1");
    paths = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        String msgId = "msg_" + i;
        paths.add(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(root, "host_1");
    paths = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        String msgId = "msg_" + i;
        paths.add(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(root, "host_1");
    paths = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        String msgId = "msg_" + i;
        paths.add(PropertyPathBuilder.instanceMessage(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 = PropertyPathBuilder.instanceMessage(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()));
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Stat(org.apache.zookeeper.data.Stat) DataUpdater(org.I0Itec.zkclient.DataUpdater) ZNRecordUpdater(org.apache.helix.ZNRecordUpdater) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 8 with ZNRecordUpdater

use of org.apache.helix.ZNRecordUpdater in project helix by apache.

the class TestZkBaseDataAccessor method testSyncUpdate.

@Test
public void testSyncUpdate() {
    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", testName, "msg_0");
    ZNRecord record = new ZNRecord("msg_0");
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    boolean success = accessor.update(path, new ZNRecordUpdater(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.update(path, new ZNRecordUpdater(record), AccessOption.PERSISTENT);
    Assert.assertTrue(success);
    getRecord = _gZkClient.readData(path);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(getRecord.getSimpleFields().size(), 1);
    Assert.assertNotNull(getRecord.getSimpleField("key0"));
    Assert.assertEquals(getRecord.getSimpleField("key0"), "value0");
    // test throw exception from updater
    success = accessor.update(path, new DataUpdater<ZNRecord>() {

        @Override
        public ZNRecord update(ZNRecord currentData) {
            throw new RuntimeException("IGNORABLE: test throw exception from updater");
        }
    }, AccessOption.PERSISTENT);
    Assert.assertFalse(success);
    getRecord = _gZkClient.readData(path);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(getRecord.getSimpleFields().size(), 1);
    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZNRecordUpdater(org.apache.helix.ZNRecordUpdater) DataUpdater(org.I0Itec.zkclient.DataUpdater) Date(java.util.Date) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Aggregations

Date (java.util.Date)8 ZNRecord (org.apache.helix.ZNRecord)8 ZNRecordUpdater (org.apache.helix.ZNRecordUpdater)8 Test (org.testng.annotations.Test)8 ArrayList (java.util.ArrayList)5 DataUpdater (org.I0Itec.zkclient.DataUpdater)5 Stat (org.apache.zookeeper.data.Stat)2 TreeSet (java.util.TreeSet)1