Search in sources :

Example 1 with BadVersionException

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

the class ManagedLedgerImpl method createComplete.

// //////////////////////////////////////////////////////////////////////
// Callbacks
@Override
public synchronized void createComplete(int rc, final LedgerHandle lh, Object ctx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] createComplete rc={} ledger={}", name, rc, lh != null ? lh.getId() : -1);
    }
    mbean.endDataLedgerCreateOp();
    if (rc != BKException.Code.OK) {
        log.error("[{}] Error creating ledger rc={} {}", name, rc, BKException.getMessage(rc));
        ManagedLedgerException status = new ManagedLedgerException(BKException.getMessage(rc));
        // Empty the list of pending requests and make all of them fail
        clearPendingAddEntries(status);
        lastLedgerCreationFailureTimestamp = System.currentTimeMillis();
        STATE_UPDATER.set(this, State.ClosedLedger);
    } else {
        log.info("[{}] Created new ledger {}", name, lh.getId());
        ledgers.put(lh.getId(), LedgerInfo.newBuilder().setLedgerId(lh.getId()).setTimestamp(0).build());
        currentLedger = lh;
        currentLedgerEntries = 0;
        currentLedgerSize = 0;
        final MetaStoreCallback<Void> cb = new MetaStoreCallback<Void>() {

            @Override
            public void operationComplete(Void v, Stat stat) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Updating of ledgers list after create complete. version={}", name, stat);
                }
                ledgersStat = stat;
                ledgersListMutex.unlock();
                updateLedgersIdsComplete(stat);
                synchronized (ManagedLedgerImpl.this) {
                    mbean.addLedgerSwitchLatencySample(System.nanoTime() - lastLedgerCreationInitiationTimestamp, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public void operationFailed(MetaStoreException e) {
                if (e instanceof BadVersionException) {
                    synchronized (ManagedLedgerImpl.this) {
                        log.error("[{}] Failed to udpate ledger list. z-node version mismatch. Closing managed ledger", name);
                        STATE_UPDATER.set(ManagedLedgerImpl.this, State.Fenced);
                        clearPendingAddEntries(e);
                        return;
                    }
                }
                log.warn("[{}] Error updating meta data with the new list of ledgers: {}", name, e.getMessage());
                // Remove the ledger, since we failed to update the list
                ledgers.remove(lh.getId());
                mbean.startDataLedgerDeleteOp();
                bookKeeper.asyncDeleteLedger(lh.getId(), (rc1, ctx1) -> {
                    mbean.endDataLedgerDeleteOp();
                    if (rc1 != BKException.Code.OK) {
                        log.warn("[{}] Failed to delete ledger {}: {}", name, lh.getId(), BKException.getMessage(rc1));
                    }
                }, null);
                ledgersListMutex.unlock();
                synchronized (ManagedLedgerImpl.this) {
                    lastLedgerCreationFailureTimestamp = System.currentTimeMillis();
                    STATE_UPDATER.set(ManagedLedgerImpl.this, State.ClosedLedger);
                    clearPendingAddEntries(e);
                }
            }
        };
        updateLedgersListAfterRollover(cb);
    }
}
Also used : MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)

Example 2 with BadVersionException

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

the class MetaStoreImplZookeeper method asyncUpdateCursorInfo.

@Override
public void asyncUpdateCursorInfo(final String ledgerName, final String cursorName, final ManagedCursorInfo info, Stat stat, final MetaStoreCallback<Void> callback) {
    log.info("[{}] [{}] Updating cursor info ledgerId={} mark-delete={}:{}", ledgerName, cursorName, info.getCursorsLedgerId(), info.getMarkDeleteLedgerId(), info.getMarkDeleteEntryId());
    String path = prefix + ledgerName + "/" + cursorName;
    byte[] content = //
    protobufFormat == ZNodeProtobufFormat.Text ? // Text format
    info.toString().getBytes(Encoding) : // Binary format
    info.toByteArray();
    if (stat == null) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Creating consumer {} on meta-data store with {}", ledgerName, cursorName, info);
        }
        zk.create(path, content, Acl, CreateMode.PERSISTENT, (rc, path1, ctx, name) -> executor.submit(safeRun(() -> {
            if (rc != Code.OK.intValue()) {
                log.warn("[{}] Error creating cosumer {} node on meta-data store with {}: ", ledgerName, cursorName, info, Code.get(rc));
                callback.operationFailed(new MetaStoreException(KeeperException.create(Code.get(rc))));
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Created consumer {} on meta-data store with {}", ledgerName, cursorName, info);
                }
                callback.operationComplete(null, new ZKStat());
            }
        })), null);
    } else {
        ZKStat zkStat = (ZKStat) stat;
        if (log.isDebugEnabled()) {
            log.debug("[{}] Updating consumer {} on meta-data store with {}", ledgerName, cursorName, info);
        }
        zk.setData(path, content, zkStat.getVersion(), (rc, path1, ctx, stat1) -> executor.submit(safeRun(() -> {
            if (rc == Code.BADVERSION.intValue()) {
                callback.operationFailed(new BadVersionException(KeeperException.create(Code.get(rc))));
            } else if (rc != Code.OK.intValue()) {
                callback.operationFailed(new MetaStoreException(KeeperException.create(Code.get(rc))));
            } 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 3 with BadVersionException

use of org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException 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 4 with BadVersionException

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

the class ManagedLedgerErrorsTest method recoverAfterZnodeVersionError.

@Test
public void recoverAfterZnodeVersionError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    zkc.failNow(Code.BADVERSION);
    // First write will succeed
    ledger.addEntry("test".getBytes());
    try {
        // This write will try to create a ledger and it will fail at it
        ledger.addEntry("entry".getBytes());
        fail("should fail");
    } catch (BadVersionException e) {
    // ok
    }
    try {
        // At this point the ledger should be fenced for good
        ledger.addEntry("entry".getBytes());
        fail("should fail");
    } catch (ManagedLedgerFencedException e) {
    // ok
    }
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

BadVersionException (org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)4 MetaStoreException (org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException)3 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)1 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 ManagedLedgerFencedException (org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)1 MetaStoreCallback (org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback)1 Stat (org.apache.bookkeeper.mledger.impl.MetaStore.Stat)1 Test (org.testng.annotations.Test)1