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);
}
}
}
Aggregations