Search in sources :

Example 6 with LedgerManagerFactory

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

the class ListUnderReplicatedLedgerService method handle.

/*
     * Print the node which holds the auditor lock.
     */
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    // parameter as this: ?missingreplica=<bookie_address>&excludingmissingreplica=<bookid_address>
    Map<String, String> params = request.getParams();
    if (HttpServer.Method.GET == request.getMethod()) {
        final String includingBookieId;
        final String excludingBookieId;
        if (params != null && params.containsKey("missingreplica")) {
            includingBookieId = params.get("missingreplica");
        } else {
            includingBookieId = null;
        }
        if (params != null && params.containsKey("excludingmissingreplica")) {
            excludingBookieId = params.get("excludingmissingreplica");
        } else {
            excludingBookieId = null;
        }
        Predicate<List<String>> predicate = null;
        if (!StringUtils.isBlank(includingBookieId) && !StringUtils.isBlank(excludingBookieId)) {
            predicate = replicasList -> (replicasList.contains(includingBookieId) && !replicasList.contains(excludingBookieId));
        } else if (!StringUtils.isBlank(includingBookieId)) {
            predicate = replicasList -> replicasList.contains(includingBookieId);
        } else if (!StringUtils.isBlank(excludingBookieId)) {
            predicate = replicasList -> !replicasList.contains(excludingBookieId);
        }
        try {
            List<Long> outputLedgers = Lists.newArrayList();
            LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
            LedgerUnderreplicationManager underreplicationManager = mFactory.newLedgerUnderreplicationManager();
            Iterator<Long> iter = underreplicationManager.listLedgersToRereplicate(predicate);
            while (iter.hasNext()) {
                outputLedgers.add(iter.next());
            }
            if (outputLedgers.isEmpty()) {
                response.setCode(HttpServer.StatusCode.NOT_FOUND);
                response.setBody("No under replicated ledgers found");
                return response;
            } else {
                response.setCode(HttpServer.StatusCode.OK);
                String jsonResponse = JsonUtil.toJson(outputLedgers);
                LOG.debug("output body: " + jsonResponse);
                response.setBody(jsonResponse);
                return response;
            }
        } catch (Exception e) {
            LOG.error("Exception occurred while listing under replicated ledgers", e);
            response.setCode(HttpServer.StatusCode.NOT_FOUND);
            response.setBody("Exception when get." + e.getMessage());
            return response;
        }
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be GET method");
        return response;
    }
}
Also used : HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) Predicate(java.util.function.Predicate) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) HttpServer(org.apache.bookkeeper.http.HttpServer) BookieServer(org.apache.bookkeeper.proto.BookieServer) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) List(java.util.List) Lists(com.google.common.collect.Lists) Map(java.util.Map) HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) JsonUtil(org.apache.bookkeeper.util.JsonUtil) LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) List(java.util.List) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory)

Example 7 with LedgerManagerFactory

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

the class AbstractConfigurationTest method testUnsupportedLedgerManagerFactory.

@SuppressWarnings({ "unchecked" })
@Test(expected = UnsupportedOperationException.class)
public void testUnsupportedLedgerManagerFactory() throws Exception {
    LedgerManagerFactory mockFactory = mock(LedgerManagerFactory.class, CALLS_REAL_METHODS);
    conf.setLedgerManagerFactoryClass(mockFactory.getClass());
    conf.getMetadataServiceUri();
}
Also used : HierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) LongHierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory) AbstractZkLedgerManagerFactory(org.apache.bookkeeper.meta.AbstractZkLedgerManagerFactory) Test(org.junit.Test)

Example 8 with LedgerManagerFactory

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

the class ZKRegistrationManager method nukeExistingCluster.

@Override
public boolean nukeExistingCluster() throws Exception {
    String zkServers = conf.getZkServers();
    log.info("Nuking ZooKeeper metadata of existing cluster, ZKServers: {} ledger root path: {}", zkServers, ledgersRootPath);
    boolean ledgerRootExists = null != zk.exists(ledgersRootPath, false);
    if (!ledgerRootExists) {
        log.info("There is no existing cluster with ledgersRootPath: {} in ZKServers: {}, " + "so exiting nuke operation", ledgersRootPath, zkServers);
        return true;
    }
    boolean availableNodeExists = null != zk.exists(bookieRegistrationPath, false);
    try (RegistrationClient regClient = new ZKRegistrationClient(zk, ledgersRootPath, null)) {
        if (availableNodeExists) {
            Collection<BookieSocketAddress> rwBookies = FutureUtils.result(regClient.getWritableBookies(), EXCEPTION_FUNC).getValue();
            if (rwBookies != null && !rwBookies.isEmpty()) {
                log.error("Bookies are still up and connected to this cluster, " + "stop all bookies before nuking the cluster");
                return false;
            }
            boolean readonlyNodeExists = null != zk.exists(bookieReadonlyRegistrationPath, false);
            if (readonlyNodeExists) {
                Collection<BookieSocketAddress> roBookies = FutureUtils.result(regClient.getReadOnlyBookies(), EXCEPTION_FUNC).getValue();
                if (roBookies != null && !roBookies.isEmpty()) {
                    log.error("Readonly Bookies are still up and connected to this cluster, " + "stop all bookies before nuking the cluster");
                    return false;
                }
            }
        }
    }
    LedgerManagerFactory ledgerManagerFactory = AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, layoutManager);
    return ledgerManagerFactory.validateAndNukeExistingCluster(conf, layoutManager);
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) AbstractZkLedgerManagerFactory(org.apache.bookkeeper.meta.AbstractZkLedgerManagerFactory)

Example 9 with LedgerManagerFactory

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

the class TestReplicationWorker method testRWShouldReplicateTheLedgersAfterTimeoutIfLastFragmentIsUR.

/**
 * Tests that ReplicationWorker should fence the ledger and release ledger
 * lock after timeout. Then replication should happen normally.
 */
@Test
public void testRWShouldReplicateTheLedgersAfterTimeoutIfLastFragmentIsUR() throws Exception {
    LedgerHandle lh = bkc.createLedger(3, 3, BookKeeper.DigestType.CRC32, TESTPASSWD);
    for (int i = 0; i < 10; i++) {
        lh.addEntry(data);
    }
    BookieSocketAddress replicaToKill = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);
    LOG.info("Killing Bookie", replicaToKill);
    killBookie(replicaToKill);
    BookieSocketAddress newBkAddr = startNewBookieAndReturnAddress();
    LOG.info("New Bookie addr : {}", newBkAddr);
    // set to 3s instead of default 30s
    baseConf.setOpenLedgerRereplicationGracePeriod("3000");
    ReplicationWorker rw = new ReplicationWorker(zkc, baseConf);
    @Cleanup MetadataClientDriver clientDriver = MetadataDrivers.getClientDriver(URI.create(baseClientConf.getMetadataServiceUri()));
    clientDriver.initialize(baseClientConf, scheduler, NullStatsLogger.INSTANCE, Optional.empty());
    LedgerManagerFactory mFactory = clientDriver.getLedgerManagerFactory();
    LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
    rw.start();
    try {
        underReplicationManager.markLedgerUnderreplicated(lh.getId(), replicaToKill.toString());
        while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)) {
            Thread.sleep(100);
        }
        killAllBookies(lh, newBkAddr);
        // Should be able to read the entries from 0-9
        verifyRecoveredLedgers(lh, 0, 9);
        lh = bkc.openLedgerNoRecovery(lh.getId(), BookKeeper.DigestType.CRC32, TESTPASSWD);
        assertFalse("Ledger must have been closed by RW", ClientUtil.isLedgerOpen(lh));
    } finally {
        rw.shutdown();
        underReplicationManager.close();
    }
}
Also used : LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) MetadataClientDriver(org.apache.bookkeeper.meta.MetadataClientDriver) Cleanup(lombok.Cleanup) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) Test(org.junit.Test)

Example 10 with LedgerManagerFactory

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

the class TestReplicationWorker method testRWShouldReplicateTheLedgersAfterTimeoutIfLastFragmentIsNotUR.

/**
 * Tests that ReplicationWorker should not have identified for postponing
 * the replication if ledger is in open state and lastFragment is not in
 * underReplication state. Note that RW should not fence such ledgers.
 */
@Test
public void testRWShouldReplicateTheLedgersAfterTimeoutIfLastFragmentIsNotUR() throws Exception {
    LedgerHandle lh = bkc.createLedger(3, 3, BookKeeper.DigestType.CRC32, TESTPASSWD);
    for (int i = 0; i < 10; i++) {
        lh.addEntry(data);
    }
    BookieSocketAddress replicaToKill = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);
    LOG.info("Killing Bookie", replicaToKill);
    killBookie(replicaToKill);
    BookieSocketAddress newBkAddr = startNewBookieAndReturnAddress();
    LOG.info("New Bookie addr : {}", newBkAddr);
    // under-replication
    for (int i = 0; i < 10; i++) {
        lh.addEntry(data);
    }
    ReplicationWorker rw = new ReplicationWorker(zkc, baseConf);
    baseClientConf.setZkServers(zkUtil.getZooKeeperConnectString());
    @Cleanup MetadataClientDriver driver = MetadataDrivers.getClientDriver(URI.create(baseClientConf.getMetadataServiceUri()));
    driver.initialize(baseClientConf, scheduler, NullStatsLogger.INSTANCE, Optional.empty());
    LedgerManagerFactory mFactory = driver.getLedgerManagerFactory();
    LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
    rw.start();
    try {
        underReplicationManager.markLedgerUnderreplicated(lh.getId(), replicaToKill.toString());
        while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)) {
            Thread.sleep(100);
        }
        killAllBookies(lh, newBkAddr);
        // Should be able to read the entries from 0-9
        verifyRecoveredLedgers(lh, 0, 9);
        lh = bkc.openLedgerNoRecovery(lh.getId(), BookKeeper.DigestType.CRC32, TESTPASSWD);
        // Ledger should be still in open state
        assertTrue("Ledger must have been closed by RW", ClientUtil.isLedgerOpen(lh));
    } finally {
        rw.shutdown();
        underReplicationManager.close();
    }
}
Also used : LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) MetadataClientDriver(org.apache.bookkeeper.meta.MetadataClientDriver) Cleanup(lombok.Cleanup) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) Test(org.junit.Test)

Aggregations

LedgerManagerFactory (org.apache.bookkeeper.meta.LedgerManagerFactory)13 Test (org.junit.Test)8 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)7 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)6 AbstractZkLedgerManagerFactory (org.apache.bookkeeper.meta.AbstractZkLedgerManagerFactory)4 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)4 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Cleanup (lombok.Cleanup)2 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)2 LedgerManager (org.apache.bookkeeper.meta.LedgerManager)2 MetadataClientDriver (org.apache.bookkeeper.meta.MetadataClientDriver)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 FilenameFilter (java.io.FilenameFilter)1