Search in sources :

Example 16 with LogNotFoundException

use of org.apache.distributedlog.exceptions.LogNotFoundException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method createMissingMetadata.

static void createMissingMetadata(final ZooKeeper zk, final String basePath, final String logRootPath, final List<Versioned<byte[]>> metadatas, final List<ACL> acl, final boolean ownAllocator, final boolean createIfNotExists, final CompletableFuture<List<Versioned<byte[]>>> promise) {
    final List<byte[]> pathsToCreate = Lists.newArrayListWithExpectedSize(metadatas.size());
    final List<Op> zkOps = Lists.newArrayListWithExpectedSize(metadatas.size());
    CreateMode createMode = CreateMode.PERSISTENT;
    // log root parent path
    String logRootParentPath = Utils.getParent(logRootPath);
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT_PARENT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootParentPath, EMPTY_BYTES, acl, createMode));
    }
    // log root path
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath, EMPTY_BYTES, acl, createMode));
    }
    // max id
    if (pathExists(metadatas.get(MetadataIndex.MAX_TXID))) {
        pathsToCreate.add(null);
    } else {
        byte[] zeroTxnIdData = DLUtils.serializeTransactionId(0L);
        pathsToCreate.add(zeroTxnIdData);
        zkOps.add(Op.create(logRootPath + MAX_TXID_PATH, zeroTxnIdData, acl, createMode));
    }
    // version
    if (pathExists(metadatas.get(MetadataIndex.VERSION))) {
        pathsToCreate.add(null);
    } else {
        byte[] versionData = intToBytes(LAYOUT_VERSION);
        pathsToCreate.add(versionData);
        zkOps.add(Op.create(logRootPath + VERSION_PATH, versionData, acl, createMode));
    }
    // lock path
    if (pathExists(metadatas.get(MetadataIndex.LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + LOCK_PATH, EMPTY_BYTES, acl, createMode));
    }
    // read lock path
    if (pathExists(metadatas.get(MetadataIndex.READ_LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + READ_LOCK_PATH, EMPTY_BYTES, acl, createMode));
    }
    // log segments path
    if (pathExists(metadatas.get(MetadataIndex.LOGSEGMENTS))) {
        pathsToCreate.add(null);
    } else {
        byte[] logSegmentsData = DLUtils.serializeLogSegmentSequenceNumber(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO);
        pathsToCreate.add(logSegmentsData);
        zkOps.add(Op.create(logRootPath + LOGSEGMENTS_PATH, logSegmentsData, acl, createMode));
    }
    // allocation path
    if (ownAllocator) {
        if (pathExists(metadatas.get(MetadataIndex.ALLOCATION))) {
            pathsToCreate.add(null);
        } else {
            pathsToCreate.add(EMPTY_BYTES);
            zkOps.add(Op.create(logRootPath + ALLOCATION_PATH, EMPTY_BYTES, acl, createMode));
        }
    }
    if (zkOps.isEmpty()) {
        // nothing missed
        promise.complete(metadatas);
        return;
    }
    if (!createIfNotExists) {
        promise.completeExceptionally(new LogNotFoundException("Log " + logRootPath + " not found"));
        return;
    }
    getMissingPaths(zk, basePath, Utils.getParent(logRootParentPath)).whenComplete(new FutureEventListener<List<String>>() {

        @Override
        public void onSuccess(List<String> paths) {
            for (String path : paths) {
                pathsToCreate.add(EMPTY_BYTES);
                zkOps.add(0, Op.create(path, EMPTY_BYTES, acl, createMode));
            }
            executeCreateMissingPathTxn(zk, zkOps, pathsToCreate, metadatas, logRootPath, promise);
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.completeExceptionally(cause);
            return;
        }
    });
}
Also used : Op(org.apache.zookeeper.Op) CreateMode(org.apache.zookeeper.CreateMode) List(java.util.List) LinkedList(java.util.LinkedList) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Example 17 with LogNotFoundException

use of org.apache.distributedlog.exceptions.LogNotFoundException in project bookkeeper by apache.

the class BKAsyncLogReader method safeRun.

@Override
public void safeRun() {
    synchronized (scheduleLock) {
        if (scheduleDelayStopwatch.isRunning()) {
            scheduleLatency.registerSuccessfulEvent(scheduleDelayStopwatch.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
        }
        Stopwatch runTime = Stopwatch.createStarted();
        int iterations = 0;
        long scheduleCountLocal = scheduleCountUpdater.get(this);
        LOG.debug("{}: Scheduled Background Reader", readHandler.getFullyQualifiedName());
        while (true) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{}: Executing Iteration: {}", readHandler.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", readHandler.getFullyQualifiedName());
                    scheduleCountUpdater.set(this, 0);
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
                    return;
                }
                if (disableProcessingReadRequests) {
                    LOG.info("Reader of {} is forced to stop processing read requests", readHandler.getFullyQualifiedName());
                    return;
                }
            }
            lastProcessTime.reset().start();
            // know the last consumed read
            if (null == lastExceptionUpdater.get(this)) {
                if (nextRequest.getPromise().isCancelled()) {
                    setLastException(new DLInterruptedException("Interrupted on reading " + readHandler.getFullyQualifiedName()));
                }
            }
            if (checkClosedOrInError("readNext")) {
                Throwable lastException = lastExceptionUpdater.get(this);
                if (lastException != null && !(lastException.getCause() instanceof LogNotFoundException)) {
                    LOG.warn("{}: Exception", readHandler.getFullyQualifiedName(), lastException);
                }
                backgroundReaderRunTime.registerFailedEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
                return;
            }
            try {
                // Fail 10% of the requests when asked to simulate errors
                if (bkDistributedLogManager.getFailureInjector().shouldInjectErrors()) {
                    throw new IOException("Reader Simulated Exception");
                }
                LogRecordWithDLSN record;
                while (!nextRequest.hasReadEnoughRecords()) {
                    // read single record
                    do {
                        record = readNextRecord();
                    } 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 " + readHandler.getFullyQualifiedName()));
                            break;
                        }
                        // gap detection
                        if (recordPositionsContainsGap(record, lastPosition)) {
                            bkDistributedLogManager.raiseAlert("Gap detected between records at record = {}", record);
                            if (positionGapDetectionEnabled) {
                                throw new DLIllegalStateException("Gap detected between records at record = " + record);
                            }
                        }
                        lastPosition = record.getLastPositionWithinLogSegment();
                        nextRequest.addRecord(record);
                    }
                }
            } catch (IOException exc) {
                setLastException(exc);
                if (!(exc instanceof LogNotFoundException)) {
                    LOG.warn("{} : read with skip Exception", readHandler.getFullyQualifiedName(), lastExceptionUpdater.get(this));
                }
                continue;
            }
            if (nextRequest.hasReadRecords()) {
                long remainingWaitTime = nextRequest.getRemainingWaitTime();
                if (remainingWaitTime > 0 && !nextRequest.hasReadEnoughRecords()) {
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
                    scheduleDelayStopwatch.reset().start();
                    scheduleCountUpdater.set(this, 0);
                    // the request could still wait for more records
                    backgroundScheduleTask = scheduler.scheduleOrdered(streamName, 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.completeExceptionally(ise);
                    if (null != request) {
                        request.completeExceptionally(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", lastExceptionUpdater.get(this));
                    backgroundReaderRunTime.registerSuccessfulEvent(runTime.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
                    return;
                }
                scheduleCountLocal = scheduleCountUpdater.decrementAndGet(this);
            }
        }
    }
}
Also used : EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) Stopwatch(com.google.common.base.Stopwatch) DLIllegalStateException(org.apache.distributedlog.exceptions.DLIllegalStateException) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException) IOException(java.io.IOException)

Aggregations

LogNotFoundException (org.apache.distributedlog.exceptions.LogNotFoundException)17 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)8 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 LogReader (org.apache.distributedlog.api.LogReader)4 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)4 LogEmptyException (org.apache.distributedlog.exceptions.LogEmptyException)4 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ZooKeeperConnectionException (org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException)3 ZKException (org.apache.distributedlog.exceptions.ZKException)3 BufferedOutputStream (java.io.BufferedOutputStream)2 URI (java.net.URI)2 LinkedList (java.util.LinkedList)2 DLSN (org.apache.distributedlog.DLSN)2 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)2 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)2 DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)2 BufferedFSInputStream (org.apache.hadoop.fs.BufferedFSInputStream)2 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)2