Search in sources :

Example 1 with ZKMetadataBookieDriver

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();
}
Also used : ZKMetadataBookieDriver(org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver)

Example 2 with 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());
        }
    }
}
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 ZKMetadataBookieDriver

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());
}
Also used : RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) MetadataStoreException(org.apache.bookkeeper.bookie.BookieException.MetadataStoreException) 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) BookieServer(org.apache.bookkeeper.proto.BookieServer) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) Test(org.junit.Test)

Example 4 with ZKMetadataBookieDriver

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;
        }
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) 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) ExecutionException(java.util.concurrent.ExecutionException) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Aggregations

ZKMetadataBookieDriver (org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver)4 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 MetadataBookieDriver (org.apache.bookkeeper.meta.MetadataBookieDriver)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ExecutionException (java.util.concurrent.ExecutionException)2 MetadataStoreException (org.apache.bookkeeper.bookie.BookieException.MetadataStoreException)2 KeeperException (org.apache.zookeeper.KeeperException)2 Stat (org.apache.zookeeper.data.Stat)2 File (java.io.File)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 AccessControlException (java.security.AccessControlException)1 DiskPartitionDuplicationException (org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException)1 NoWritableLedgerDirException (org.apache.bookkeeper.bookie.LedgerDirsManager.NoWritableLedgerDirException)1 RegistrationManager (org.apache.bookkeeper.discover.RegistrationManager)1 BookieServer (org.apache.bookkeeper.proto.BookieServer)1 CompatibilityException (org.apache.bookkeeper.replication.ReplicationException.CompatibilityException)1 UnavailableException (org.apache.bookkeeper.replication.ReplicationException.UnavailableException)1 SecurityException (org.apache.bookkeeper.tls.SecurityException)1