Search in sources :

Example 1 with TooManyRequestsException

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

the class EntryCacheImpl method asyncReadEntry.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void asyncReadEntry(LedgerHandle lh, long firstEntry, long lastEntry, boolean isSlowestReader, final ReadEntriesCallback callback, Object ctx) {
    final long ledgerId = lh.getId();
    final int entriesToRead = (int) (lastEntry - firstEntry) + 1;
    final PositionImpl firstPosition = PositionImpl.get(lh.getId(), firstEntry);
    final PositionImpl lastPosition = PositionImpl.get(lh.getId(), lastEntry);
    if (log.isDebugEnabled()) {
        log.debug("[{}] Reading entries range ledger {}: {} to {}", ml.getName(), ledgerId, firstEntry, lastEntry);
    }
    Collection<EntryImpl> cachedEntries = entries.getRange(firstPosition, lastPosition);
    firstPosition.recycle();
    lastPosition.recycle();
    if (cachedEntries.size() == entriesToRead) {
        long totalCachedSize = 0;
        final List<EntryImpl> entriesToReturn = Lists.newArrayListWithExpectedSize(entriesToRead);
        // All entries found in cache
        for (EntryImpl entry : cachedEntries) {
            entriesToReturn.add(new EntryImpl(entry));
            totalCachedSize += entry.getLength();
            entry.release();
        }
        manager.mlFactoryMBean.recordCacheHits(entriesToReturn.size(), totalCachedSize);
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ledger {} -- Found in cache entries: {}-{}", ml.getName(), ledgerId, firstEntry, lastEntry);
        }
        callback.readEntriesComplete((List) entriesToReturn, ctx);
    } else {
        if (!cachedEntries.isEmpty()) {
            cachedEntries.forEach(entry -> entry.release());
        }
        // Read all the entries from bookkeeper
        lh.asyncReadEntries(firstEntry, lastEntry, (rc, lh1, sequence, cb) -> {
            if (rc != BKException.Code.OK) {
                if (rc == BKException.Code.TooManyRequestsException) {
                    callback.readEntriesFailed(new TooManyRequestsException("Too many request error from bookies"), ctx);
                } else {
                    ml.invalidateLedgerHandle(lh1, rc);
                    callback.readEntriesFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
                }
                return;
            }
            checkNotNull(ml.getName());
            checkNotNull(ml.getExecutor());
            ml.getExecutor().submitOrdered(ml.getName(), safeRun(() -> {
                // We got the entries, we need to transform them to a List<> type
                long totalSize = 0;
                final List<EntryImpl> entriesToReturn = Lists.newArrayListWithExpectedSize(entriesToRead);
                while (sequence.hasMoreElements()) {
                    // Insert the entries at the end of the list (they will be unsorted for now)
                    EntryImpl entry = new EntryImpl(sequence.nextElement());
                    entriesToReturn.add(entry);
                    totalSize += entry.getLength();
                }
                manager.mlFactoryMBean.recordCacheMiss(entriesToReturn.size(), totalSize);
                ml.getMBean().addReadEntriesSample(entriesToReturn.size(), totalSize);
                callback.readEntriesComplete((List) entriesToReturn, ctx);
            }));
        }, callback);
    }
}
Also used : TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) List(java.util.List)

Example 2 with TooManyRequestsException

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

the class PersistentDispatcherSingleActiveConsumer method readEntriesFailed.

@Override
public synchronized void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
    havePendingRead = false;
    Consumer c = (Consumer) ctx;
    long waitTimeMillis = readFailureBackoff.next();
    if (!(exception instanceof TooManyRequestsException)) {
        log.error("[{}] Error reading entries at {} : {} - Retrying to read in {} seconds", c, cursor.getReadPosition(), exception.getMessage(), waitTimeMillis / 1000.0);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Got throttled by bookies while reading at {} : {} - Retrying to read in {} seconds", c, cursor.getReadPosition(), exception.getMessage(), waitTimeMillis / 1000.0);
        }
    }
    checkNotNull(c);
    // Reduce read batch size to avoid flooding bookies with retries
    readBatchSize = 1;
    topic.getBrokerService().executor().schedule(() -> {
        synchronized (PersistentDispatcherSingleActiveConsumer.this) {
            Consumer currentConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
            // we should retry the read if we have an active consumer and there is no pending read
            if (currentConsumer != null && !havePendingRead) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Retrying read operation", c);
                }
                readMoreEntries(currentConsumer);
            } else {
                log.info("[{}] Skipping read retry: Current Consumer {}, havePendingRead {}", c, currentConsumer, havePendingRead);
            }
        }
    }, waitTimeMillis, TimeUnit.MILLISECONDS);
}
Also used : TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) Consumer(com.yahoo.pulsar.broker.service.Consumer)

Example 3 with TooManyRequestsException

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

the class PersistentDispatcherMultipleConsumers method readEntriesFailed.

@Override
public synchronized void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
    ReadType readType = (ReadType) ctx;
    long waitTimeMillis = readFailureBackoff.next();
    if (!(exception instanceof TooManyRequestsException)) {
        log.error("[{}] Error reading entries at {} : {}, Read Type {} - Retrying to read in {} seconds", name, cursor.getReadPosition(), exception.getMessage(), readType, waitTimeMillis / 1000.0);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Error reading entries at {} : {}, Read Type {} - Retrying to read in {} seconds", name, cursor.getReadPosition(), exception.getMessage(), readType, waitTimeMillis / 1000.0);
        }
    }
    if (shouldRewindBeforeReadingOrReplaying) {
        shouldRewindBeforeReadingOrReplaying = false;
        cursor.rewind();
    }
    if (readType == ReadType.Normal) {
        havePendingRead = false;
    } else {
        havePendingReplayRead = false;
        if (exception instanceof ManagedLedgerException.InvalidReplayPositionException) {
            PositionImpl markDeletePosition = (PositionImpl) cursor.getMarkDeletedPosition();
            messagesToReplay.removeIf(current -> current.compareTo(markDeletePosition) <= 0);
        }
    }
    readBatchSize = 1;
    topic.getBrokerService().executor().schedule(() -> {
        synchronized (PersistentDispatcherMultipleConsumers.this) {
            if (!havePendingRead) {
                log.info("[{}] Retrying read operation", name);
                readMoreEntries();
            } else {
                log.info("[{}] Skipping read retry: havePendingRead {}", name, havePendingRead, exception);
            }
        }
    }, waitTimeMillis, TimeUnit.MILLISECONDS);
}
Also used : TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl)

Aggregations

TooManyRequestsException (org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException)3 Consumer (com.yahoo.pulsar.broker.service.Consumer)1 List (java.util.List)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)1