org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto Java Examples

The following examples show how to use org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto. 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 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 #2
Source File: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
public static String smProtoToString(RaftGroupId gid,
                                 ContainerController containerController,
                                 StateMachineLogEntryProto proto) {
  StringBuilder builder = new StringBuilder();
  try {
    ContainerCommandRequestProto requestProto =
        getContainerCommandRequestProto(gid, proto.getLogData());
    long contId = requestProto.getContainerID();

    builder.append(TextFormat.shortDebugString(requestProto));

    if (containerController != null) {
      String location = containerController.getContainerLocation(contId);
      builder.append(", container path=");
      builder.append(location);
    }
  } catch (Exception t) {
    LOG.info("smProtoToString failed", t);
    builder.append("smProtoToString failed with");
    builder.append(t.getMessage());
  }
  return builder.toString();
}
 
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 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 #5
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 #6
Source File: TransactionContextImpl.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link TransactionContext} from a client request.
 * Used by the state machine to start a transaction
 * and send the Log entry representing the transaction data
 * to be applied to the raft log.
 */
public TransactionContextImpl(
    StateMachine stateMachine, RaftClientRequest clientRequest,
    StateMachineLogEntryProto smLogEntryProto, Object stateMachineContext) {
  this(RaftPeerRole.LEADER, stateMachine);
  this.clientRequest = clientRequest;
  this.smLogEntryProto = smLogEntryProto != null? smLogEntryProto
      : ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, null, null);
  this.stateMachineContext = stateMachineContext;
}
 
Example #7
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 #8
Source File: TransactionContextImpl.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link TransactionContext} from a client request.
 * Used by the state machine to start a transaction
 * and send the Log entry representing the transaction data
 * to be applied to the raft log.
 */
public TransactionContextImpl(
    StateMachine stateMachine, RaftClientRequest clientRequest,
    StateMachineLogEntryProto smLogEntryProto, Object stateMachineContext) {
  this(RaftPeerRole.LEADER, stateMachine);
  this.clientRequest = clientRequest;
  this.smLogEntryProto = smLogEntryProto != null? smLogEntryProto
      : ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, null, null);
  this.stateMachineContext = stateMachineContext;
}
 
Example #9
Source File: ParseRatisLog.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private ParseRatisLog(File f , Function<StateMachineLogEntryProto, String> smLogToString) {
  this.file = f;
  this.smLogToString = smLogToString;
  this.numConfEntries = 0;
  this.numMetadataEntries = 0;
  this.numStateMachineEntries = 0;
  this.numInvalidEntries = 0;
}
 
Example #10
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 #11
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 #12
Source File: OMRatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Convert StateMachineLogEntryProto to String.
 * @param proto - {@link StateMachineLogEntryProto}
 * @return String
 */
public static String smProtoToString(StateMachineLogEntryProto proto) {
  StringBuilder builder = new StringBuilder();
  try {
    builder.append(TextFormat.shortDebugString(
        OMRatisHelper.convertByteStringToOMRequest(proto.getLogData())));

  } catch (Throwable ex) {
    LOG.info("smProtoToString failed", ex);
    builder.append("smProtoToString failed with");
    builder.append(ex.getMessage());
  }
  return builder.toString();
}
 
Example #13
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 #14
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 #15
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 #16
Source File: RaftTestUtil.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public StateMachineLogEntryProto getLogEntryContent() {
  return smLogEntryProto;
}
 
Example #17
Source File: TransactionContextImpl.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public StateMachineLogEntryProto getStateMachineLogEntry() {
  return smLogEntryProto;
}
 
Example #18
Source File: TransactionContext.java    From ratis with Apache License 2.0 4 votes vote down vote up
public Builder setStateMachineLogEntry(StateMachineLogEntryProto stateMachineLogEntry) {
  this.stateMachineLogEntry = stateMachineLogEntry;
  return this;
}
 
Example #19
Source File: TransactionContextImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public StateMachineLogEntryProto getStateMachineLogEntry() {
  return smLogEntryProto;
}
 
Example #20
Source File: TransactionContextImpl.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionContext setStateMachineLogEntryProto(StateMachineLogEntryProto smLogEntryProto) {
  this.smLogEntryProto = smLogEntryProto;
  return this;
}
 
Example #21
Source File: RaftTestUtil.java    From ratis with Apache License 2.0 4 votes vote down vote up
public StateMachineLogEntryProto getLogEntryContent() {
  return smLogEntryProto;
}
 
Example #22
Source File: TransactionContextImpl.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionContext setStateMachineLogEntryProto(StateMachineLogEntryProto logEntryProto) {
  this.smLogEntryProto = logEntryProto;
  return this;
}
 
Example #23
Source File: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private ByteString getStateMachineData(StateMachineLogEntryProto entryProto) {
  return entryProto.getStateMachineEntry().getStateMachineData();
}
 
Example #24
Source File: TransactionContext.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public Builder setStateMachineLogEntry(StateMachineLogEntryProto stateMachineLogEntry) {
  this.stateMachineLogEntry = stateMachineLogEntry;
  return this;
}
 
Example #25
Source File: ParseRatisLog.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public Builder setSMLogToString(Function<StateMachineLogEntryProto, String> smLogToStr) {
  this.smLogToString = smLogToStr;
  return this;
}
 
Example #26
Source File: ArithmeticLogDump.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private static String smToArithmeticLogString(StateMachineLogEntryProto logEntryProto) {
  AssignmentMessage message = new AssignmentMessage(logEntryProto.getLogData());
  return message.toString();
}
 
Example #27
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public String toStateMachineLogEntryString(StateMachineLogEntryProto proto) {
  return OMRatisHelper.smProtoToString(proto);
}
 
Example #28
Source File: DatanodeRatisLogParser.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static String smToContainerLogString(
    StateMachineLogEntryProto logEntryProto) {
  return ContainerStateMachine.
      smProtoToString(DUMMY_PIPELINE_ID, null, logEntryProto);
}
 
Example #29
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Create the appendLogEntry message (Raft envelope + ozone payload).
 */
private AppendEntriesRequestProto createAppendLogEntry(long sequence,
    long callId) {
  AppendEntriesRequestProto.Builder requestBuilder =
      AppendEntriesRequestProto.newBuilder();

  if (rateLimiter != null) {
    try {
      rateLimiter.acquire();
    } catch (InterruptedException e) {
      LOG.error("Rate limiter acquire has been interrupted", e);
    }
  }
  long previousLog = nextIndex - 1;
  for (int i = 0; i < batching; i++) {
    long index = nextIndex++;

    long chunkId = batching * sequence + i;
    long blockId = chunkId / 1000;
    long containerId = blockId / 1000;
    //ozone specific
    ByteString payload = ContainerCommandRequestProto.newBuilder()
        .setContainerID(containerId)
        .setCmdType(Type.WriteChunk)
        .setDatanodeUuid(serverId)
        .setWriteChunk(WriteChunkRequestProto.newBuilder()
            .setData(ByteString.EMPTY)
            .setBlockID(DatanodeBlockID.newBuilder()
                .setContainerID(containerId)
                .setLocalID(blockId)
                .build())
            .setChunkData(ChunkInfo.newBuilder()
                .setChunkName("chunk" + chunkId)
                .setLen(dataToWrite.size())
                .setOffset(0)
                .setChecksumData(ChecksumData.newBuilder()
                    .setBytesPerChecksum(0)
                    .setType(ChecksumType.NONE)
                    .build())
                .build())
            .build())
        .build().toByteString();

    //ratis specific
    Builder stateMachinelogEntry = StateMachineLogEntryProto.newBuilder()
        .setCallId(callId)
        .setClientId(ClientId.randomId().toByteString())
        .setLogData(payload)
        .setStateMachineEntry(
            StateMachineEntryProto.newBuilder()
                .setStateMachineData(dataToWrite)
                .build());

    //ratis specific
    LogEntryProto logEntry = LogEntryProto.newBuilder()
        .setTerm(term)
        .setIndex(index)
        .setStateMachineLogEntry(stateMachinelogEntry)
        .build();

    requestBuilder.addEntries(logEntry);
  }

  //the final AppendEntriesRequest includes all the previous chunk writes
  // (log entries).
  requestBuilder
      .setPreviousLog(
          TermIndexProto.newBuilder().setTerm(term)
              .setIndex(previousLog).build())
      .setLeaderCommit(Math.max(0, previousLog - batching))
      .addCommitInfos(CommitInfoProto
          .newBuilder()
          .setServer(requestor)
          .setCommitIndex(Math.max(0, previousLog - batching))
          .build())
      .setLeaderTerm(term)
      .setServerRequest(createServerRequest(callId))
      .build();

  return requestBuilder.build();

}
 
Example #30
Source File: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public String toStateMachineLogEntryString(StateMachineLogEntryProto proto) {
  return smProtoToString(gid, containerController, proto);
}