Search in sources :

Example 1 with BKNoSuchLedgerExistsException

use of org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException in project bookkeeper by apache.

the class ReplicationWorker method deferLedgerLockRelease.

/**
 * Schedules a timer task for releasing the lock which will be scheduled
 * after open ledger fragment replication time. Ledger will be fenced if it
 * is still in open state when timer task fired.
 */
private void deferLedgerLockRelease(final long ledgerId) {
    long gracePeriod = this.openLedgerRereplicationGracePeriod;
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            boolean isRecoveryOpen = false;
            LedgerHandle lh = null;
            try {
                lh = admin.openLedgerNoRecovery(ledgerId);
                if (isLastSegmentOpenAndMissingBookies(lh)) {
                    // Need recovery open, close the old ledger handle.
                    lh.close();
                    // Recovery open could result in client write failure.
                    LOG.warn("Missing bookie(s) from last segment. Opening Ledger{} for Recovery.", ledgerId);
                    lh = admin.openLedger(ledgerId);
                    isRecoveryOpen = true;
                }
                if (!isRecoveryOpen) {
                    Set<LedgerFragment> fragments = getUnderreplicatedFragments(lh, conf.getAuditorLedgerVerificationPercentage());
                    for (LedgerFragment fragment : fragments) {
                        if (!fragment.isClosed()) {
                            // Need recovery open, close the old ledger handle.
                            lh.close();
                            // Recovery open could result in client write failure.
                            LOG.warn("Open Fragment{}. Opening Ledger{} for Recovery.", fragment.getEnsemble(), ledgerId);
                            lh = admin.openLedger(ledgerId);
                            isRecoveryOpen = true;
                            break;
                        }
                    }
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                LOG.info("InterruptedException while fencing the ledger {}" + " for rereplication of postponed ledgers", ledgerId, e);
            } catch (BKNoSuchLedgerExistsException bknsle) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Ledger {} was deleted, safe to continue", ledgerId, bknsle);
                }
            } catch (BKException e) {
                LOG.error("BKException while fencing the ledger {}" + " for rereplication of postponed ledgers", ledgerId, e);
            } finally {
                try {
                    if (lh != null) {
                        lh.close();
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    LOG.info("InterruptedException while closing ledger {}", ledgerId, e);
                } catch (BKException e) {
                    // Lets go ahead and release the lock. Catch actual
                    // exception in normal replication flow and take
                    // action.
                    LOG.warn("BKException while closing ledger {} ", ledgerId, e);
                } finally {
                    try {
                        underreplicationManager.releaseUnderreplicatedLedger(ledgerId);
                    } catch (UnavailableException e) {
                        LOG.error("UnavailableException while replicating fragments of ledger {}", ledgerId, e);
                        shutdown();
                    }
                }
            }
        }
    };
    pendingReplicationTimer.schedule(timerTask, gracePeriod);
}
Also used : TimerTask(java.util.TimerTask) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException) BKException(org.apache.bookkeeper.client.BKException) LedgerFragment(org.apache.bookkeeper.client.LedgerFragment) BKNoSuchLedgerExistsException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException)

Example 2 with BKNoSuchLedgerExistsException

use of org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException in project bookkeeper by apache.

the class ReplicationWorker method rereplicate.

private boolean rereplicate(long ledgerIdToReplicate) throws InterruptedException, BKException, UnavailableException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Going to replicate the fragments of the ledger: {}", ledgerIdToReplicate);
    }
    boolean deferLedgerLockRelease = false;
    try (LedgerHandle lh = admin.openLedgerNoRecovery(ledgerIdToReplicate)) {
        Set<LedgerFragment> fragments = getUnderreplicatedFragments(lh, conf.getAuditorLedgerVerificationPercentage());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Founds fragments {} for replication from ledger: {}", fragments, ledgerIdToReplicate);
        }
        boolean foundOpenFragments = false;
        for (LedgerFragment ledgerFragment : fragments) {
            if (!ledgerFragment.isClosed()) {
                foundOpenFragments = true;
                continue;
            }
            try {
                admin.replicateLedgerFragment(lh, ledgerFragment);
            } catch (BKException.BKBookieHandleNotAvailableException e) {
                LOG.warn("BKBookieHandleNotAvailableException while replicating the fragment", e);
            } catch (BKException.BKLedgerRecoveryException e) {
                LOG.warn("BKLedgerRecoveryException while replicating the fragment", e);
            } catch (BKException.BKNotEnoughBookiesException e) {
                LOG.warn("BKNotEnoughBookiesException while replicating the fragment", e);
            }
        }
        if (foundOpenFragments || isLastSegmentOpenAndMissingBookies(lh)) {
            deferLedgerLockRelease = true;
            deferLedgerLockRelease(ledgerIdToReplicate);
            return false;
        }
        fragments = getUnderreplicatedFragments(lh, conf.getAuditorLedgerVerificationPercentage());
        if (fragments.size() == 0) {
            LOG.info("Ledger replicated successfully. ledger id is: " + ledgerIdToReplicate);
            underreplicationManager.markLedgerReplicated(ledgerIdToReplicate);
            return true;
        } else {
            // for the replication again for the pending fragments
            return false;
        }
    } catch (BKNoSuchLedgerExistsException e) {
        // Ledger might have been deleted by user
        LOG.info("BKNoSuchLedgerExistsException while opening " + "ledger {} for replication. Other clients " + "might have deleted the ledger. " + "So, no harm to continue", ledgerIdToReplicate);
        underreplicationManager.markLedgerReplicated(ledgerIdToReplicate);
        getExceptionCounter("BKNoSuchLedgerExistsException").inc();
        return false;
    } catch (BKNotEnoughBookiesException e) {
        logBKExceptionAndReleaseLedger(e, ledgerIdToReplicate);
        throw e;
    } catch (BKException e) {
        logBKExceptionAndReleaseLedger(e, ledgerIdToReplicate);
        return false;
    } finally {
        // already been released, this is a no-op
        if (!deferLedgerLockRelease) {
            try {
                underreplicationManager.releaseUnderreplicatedLedger(ledgerIdToReplicate);
            } catch (UnavailableException e) {
                LOG.error("UnavailableException while releasing the underreplicated lock for ledger {}:", ledgerIdToReplicate, e);
                shutdown();
            }
        }
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException) BKException(org.apache.bookkeeper.client.BKException) LedgerFragment(org.apache.bookkeeper.client.LedgerFragment) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) BKNoSuchLedgerExistsException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException)

Example 3 with BKNoSuchLedgerExistsException

use of org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException in project bookkeeper by apache.

the class TestDistributedLogTool method testToolReadEntriesCommand.

@Test(timeout = 60000)
public void testToolReadEntriesCommand() throws Exception {
    ReadEntriesCommand cmd = new ReadEntriesCommand();
    cmd.setUri(defaultUri);
    cmd.setLedgerId(99999999);
    try {
        cmd.runCmd();
    } catch (BKNoSuchLedgerExistsException ex) {
    }
}
Also used : ReadEntriesCommand(org.apache.distributedlog.tools.DistributedLogTool.ReadEntriesCommand) BKNoSuchLedgerExistsException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException) Test(org.junit.Test)

Example 4 with BKNoSuchLedgerExistsException

use of org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException in project bookkeeper by apache.

the class TestDistributedLogTool method testToolReadLastConfirmed.

@Test(timeout = 60000)
public void testToolReadLastConfirmed() throws Exception {
    ReadLastConfirmedCommand cmd = new ReadLastConfirmedCommand();
    cmd.setUri(defaultUri);
    cmd.setLedgerId(99999999);
    // correct functionality.
    try {
        cmd.runCmd();
    } catch (BKNoSuchLedgerExistsException ex) {
    }
}
Also used : BKNoSuchLedgerExistsException(org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException) ReadLastConfirmedCommand(org.apache.distributedlog.tools.DistributedLogTool.ReadLastConfirmedCommand) Test(org.junit.Test)

Aggregations

BKNoSuchLedgerExistsException (org.apache.bookkeeper.client.BKException.BKNoSuchLedgerExistsException)4 BKException (org.apache.bookkeeper.client.BKException)2 LedgerFragment (org.apache.bookkeeper.client.LedgerFragment)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 UnavailableException (org.apache.bookkeeper.replication.ReplicationException.UnavailableException)2 Test (org.junit.Test)2 TimerTask (java.util.TimerTask)1 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)1 ReadEntriesCommand (org.apache.distributedlog.tools.DistributedLogTool.ReadEntriesCommand)1 ReadLastConfirmedCommand (org.apache.distributedlog.tools.DistributedLogTool.ReadLastConfirmedCommand)1