Search in sources :

Example 11 with Timer

use of io.pravega.common.Timer in project pravega by pravega.

the class RocksDBCache method get.

@Override
public byte[] get(Key key) {
    ensureInitializedAndNotClosed();
    Timer timer = new Timer();
    byte[] result;
    try {
        result = this.database.get().get(key.serialize());
    } catch (RocksDBException ex) {
        throw convert(ex, "get key '%s'", key);
    }
    RocksDBMetrics.get(timer.getElapsedMillis());
    return result;
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Timer(io.pravega.common.Timer)

Example 12 with Timer

use of io.pravega.common.Timer in project pravega by pravega.

the class RocksDBCache method insert.

// endregion
// region Cache Implementation
@Override
public void insert(Key key, byte[] data) {
    ensureInitializedAndNotClosed();
    Timer timer = new Timer();
    try {
        this.database.get().put(this.writeOptions, key.serialize(), data);
    } catch (RocksDBException ex) {
        throw convert(ex, "insert key '%s'", key);
    }
    RocksDBMetrics.insert(timer.getElapsedMillis());
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Timer(io.pravega.common.Timer)

Example 13 with Timer

use of io.pravega.common.Timer in project pravega by pravega.

the class ReadOperation method call.

@Override
public Integer call() throws IOException {
    HDFSSegmentHandle handle = getTarget();
    long traceId = LoggerHelpers.traceEnter(log, "read", handle, this.offset, this.length);
    Timer timer = new Timer();
    // Make sure arguments are valid. Refresh the handle if needed (and allowed).
    validateOffsetAndRefresh(handle);
    int attemptCount = 0;
    AtomicInteger totalBytesRead = new AtomicInteger();
    boolean needsRefresh = true;
    while (needsRefresh && attemptCount < MAX_ATTEMPT_COUNT) {
        attemptCount++;
        try {
            // Read data.
            read(handle, totalBytesRead);
            needsRefresh = false;
        } catch (FileNotFoundException fnf) {
            if (!handle.isReadOnly() || attemptCount >= MAX_ATTEMPT_COUNT) {
                // Also throw this if we tried enough times.
                throw fnf;
            }
            log.info("Unable to read from file '{}' (attempt {}/{}). Refreshing and retrying.", fnf.getMessage(), attemptCount, MAX_ATTEMPT_COUNT);
            refreshHandle(handle);
        }
    }
    HDFSMetrics.READ_LATENCY.reportSuccessEvent(timer.getElapsed());
    HDFSMetrics.READ_BYTES.add(totalBytesRead.get());
    LoggerHelpers.traceLeave(log, "read", traceId, handle, this.offset, totalBytesRead);
    return totalBytesRead.get();
}
Also used : Timer(io.pravega.common.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileNotFoundException(java.io.FileNotFoundException)

Example 14 with Timer

use of io.pravega.common.Timer in project pravega by pravega.

the class AppendTest method timeWrites.

private long timeWrites(String testString, int number, EventStreamWriter<String> producer, boolean synchronous) throws InterruptedException, ExecutionException, TimeoutException {
    Timer timer = new Timer();
    for (int i = 0; i < number; i++) {
        Future<Void> ack = producer.writeEvent(testString);
        if (synchronous) {
            ack.get(5, TimeUnit.SECONDS);
        }
    }
    producer.flush();
    return timer.getElapsedMillis();
}
Also used : Timer(io.pravega.common.Timer)

Example 15 with Timer

use of io.pravega.common.Timer in project pravega by pravega.

the class WriteOperation method run.

@Override
public void run() throws BadOffsetException, IOException, StorageNotPrimaryException {
    HDFSSegmentHandle handle = getTarget();
    long traceId = LoggerHelpers.traceEnter(log, "write", handle, this.offset, this.length);
    FileDescriptor lastFile = handle.getLastFile();
    Timer timer = new Timer();
    try (FSDataOutputStream stream = this.context.fileSystem.append(lastFile.getPath())) {
        if (this.offset != lastFile.getLastOffset()) {
            // before we throw BadOffsetException.
            throw new BadOffsetException(handle.getSegmentName(), lastFile.getLastOffset(), this.offset);
        } else if (stream.getPos() != lastFile.getLength()) {
            // Looks like the filesystem changed from underneath us. This could be our bug, but it could be something else.
            // Update our knowledge of the filesystem and throw a BadOffsetException - this should cause upstream code
            // to try to reconcile; if it can't then the upstream code should shut down or take other appropriate measures.
            log.warn("File changed detected for '{}'. Expected length = {}, actual length = {}.", lastFile, lastFile.getLength(), stream.getPos());
            lastFile.setLength(stream.getPos());
            throw new BadOffsetException(handle.getSegmentName(), lastFile.getLastOffset(), this.offset);
        }
        if (this.length == 0) {
            // Note: IOUtils.copyBytes with length == 0 will enter an infinite loop, hence the need for this check.
            return;
        }
        // We need to be very careful with IOUtils.copyBytes. There are many overloads with very similar signatures.
        // There is a difference between (InputStream, OutputStream, int, boolean) and (InputStream, OutputStream, long, boolean),
        // in that the one with "int" uses the third arg as a buffer size, and the one with "long" uses it as the number
        // of bytes to copy.
        IOUtils.copyBytes(this.data, stream, (long) this.length, false);
        stream.flush();
        lastFile.increaseLength(this.length);
    } catch (FileNotFoundException | AclException ex) {
        checkForFenceOut(handle.getSegmentName(), handle.getFiles().size(), handle.getLastFile());
        // If we were not fenced out, then this is a legitimate exception - rethrow it.
        throw ex;
    }
    HDFSMetrics.WRITE_LATENCY.reportSuccessEvent(timer.getElapsed());
    HDFSMetrics.WRITE_BYTES.add(this.length);
    LoggerHelpers.traceLeave(log, "write", traceId, handle, offset, length);
}
Also used : Timer(io.pravega.common.Timer) FileNotFoundException(java.io.FileNotFoundException) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) AclException(org.apache.hadoop.hdfs.protocol.AclException)

Aggregations

Timer (io.pravega.common.Timer)18 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Preconditions (com.google.common.base.Preconditions)4 Exceptions (io.pravega.common.Exceptions)4 LoggerHelpers (io.pravega.common.LoggerHelpers)4 Duration (java.time.Duration)4 List (java.util.List)4 AuthHandler (io.pravega.auth.AuthHandler)3 AuthenticationException (io.pravega.common.auth.AuthenticationException)3 AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)3 AttributeUpdateType (io.pravega.segmentstore.contracts.AttributeUpdateType)3 BadAttributeUpdateException (io.pravega.segmentstore.contracts.BadAttributeUpdateException)3 ContainerNotFoundException (io.pravega.segmentstore.contracts.ContainerNotFoundException)3 StreamSegmentSealedException (io.pravega.segmentstore.contracts.StreamSegmentSealedException)3 ByteBuffer (java.nio.ByteBuffer)3 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)2 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)2 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)2