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



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMDBTransactionBuffer.java
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership.  The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.hadoop.hdds.scm.ha;
+
+import org.apache.hadoop.hdds.scm.metadata.SCMMetadataStore;
+import org.apache.hadoop.hdds.utils.db.BatchOperation;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.ratis.statemachine.SnapshotInfo;
+
+import java.io.IOException;
+
+import static org.apache.hadoop.ozone.OzoneConsts.TRANSACTION_INFO_KEY;
+
+/**
+ * This is a transaction buffer that buffers SCM DB operations for Pipeline and
+ * Container. When flush this buffer to DB, a transaction info will also be
+ * written into DB to indicate the term and transaction index for the latest
+ * operation in DB.
+ */
+public class SCMDBTransactionBuffer {
+  private final SCMMetadataStore metadataStore;
+  private BatchOperation currentBatchOperation;
+  private SCMTransactionInfo latestTrxInfo;
+  private SnapshotInfo latestSnapshot;
+
+  public SCMDBTransactionBuffer(SCMMetadataStore store) throws IOException {
+    this.metadataStore = store;
+
+    // initialize a batch operation during construction time
+    currentBatchOperation = this.metadataStore.getStore().initBatchOperation();
+    latestTrxInfo = store.getTransactionInfoTable().get(TRANSACTION_INFO_KEY);
+    if (latestTrxInfo == null) {
+      // transaction table is empty
+      latestTrxInfo =
+          SCMTransactionInfo
+              .builder()
+              .setTransactionIndex(-1)
+              .setCurrentTerm(0)
+              .build();
+    }
+    latestSnapshot = latestTrxInfo.toSnapshotInfo();
+  }
+
+  public BatchOperation getCurrentBatchOperation() {
+    return currentBatchOperation;
+  }
+
+  public void updateLatestTrxInfo(SCMTransactionInfo info) {
+    if (info.compareTo(this.latestTrxInfo) <= 0) {
+      throw new IllegalArgumentException(
+          "Updating DB buffer transaction info by an older transaction info, "
+          + "current: " + this.latestTrxInfo + ", updating to: " + info);
+    }
+    this.latestTrxInfo = info;
+  }
+
+  public SCMTransactionInfo getLatestTrxInfo() {
+    return this.latestTrxInfo;
+  }
+
+  public SnapshotInfo getLatestSnapshot() {
+    return latestSnapshot;
+  }
+
+  public void setLatestSnapshot(SnapshotInfo latestSnapshot) {
+    this.latestSnapshot = latestSnapshot;
+  }
+
+  public void flush() throws IOException {
+    // write latest trx info into trx table in the same batch
+    Table<String, SCMTransactionInfo> transactionInfoTable
+        = metadataStore.getTransactionInfoTable();
+    transactionInfoTable.putWithBatch(currentBatchOperation,
+        TRANSACTION_INFO_KEY, latestTrxInfo);
+
+    metadataStore.getStore().commitBatchOperation(currentBatchOperation);
+    currentBatchOperation.close();
+

Review comment:
       Will it be better to call `latestSnapshot = 
latestTrxInfo.toSnapshotInfo();` here, and remove the `setLatestSnapshot ()` 
method ? 
   If caller always has to call `flush()` and `setLatestSnapshot()` together, 
better to merge them to avoid  human mistake.

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
##########
@@ -55,15 +84,25 @@ public void registerHandler(RequestType type, Object 
handler) {
     try {
       final SCMRatisRequest request = SCMRatisRequest.decode(
           Message.valueOf(trx.getStateMachineLogEntry().getLogData()));
-      applyTransactionFuture.complete(process(request));
+      applyTransactionFuture.complete(
+          process(

Review comment:
       How about revert the change to `process()` and change like this ?
   ```
   applyTransactionFuture.complete(process(request));
   transactionBuffer.updateLatestTrxInfo(SCMTransactionInfo.builder()
                      .setCurrentTerm(trx.getLogEntry().getTerm())
                      .setTransactionIndex(trx.getLogEntry().getIndex())
                      .build()));
   ```
   the `process()` does not needs to know about the `trxInfo `

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
##########
@@ -55,15 +84,25 @@ public void registerHandler(RequestType type, Object 
handler) {
     try {
       final SCMRatisRequest request = SCMRatisRequest.decode(
           Message.valueOf(trx.getStateMachineLogEntry().getLogData()));
-      applyTransactionFuture.complete(process(request));
+      applyTransactionFuture.complete(
+          process(
+              request,
+              SCMTransactionInfo.builder()
+                  .setCurrentTerm(trx.getLogEntry().getTerm())
+                  .setTransactionIndex(trx.getLogEntry().getIndex())
+                  .build()));
     } catch (Exception ex) {
       applyTransactionFuture.completeExceptionally(ex);
     }
     return applyTransactionFuture;
   }
 
-  private Message process(final SCMRatisRequest request)
-      throws Exception {
+  private boolean shouldUpdate(SCMTransactionInfo info) {
+    return !(info.getTransactionIndex() == -1 && info.getTerm() == 0);

Review comment:
       How about move this `shouldUpdate()` in to `SCMTransactionInfo`, as a 
method `isEmpty()`? We'd better encapulate  the magic number `0` and `-1` into 
`SCMTransactionInfo `.

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
##########
@@ -89,4 +128,33 @@ private Message process(final SCMRatisRequest request)
     }
   }
 
+  @Override
+  public long takeSnapshot() throws IOException {
+    LOG.info("Current Snapshot Index {}", getLastAppliedTermIndex());
+    long startTime = Time.monotonicNow();
+    TermIndex lastTermIndex = getLastAppliedTermIndex();
+    long lastAppliedIndex = lastTermIndex.getIndex();
+    SCMTransactionInfo lastAppliedTrxInfo =
+        SCMTransactionInfo.fromTermIndex(lastTermIndex);
+    if (transactionBuffer.getLatestTrxInfo()
+        .compareTo(lastAppliedTrxInfo) < 0) {
+      transactionBuffer.updateLatestTrxInfo(
+          SCMTransactionInfo.builder()
+              .setCurrentTerm(lastTermIndex.getTerm())
+              .setTransactionIndex(lastTermIndex.getIndex())
+              .build());
+      transactionBuffer.setLatestSnapshot(
+          transactionBuffer.getLatestTrxInfo().toSnapshotInfo());
+    } else {
+      lastAppliedIndex =
+          transactionBuffer.getLatestTrxInfo().getTransactionIndex();
+    }
+
+    transactionBuffer.flush();
+    transactionBuffer.setLatestSnapshot(
+        transactionBuffer.getLatestTrxInfo().toSnapshotInfo());
+    LOG.debug("SCM takeSnapshot: {} took {} ms",

Review comment:
       Better remove the info in line 133 and replace 156 as 
   
   ```
   LOG.info("Current Snapshot Index {}, takeSnapshot took {} ms", 
       getLastAppliedTermIndex(), Time.monotonicNow() - startTime);
   ```

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMStateMachine.java
##########
@@ -89,4 +98,12 @@ private Message process(final SCMRatisRequest request)
     }
   }
 
+  @Override
+  public long takeSnapshot() throws IOException {
+    LOG.info("Current Snapshot Index {}", getLastAppliedTermIndex());
+    TermIndex lastTermIndex = getLastAppliedTermIndex();

Review comment:
       You need call `updateLastAppliedTermIndex ` in `applyTransaction()`, the 
`lastAppliedTermIndex` in `BaseStateMachine` should be updated manually when 
apply a entry.
   
   Please double check `applyTransaction` in `BaseStateMachine`
   ```
     @Override
     public CompletableFuture<Message> applyTransaction(TransactionContext trx) 
{
       // return the same message contained in the entry
       RaftProtos.LogEntryProto entry = 
Objects.requireNonNull(trx.getLogEntry());
       updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
       return CompletableFuture.completedFuture(
           
Message.valueOf(trx.getLogEntry().getStateMachineLogEntry().getLogData()));
     }
   ```
   and  `applyTransaction` in `ArithmeticStateMachine`.




----------------------------------------------------------------
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