Search in sources :

Example 16 with MetaStoreException

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

the class ManagedLedgerTest method testBackwardCompatiblityForMeta.

@Test
public void testBackwardCompatiblityForMeta() throws Exception {
    final ManagedLedgerInfo[] storedMLInfo = new ManagedLedgerInfo[3];
    final Stat[] versions = new Stat[1];
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig conf = new ManagedLedgerConfig();
    conf.setMaxEntriesPerLedger(1);
    conf.setRetentionSizeInMB(10);
    conf.setRetentionTime(1, TimeUnit.HOURS);
    ManagedLedger ml = factory.open("backward_test_ledger", conf);
    ml.openCursor("c1");
    ml.addEntry("msg1".getBytes());
    ml.addEntry("msg2".getBytes());
    ml.close();
    MetaStore store = new MetaStoreImplZookeeper(zkc, executor);
    CountDownLatch l1 = new CountDownLatch(1);
    // obtain the ledger info
    store.getManagedLedgerInfo("backward_test_ledger", new MetaStoreCallback<ManagedLedgerInfo>() {

        @Override
        public void operationComplete(ManagedLedgerInfo result, Stat version) {
            storedMLInfo[0] = result;
            versions[0] = version;
            l1.countDown();
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            fail("on get ManagedLedgerInfo backward_test_ledger");
        }
    });
    l1.await();
    ManagedLedgerInfo.Builder builder1 = ManagedLedgerInfo.newBuilder();
    // simulate test for old ledger with no timestampl
    for (LedgerInfo info : storedMLInfo[0].getLedgerInfoList()) {
        LedgerInfo noTimestamp = ManagedLedgerInfo.LedgerInfo.newBuilder().mergeFrom(info).clearTimestamp().build();
        assertFalse(noTimestamp.hasTimestamp(), "expected old version info with no timestamp");
        builder1.addLedgerInfo(noTimestamp);
    }
    storedMLInfo[1] = builder1.build();
    // test timestamp on new ledger
    CountDownLatch l2 = new CountDownLatch(1);
    store.asyncUpdateLedgerIds("backward_test_ledger", storedMLInfo[1], versions[0], new MetaStoreCallback<Void>() {

        @Override
        public void operationComplete(Void result, Stat version) {
            l2.countDown();
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            fail("on asyncUpdateLedgerIds");
        }
    });
    // verify that after update ledgers have timestamp
    ManagedLedgerImpl newVersionLedger = (ManagedLedgerImpl) factory.open("backward_test_ledger", conf);
    List<LedgerInfo> mlInfo = newVersionLedger.getLedgersInfoAsList();
    assertTrue(mlInfo.stream().allMatch(new Predicate<LedgerInfo>() {

        @Override
        public boolean test(LedgerInfo ledgerInfo) {
            return ledgerInfo.hasTimestamp();
        }
    }));
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) Predicate(java.util.function.Predicate) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 17 with MetaStoreException

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

the class ManagedLedgerImpl method asyncDeleteCursor.

@Override
public synchronized void asyncDeleteCursor(final String consumerName, final DeleteCursorCallback callback, final Object ctx) {
    final ManagedCursorImpl cursor = (ManagedCursorImpl) cursors.get(consumerName);
    if (cursor == null) {
        callback.deleteCursorFailed(new ManagedLedgerException("ManagedCursor not found: " + consumerName), ctx);
        return;
    }
    // First remove the consumer form the MetaStore. If this operation succeeds and the next one (removing the
    // ledger from BK) don't, we end up having a loose ledger leaked but the state will be consistent.
    store.asyncRemoveCursor(ManagedLedgerImpl.this.name, consumerName, new MetaStoreCallback<Void>() {

        @Override
        public void operationComplete(Void result, Stat stat) {
            cursor.asyncDeleteCursorLedger();
            cursors.removeCursor(consumerName);
            // Redo invalidation of entries in cache
            PositionImpl slowestConsumerPosition = cursors.getSlowestReaderPosition();
            if (slowestConsumerPosition != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Doing cache invalidation up to {}", slowestConsumerPosition);
                }
                entryCache.invalidateEntries(slowestConsumerPosition);
            } else {
                entryCache.clear();
            }
            trimConsumedLedgersInBackground();
            log.info("[{}] [{}] Deleted cursor", name, consumerName);
            callback.deleteCursorComplete(ctx);
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            callback.deleteCursorFailed(e, ctx);
        }
    });
}
Also used : MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat)

Example 18 with MetaStoreException

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

the class MetaStoreImplZookeeper method asyncUpdateLedgerIds.

@Override
public void asyncUpdateLedgerIds(String ledgerName, ManagedLedgerInfo mlInfo, Stat stat, final MetaStoreCallback<Void> callback) {
    ZKStat zkStat = (ZKStat) stat;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Updating metadata version={} with content={}", ledgerName, zkStat.version, mlInfo);
    }
    byte[] serializedMlInfo = //
    protobufFormat == ZNodeProtobufFormat.Text ? // Text format
    mlInfo.toString().getBytes(Encoding) : // Binary format
    mlInfo.toByteArray();
    zk.setData(prefix + ledgerName, serializedMlInfo, zkStat.getVersion(), (rc, path, zkCtx, stat1) -> executor.submit(safeRun(() -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] UpdateLedgersIdsCallback.processResult rc={} newVersion={}", ledgerName, Code.get(rc), stat != null ? stat.getVersion() : "null");
        }
        MetaStoreException status = null;
        if (rc == Code.BADVERSION.intValue()) {
            // Content has been modified on ZK since our last read
            status = new BadVersionException(KeeperException.create(Code.get(rc)));
            callback.operationFailed(status);
        } else if (rc != Code.OK.intValue()) {
            status = new MetaStoreException(KeeperException.create(Code.get(rc)));
            callback.operationFailed(status);
        } else {
            callback.operationComplete(null, new ZKStat(stat1));
        }
    })), null);
}
Also used : MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)

Example 19 with MetaStoreException

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

the class ManagedLedgerTest method cursorReadsWithDiscardedEmptyLedgersStillListed.

@Test
public void cursorReadsWithDiscardedEmptyLedgersStillListed() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("entry-1".getBytes());
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    ledger.addEntry("entry-2".getBytes());
    final LedgerInfo l1info = ledger.getLedgersInfoAsList().get(0);
    final LedgerInfo l2info = ledger.getLedgersInfoAsList().get(1);
    ledger.close();
    // Add the deleted ledger back in the meta-data to simulate an empty ledger that was deleted but not removed
    // from the list of ledgers
    final CountDownLatch counter = new CountDownLatch(1);
    final MetaStore store = factory.getMetaStore();
    store.getManagedLedgerInfo("my_test_ledger", new MetaStoreCallback<ManagedLedgerInfo>() {

        @Override
        public void operationComplete(ManagedLedgerInfo result, Stat version) {
            // Update the list
            ManagedLedgerInfo.Builder info = ManagedLedgerInfo.newBuilder(result);
            info.clearLedgerInfo();
            info.addLedgerInfo(LedgerInfo.newBuilder().setLedgerId(l1info.getLedgerId()).build());
            info.addLedgerInfo(l2info);
            store.asyncUpdateLedgerIds("my_test_ledger", info.build(), version, new MetaStoreCallback<Void>() {

                @Override
                public void operationComplete(Void result, Stat version) {
                    counter.countDown();
                }

                @Override
                public void operationFailed(MetaStoreException e) {
                    counter.countDown();
                }
            });
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            counter.countDown();
        }
    });
    // Wait for the change to be effective
    counter.await();
    // Delete the ledger and mantain it in the ledgers list
    bkc.deleteLedger(l1info.getLedgerId());
    // re-open
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    assertEquals(c1.getNumberOfEntries(), 1);
    assertEquals(c1.hasMoreEntries(), true);
    assertEquals(ledger.getLedgersInfoAsList().size(), 2);
    List<Entry> entries = c1.readEntries(10);
    assertEquals(entries.size(), 1);
    entries.forEach(e -> e.release());
    assertEquals(c1.hasMoreEntries(), false);
    entries = c1.readEntries(1);
    assertEquals(entries.size(), 0);
    entries.forEach(e -> e.release());
}
Also used : MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) Entry(org.apache.bookkeeper.mledger.Entry) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) Test(org.testng.annotations.Test)

Aggregations

MetaStoreException (org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException)19 Stat (org.apache.bookkeeper.mledger.impl.MetaStore.Stat)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 ManagedLedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo)9 Test (org.testng.annotations.Test)8 MetaStoreCallback (org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback)6 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)5 LedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo)5 BadVersionException (org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)4 ManagedCursorInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ParseException (com.google.protobuf.TextFormat.ParseException)2 OpenCallback (org.apache.bookkeeper.client.AsyncCallback.OpenCallback)2 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)2 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 BoundType (com.google.common.collect.BoundType)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Queues (com.google.common.collect.Queues)1