use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto in project incubator-ratis by apache.
the class FileStoreStateMachine method applyTransaction.
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
final LogEntryProto entry = trx.getLogEntry();
final long index = entry.getIndex();
updateLastAppliedTermIndex(entry.getTerm(), index);
final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
final FileStoreRequestProto request;
try {
request = FileStoreRequestProto.parseFrom(smLog.getLogData());
} catch (InvalidProtocolBufferException e) {
return FileStoreCommon.completeExceptionally(index, "Failed to parse logData in" + smLog, e);
}
switch(request.getRequestCase()) {
case DELETE:
return delete(index, request.getDelete());
case WRITEHEADER:
return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size());
case STREAM:
return streamCommit(request.getStream());
case WRITE:
// startTransaction converts WRITE requests to WRITEHEADER requests.
default:
LOG.error(getId() + ": Unexpected request case " + request.getRequestCase());
return FileStoreCommon.completeExceptionally(index, "Unexpected request case " + request.getRequestCase());
}
}
use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto in project incubator-ratis by apache.
the class FileStoreStateMachine method read.
@Override
public CompletableFuture<ByteString> read(LogEntryProto entry) {
final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
final ByteString data = smLog.getLogData();
final FileStoreRequestProto proto;
try {
proto = FileStoreRequestProto.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
}
if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
return null;
}
final WriteRequestHeaderProto h = proto.getWriteHeader();
CompletableFuture<ExamplesProtos.ReadReplyProto> reply = files.read(h.getPath().toStringUtf8(), h.getOffset(), h.getLength(), false);
return reply.thenApply(ExamplesProtos.ReadReplyProto::getData);
}
use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto in project incubator-ratis by apache.
the class FileStoreStateMachine method write.
@Override
public CompletableFuture<Integer> write(LogEntryProto entry) {
final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
final ByteString data = smLog.getLogData();
final FileStoreRequestProto proto;
try {
proto = FileStoreRequestProto.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
return FileStoreCommon.completeExceptionally(entry.getIndex(), "Failed to parse data, entry=" + entry, e);
}
if (proto.getRequestCase() != FileStoreRequestProto.RequestCase.WRITEHEADER) {
return null;
}
final WriteRequestHeaderProto h = proto.getWriteHeader();
final CompletableFuture<Integer> f = files.write(entry.getIndex(), h.getPath().toStringUtf8(), h.getClose(), h.getSync(), h.getOffset(), smLog.getStateMachineEntry().getStateMachineData());
// sync only if closing the file
return h.getClose() ? f : null;
}
use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto in project incubator-ratis by apache.
the class TestLogSegment method testAppendWithGap.
@Test
public void testAppendWithGap() throws Exception {
LogSegment segment = LogSegment.newOpenSegment(null, 1000, null);
SimpleOperation op = new SimpleOperation("m");
final StateMachineLogEntryProto m = op.getLogEntryContent();
try {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
Assert.fail("should fail since the entry's index needs to be 1000");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
try {
entry = LogProtoUtils.toLogEntryProto(m, 0, 1002);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
Assert.fail("should fail since the entry's index needs to be 1001");
} catch (IllegalStateException e) {
// the exception is expected.
}
}
use of org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto in project incubator-ratis by apache.
the class DataStreamTestUtils method assertLogEntry.
static void assertLogEntry(LogEntryProto logEntry, RaftClientRequest request) {
Assert.assertNotNull(logEntry);
Assert.assertTrue(logEntry.hasStateMachineLogEntry());
final StateMachineLogEntryProto s = logEntry.getStateMachineLogEntry();
Assert.assertEquals(StateMachineLogEntryProto.Type.DATASTREAM, s.getType());
Assert.assertEquals(request.getCallId(), s.getCallId());
Assert.assertEquals(request.getClientId().toByteString(), s.getClientId());
}
Aggregations