-
Notifications
You must be signed in to change notification settings - Fork 601
HDDS-15145. Use enum for the ID type in SequenceIdGenerator #10211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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(); | ||
|
|
||
| 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; | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
|
|
@@ -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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| Preconditions.checkArgument(batch.nextId <= batch.lastId); | ||
|
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change them to private.