Java Code Examples for org.apache.ratis.proto.RaftProtos.LogEntryProto#getStateMachineLogEntry()

The following examples show how to use org.apache.ratis.proto.RaftProtos.LogEntryProto#getStateMachineLogEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileStoreStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@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.getOffset(), smLog.getStateMachineEntry().getStateMachineData());
  // sync only if closing the file
  return h.getClose()? f: null;
}
 
Example 2
Source File: FileStoreStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@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());

  return reply.thenApply(ExamplesProtos.ReadReplyProto::getData);
}
 
Example 3
Source File: FileStoreStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Integer> writeStateMachineData(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.getOffset(), smLog.getStateMachineEntry().getStateMachineData());
  // sync only if closing the file
  return h.getClose()? f: null;
}
 
Example 4
Source File: FileStoreStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<ByteString> readStateMachineData(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());

  return reply.thenApply(ExamplesProtos.ReadReplyProto::getData);
}
 
Example 5
Source File: FileStoreStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@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 WRITE:
      // WRITE should not happen here since
      // 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());
  }
}
 
Example 6
Source File: RetryCacheTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void createEntry(RetryCache cache, LogEntryProto logEntry){
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    cache.getOrCreateEntry(clientId, callId);
  }
}
 
Example 7
Source File: RetryCacheTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void assertFailure(RetryCache cache, LogEntryProto logEntry, boolean isFailed) {
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    Assert.assertEquals(isFailed, cache.get(clientId, callId).isFailed());
  }
}
 
Example 8
Source File: FileStoreStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
@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 WRITE:
      // WRITE should not happen here since
      // 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());
  }
}
 
Example 9
Source File: RetryCacheTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void createEntry(RetryCache cache, LogEntryProto logEntry){
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    cache.getOrCreateEntry(clientId, callId);
  }
}
 
Example 10
Source File: RetryCacheTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void assertFailure(RetryCache cache, LogEntryProto logEntry, boolean isFailed) {
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    Assert.assertEquals(isFailed, cache.get(clientId, callId).isFailed());
  }
}
 
Example 11
Source File: TransactionContextImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a {@link TransactionContext} from a {@link LogEntryProto}.
 * Used by followers for applying committed entries to the state machine.
 * @param logEntry the log entry to be applied
 */
public TransactionContextImpl(RaftPeerRole serverRole, StateMachine stateMachine, LogEntryProto logEntry) {
  this(serverRole, stateMachine);
  this.logEntry = logEntry;
  this.smLogEntryProto = logEntry.getStateMachineLogEntry();
}
 
Example 12
Source File: TransactionContextImpl.java    From ratis with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a {@link TransactionContext} from a {@link LogEntryProto}.
 * Used by followers for applying committed entries to the state machine.
 * @param logEntry the log entry to be applied
 */
public TransactionContextImpl(RaftPeerRole serverRole, StateMachine stateMachine, LogEntryProto logEntry) {
  this(serverRole, stateMachine);
  this.logEntry = logEntry;
  this.smLogEntryProto = logEntry.getStateMachineLogEntry();
}