use of org.apache.bookkeeper.zookeeper.ZooKeeperClient in project bookkeeper by apache.
the class ZKMetadataDriverBaseTest method testInitializeExternalZooKeeper.
@Test
public void testInitializeExternalZooKeeper() throws Exception {
ZooKeeperClient anotherZk = mock(ZooKeeperClient.class);
driver.initialize(conf, NullStatsLogger.INSTANCE, retryPolicy, Optional.of(anotherZk));
assertEquals("/ledgers", driver.ledgersRootPath);
assertFalse(driver.ownZKHandle);
String readonlyPath = "/path/to/ledgers/" + AVAILABLE_NODE;
assertSame(anotherZk, driver.zk);
verifyStatic(ZooKeeperClient.class, times(0));
ZooKeeperClient.newBuilder();
verify(mockZkBuilder, times(0)).build();
verify(mockZkc, times(0)).exists(eq(readonlyPath), eq(false));
assertNotNull(driver.layoutManager);
assertNull(driver.lmFactory);
driver.close();
verify(mockZkc, times(0)).close();
assertNotNull(driver.zk);
}
use of org.apache.bookkeeper.zookeeper.ZooKeeperClient 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.zookeeper.ZooKeeperClient in project bookkeeper by apache.
the class LocalBookKeeper method initializeZookeeper.
@SuppressWarnings("deprecation")
private void initializeZookeeper(AbstractConfiguration conf, String zkHost, int zkPort) throws IOException {
LOG.info("Instantiate ZK Client");
// initialize the zk client with values
try (ZooKeeperClient zkc = ZooKeeperClient.newBuilder().connectString(zkHost + ":" + zkPort).sessionTimeoutMs(zkSessionTimeOut).build()) {
List<Op> multiOps = Lists.newArrayListWithExpectedSize(3);
multiOps.add(Op.create(conf.getZkLedgersRootPath(), new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
multiOps.add(Op.create(conf.getZkAvailableBookiesPath(), new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
multiOps.add(Op.create(conf.getZkAvailableBookiesPath() + "/" + READONLY, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
zkc.multi(multiOps);
// No need to create an entry for each requested bookie anymore as the
// BookieServers will register themselves with ZooKeeper on startup.
} catch (KeeperException e) {
LOG.error("Exception while creating znodes", e);
throw new IOException("Error creating znodes : ", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Interrupted while creating znodes", e);
throw new IOException("Error creating znodes : ", e);
}
}
Aggregations