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;
}
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());
}
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();
}
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();
}
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);
}
Aggregations