szetszwo commented on code in PR #10211:
URL: https://github.com/apache/ozone/pull/10211#discussion_r3210589616


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIdType.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents the sequence ID types managed by {@link SequenceIdGenerator} and 
their persisted RocksDB keys.
+ */
+public enum SequenceIdType {
+
+  LOCAL_ID("localId"),
+
+  DEL_TXN_ID("delTxnId"),
+
+  CONTAINER_ID("containerId"),
+
+  /** Certificate ID for all services, including root certificates. */
+  CERTIFICATE_ID("CertificateId"),
+
+  /**
+   * @deprecated Use {@link #CERTIFICATE_ID} instead.
+   */
+  @Deprecated
+  ROOT_CERTIFICATE_ID("rootCertificateId");
+
+  /**
+   * The key string stored in the RocksDB sequenceId table.
+   */
+  private final String dbKey;
+
+  /**
+   * Reverse lookup map from db key string to enum constant.
+   */
+  private static final Map<String, SequenceIdType> DB_KEY_MAP;
+
+  static {
+    Map<String, SequenceIdType> map = new HashMap<>();
+    for (SequenceIdType type : values()) {
+      map.put(type.dbKey, type);
+    }
+    DB_KEY_MAP = Collections.unmodifiableMap(map);
+  }
+
+  SequenceIdType(String dbKey) {
+    this.dbKey = dbKey;
+  }
+
+  /**
+   * Returns the key string used to persist this sequence ID in RocksDB.
+   * This value must not be changed to keep backward compatibility with
+   * existing databases.
+   */
+  public String getDbKey() {
+    return dbKey;
+  }
+
+  /**
+   * Returns the {@link SequenceIdType} corresponding to the provided RocksDB 
key string, or null if unmapped.
+   */
+  public static SequenceIdType fromDbKey(String dbKey) {

Review Comment:
   This method is unused except testReturnsNullIfEnumConstantNotAvailable().  
Let's remove it and also DB_KEY_MAP.
   
   Indeed, we should always use SequenceIdType and never convert a String to 
SequenceIdType.



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIdGenerator.java:
##########
@@ -64,19 +64,19 @@ public class SequenceIdGenerator {
   /**
    * Ids supported.
    */
-  public static final String LOCAL_ID = "localId";
-  public static final String DEL_TXN_ID = "delTxnId";
-  public static final String CONTAINER_ID = "containerId";
+  public static final String LOCAL_ID = SequenceIdType.LOCAL_ID.getDbKey();
+  public static final String DEL_TXN_ID = SequenceIdType.DEL_TXN_ID.getDbKey();
+  public static final String CONTAINER_ID = 
SequenceIdType.CONTAINER_ID.getDbKey();
 
   // Certificate ID for all services, including root certificates, whose ID
   // were using "rootCertificateId" before.
-  public static final String CERTIFICATE_ID = "CertificateId";
+  public static final String CERTIFICATE_ID = 
SequenceIdType.CERTIFICATE_ID.getDbKey();
   @Deprecated
-  public static final String ROOT_CERTIFICATE_ID = "rootCertificateId";
+  public static final String ROOT_CERTIFICATE_ID = 
SequenceIdType.ROOT_CERTIFICATE_ID.getDbKey();

Review Comment:
   Change them to private.



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIdGenerator.java:
##########
@@ -89,7 +89,7 @@ public class SequenceIdGenerator {
    */
   public SequenceIdGenerator(ConfigurationSource conf,
       SCMHAManager scmhaManager, Table<String, Long> sequenceIdTable) {
-    this.sequenceIdToBatchMap = new HashMap<>();
+    this.sequenceIdToBatchMap = new EnumMap<>(SequenceIdType.class);

Review Comment:
   Make it unmodifiable:
   
   ```java
       this.sequenceIdToBatchMap = newSequenceIdToBatchMap();
   ```
   
   ```java
     static Map<SequenceIdType, Batch> newSequenceIdToBatchMap() {
       final EnumMap<SequenceIdType, Batch> map = new 
EnumMap<>(SequenceIdType.class);
       for (SequenceIdType type : SequenceIdType.values()) {
         map.put(type, new Batch());
       }
       return Collections.unmodifiableMap(map);
     }
   ```



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIdGenerator.java:
##########
@@ -108,14 +108,14 @@ public StateManager createStateManager(SCMHAManager 
scmhaManager,
   }
 
   /**
-   * @param sequenceIdName : name of the sequenceId
-   * @return : next id of this sequenceId.
+   * @param idType : supported sequence ID type
+   * @return next id of this sequence ID.
    */
-  public long getNextId(String sequenceIdName) throws SCMException {
+  public long getNextId(SequenceIdType idType) throws SCMException {
     lock.lock();
     try {
       Batch batch = sequenceIdToBatchMap.computeIfAbsent(
-          sequenceIdName, key -> new Batch());
+          idType, key -> new Batch());

Review Comment:
   Use get()
   ```java
         final Batch batch = sequenceIdToBatchMap.get(idType);
   ```



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIdGenerator.java:
##########
@@ -128,18 +128,18 @@ public long getNextId(String sequenceIdName) throws 
SCMException {
 
         Preconditions.checkArgument(Long.MAX_VALUE - batch.lastId >= 
batchSize);
         long nextLastId = batch.lastId +
-            ((sequenceIdName.equals(CERTIFICATE_ID)) ? 1 : batchSize);
+            (idType == SequenceIdType.CERTIFICATE_ID ? 1 : batchSize);
 
-        if (stateManager.allocateBatch(sequenceIdName,
+        if (stateManager.allocateBatch(idType.getDbKey(),
             prevLastId, nextLastId)) {
           batch.lastId = nextLastId;
           LOG.info("Allocate a batch for {}, change lastId from {} to {}.",
-              sequenceIdName, prevLastId, batch.lastId);
+              idType, prevLastId, batch.lastId);
           break;
         }
 
         // reload lastId from RocksDB.
-        batch.lastId = stateManager.getLastId(sequenceIdName);
+        batch.lastId = stateManager.getLastId(idType.getDbKey());

Review Comment:
   - Change the parameter to SequenceIdType
   - StateManagerImpl.sequenceIdToLastIdMap key to SequenceIdType



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

To unsubscribe, e-mail: [email protected]

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