use of io.pravega.shared.protocol.netty.WireCommands.MergeSegments 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.MergeSegments in project pravega by pravega.
the class LargeEventWriter method write.
private void write(Segment parentSegment, List<ByteBuf> payloads, RawClient client, DelegationTokenProvider tokenProvider) throws TokenExpiredException, NoSuchSegmentException, AuthenticationException, SegmentSealedException, ConnectionFailedException {
long requestId = client.getFlow().getNextSequenceNumber();
log.debug("Writing large event to segment {} with writer id {}", parentSegment, writerId);
String token = getThrowingException(tokenProvider.retrieveToken());
CreateTransientSegment createSegment = new CreateTransientSegment(requestId, writerId, parentSegment.getScopedName(), token);
SegmentCreated created = transformSegmentCreated(getThrowingException(client.sendRequest(requestId, createSegment)), parentSegment.getScopedName());
requestId = client.getFlow().getNextSequenceNumber();
SetupAppend setup = new SetupAppend(requestId, writerId, created.getSegment(), token);
AppendSetup appendSetup = transformAppendSetup(getThrowingException(client.sendRequest(requestId, setup)), created.getSegment());
if (appendSetup.getLastEventNumber() != WireCommands.NULL_ATTRIBUTE_VALUE) {
throw new IllegalStateException("Server indicates that transient segment was already written to: " + created.getSegment());
}
long expectedOffset = 0;
val futures = new ArrayList<CompletableFuture<Reply>>();
for (int i = 0; i < payloads.size(); i++) {
requestId = client.getFlow().getNextSequenceNumber();
ByteBuf payload = payloads.get(i);
val request = new ConditionalBlockEnd(writerId, i, expectedOffset, Unpooled.wrappedBuffer(payload), requestId);
expectedOffset += payload.readableBytes();
val reply = client.sendRequest(requestId, request);
failFast(futures, created.getSegment());
futures.add(reply);
}
for (int i = 0; i < futures.size(); i++) {
transformDataAppended(getThrowingException(futures.get(i)), created.getSegment());
}
requestId = client.getFlow().getNextSequenceNumber();
MergeSegments merge = new MergeSegments(requestId, parentSegment.getScopedName(), created.getSegment(), token);
transformSegmentMerged(getThrowingException(client.sendRequest(requestId, merge)), created.getSegment());
}
use of io.pravega.shared.protocol.netty.WireCommands.MergeSegments in project pravega by pravega.
the class PravegaRequestProcessor method mergeSegmentsBatch.
@Override
public void mergeSegmentsBatch(WireCommands.MergeSegmentsBatch mergeSegments) {
final String operation = "mergeSegmentsBatch";
List<String> sources = mergeSegments.getSourceSegmentIds();
if (!verifyTokenForUpdate(mergeSegments.getTargetSegmentId(), mergeSegments.getRequestId(), mergeSegments.getDelegationToken(), operation)) {
return;
}
for (String s : sources) {
if (!verifyToken(s, mergeSegments.getRequestId(), mergeSegments.getDelegationToken(), operation)) {
return;
}
}
log.info(mergeSegments.getRequestId(), "Merging Segments Batch in-order {} ", mergeSegments);
Futures.allOfWithResults(sources.stream().map(source -> Futures.handleCompose(segmentStore.mergeStreamSegment(mergeSegments.getTargetSegmentId(), source, TIMEOUT), (r, e) -> {
if (e != null) {
Throwable unwrap = Exceptions.unwrap(e);
if (unwrap instanceof StreamSegmentMergedException) {
log.info(mergeSegments.getRequestId(), "Stream segment already merged '{}'.", source);
return segmentStore.getStreamSegmentInfo(mergeSegments.getTargetSegmentId(), TIMEOUT).thenApply(SegmentProperties::getLength);
}
if (unwrap instanceof StreamSegmentNotExistsException) {
StreamSegmentNotExistsException ex = (StreamSegmentNotExistsException) unwrap;
if (ex.getStreamSegmentName().equals(source)) {
log.info(mergeSegments.getRequestId(), "Stream segment already merged '{}'.", source);
return segmentStore.getStreamSegmentInfo(mergeSegments.getTargetSegmentId(), TIMEOUT).thenApply(SegmentProperties::getLength);
}
}
throw new CompletionException(e);
} else {
recordStatForTransaction(r, mergeSegments.getTargetSegmentId());
return CompletableFuture.completedFuture(r.getTargetSegmentLength());
}
})).collect(Collectors.toUnmodifiableList())).thenAccept(mergeResults -> {
connection.send(new WireCommands.SegmentsBatchMerged(mergeSegments.getRequestId(), mergeSegments.getTargetSegmentId(), sources, mergeResults));
}).exceptionally(e -> {
log.debug("error");
return handleException(mergeSegments.getRequestId(), mergeSegments.getTargetSegmentId(), operation, e);
});
}
use of io.pravega.shared.protocol.netty.WireCommands.MergeSegments in project pravega by pravega.
the class FailingRequestProcessorTest method testEverythingThrows.
@Test
public void testEverythingThrows() {
assertThrows(IllegalStateException.class, () -> rp.hello(new Hello(0, 0)));
assertThrows(IllegalStateException.class, () -> rp.setupAppend(new SetupAppend(0, null, "", "")));
assertThrows(IllegalStateException.class, () -> rp.append(new Append("", null, 0, EMPTY_EVENT, 0)));
assertThrows(IllegalStateException.class, () -> rp.readSegment(new ReadSegment("", 0, 0, "", 0)));
assertThrows(IllegalStateException.class, () -> rp.updateSegmentAttribute(new UpdateSegmentAttribute(0, "", null, 0, 0, "")));
assertThrows(IllegalStateException.class, () -> rp.getSegmentAttribute(new GetSegmentAttribute(0, "", null, "")));
assertThrows(IllegalStateException.class, () -> rp.getStreamSegmentInfo(new WireCommands.GetStreamSegmentInfo(0, "", "")));
assertThrows(IllegalStateException.class, () -> rp.createSegment(new CreateSegment(0, "", (byte) 0, 0, "", 0)));
assertThrows(IllegalStateException.class, () -> rp.updateSegmentPolicy(new UpdateSegmentPolicy(0, "", (byte) 0, 0, "")));
assertThrows(IllegalStateException.class, () -> rp.createTableSegment(new CreateTableSegment(0, "", false, 0, "", 0)));
assertThrows(IllegalStateException.class, () -> rp.deleteTableSegment(new DeleteTableSegment(0, "", false, "")));
assertThrows(IllegalStateException.class, () -> rp.updateTableEntries(new UpdateTableEntries(0, "", "", null, 0)));
assertThrows(IllegalStateException.class, () -> rp.removeTableKeys(new RemoveTableKeys(0, "", "", null, 0)));
assertThrows(IllegalStateException.class, () -> rp.readTable(new ReadTable(0, "", "", null)));
assertThrows(IllegalStateException.class, () -> rp.readTableKeys(new ReadTableKeys(0, "", "", 0, null)));
assertThrows(IllegalStateException.class, () -> rp.readTableEntries(new ReadTableEntries(0, "", "", 0, null)));
assertThrows(IllegalStateException.class, () -> rp.mergeSegments(new MergeSegments(0, "", "", "")));
assertThrows(IllegalStateException.class, () -> rp.sealSegment(new SealSegment(0, "", "")));
assertThrows(IllegalStateException.class, () -> rp.truncateSegment(new TruncateSegment(0, "", 0, "")));
assertThrows(IllegalStateException.class, () -> rp.deleteSegment(new DeleteSegment(0, "", "")));
assertThrows(IllegalStateException.class, () -> rp.readTableEntries(new ReadTableEntries(0, "", "", 0, null)));
assertThrows(IllegalStateException.class, () -> rp.createTableSegment(new CreateTableSegment(0, "", false, 0, "", 0)));
assertThrows(IllegalStateException.class, () -> rp.readTableEntriesDelta(new ReadTableEntriesDelta(0, "", "", 0, 0)));
assertThrows(IllegalStateException.class, () -> rp.createTransientSegment(new CreateTransientSegment(0, new UUID(0, 0), "", "")));
assertThrows(IllegalStateException.class, () -> rp.connectionDropped());
}
Aggregations