use of io.pravega.segmentstore.storage.metadata.SegmentMetadata in project pravega by pravega.
the class ChunkedSegmentStorage method exists.
@Override
public CompletableFuture<Boolean> exists(String streamSegmentName, Duration timeout) {
checkInitialized();
return executeParallel(() -> {
val traceId = LoggerHelpers.traceEnter(log, "exists", streamSegmentName);
Preconditions.checkNotNull(streamSegmentName, "streamSegmentName");
return tryWith(metadataStore.beginTransaction(true, streamSegmentName), txn -> txn.get(streamSegmentName).thenApplyAsync(storageMetadata -> {
SegmentMetadata segmentMetadata = (SegmentMetadata) storageMetadata;
val retValue = segmentMetadata != null && segmentMetadata.isActive();
LoggerHelpers.traceLeave(log, "exists", traceId, retValue);
return retValue;
}, executor), executor);
}, streamSegmentName);
}
use of io.pravega.segmentstore.storage.metadata.SegmentMetadata in project pravega by pravega.
the class ChunkedSegmentStorage method create.
@Override
public CompletableFuture<SegmentHandle> create(String streamSegmentName, SegmentRollingPolicy rollingPolicy, Duration timeout) {
checkInitialized();
return executeSerialized(() -> {
val traceId = LoggerHelpers.traceEnter(log, "create", streamSegmentName, rollingPolicy);
val timer = new Timer();
log.debug("{} create - started segment={}, rollingPolicy={}.", logPrefix, streamSegmentName, rollingPolicy);
return tryWith(metadataStore.beginTransaction(false, streamSegmentName), txn -> {
// Retrieve metadata and make sure it does not exist.
return txn.get(streamSegmentName).thenComposeAsync(storageMetadata -> {
val oldSegmentMetadata = (SegmentMetadata) storageMetadata;
if (null != oldSegmentMetadata) {
throw new CompletionException(new StreamSegmentExistsException(streamSegmentName));
}
// Create a new record.
val newSegmentMetadata = SegmentMetadata.builder().name(streamSegmentName).maxRollinglength(rollingPolicy.getMaxLength() == 0 ? SegmentRollingPolicy.NO_ROLLING.getMaxLength() : rollingPolicy.getMaxLength()).ownerEpoch(this.epoch).build();
newSegmentMetadata.setActive(true);
txn.create(newSegmentMetadata);
// commit.
return txn.commit().thenApplyAsync(v -> {
val retValue = SegmentStorageHandle.writeHandle(streamSegmentName);
Duration elapsed = timer.getElapsed();
SLTS_CREATE_LATENCY.reportSuccessEvent(elapsed);
SLTS_CREATE_COUNT.inc();
log.debug("{} create - finished segment={}, rollingPolicy={}, latency={}.", logPrefix, streamSegmentName, rollingPolicy, elapsed.toMillis());
LoggerHelpers.traceLeave(log, "create", traceId, retValue);
return retValue;
}, executor);
}, executor);
}, executor).handleAsync((v, e) -> {
if (null != e) {
log.debug("{} create - exception segment={}, rollingPolicy={}, latency={}.", logPrefix, streamSegmentName, rollingPolicy, timer.getElapsedMillis(), e);
handleException(streamSegmentName, e);
}
return v;
}, executor);
}, streamSegmentName);
}
use of io.pravega.segmentstore.storage.metadata.SegmentMetadata in project pravega by pravega.
the class ChunkedSegmentStorage method openRead.
@Override
public CompletableFuture<SegmentHandle> openRead(String streamSegmentName) {
checkInitialized();
return executeParallel(() -> {
val traceId = LoggerHelpers.traceEnter(log, "openRead", streamSegmentName);
val timer = new Timer();
// Validate preconditions and return handle.
Preconditions.checkNotNull(streamSegmentName, "streamSegmentName");
log.debug("{} openRead - started segment={}.", logPrefix, streamSegmentName);
return tryWith(metadataStore.beginTransaction(false, streamSegmentName), txn -> txn.get(streamSegmentName).thenComposeAsync(storageMetadata -> {
val segmentMetadata = (SegmentMetadata) storageMetadata;
checkSegmentExists(streamSegmentName, segmentMetadata);
segmentMetadata.checkInvariants();
// This segment was created by an older segment store. Then claim ownership and adjust length.
final CompletableFuture<Void> f;
if (segmentMetadata.getOwnerEpoch() < this.epoch) {
log.debug("{} openRead - Segment needs ownership change. segment={}.", logPrefix, segmentMetadata.getName());
// In case of a fail-over, length recorded in metadata will be lagging behind its actual length in the storage.
// This can happen with lazy commits that were still not committed at the time of fail-over.
f = executeSerialized(() -> claimOwnership(txn, segmentMetadata), streamSegmentName);
} else {
f = CompletableFuture.completedFuture(null);
}
return f.thenApplyAsync(v -> {
val retValue = SegmentStorageHandle.readHandle(streamSegmentName);
log.debug("{} openRead - finished segment={} latency={}.", logPrefix, streamSegmentName, timer.getElapsedMillis());
LoggerHelpers.traceLeave(log, "openRead", traceId, retValue);
return retValue;
}, executor);
}, executor), executor).handleAsync((v, ex) -> {
if (null != ex) {
log.debug("{} openRead - exception segment={} latency={}.", logPrefix, streamSegmentName, timer.getElapsedMillis(), ex);
handleException(streamSegmentName, ex);
}
return v;
}, executor);
}, streamSegmentName);
}
use of io.pravega.segmentstore.storage.metadata.SegmentMetadata in project pravega by pravega.
the class ChunkedSegmentStorage method openWrite.
@Override
public CompletableFuture<SegmentHandle> openWrite(String streamSegmentName) {
checkInitialized();
return executeSerialized(() -> {
val traceId = LoggerHelpers.traceEnter(log, "openWrite", streamSegmentName);
val timer = new Timer();
Preconditions.checkNotNull(streamSegmentName, "streamSegmentName");
log.debug("{} openWrite - started segment={}.", logPrefix, streamSegmentName);
return tryWith(metadataStore.beginTransaction(false, streamSegmentName), txn -> txn.get(streamSegmentName).thenComposeAsync(storageMetadata -> {
val segmentMetadata = (SegmentMetadata) storageMetadata;
checkSegmentExists(streamSegmentName, segmentMetadata);
segmentMetadata.checkInvariants();
// This segment was created by an older segment store. Need to start a fresh new chunk.
final CompletableFuture<Void> f;
if (segmentMetadata.getOwnerEpoch() < this.epoch) {
log.debug("{} openWrite - Segment needs ownership change - segment={}.", logPrefix, segmentMetadata.getName());
f = claimOwnership(txn, segmentMetadata);
} else {
f = CompletableFuture.completedFuture(null);
}
return f.thenApplyAsync(v -> {
// If created by newer instance then abort.
checkOwnership(streamSegmentName, segmentMetadata);
// This instance is the owner, return a handle.
val retValue = SegmentStorageHandle.writeHandle(streamSegmentName);
log.debug("{} openWrite - finished segment={} latency={}.", logPrefix, streamSegmentName, timer.getElapsedMillis());
LoggerHelpers.traceLeave(log, "openWrite", traceId, retValue);
return retValue;
}, executor);
}, executor), executor).handleAsync((v, ex) -> {
if (null != ex) {
log.debug("{} openWrite - exception segment={} latency={}.", logPrefix, streamSegmentName, timer.getElapsedMillis(), ex);
handleException(streamSegmentName, ex);
}
return v;
}, executor);
}, streamSegmentName);
}
use of io.pravega.segmentstore.storage.metadata.SegmentMetadata in project pravega by pravega.
the class ChunkedSegmentStorage method getStreamSegmentInfo.
@Override
public CompletableFuture<SegmentProperties> getStreamSegmentInfo(String streamSegmentName, Duration timeout) {
checkInitialized();
return executeParallel(() -> {
val traceId = LoggerHelpers.traceEnter(log, "getStreamSegmentInfo", streamSegmentName);
val timer = new Timer();
Preconditions.checkNotNull(streamSegmentName, "streamSegmentName");
log.debug("{} getStreamSegmentInfo - started segment={}.", logPrefix, streamSegmentName);
return tryWith(metadataStore.beginTransaction(true, streamSegmentName), txn -> txn.get(streamSegmentName).thenApplyAsync(storageMetadata -> {
SegmentMetadata segmentMetadata = (SegmentMetadata) storageMetadata;
checkSegmentExists(streamSegmentName, segmentMetadata);
segmentMetadata.checkInvariants();
val retValue = StreamSegmentInformation.builder().name(streamSegmentName).sealed(segmentMetadata.isSealed()).length(segmentMetadata.getLength()).startOffset(segmentMetadata.getStartOffset()).lastModified(new ImmutableDate(segmentMetadata.getLastModified())).build();
log.debug("{} getStreamSegmentInfo - finished segment={} latency={}.", logPrefix, streamSegmentName, timer.getElapsedMillis());
LoggerHelpers.traceLeave(log, "getStreamSegmentInfo", traceId, retValue);
return retValue;
}, executor), executor).handleAsync((v, ex) -> {
if (null != ex) {
log.debug("{} getStreamSegmentInfo - exception segment={}.", logPrefix, streamSegmentName, ex);
handleException(streamSegmentName, ex);
}
return v;
}, executor);
}, streamSegmentName);
}
Aggregations