Search in sources :

Example 1 with CompactableLedgerStorage

use of org.apache.bookkeeper.bookie.CompactableLedgerStorage in project bookkeeper by apache.

the class GcLedgersTest method testGarbageCollectLedgers.

@Test
public void testGarbageCollectLedgers() throws Exception {
    int numLedgers = 100;
    int numRemovedLedgers = 10;
    final Set<Long> createdLedgers = new HashSet<Long>();
    final Set<Long> removedLedgers = new HashSet<Long>();
    // create 100 ledgers
    createLedgers(numLedgers, createdLedgers);
    Random r = new Random(System.currentTimeMillis());
    final List<Long> tmpList = new ArrayList<Long>();
    tmpList.addAll(createdLedgers);
    Collections.shuffle(tmpList, r);
    // random remove several ledgers
    for (int i = 0; i < numRemovedLedgers; i++) {
        long ledgerId = tmpList.get(i);
        synchronized (removedLedgers) {
            getLedgerManager().removeLedgerMetadata(ledgerId, Version.ANY, new GenericCallback<Void>() {

                @Override
                public void operationComplete(int rc, Void result) {
                    synchronized (removedLedgers) {
                        removedLedgers.notify();
                    }
                }
            });
            removedLedgers.wait();
        }
        removedLedgers.add(ledgerId);
        createdLedgers.remove(ledgerId);
    }
    final CountDownLatch inGcProgress = new CountDownLatch(1);
    final CountDownLatch createLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(2);
    final CompactableLedgerStorage mockLedgerStorage = new MockLedgerStorage();
    TestStatsProvider stats = new TestStatsProvider();
    final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(getLedgerManager(), mockLedgerStorage, baseConf, stats.getStatsLogger("gc"));
    Thread gcThread = new Thread() {

        @Override
        public void run() {
            garbageCollector.gc(new GarbageCollector.GarbageCleaner() {

                boolean paused = false;

                @Override
                public void clean(long ledgerId) {
                    try {
                        mockLedgerStorage.deleteLedger(ledgerId);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    if (!paused) {
                        inGcProgress.countDown();
                        try {
                            createLatch.await();
                        } catch (InterruptedException ie) {
                            Thread.currentThread().interrupt();
                        }
                        paused = true;
                    }
                    LOG.info("Garbage Collected ledger {}", ledgerId);
                }
            });
            LOG.info("Gc Thread quits.");
            endLatch.countDown();
        }
    };
    Thread createThread = new Thread() {

        @Override
        public void run() {
            try {
                inGcProgress.await();
                // create 10 more ledgers
                createLedgers(10, createdLedgers);
                LOG.info("Finished creating 10 more ledgers.");
                createLatch.countDown();
            } catch (Exception e) {
            }
            LOG.info("Create Thread quits.");
            endLatch.countDown();
        }
    };
    createThread.start();
    gcThread.start();
    endLatch.await();
    // test ledgers
    for (Long ledger : removedLedgers) {
        assertFalse(activeLedgers.containsKey(ledger));
    }
    for (Long ledger : createdLedgers) {
        assertTrue(activeLedgers.containsKey(ledger));
    }
    assertTrue("Wrong DELETED_LEDGER_COUNT", stats.getCounter("gc." + DELETED_LEDGER_COUNT).get() == removedLedgers.size());
    assertTrue("Wrong ACTIVE_LEDGER_COUNT", stats.getGauge("gc." + ACTIVE_LEDGER_COUNT).getSample().intValue() == createdLedgers.size());
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) ArrayList(java.util.ArrayList) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) BookieException(org.apache.bookkeeper.bookie.BookieException) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException) Random(java.util.Random) CompactableLedgerStorage(org.apache.bookkeeper.bookie.CompactableLedgerStorage) HashSet(java.util.HashSet) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) GarbageCollector(org.apache.bookkeeper.bookie.GarbageCollector) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BookieException (org.apache.bookkeeper.bookie.BookieException)1 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)1 CompactableLedgerStorage (org.apache.bookkeeper.bookie.CompactableLedgerStorage)1 GarbageCollector (org.apache.bookkeeper.bookie.GarbageCollector)1 ScanAndCompareGarbageCollector (org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector)1 BKException (org.apache.bookkeeper.client.BKException)1 TestStatsProvider (org.apache.bookkeeper.test.TestStatsProvider)1 Test (org.junit.Test)1