Search in sources :

Example 1 with MetadataBookieDriver

use of org.apache.bookkeeper.meta.MetadataBookieDriver in project bookkeeper by apache.

the class Bookie method instantiateMetadataDriver.

/**
 * Instantiate the metadata driver for the Bookie.
 */
private MetadataBookieDriver instantiateMetadataDriver(ServerConfiguration conf) throws BookieException {
    try {
        String metadataServiceUriStr = conf.getMetadataServiceUri();
        if (null == metadataServiceUriStr) {
            return null;
        }
        MetadataBookieDriver driver = MetadataDrivers.getBookieDriver(URI.create(metadataServiceUriStr));
        driver.initialize(conf, () -> {
            stateManager.forceToUnregistered();
            // schedule a re-register operation
            stateManager.registerBookie(false);
        }, statsLogger);
        return driver;
    } catch (MetadataException me) {
        throw new MetadataStoreException("Failed to initialize metadata bookie driver", me);
    } catch (ConfigurationException e) {
        throw new BookieIllegalOpException(e);
    }
}
Also used : MetadataStoreException(org.apache.bookkeeper.bookie.BookieException.MetadataStoreException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) MetadataException(org.apache.bookkeeper.meta.exceptions.MetadataException) BookieIllegalOpException(org.apache.bookkeeper.bookie.BookieException.BookieIllegalOpException)

Example 2 with MetadataBookieDriver

use of org.apache.bookkeeper.meta.MetadataBookieDriver in project bookkeeper by apache.

the class ExpandStorageService method handle.

/*
     * Add new empty ledger/index directories.
     * Update the directories info in the conf file before running the command.
     */
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    if (HttpServer.Method.PUT == request.getMethod()) {
        File[] ledgerDirectories = Bookie.getCurrentDirectories(conf.getLedgerDirs());
        File[] journalDirectories = Bookie.getCurrentDirectories(conf.getJournalDirs());
        File[] indexDirectories;
        if (null == conf.getIndexDirs()) {
            indexDirectories = ledgerDirectories;
        } else {
            indexDirectories = Bookie.getCurrentDirectories(conf.getIndexDirs());
        }
        List<File> allLedgerDirs = Lists.newArrayList();
        allLedgerDirs.addAll(Arrays.asList(ledgerDirectories));
        if (indexDirectories != ledgerDirectories) {
            allLedgerDirs.addAll(Arrays.asList(indexDirectories));
        }
        try (MetadataBookieDriver driver = MetadataDrivers.getBookieDriver(URI.create(conf.getMetadataServiceUri()))) {
            driver.initialize(conf, () -> {
            }, NullStatsLogger.INSTANCE);
            Bookie.checkEnvironmentWithStorageExpansion(conf, driver, Lists.newArrayList(journalDirectories), allLedgerDirs);
        } catch (BookieException e) {
            LOG.error("Exception occurred while updating cookie for storage expansion", e);
            response.setCode(HttpServer.StatusCode.INTERNAL_ERROR);
            response.setBody("Exception while updating cookie for storage expansion");
            return response;
        }
        String jsonResponse = "Success expand storage";
        LOG.debug("output body:" + jsonResponse);
        response.setBody(jsonResponse);
        response.setCode(HttpServer.StatusCode.OK);
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be PUT method");
        return response;
    }
}
Also used : MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) File(java.io.File) BookieException(org.apache.bookkeeper.bookie.BookieException)

Example 3 with MetadataBookieDriver

use of org.apache.bookkeeper.meta.MetadataBookieDriver 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 4 with MetadataBookieDriver

use of org.apache.bookkeeper.meta.MetadataBookieDriver 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 5 with MetadataBookieDriver

use of org.apache.bookkeeper.meta.MetadataBookieDriver 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

MetadataBookieDriver (org.apache.bookkeeper.meta.MetadataBookieDriver)5 MetadataStoreException (org.apache.bookkeeper.bookie.BookieException.MetadataStoreException)3 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 ZKMetadataBookieDriver (org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 File (java.io.File)2 ExecutionException (java.util.concurrent.ExecutionException)2 KeeperException (org.apache.zookeeper.KeeperException)2 Stat (org.apache.zookeeper.data.Stat)2 IOException (java.io.IOException)1 BindException (java.net.BindException)1 AccessControlException (java.security.AccessControlException)1 BookieException (org.apache.bookkeeper.bookie.BookieException)1 BookieIllegalOpException (org.apache.bookkeeper.bookie.BookieException.BookieIllegalOpException)1 DiskPartitionDuplicationException (org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException)1 NoWritableLedgerDirException (org.apache.bookkeeper.bookie.LedgerDirsManager.NoWritableLedgerDirException)1 RegistrationManager (org.apache.bookkeeper.discover.RegistrationManager)1 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)1 MetadataException (org.apache.bookkeeper.meta.exceptions.MetadataException)1