Search in sources :

Example 21 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKAsyncLogReaderDLSN method run.

@Override
public void run() {
    synchronized (scheduleLock) {
        if (scheduleDelayStopwatch.isRunning()) {
            scheduleLatency.registerSuccessfulEvent(scheduleDelayStopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        }
        Stopwatch runTime = Stopwatch.createStarted();
        int iterations = 0;
        long scheduleCountLocal = scheduleCount.get();
        LOG.debug("{}: Scheduled Background Reader", bkLedgerManager.getFullyQualifiedName());
        while (true) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{}: Executing Iteration: {}", bkLedgerManager.getFullyQualifiedName(), iterations++);
            }
            PendingReadRequest nextRequest = null;
            synchronized (this) {
                nextRequest = pendingRequests.peek();
                // Queue is empty, nothing to read, return
                if (null == nextRequest) {
                    LOG.trace("{}: Queue Empty waiting for Input", bkLedgerManager.getFullyQualifiedName());
                    scheduleCount.set(0);
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS));
                    return;
                }
                if (disableProcessingReadRequests) {
                    LOG.info("Reader of {} is forced to stop processing read requests", bkLedgerManager.getFullyQualifiedName());
                    return;
                }
            }
            // know the last consumed read
            if (null == lastException.get()) {
                if (nextRequest.getPromise().isInterrupted().isDefined()) {
                    setLastException(new DLInterruptedException("Interrupted on reading " + bkLedgerManager.getFullyQualifiedName() + " : ", nextRequest.getPromise().isInterrupted().get()));
                }
            }
            if (checkClosedOrInError("readNext")) {
                if (!(lastException.get().getCause() instanceof LogNotFoundException)) {
                    LOG.warn("{}: Exception", bkLedgerManager.getFullyQualifiedName(), lastException.get());
                }
                backgroundReaderRunTime.registerFailedEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS));
                return;
            }
            try {
                // Fail 10% of the requests when asked to simulate errors
                if (failureInjector.shouldInjectErrors()) {
                    throw new IOException("Reader Simulated Exception");
                }
                LogRecordWithDLSN record;
                while (!nextRequest.hasReadEnoughRecords()) {
                    // read single record
                    do {
                        record = bkLedgerManager.getNextReadAheadRecord();
                    } while (null != record && (record.isControl() || (record.getDlsn().compareTo(getStartDLSN()) < 0)));
                    if (null == record) {
                        break;
                    } else {
                        if (record.isEndOfStream() && !returnEndOfStreamRecord) {
                            setLastException(new EndOfStreamException("End of Stream Reached for " + bkLedgerManager.getFullyQualifiedName()));
                            break;
                        }
                        // gap detection
                        if (recordPositionsContainsGap(record, lastPosition)) {
                            bkDistributedLogManager.raiseAlert("Gap detected between records at dlsn = {}", record.getDlsn());
                            if (positionGapDetectionEnabled) {
                                throw new DLIllegalStateException("Gap detected between records at dlsn = " + record.getDlsn());
                            }
                        }
                        lastPosition = record.getLastPositionWithinLogSegment();
                        nextRequest.addRecord(record);
                    }
                }
                ;
            } catch (IOException exc) {
                setLastException(exc);
                if (!(exc instanceof LogNotFoundException)) {
                    LOG.warn("{} : read with skip Exception", bkLedgerManager.getFullyQualifiedName(), lastException.get());
                }
                continue;
            }
            if (nextRequest.hasReadRecords()) {
                long remainingWaitTime = nextRequest.getRemainingWaitTime();
                if (remainingWaitTime > 0 && !nextRequest.hasReadEnoughRecords()) {
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS));
                    scheduleDelayStopwatch.reset().start();
                    scheduleCount.set(0);
                    // the request could still wait for more records
                    backgroundScheduleTask = executorService.schedule(BACKGROUND_READ_SCHEDULER, remainingWaitTime, nextRequest.deadlineTimeUnit);
                    return;
                }
                PendingReadRequest request = pendingRequests.poll();
                if (null != request && nextRequest == request) {
                    request.complete();
                    if (null != backgroundScheduleTask) {
                        backgroundScheduleTask.cancel(true);
                        backgroundScheduleTask = null;
                    }
                } else {
                    DLIllegalStateException ise = new DLIllegalStateException("Unexpected condition at dlsn = " + nextRequest.records.get(0).getDlsn());
                    nextRequest.setException(ise);
                    if (null != request) {
                        request.setException(ise);
                    }
                    // We should never get here as we should have exited the loop if
                    // pendingRequests were empty
                    bkDistributedLogManager.raiseAlert("Unexpected condition at dlsn = {}", nextRequest.records.get(0).getDlsn());
                    setLastException(ise);
                }
            } else {
                if (0 == scheduleCountLocal) {
                    LOG.trace("Schedule count dropping to zero", lastException.get());
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS));
                    return;
                }
                scheduleCountLocal = scheduleCount.decrementAndGet();
            }
        }
    }
}
Also used : EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) Stopwatch(com.google.common.base.Stopwatch) DLIllegalStateException(com.twitter.distributedlog.exceptions.DLIllegalStateException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) IOException(java.io.IOException)

Example 22 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKDistributedLogManager method asyncCreateWriteHandler.

Future<BKLogWriteHandler> asyncCreateWriteHandler(final boolean lockHandler) {
    final ZooKeeper zk;
    try {
        zk = writerZKC.get();
    } catch (InterruptedException e) {
        LOG.error("Failed to initialize zookeeper client : ", e);
        return Future.exception(new DLInterruptedException("Failed to initialize zookeeper client", e));
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return Future.exception(FutureUtils.zkException(e, uri.getPath()));
    }
    boolean ownAllocator = null == ledgerAllocator;
    // Fetching Log Metadata
    Future<ZKLogMetadataForWriter> metadataFuture = ZKLogMetadataForWriter.of(uri, name, streamIdentifier, zk, writerZKC.getDefaultACL(), ownAllocator, conf.getCreateStreamIfNotExists() || ownAllocator);
    return metadataFuture.flatMap(new AbstractFunction1<ZKLogMetadataForWriter, Future<BKLogWriteHandler>>() {

        @Override
        public Future<BKLogWriteHandler> apply(ZKLogMetadataForWriter logMetadata) {
            Promise<BKLogWriteHandler> createPromise = new Promise<BKLogWriteHandler>();
            createWriteHandler(logMetadata, lockHandler, createPromise);
            return createPromise;
        }
    });
}
Also used : DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) ZKLogMetadataForWriter(com.twitter.distributedlog.impl.metadata.ZKLogMetadataForWriter) Promise(com.twitter.util.Promise) ZooKeeper(org.apache.zookeeper.ZooKeeper) Future(com.twitter.util.Future) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Aggregations

DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)22 IOException (java.io.IOException)15 KeeperException (org.apache.zookeeper.KeeperException)8 Stat (org.apache.zookeeper.data.Stat)6 ZKException (com.twitter.distributedlog.exceptions.ZKException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AsyncCallback (org.apache.zookeeper.AsyncCallback)5 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)4 Promise (com.twitter.util.Promise)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ArrayList (java.util.ArrayList)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Stopwatch (com.google.common.base.Stopwatch)2 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)2 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2