use of org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
zkUtil.createBKEnsemble("/" + runtime.getMethodName());
driver = new ZKMetadataBookieDriver();
}
use of org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method testBookieRegistration.
/**
* Verify the bookie reg. Restarting bookie server will wait for the session
* timeout when previous reg node exists in zk. On zNode delete event,
* should continue startup
*/
@Test
public void testBookieRegistration() throws Exception {
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setZkServers(zkUtil.getZooKeeperConnectString()).setListeningInterface(null);
String bookieId = Bookie.getBookieAddress(conf).toString();
final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + bookieId;
driver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager manager = new BookieStateManager(conf, driver)) {
manager.registerBookie(true).get();
}
Stat bkRegNode1 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode1);
// zkclient and doing the registration.
try (MetadataBookieDriver newDriver = new ZKMetadataBookieDriver()) {
newDriver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (ZooKeeperClient newZk = createNewZKClient()) {
// deleting the znode, so that the bookie registration should
// continue successfully on NodeDeleted event
new Thread(() -> {
try {
Thread.sleep(conf.getZkTimeout() / 3);
zkc.delete(bkRegPath, -1);
} catch (Exception e) {
// Not handling, since the testRegisterBookie will fail
LOG.error("Failed to delete the znode :" + bkRegPath, e);
}
}).start();
try (StateManager newMgr = new BookieStateManager(conf, newDriver)) {
newMgr.registerBookie(true).get();
} catch (IOException e) {
Throwable t = e.getCause();
if (t instanceof KeeperException) {
KeeperException ke = (KeeperException) t;
assertTrue("ErrorCode:" + ke.code() + ", Registration node exists", ke.code() != KeeperException.Code.NODEEXISTS);
}
throw e;
}
// verify ephemeral owner of the bkReg znode
Stat bkRegNode2 = newZk.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode2);
assertTrue("Bookie is referring to old registration znode:" + bkRegNode1 + ", New ZNode:" + bkRegNode2, bkRegNode1.getEphemeralOwner() != bkRegNode2.getEphemeralOwner());
}
}
}
use of org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method testExitCodeZK_REG_FAIL.
/**
* Verify the bookie server exit code. On ZooKeeper exception, should return
* exit code ZK_REG_FAIL = 4
*/
@Test
public void testExitCodeZK_REG_FAIL() throws Exception {
File tmpDir = createTempDir("bookie", "test");
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setZkServers(zkUtil.getZooKeeperConnectString());
RegistrationManager rm = mock(RegistrationManager.class);
doThrow(new MetadataStoreException("mocked exception")).when(rm).registerBookie(anyString(), anyBoolean());
// simulating ZooKeeper exception by assigning a closed zk client to bk
BookieServer bkServer = new BookieServer(conf) {
protected Bookie newBookie(ServerConfiguration conf) throws IOException, KeeperException, InterruptedException, BookieException {
Bookie bookie = new Bookie(conf);
MetadataBookieDriver driver = Whitebox.getInternalState(bookie, "metadataDriver");
((ZKMetadataBookieDriver) driver).setRegManager(rm);
return bookie;
}
};
bkServer.start();
bkServer.join();
assertEquals("Failed to return ExitCode.ZK_REG_FAIL", ExitCode.ZK_REG_FAIL, bkServer.getExitCode());
}
use of org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method testRegNodeExistsAfterSessionTimeOut.
/**
* Verify the bookie registration, it should throw
* KeeperException.NodeExistsException if the znode still exists even after
* the zk session timeout.
*/
@Test
public void testRegNodeExistsAfterSessionTimeOut() throws Exception {
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setListeningInterface(null);
String bookieId = InetAddress.getLocalHost().getHostAddress() + ":" + conf.getBookiePort();
String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + bookieId;
driver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager manager = new BookieStateManager(conf, driver)) {
manager.registerBookie(true).get();
assertTrue("Bookie registration node doesn't exists!", driver.getRegistrationManager().isBookieRegistered(bookieId));
}
Stat bkRegNode1 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode1);
// zkclient and doing the registration.
try (MetadataBookieDriver newDriver = new ZKMetadataBookieDriver()) {
newDriver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager newMgr = new BookieStateManager(conf, newDriver)) {
newMgr.registerBookie(true).get();
fail("Should throw NodeExistsException as the znode is not getting expired");
} catch (ExecutionException ee) {
// IOException
Throwable e = ee.getCause();
// BookieException.MetadataStoreException
Throwable t1 = e.getCause();
// IOException
Throwable t2 = t1.getCause();
// KeeperException.NodeExistsException
Throwable t3 = t2.getCause();
if (t3 instanceof KeeperException) {
KeeperException ke = (KeeperException) t3;
assertTrue("ErrorCode:" + ke.code() + ", Registration node doesn't exists", ke.code() == KeeperException.Code.NODEEXISTS);
// verify ephemeral owner of the bkReg znode
Stat bkRegNode2 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode2);
assertTrue("Bookie wrongly registered. Old registration znode:" + bkRegNode1 + ", New znode:" + bkRegNode2, bkRegNode1.getEphemeralOwner() == bkRegNode2.getEphemeralOwner());
return;
}
throw ee;
}
}
}
Aggregations