Search in sources :

Example 11 with GenericCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.

the class TestZkLedgerIdGenerator method testGenerateLedgerId.

@Test
public void testGenerateLedgerId() throws Exception {
    // Create *nThread* threads each generate *nLedgers* ledger id,
    // and then check there is no identical ledger id.
    final int nThread = 2;
    final int nLedgers = 2000;
    final CountDownLatch countDownLatch = new CountDownLatch(nThread * nLedgers);
    final AtomicInteger errCount = new AtomicInteger(0);
    final ConcurrentLinkedQueue<Long> ledgerIds = new ConcurrentLinkedQueue<Long>();
    final GenericCallback<Long> cb = new GenericCallback<Long>() {

        @Override
        public void operationComplete(int rc, Long result) {
            if (Code.OK.intValue() == rc) {
                ledgerIds.add(result);
            } else {
                errCount.incrementAndGet();
            }
            countDownLatch.countDown();
        }
    };
    long start = System.currentTimeMillis();
    for (int i = 0; i < nThread; i++) {
        new Thread() {

            @Override
            public void run() {
                for (int j = 0; j < nLedgers; j++) {
                    ledgerIdGenerator.generateLedgerId(cb);
                }
            }
        }.start();
    }
    assertTrue("Wait ledger id generation threads to stop timeout : ", countDownLatch.await(30, TimeUnit.SECONDS));
    LOG.info("Number of generated ledger id: {}, time used: {}", ledgerIds.size(), System.currentTimeMillis() - start);
    assertEquals("Error occur during ledger id generation : ", 0, errCount.get());
    Set<Long> ledgers = new HashSet<Long>();
    while (!ledgerIds.isEmpty()) {
        Long ledger = ledgerIds.poll();
        assertNotNull("Generated ledger id is null : ", ledger);
        assertFalse("Ledger id [" + ledger + "] conflict : ", ledgers.contains(ledger));
        ledgers.add(ledger);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with GenericCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.

the class GcLedgersTest method testGcLedgersIfReadLedgerMetadataSaysNoSuchLedger.

/*
     * In this test scenario no ledger is deleted, but LedgerManager.readLedgerMetadata says there is NoSuchLedger. So
     * even in that case, GarbageCollector.gc shouldn't delete ledgers data.
     *
     * Consider the possible scenario - when the LedgerIterator is created that ledger is not deleted, so as per
     * LedgerIterator that is live ledger. But right after the LedgerIterator creation that ledger is deleted, so
     * readLedgerMetadata call would return NoSuchLedger. In this testscenario we are validating that as per Iterator if
     * that ledger is alive though currently that ledger is deleted, we should not clean data of that ledger.
     *
     * ScanAndCompareGarbageCollector/GC should clean data of ledger only if both the LedgerManager.getLedgerRanges says
     * that ledger is not existing and also ledgerManager.readLedgerMetadata fails with error
     * NoSuchLedgerExistsException.
     *
     */
@Test
public void testGcLedgersIfReadLedgerMetadataSaysNoSuchLedger() throws Exception {
    final SortedSet<Long> createdLedgers = Collections.synchronizedSortedSet(new TreeSet<Long>());
    final SortedSet<Long> cleaned = Collections.synchronizedSortedSet(new TreeSet<Long>());
    // Create few ledgers
    final int numLedgers = 5;
    createLedgers(numLedgers, createdLedgers);
    LedgerManager mockLedgerManager = new CleanupLedgerManager(getLedgerManager()) {

        @Override
        public void readLedgerMetadata(long ledgerId, GenericCallback<LedgerMetadata> readCb) {
            readCb.operationComplete(BKException.Code.NoSuchLedgerExistsException, null);
        }
    };
    final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(mockLedgerManager, new MockLedgerStorage(), baseConf, NullStatsLogger.INSTANCE);
    GarbageCollector.GarbageCleaner cleaner = new GarbageCollector.GarbageCleaner() {

        @Override
        public void clean(long ledgerId) {
            LOG.info("Cleaned {}", ledgerId);
            cleaned.add(ledgerId);
        }
    };
    validateLedgerRangeIterator(createdLedgers);
    garbageCollector.gc(cleaner);
    assertTrue("Should have cleaned nothing", cleaned.isEmpty());
}
Also used : ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) GarbageCollector(org.apache.bookkeeper.bookie.GarbageCollector) Test(org.junit.Test)

Example 13 with GenericCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback in project bookkeeper by apache.

the class GcLedgersTest method createLedgers.

/**
 * Create ledgers.
 */
private void createLedgers(int numLedgers, final Set<Long> createdLedgers) throws IOException {
    final AtomicInteger expected = new AtomicInteger(numLedgers);
    for (int i = 0; i < numLedgers; i++) {
        getLedgerIdGenerator().generateLedgerId(new GenericCallback<Long>() {

            @Override
            public void operationComplete(int rc, final Long ledgerId) {
                if (BKException.Code.OK != rc) {
                    synchronized (expected) {
                        int num = expected.decrementAndGet();
                        if (num == 0) {
                            expected.notify();
                        }
                    }
                    return;
                }
                getLedgerManager().createLedgerMetadata(ledgerId, new LedgerMetadata(1, 1, 1, DigestType.MAC, "".getBytes()), new GenericCallback<Void>() {

                    @Override
                    public void operationComplete(int rc, Void result) {
                        if (rc == BKException.Code.OK) {
                            activeLedgers.put(ledgerId, true);
                            createdLedgers.add(ledgerId);
                        }
                        synchronized (expected) {
                            int num = expected.decrementAndGet();
                            if (num == 0) {
                                expected.notify();
                            }
                        }
                    }
                });
            }
        });
    }
    synchronized (expected) {
        try {
            while (expected.get() > 0) {
                expected.wait(100);
            }
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback)

Aggregations

GenericCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback)13 Test (org.junit.Test)6 HashSet (java.util.HashSet)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)3 Channel (io.netty.channel.Channel)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 GarbageCollector (org.apache.bookkeeper.bookie.GarbageCollector)2 ScanAndCompareGarbageCollector (org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector)2 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)2 OrderedExecutor (org.apache.bookkeeper.common.util.OrderedExecutor)2 AsyncCallback (org.apache.zookeeper.AsyncCallback)2