-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-4315. Use Epoch to generate unique ObjectIDs #1480
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
|
|
||
| package org.apache.hadoop.ozone; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.protobuf.ServiceException; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
@@ -76,6 +78,17 @@ public final class OmUtils { | |
| private static final SecureRandom SRAND = new SecureRandom(); | ||
| private static byte[] randomBytes = new byte[32]; | ||
|
|
||
| private static final long TRANSACTION_ID_SHIFT = 8; | ||
| // from the 64 bits of ObjectID (long variable), 2 bits are reserved for | ||
| // epoch and 8 bits for recursive directory creation, if required. This | ||
| // leaves 54 bits for the transaction ID. Also, the last transaction ID is | ||
| // reserved for creating S3G volume on OM start {@link | ||
| // OzoneManager#addS3GVolumeToDB()}. | ||
| public static final long EPOCH_ID_SHIFT = 62; // 64 - 2 | ||
| public static final long MAX_TRXN_ID = (long) (Math.pow(2, 54) - 2); | ||
| public static final int EPOCH_WHEN_RATIS_NOT_ENABLED = 1; | ||
|
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. Don't we want these values to be 0 and 1 instead of 1 & 2 ?
Contributor
Author
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. Wanted to avoid 0 as we can assume that currently it is 0. This would give us a way to separate out objectIds created before this fix. If ever, these non-unique objectIds need to be fixed, it would be easy to identify them. |
||
| public static final int EPOCH_WHEN_RATIS_ENABLED = 2; | ||
|
|
||
| private OmUtils() { | ||
| } | ||
|
|
||
|
|
@@ -360,8 +373,6 @@ public static Collection<String> emptyAsSingletonNull(Collection<String> | |
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * If a OM conf is only set with key suffixed with OM Node ID, return the | ||
| * set value. | ||
|
|
@@ -524,6 +535,48 @@ public static OmKeyInfo prepareKeyForRecover(OmKeyInfo keyInfo, | |
| } | ||
| } | ||
|
|
||
| public static int getOMEpoch(boolean isRatisEnabled) { | ||
| return isRatisEnabled ? EPOCH_WHEN_RATIS_ENABLED : | ||
| EPOCH_WHEN_RATIS_NOT_ENABLED; | ||
| } | ||
|
|
||
| /** | ||
| * Get the valid base object id given the transaction id. | ||
| * @param epoch a 2 bit epoch number. The 2 most significant bits of the | ||
| * object will be set to this epoch. | ||
| * @param id of the transaction. This value cannot exceed 2^54 - 1 as | ||
| * out of the 64 bits for a long, 2 are reserved for the epoch | ||
| * and 8 for recursive directory creation. | ||
| * @return base object id allocated against the transaction | ||
| */ | ||
| public static long getObjectIdFromTxId(long epoch, long id) { | ||
| Preconditions.checkArgument(id <= MAX_TRXN_ID, "TransactionID " + | ||
| "exceeds max limit of " + MAX_TRXN_ID); | ||
|
||
| return addEpochToObjectId(epoch, id); | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Note - This function should not be called directly. It is directly called | ||
| * only from OzoneManager#addS3GVolumeToDB() which is a one time operation | ||
| * when OM is started first time to add S3G volume. In call other cases, | ||
| * getObjectIdFromTxId() should be called to append epoch to objectID. | ||
| */ | ||
| public static long addEpochToObjectId(long epoch, long id) { | ||
|
||
| long lsb54 = id << TRANSACTION_ID_SHIFT; | ||
| long msb2 = epoch << EPOCH_ID_SHIFT; | ||
|
|
||
| return msb2 | lsb54; | ||
| } | ||
|
|
||
| /** | ||
| * Given an objectId, unset the 2 most significant bits to get the | ||
| * corresponding transaction index. | ||
| */ | ||
| @VisibleForTesting | ||
| public static long getTxIdFromObjectId(long objectId) { | ||
| return ((Long.MAX_VALUE >> 2) & objectId) >> 8; | ||
hanishakoneru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Verify key name is a valid name. | ||
| */ | ||
|
|
||
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.
can we use 1 << 54 instead of Math.pow ?
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.
Done.