Search in sources :

Example 1 with LedgerRange

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRange in project bookkeeper by apache.

the class ScanAndCompareGarbageCollector method gc.

@Override
public void gc(GarbageCleaner garbageCleaner) {
    if (null == ledgerManager) {
        // so skip garbage collection
        return;
    }
    try {
        // Get a set of all ledgers on the bookie
        NavigableSet<Long> bkActiveLedgers = Sets.newTreeSet(ledgerStorage.getActiveLedgersInRange(0, Long.MAX_VALUE));
        this.activeLedgerCounter = bkActiveLedgers.size();
        long curTime = MathUtils.now();
        boolean checkOverreplicatedLedgers = (enableGcOverReplicatedLedger && curTime - lastOverReplicatedLedgerGcTimeMillis > gcOverReplicatedLedgerIntervalMillis);
        if (checkOverreplicatedLedgers) {
            zk = ZooKeeperClient.newBuilder().connectString(conf.getZkServers()).sessionTimeoutMs(conf.getZkTimeout()).build();
            // remove all the overreplicated ledgers from the local bookie
            Set<Long> overReplicatedLedgers = removeOverReplicatedledgers(bkActiveLedgers, garbageCleaner);
            if (overReplicatedLedgers.isEmpty()) {
                LOG.info("No over-replicated ledgers found.");
            } else {
                LOG.info("Removed over-replicated ledgers: {}", overReplicatedLedgers);
            }
            lastOverReplicatedLedgerGcTimeMillis = MathUtils.now();
        }
        // Iterate over all the ledger on the metadata store
        LedgerRangeIterator ledgerRangeIterator = ledgerManager.getLedgerRanges();
        Set<Long> ledgersInMetadata = null;
        long start;
        long end = -1;
        boolean done = false;
        while (!done) {
            start = end + 1;
            if (ledgerRangeIterator.hasNext()) {
                LedgerRange lRange = ledgerRangeIterator.next();
                ledgersInMetadata = lRange.getLedgers();
                end = lRange.end();
            } else {
                ledgersInMetadata = new TreeSet<>();
                end = Long.MAX_VALUE;
                done = true;
            }
            Iterable<Long> subBkActiveLedgers = bkActiveLedgers.subSet(start, true, end, true);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Active in metadata {}, Active in bookie {}", ledgersInMetadata, subBkActiveLedgers);
            }
            for (Long bkLid : subBkActiveLedgers) {
                if (!ledgersInMetadata.contains(bkLid)) {
                    if (verifyMetadataOnGc) {
                        CountDownLatch latch = new CountDownLatch(1);
                        final AtomicInteger metaRC = new AtomicInteger(0);
                        ledgerManager.readLedgerMetadata(bkLid, (int rc, LedgerMetadata x) -> {
                            metaRC.set(rc);
                            latch.countDown();
                        });
                        latch.await();
                        if (metaRC.get() != BKException.Code.NoSuchLedgerExistsException) {
                            LOG.warn("Ledger {} Missing in metadata list, but ledgerManager returned rc: {}.", bkLid, metaRC.get());
                            continue;
                        }
                    }
                    deletedLedgerCounter.inc();
                    garbageCleaner.clean(bkLid);
                }
            }
        }
    } catch (Throwable t) {
        // ignore exception, collecting garbage next time
        LOG.warn("Exception when iterating over the metadata {}", t);
    } finally {
        if (zk != null) {
            try {
                zk.close();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                LOG.error("Error closing zk session", e);
            }
            zk = null;
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerRange(org.apache.bookkeeper.meta.LedgerManager.LedgerRange)

Example 2 with LedgerRange

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRange in project bookkeeper by apache.

the class GcLedgersTest method testGcLedgersNotLast.

@Test
public void testGcLedgersNotLast() throws Exception {
    final SortedSet<Long> createdLedgers = Collections.synchronizedSortedSet(new TreeSet<Long>());
    final List<Long> cleaned = new ArrayList<Long>();
    // Create enough ledgers to span over 4 ranges in the hierarchical ledger manager implementation
    final int numLedgers = 30001;
    createLedgers(numLedgers, createdLedgers);
    final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(getLedgerManager(), new MockLedgerStorage(), baseConf, NullStatsLogger.INSTANCE);
    GarbageCollector.GarbageCleaner cleaner = new GarbageCollector.GarbageCleaner() {

        @Override
        public void clean(long ledgerId) {
            LOG.info("Cleaned {}", ledgerId);
            cleaned.add(ledgerId);
        }
    };
    SortedSet<Long> scannedLedgers = new TreeSet<Long>();
    LedgerRangeIterator iterator = getLedgerManager().getLedgerRanges();
    while (iterator.hasNext()) {
        LedgerRange ledgerRange = iterator.next();
        scannedLedgers.addAll(ledgerRange.getLedgers());
    }
    assertEquals(createdLedgers, scannedLedgers);
    garbageCollector.gc(cleaner);
    assertTrue("Should have cleaned nothing", cleaned.isEmpty());
    long first = createdLedgers.first();
    removeLedger(first);
    garbageCollector.gc(cleaner);
    assertEquals("Should have cleaned something", 1, cleaned.size());
    assertEquals("Should have cleaned first ledger" + first, (long) first, (long) cleaned.get(0));
}
Also used : ArrayList(java.util.ArrayList) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) TreeSet(java.util.TreeSet) LedgerRange(org.apache.bookkeeper.meta.LedgerManager.LedgerRange) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) GarbageCollector(org.apache.bookkeeper.bookie.GarbageCollector) Test(org.junit.Test)

Example 3 with LedgerRange

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRange in project bookkeeper by apache.

the class GcLedgersTest method validateLedgerRangeIterator.

public void validateLedgerRangeIterator(SortedSet<Long> createdLedgers) throws IOException {
    SortedSet<Long> scannedLedgers = new TreeSet<Long>();
    LedgerRangeIterator iterator = getLedgerManager().getLedgerRanges();
    while (iterator.hasNext()) {
        LedgerRange ledgerRange = iterator.next();
        scannedLedgers.addAll(ledgerRange.getLedgers());
    }
    assertEquals(createdLedgers, scannedLedgers);
}
Also used : LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) TreeSet(java.util.TreeSet) LedgerRange(org.apache.bookkeeper.meta.LedgerManager.LedgerRange)

Aggregations

LedgerRange (org.apache.bookkeeper.meta.LedgerManager.LedgerRange)3 LedgerRangeIterator (org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator)3 TreeSet (java.util.TreeSet)2 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)1 GarbageCollector (org.apache.bookkeeper.bookie.GarbageCollector)1 ScanAndCompareGarbageCollector (org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector)1 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)1 Test (org.junit.Test)1