Search in sources :

Example 1 with MetaRegionLocationCache

use of org.apache.hadoop.hbase.MetaRegionLocationCache in project hbase by apache.

the class TestMetaRegionLocationCache method testMetaRegionLocationCache.

/**
 * Tests MetaRegionLocationCache's init procedure to make sure that it correctly watches the base
 * znode for notifications.
 */
@Test
public void testMetaRegionLocationCache() throws Exception {
    final String parentZnodeName = "/randomznodename";
    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, parentZnodeName);
    ServerName sn = ServerName.valueOf("localhost", 1234, 5678);
    try (ZKWatcher zkWatcher = new ZKWatcher(conf, null, null, true)) {
        // A thread that repeatedly creates and drops an unrelated child znode. This is to simulate
        // some ZK activity in the background.
        MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf);
        ctx.addThread(new MultithreadedTestUtil.RepeatingTestThread(ctx) {

            @Override
            public void doAnAction() throws Exception {
                final String testZnode = parentZnodeName + "/child";
                ZKUtil.createNodeIfNotExistsAndWatch(zkWatcher, testZnode, testZnode.getBytes());
                ZKUtil.deleteNode(zkWatcher, testZnode);
            }
        });
        ctx.startThreads();
        try {
            MetaRegionLocationCache metaCache = new MetaRegionLocationCache(zkWatcher);
            // meta znodes do not exist at this point, cache should be empty.
            assertTrue(metaCache.getMetaRegionLocations().isEmpty());
            // assignment.
            for (int i = 0; i < 3; i++) {
                // Updates the meta znodes.
                MetaTableLocator.setMetaLocation(zkWatcher, sn, i, RegionState.State.OPEN);
            }
            // Wait until the meta cache is populated.
            int iters = 0;
            while (iters++ < 10) {
                if (metaCache.getMetaRegionLocations().size() == 3) {
                    break;
                }
                Thread.sleep(1000);
            }
            List<HRegionLocation> metaLocations = metaCache.getMetaRegionLocations();
            assertEquals(3, metaLocations.size());
            for (HRegionLocation location : metaLocations) {
                assertEquals(sn, location.getServerName());
            }
        } finally {
            // clean up.
            ctx.stop();
            ZKUtil.deleteChildrenRecursively(zkWatcher, parentZnodeName);
        }
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) MetaRegionLocationCache(org.apache.hadoop.hbase.MetaRegionLocationCache) ServerName(org.apache.hadoop.hbase.ServerName) ZKWatcher(org.apache.hadoop.hbase.zookeeper.ZKWatcher) MultithreadedTestUtil(org.apache.hadoop.hbase.MultithreadedTestUtil) Test(org.junit.Test)

Aggregations

Configuration (org.apache.hadoop.conf.Configuration)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 MetaRegionLocationCache (org.apache.hadoop.hbase.MetaRegionLocationCache)1 MultithreadedTestUtil (org.apache.hadoop.hbase.MultithreadedTestUtil)1 ServerName (org.apache.hadoop.hbase.ServerName)1 ZKWatcher (org.apache.hadoop.hbase.zookeeper.ZKWatcher)1 Test (org.junit.Test)1