amaliujia commented on a change in pull request #1725:
URL: https://github.com/apache/ozone/pull/1725#discussion_r559798815



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
##########
@@ -26,27 +26,55 @@
 import java.util.concurrent.CompletableFuture;
 
 import com.google.protobuf.InvalidProtocolBufferException;
+import org.apache.hadoop.hdds.scm.exceptions.SCMException;
+import org.apache.hadoop.util.Time;
 import org.apache.ratis.protocol.Message;
+import org.apache.ratis.server.protocol.TermIndex;
+import org.apache.ratis.statemachine.SnapshotInfo;
 import org.apache.ratis.statemachine.TransactionContext;
 import org.apache.ratis.statemachine.impl.BaseStateMachine;
 
 import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static 
org.apache.hadoop.hdds.scm.exceptions.SCMException.ResultCodes.SCM_NOT_INITIALIZED;
 
 /**
  * TODO.
  */
 public class SCMStateMachine extends BaseStateMachine {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(BaseStateMachine.class);
 
   private final Map<RequestType, Object> handlers;
+  private final SCMDBTransactionBuffer transactionBuffer;
 
-  public SCMStateMachine() {
+  public SCMStateMachine(SCMDBTransactionBuffer buffer) throws SCMException {
     this.handlers = new EnumMap<>(RequestType.class);
+    this.transactionBuffer = buffer;
+    SCMTransactionInfo latestTrxInfo = buffer.getLatestTrxInfo();
+    if (!latestTrxInfo.isInitialized()) {
+      if (!updateLastAppliedTermIndex(latestTrxInfo.getTerm(),
+          latestTrxInfo.getTransactionIndex())) {
+        throw new SCMException(
+            String.format("Failed to update LastAppliedTermIndex " +
+                    "in StateMachine to term:{} index:{}",
+                latestTrxInfo.getTerm(), latestTrxInfo.getTransactionIndex()
+            ), SCM_NOT_INITIALIZED);
+      }
+    }
   }
 
   public void registerHandler(RequestType type, Object handler) {
     handlers.put(type, handler);
   }
 
+  @Override
+  public SnapshotInfo getLatestSnapshot() {
+    return transactionBuffer.getLatestSnapshot();
+  }
+
   @Override
   public CompletableFuture<Message> applyTransaction(

Review comment:
       I created https://issues.apache.org/jira/browse/HDDS-4684.
   
   After check the Ratis state machine interface, I can see two functions:
   ```
     /**
      * Called for transactions that have been committed to the RAFT log. This 
step is called
      * sequentially in strict serial order that the transactions have been 
committed in the log.
      * The SM is expected to do only necessary work, and leave the actual 
apply operation to the
      * applyTransaction calls that can happen concurrently.
      * @param trx the transaction state including the log entry that has been 
committed to a quorum
      *            of the raft peers
      * @return The Transaction context.
      */
     TransactionContext applyTransactionSerial(TransactionContext trx);
   
     /**
      * Apply a committed log entry to the state machine. This method can be 
called concurrently with
      * the other calls, and there is no guarantee that the calls will be 
ordered according to the
      * log commit order.
      * @param trx the transaction state including the log entry that has been 
committed to a quorum
      *            of the raft peers
      */
     CompletableFuture<Message> applyTransaction(TransactionContext trx);
   ```
   
   So there is no API as the following 
   ```
     Message applyTransactionSerial(TransactionContext trx);
   ```
   
   Basically a function to apply transaction in strict serial order where it 
returns a Message.
   
   I am planning to send an email to Ratis community to discuss the intention 
of only having one applyTransaction that returns Message 
(CompletableFuture<Message>) but can be called concurrently.
   
   We can address this API change in HDDS-4684.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to