Search in sources :

Example 31 with ManagedCursor

use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.

the class ManagedLedgerBkTest method ledgerFencedByAutoReplication.

/**
     * When auto-replication is triggered, if there were no writes on the ML during the grace period, auto-replication
     * will close the ledger an re-replicate it. After that, the next write will get a FencedException. We should
     * recover from this condition by creating a new ledger and retrying the write.
     */
@Test
public void ledgerFencedByAutoReplication() throws Exception {
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", config);
    ManagedCursor c1 = ledger.openCursor("c1");
    PositionImpl p1 = (PositionImpl) ledger.addEntry("entry-1".getBytes());
    // Trigger the closure of the data ledger
    bkc.openLedger(p1.getLedgerId(), DigestType.MAC, new byte[] {});
    ledger.addEntry("entry-2".getBytes());
    assertEquals(2, c1.getNumberOfEntries());
    assertEquals(2, c1.getNumberOfEntriesInBacklog());
    PositionImpl p3 = (PositionImpl) ledger.addEntry("entry-3".getBytes());
    // Now entry-2 should have been written before entry-3
    assertEquals(3, c1.getNumberOfEntries());
    assertEquals(3, c1.getNumberOfEntriesInBacklog());
    assertTrue(p1.getLedgerId() != p3.getLedgerId());
    factory.shutdown();
}
Also used : ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 32 with ManagedCursor

use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.

the class ManagedLedgerBkTest method ledgerFencedByFailover.

/**
     * When another process steals the ML, the old instance should not succeed in any operation
     */
@Test
public void ledgerFencedByFailover() throws Exception {
    ManagedLedgerFactoryImpl factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedgerImpl ledger1 = (ManagedLedgerImpl) factory1.open("my_test_ledger", config);
    ledger1.openCursor("c");
    ledger1.addEntry("entry-1".getBytes());
    // Open the ML from another factory
    ManagedLedgerFactoryImpl factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerImpl ledger2 = (ManagedLedgerImpl) factory2.open("my_test_ledger", config);
    ManagedCursor c2 = ledger2.openCursor("c");
    try {
        ledger1.addEntry("entry-2".getBytes());
        fail("Should have failed");
    } catch (ManagedLedgerException e) {
    // Ok
    }
    ledger2.addEntry("entry-2".getBytes());
    try {
        ledger1.addEntry("entry-2".getBytes());
        fail("Should have failed");
    } catch (ManagedLedgerException bve) {
    // Ok
    }
    assertEquals(2, c2.getNumberOfEntriesInBacklog());
    factory1.shutdown();
    factory2.shutdown();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 33 with ManagedCursor

use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.

the class ManagedLedgerErrorsTest method handleCursorRecoveryFailure.

@Test
public void handleCursorRecoveryFailure() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("my-cursor");
    Position p0 = cursor.getMarkDeletedPosition();
    Position p1 = ledger.addEntry("entry-1".getBytes());
    cursor.markDelete(p1);
    // Re-open from a different factory
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, zkc);
    bkc.failAfter(3, BKException.Code.LedgerRecoveryException);
    ledger = factory2.open("my_test_ledger");
    cursor = ledger.openCursor("my-cursor");
    // Since the cursor was rewind, it will be back to p0
    assertEquals(cursor.getMarkDeletedPosition(), p0);
    factory2.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 34 with ManagedCursor

use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.

the class ManagedLedgerTest method testActiveDeactiveCursor.

@Test
public void testActiveDeactiveCursor() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("cache_eviction_ledger");
    Field cacheField = ManagedLedgerImpl.class.getDeclaredField("entryCache");
    cacheField.setAccessible(true);
    EntryCacheImpl entryCache = (EntryCacheImpl) cacheField.get(ledger);
    final int totalInsertedEntries = 20;
    for (int i = 0; i < totalInsertedEntries; i++) {
        // 5 bytes
        String content = "entry";
        ledger.addEntry(content.getBytes());
    }
    // (1) Validate: cache not stores entries as no active cursor
    assertEquals(0, entryCache.getSize());
    // Open Cursor also adds cursor into activeCursor-container
    ManagedCursor cursor1 = ledger.openCursor("c1");
    ManagedCursor cursor2 = ledger.openCursor("c2");
    ledger.deactivateCursor(cursor2);
    for (int i = 0; i < totalInsertedEntries; i++) {
        // 5 bytes
        String content = "entry";
        ledger.addEntry(content.getBytes());
    }
    // (2) Validate: cache stores entries as active cursor has not read message
    assertEquals((5 * totalInsertedEntries), entryCache.getSize());
    // read 20 entries
    List<Entry> entries1 = cursor1.readEntries(totalInsertedEntries);
    for (Entry entry : entries1) {
        log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
        entry.release();
    }
    // (3) Validate: cache discards all entries as read by active cursor
    assertEquals(0, entryCache.getSize());
    ledger.close();
}
Also used : Field(java.lang.reflect.Field) Entry(org.apache.bookkeeper.mledger.Entry) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 35 with ManagedCursor

use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.

the class ManagedLedgerTest method readFromOlderLedgers.

@Test(timeOut = 20000)
public void readFromOlderLedgers() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    ManagedCursor cursor = ledger.openCursor("test");
    ledger.addEntry("entry-1".getBytes(Encoding));
    ledger.addEntry("entry-2".getBytes(Encoding));
    ledger.addEntry("entry-3".getBytes(Encoding));
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), false);
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)302 Test (org.testng.annotations.Test)281 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)228 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)153 Position (org.apache.bookkeeper.mledger.Position)131 Entry (org.apache.bookkeeper.mledger.Entry)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)111 CountDownLatch (java.util.concurrent.CountDownLatch)66 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 CyclicBarrier (java.util.concurrent.CyclicBarrier)30 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)26 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)24 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 BKException (org.apache.bookkeeper.client.BKException)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 List (java.util.List)18 Future (java.util.concurrent.Future)18