use of io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated in project pravega by pravega.
the class SegmentMetadataClientImpl method updatePropertyAsync.
private CompletableFuture<SegmentAttributeUpdated> updatePropertyAsync(UUID attributeId, long expected, long value, String delegationToken) {
long requestId = requestIdGenerator.get();
log.trace("Updating segment attribute: {}", attributeId);
RawClient connection = getConnection();
return connection.sendRequest(requestId, new UpdateSegmentAttribute(requestId, segmentId.getScopedName(), attributeId, value, expected, delegationToken)).thenApply(r -> transformReply(r, SegmentAttributeUpdated.class));
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated in project pravega by pravega.
the class SegmentMetadataClientTest method compareAndSetAttribute.
@Test(timeout = 10000)
public void compareAndSetAttribute() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(endpoint, connection);
@Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
client.getConnection();
ReplyProcessor processor = cf.getProcessor(endpoint);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.UpdateSegmentAttribute request = invocation.getArgument(0);
processor.process(new SegmentAttributeUpdated(request.getRequestId(), true));
return null;
}
}).when(connection).send(any(WireCommands.UpdateSegmentAttribute.class));
assertTrue(client.compareAndSetAttribute(SegmentAttribute.RevisionStreamClientMark, -1234, 1234).join());
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated in project pravega by pravega.
the class PravegaRequestProcessor method mergeSegments.
@Override
public void mergeSegments(MergeSegments mergeSegments) {
final String operation = "mergeSegments";
if (!verifyToken(mergeSegments.getSource(), mergeSegments.getRequestId(), mergeSegments.getDelegationToken(), operation)) {
return;
}
log.info(mergeSegments.getRequestId(), "Merging Segments {} ", mergeSegments);
// Populate the AttributeUpdates for this mergeSegments operation, if any.
AttributeUpdateCollection attributeUpdates = new AttributeUpdateCollection();
if (mergeSegments.getAttributeUpdates() != null) {
for (WireCommands.ConditionalAttributeUpdate update : mergeSegments.getAttributeUpdates()) {
attributeUpdates.add(new AttributeUpdate(AttributeId.fromUUID(update.getAttributeId()), AttributeUpdateType.get(update.getAttributeUpdateType()), update.getNewValue(), update.getOldValue()));
}
}
segmentStore.mergeStreamSegment(mergeSegments.getTarget(), mergeSegments.getSource(), attributeUpdates, TIMEOUT).thenAccept(mergeResult -> {
recordStatForTransaction(mergeResult, mergeSegments.getTarget());
connection.send(new WireCommands.SegmentsMerged(mergeSegments.getRequestId(), mergeSegments.getTarget(), mergeSegments.getSource(), mergeResult.getTargetSegmentLength()));
}).exceptionally(e -> {
if (Exceptions.unwrap(e) instanceof StreamSegmentMergedException) {
log.info(mergeSegments.getRequestId(), "Stream segment is already merged '{}'.", mergeSegments.getSource());
segmentStore.getStreamSegmentInfo(mergeSegments.getTarget(), TIMEOUT).thenAccept(properties -> {
connection.send(new WireCommands.SegmentsMerged(mergeSegments.getRequestId(), mergeSegments.getTarget(), mergeSegments.getSource(), properties.getLength()));
});
return null;
} else if (Exceptions.unwrap(e) instanceof BadAttributeUpdateException) {
log.debug(mergeSegments.getRequestId(), "Conditional merge failed (Source segment={}, " + "Target segment={}): {}", mergeSegments.getSource(), mergeSegments.getTarget(), e.toString());
connection.send(new SegmentAttributeUpdated(mergeSegments.getRequestId(), false));
return null;
} else {
return handleException(mergeSegments.getRequestId(), mergeSegments.getSource(), operation, e);
}
});
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated in project pravega by pravega.
the class PravegaRequestProcessor method updateSegmentAttribute.
@Override
public void updateSegmentAttribute(UpdateSegmentAttribute updateSegmentAttribute) {
long requestId = updateSegmentAttribute.getRequestId();
String segmentName = updateSegmentAttribute.getSegmentName();
AttributeId attributeId = updateSegmentAttribute.getAttributeId() == null ? null : AttributeId.fromUUID(updateSegmentAttribute.getAttributeId());
long newValue = updateSegmentAttribute.getNewValue();
long expectedValue = updateSegmentAttribute.getExpectedValue();
final String operation = "updateSegmentAttribute";
if (!verifyToken(segmentName, updateSegmentAttribute.getRequestId(), updateSegmentAttribute.getDelegationToken(), operation)) {
return;
}
long trace = LoggerHelpers.traceEnter(log, operation, updateSegmentAttribute);
val update = new AttributeUpdate(attributeId, AttributeUpdateType.ReplaceIfEquals, newValue, expectedValue);
segmentStore.updateAttributes(segmentName, AttributeUpdateCollection.from(update), TIMEOUT).whenComplete((v, e) -> {
LoggerHelpers.traceLeave(log, operation, trace, e);
final Consumer<Throwable> failureHandler = t -> {
log.error(requestId, "Error (Segment = '{}', Operation = '{}')", segmentName, "handling result of " + operation, t);
connection.close();
};
if (e == null) {
invokeSafely(connection::send, new SegmentAttributeUpdated(requestId, true), failureHandler);
} else {
if (Exceptions.unwrap(e) instanceof BadAttributeUpdateException) {
log.debug("Updating segment attribute {} failed due to: {}", update, e.getMessage());
invokeSafely(connection::send, new SegmentAttributeUpdated(requestId, false), failureHandler);
} else {
handleException(requestId, segmentName, operation, e);
}
}
});
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated in project pravega by pravega.
the class SegmentMetadataClientImpl method updatePropertyAsync.
private CompletableFuture<SegmentAttributeUpdated> updatePropertyAsync(UUID attributeId, long expected, long value) {
log.trace("Updating segment attribute: {}", attributeId);
RawClient connection = getConnection();
long requestId = connection.getFlow().getNextSequenceNumber();
return tokenProvider.retrieveToken().thenCompose(token -> connection.sendRequest(requestId, new UpdateSegmentAttribute(requestId, segmentId.getScopedName(), attributeId, value, expected, token))).thenApply(r -> transformReply(r, SegmentAttributeUpdated.class));
}
Aggregations