use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.
the class PravegaRequestProcessor method updateSegmentPolicy.
@Override
public void updateSegmentPolicy(UpdateSegmentPolicy updateSegmentPolicy) {
if (!verifyToken(updateSegmentPolicy.getSegment(), updateSegmentPolicy.getRequestId(), updateSegmentPolicy.getDelegationToken(), READ, "Update Segment Policy")) {
return;
}
Collection<AttributeUpdate> attributes = Arrays.asList(new AttributeUpdate(SCALE_POLICY_TYPE, AttributeUpdateType.Replace, (long) updateSegmentPolicy.getScaleType()), new AttributeUpdate(SCALE_POLICY_RATE, AttributeUpdateType.Replace, updateSegmentPolicy.getTargetRate()));
log.debug("Updating segment policy {} ", updateSegmentPolicy);
segmentStore.updateAttributes(updateSegmentPolicy.getSegment(), attributes, TIMEOUT).thenRun(() -> connection.send(new SegmentPolicyUpdated(updateSegmentPolicy.getRequestId(), updateSegmentPolicy.getSegment()))).whenComplete((r, e) -> {
if (e != null) {
handleException(updateSegmentPolicy.getRequestId(), updateSegmentPolicy.getSegment(), "Update segment", e);
} else {
if (statsRecorder != null) {
statsRecorder.policyUpdate(updateSegmentPolicy.getSegment(), updateSegmentPolicy.getScaleType(), updateSegmentPolicy.getTargetRate());
}
}
});
}
use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.
the class PravegaRequestProcessor method createSegment.
@Override
public void createSegment(CreateSegment createStreamsSegment) {
Timer timer = new Timer();
Collection<AttributeUpdate> attributes = Arrays.asList(new AttributeUpdate(SCALE_POLICY_TYPE, AttributeUpdateType.Replace, ((Byte) createStreamsSegment.getScaleType()).longValue()), new AttributeUpdate(SCALE_POLICY_RATE, AttributeUpdateType.Replace, ((Integer) createStreamsSegment.getTargetRate()).longValue()));
if (!verifyToken(createStreamsSegment.getSegment(), createStreamsSegment.getRequestId(), createStreamsSegment.getDelegationToken(), READ_UPDATE, "Create Segment")) {
return;
}
segmentStore.createStreamSegment(createStreamsSegment.getSegment(), attributes, TIMEOUT).thenAccept(v -> {
createStreamSegment.reportSuccessEvent(timer.getElapsed());
connection.send(new SegmentCreated(createStreamsSegment.getRequestId(), createStreamsSegment.getSegment()));
}).whenComplete((res, e) -> {
if (e == null) {
if (statsRecorder != null) {
statsRecorder.createSegment(createStreamsSegment.getSegment(), createStreamsSegment.getScaleType(), createStreamsSegment.getTargetRate());
}
} else {
createStreamSegment.reportFailEvent(timer.getElapsed());
handleException(createStreamsSegment.getRequestId(), createStreamsSegment.getSegment(), "Create segment", e);
}
});
}
use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.
the class PravegaRequestProcessor method createTransaction.
@Override
public void createTransaction(CreateTransaction createTransaction) {
if (!verifyToken(createStreamSegment.getName(), createTransaction.getRequestId(), createTransaction.getDelegationToken(), READ_UPDATE, "Create Transaction")) {
return;
}
Collection<AttributeUpdate> attributes = Collections.singleton(new AttributeUpdate(CREATION_TIME, AttributeUpdateType.None, System.currentTimeMillis()));
log.debug("Creating transaction {} ", createTransaction);
long requestId = createTransaction.getRequestId();
segmentStore.createTransaction(createTransaction.getSegment(), createTransaction.getTxid(), attributes, TIMEOUT).thenAccept(txName -> connection.send(new TransactionCreated(requestId, createTransaction.getSegment(), createTransaction.getTxid()))).exceptionally(e -> handleException(requestId, createTransaction.getSegment(), "Create transaction", e));
}
use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.
the class StreamSegmentMapper method createNewTransactionStreamSegment.
/**
* Creates a new Transaction StreamSegment for an existing Parent StreamSegment and persists the given attributes (in Storage).
*
* @param parentStreamSegmentName The case-sensitive StreamSegment Name of the Parent StreamSegment.
* @param transactionId A unique identifier for the transaction to be created.
* @param attributes The initial attributes for the Transaction, if any.
* @param timeout Timeout for the operation.
* @return A CompletableFuture that, when completed normally, will contain the name of the newly created Transaction StreamSegment.
* If the operation failed, this will contain the exception that caused the failure.
* @throws IllegalArgumentException If the given parent StreamSegment cannot have a Transaction (because it is deleted, sealed, inexistent).
*/
public CompletableFuture<String> createNewTransactionStreamSegment(String parentStreamSegmentName, UUID transactionId, Collection<AttributeUpdate> attributes, Duration timeout) {
long traceId = LoggerHelpers.traceEnterWithContext(log, traceObjectId, "createNewTransactionStreamSegment", parentStreamSegmentName);
// We cannot create a Transaction StreamSegment for a what looks like another Transaction.
Exceptions.checkArgument(StreamSegmentNameUtils.getParentStreamSegmentName(parentStreamSegmentName) == null, "parentStreamSegmentName", "Cannot create a Transaction for a Transaction.");
// Validate that Parent StreamSegment exists.
TimeoutTimer timer = new TimeoutTimer(timeout);
CompletableFuture<Void> parentCheck = null;
long mappedParentId = this.containerMetadata.getStreamSegmentId(parentStreamSegmentName, true);
if (isValidStreamSegmentId(mappedParentId)) {
SegmentProperties parentInfo = this.containerMetadata.getStreamSegmentMetadata(mappedParentId);
if (parentInfo != null) {
parentCheck = validateParentSegmentEligibility(parentInfo);
}
}
if (parentCheck == null) {
// The parent is not registered in the metadata. Get required info from Storage and don't map it unnecessarily.
parentCheck = this.storage.getStreamSegmentInfo(parentStreamSegmentName, timer.getRemaining()).thenCompose(this::validateParentSegmentEligibility);
}
String transactionName = StreamSegmentNameUtils.getTransactionNameFromId(parentStreamSegmentName, transactionId);
return parentCheck.thenComposeAsync(parentId -> createSegmentInStorageWithRecovery(transactionName, attributes, timer), this.executor).thenApply(v -> {
LoggerHelpers.traceLeave(log, traceObjectId, "createNewTransactionStreamSegment", traceId, parentStreamSegmentName, transactionName);
return transactionName;
});
}
use of io.pravega.segmentstore.contracts.AttributeUpdate in project pravega by pravega.
the class OperationLogTestBase method generateOperations.
// endregion
// region Operation Generation
/**
* Generates a List of Log Operations that contains the following operations, in the "correct" order.
* <ol>
* <li> A set of StreamSegmentAppend Operations (based on the streamSegmentIds arg).
* <li> A set of StreamSegmentSeal and MergeTransaction Operations (based on the TransactionIds and mergeTransactions arg).
* <li> A set of StreamSegmentSeal Operations (based on the sealStreamSegments arg).
* </ol>
*/
List<Operation> generateOperations(Collection<Long> streamSegmentIds, Map<Long, Long> transactionIds, int appendsPerStreamSegment, int metadataCheckpointsEvery, boolean mergeTransactions, boolean sealStreamSegments) {
List<Operation> result = new ArrayList<>();
// Add some appends.
int appendId = 0;
for (long streamSegmentId : streamSegmentIds) {
for (int i = 0; i < appendsPerStreamSegment; i++) {
val attributes = Collections.singletonList(new AttributeUpdate(UUID.randomUUID(), AttributeUpdateType.Replace, i));
result.add(new StreamSegmentAppendOperation(streamSegmentId, generateAppendData(appendId), attributes));
addCheckpointIfNeeded(result, metadataCheckpointsEvery);
appendId++;
}
}
addProbe(result);
for (long transactionId : transactionIds.keySet()) {
for (int i = 0; i < appendsPerStreamSegment; i++) {
val attributes = Collections.singletonList(new AttributeUpdate(UUID.randomUUID(), AttributeUpdateType.Replace, i));
result.add(new StreamSegmentAppendOperation(transactionId, generateAppendData(appendId), attributes));
addCheckpointIfNeeded(result, metadataCheckpointsEvery);
appendId++;
}
}
addProbe(result);
// Merge Transactions.
if (mergeTransactions) {
// Key = TransactionId, Value = Parent Id.
transactionIds.entrySet().forEach(mapping -> {
result.add(new StreamSegmentSealOperation(mapping.getKey()));
addCheckpointIfNeeded(result, metadataCheckpointsEvery);
result.add(new MergeTransactionOperation(mapping.getValue(), mapping.getKey()));
addCheckpointIfNeeded(result, metadataCheckpointsEvery);
});
addProbe(result);
}
// Seal the StreamSegments.
if (sealStreamSegments) {
streamSegmentIds.forEach(streamSegmentId -> {
result.add(new StreamSegmentSealOperation(streamSegmentId));
addCheckpointIfNeeded(result, metadataCheckpointsEvery);
});
addProbe(result);
}
return result;
}
Aggregations