Search in sources :

Example 1 with ZooKeeperClient

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);
}
Also used : ZooKeeperClient(org.apache.bookkeeper.zookeeper.ZooKeeperClient) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ZooKeeperClient

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());
        }
    }
}
Also used : ZKMetadataBookieDriver(org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ZKMetadataBookieDriver(org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException) DiskPartitionDuplicationException(org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException) CompatibilityException(org.apache.bookkeeper.replication.ReplicationException.CompatibilityException) BindException(java.net.BindException) MetadataStoreException(org.apache.bookkeeper.bookie.BookieException.MetadataStoreException) SecurityException(org.apache.bookkeeper.tls.SecurityException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoWritableLedgerDirException(org.apache.bookkeeper.bookie.LedgerDirsManager.NoWritableLedgerDirException) ExecutionException(java.util.concurrent.ExecutionException) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(org.apache.bookkeeper.zookeeper.ZooKeeperClient) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 3 with ZooKeeperClient

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);
    }
}
Also used : Op(org.apache.zookeeper.Op) ZooKeeperClient(org.apache.bookkeeper.zookeeper.ZooKeeperClient) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

ZooKeeperClient (org.apache.bookkeeper.zookeeper.ZooKeeperClient)3 IOException (java.io.IOException)2 KeeperException (org.apache.zookeeper.KeeperException)2 Test (org.junit.Test)2 BindException (java.net.BindException)1 AccessControlException (java.security.AccessControlException)1 ExecutionException (java.util.concurrent.ExecutionException)1 DiskPartitionDuplicationException (org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException)1 MetadataStoreException (org.apache.bookkeeper.bookie.BookieException.MetadataStoreException)1 NoWritableLedgerDirException (org.apache.bookkeeper.bookie.LedgerDirsManager.NoWritableLedgerDirException)1 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)1 MetadataBookieDriver (org.apache.bookkeeper.meta.MetadataBookieDriver)1 ZKMetadataBookieDriver (org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver)1 CompatibilityException (org.apache.bookkeeper.replication.ReplicationException.CompatibilityException)1 UnavailableException (org.apache.bookkeeper.replication.ReplicationException.UnavailableException)1 SecurityException (org.apache.bookkeeper.tls.SecurityException)1 Op (org.apache.zookeeper.Op)1 Stat (org.apache.zookeeper.data.Stat)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1