Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.hadoop.hdds.scm.block;

import static org.apache.hadoop.hdds.scm.exceptions.SCMException.ResultCodes.INVALID_BLOCK_SIZE;
import static org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator.LOCAL_ID;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -39,6 +38,7 @@
import org.apache.hadoop.hdds.scm.container.common.helpers.ExcludeList;
import org.apache.hadoop.hdds.scm.exceptions.SCMException;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.SequenceIdType;
import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
import org.apache.hadoop.hdds.scm.pipeline.PipelineManager;
import org.apache.hadoop.hdds.scm.pipeline.PipelineNotFoundException;
Expand Down Expand Up @@ -185,7 +185,7 @@ private AllocatedBlock newBlock(ContainerInfo containerInfo)
try {
final Pipeline pipeline = pipelineManager
.getPipeline(containerInfo.getPipelineID());
long localID = sequenceIdGen.getNextId(LOCAL_ID);
long localID = sequenceIdGen.getNextId(SequenceIdType.LOCAL_ID);
long containerID = containerInfo.getContainerID();
AllocatedBlock.Builder abb = new AllocatedBlock.Builder()
.setContainerBlockID(new ContainerBlockID(containerID, localID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_BLOCK_DELETION_PER_DN_DISTRIBUTION_FACTOR;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_BLOCK_DELETION_PER_DN_DISTRIBUTION_FACTOR_DEFAULT;
import static org.apache.hadoop.hdds.scm.block.SCMDeletedBlockTransactionStatusManager.SCMDeleteBlocksCommandStatusManager.CmdStatus;
import static org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator.DEL_TXN_ID;

import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -53,6 +52,7 @@
import org.apache.hadoop.hdds.scm.ha.SCMContext;
import org.apache.hadoop.hdds.scm.ha.SCMHADBTransactionBuffer;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.SequenceIdType;
import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
import org.apache.hadoop.hdds.server.events.EventHandler;
import org.apache.hadoop.hdds.server.events.EventPublisher;
Expand Down Expand Up @@ -240,7 +240,7 @@ public void addTransactions(Map<Long, List<DeletedBlock>> containerBlocksMap)
long currentBatchSizeBytes = 0;
for (Map.Entry<Long, List<DeletedBlock>> entry :
containerBlocksMap.entrySet()) {
long nextTXID = sequenceIdGen.getNextId(DEL_TXN_ID);
long nextTXID = sequenceIdGen.getNextId(SequenceIdType.DEL_TXN_ID);
DeletedBlocksTransaction tx = constructNewTransaction(nextTXID,
entry.getKey(), entry.getValue());
txsToBeAdded.add(tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.hadoop.hdds.scm.container;

import static org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator.CONTAINER_ID;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.IOException;
Expand All @@ -42,6 +40,7 @@
import org.apache.hadoop.hdds.scm.container.replication.ContainerReplicaPendingOps;
import org.apache.hadoop.hdds.scm.ha.SCMHAManager;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.SequenceIdType;
import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
import org.apache.hadoop.hdds.scm.pipeline.PipelineManager;
import org.apache.hadoop.hdds.utils.db.Table;
Expand Down Expand Up @@ -243,7 +242,7 @@ private ContainerInfo allocateContainer(final Pipeline pipeline,
return null;
}

final long uniqueId = sequenceIdGen.getNextId(CONTAINER_ID);
final long uniqueId = sequenceIdGen.getNextId(SequenceIdType.CONTAINER_ID);
Preconditions.checkState(uniqueId > 0,
"Cannot allocate container, negative container id" +
" generated. %s.", uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -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();
Comment on lines +67 to +75
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change them to private.


private static final long INVALID_SEQUENCE_ID = 0;

private final Map<String, Batch> sequenceIdToBatchMap;
private final Map<SequenceIdType, Batch> sequenceIdToBatchMap;

private final Lock lock;
private final long batchSize;
Expand All @@ -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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it unmodifiable:

    this.sequenceIdToBatchMap = newSequenceIdToBatchMap();
  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);
  }

this.lock = new ReentrantLock();
this.batchSize = conf.getInt(OZONE_SCM_SEQUENCE_ID_BATCH_SIZE,
OZONE_SCM_SEQUENCE_ID_BATCH_SIZE_DEFAULT);
Expand All @@ -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());
Comment on lines 117 to +118
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use get()

      final Batch batch = sequenceIdToBatchMap.get(idType);


if (batch.nextId <= batch.lastId) {
return batch.nextId++;
Expand All @@ -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());
Copy link
Copy Markdown
Contributor

@szetszwo szetszwo May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Change the parameter to SequenceIdType
  • Change StateManagerImpl.sequenceIdToLastIdMap key to SequenceIdType

}

Preconditions.checkArgument(batch.nextId <= batch.lastId);
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

if (dbKey == null) {
return null;
}
return DB_KEY_MAP.get(dbKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_NEW_KEY_CERT_DIR_NAME_PROGRESS_SUFFIX;
import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_NEW_KEY_CERT_DIR_NAME_SUFFIX;
import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_X509_DIR_NAME_DEFAULT;
import static org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator.CERTIFICATE_ID;
import static org.apache.hadoop.ozone.OzoneConsts.SCM_ROOT_CA_COMPONENT_NAME;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -55,6 +54,7 @@
import org.apache.hadoop.hdds.scm.ha.SCMContext;
import org.apache.hadoop.hdds.scm.ha.SCMServiceException;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.SequenceIdType;
import org.apache.hadoop.hdds.scm.ha.StatefulService;
import org.apache.hadoop.hdds.scm.server.SCMStorageConfig;
import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
Expand Down Expand Up @@ -378,7 +378,7 @@ public void run() {
CertificateServer newRootCAServer = null;
BigInteger newId = BigInteger.ONE;
try {
newId = BigInteger.valueOf(sequenceIdGen.getNextId(CERTIFICATE_ID));
newId = BigInteger.valueOf(sequenceIdGen.getNextId(SequenceIdType.CERTIFICATE_ID));
newRootCAServer =
HASecurityUtils.initializeRootCertificateServer(secConf,
scm.getCertificateStore(), scmStorageConfig, newId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.hadoop.hdds.scm.server;

import static org.apache.hadoop.hdds.scm.ScmUtils.checkIfCertSignRequestAllowed;
import static org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator.CERTIFICATE_ID;
import static org.apache.hadoop.hdds.security.exception.SCMSecretKeyException.ErrorCode.SECRET_KEY_NOT_ENABLED;
import static org.apache.hadoop.hdds.security.exception.SCMSecretKeyException.ErrorCode.SECRET_KEY_NOT_INITIALIZED;
import static org.apache.hadoop.hdds.security.exception.SCMSecurityException.ErrorCode.CERTIFICATE_NOT_FOUND;
Expand Down Expand Up @@ -63,6 +62,7 @@
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
import org.apache.hadoop.hdds.scm.exceptions.SCMException;
import org.apache.hadoop.hdds.scm.ha.SequenceIdGenerator;
import org.apache.hadoop.hdds.scm.ha.SequenceIdType;
import org.apache.hadoop.hdds.scm.protocol.SCMSecurityProtocolServerSideTranslatorPB;
import org.apache.hadoop.hdds.scm.protocol.SecretKeyProtocolServerSideTranslatorPB;
import org.apache.hadoop.hdds.security.exception.SCMSecretKeyException;
Expand Down Expand Up @@ -481,7 +481,7 @@ public List<String> removeExpiredCertificates() throws IOException {
}

private String getNextCertificateId() throws IOException {
return String.valueOf(sequenceIdGen.getNextId(CERTIFICATE_ID));
return String.valueOf(sequenceIdGen.getNextId(SequenceIdType.CERTIFICATE_ID));
}

@VisibleForTesting
Expand Down
Loading