This is an automated email from the ASF dual-hosted git repository.
adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 57703e4b91c HDDS-17464. Parse open key DB key into a structured
OpenKeyDBKey (#11782)
57703e4b91c is described below
commit 57703e4b91ca7ef8d05d26f8b3e5b68d3330ba18
Author: Alex Lin <[email protected]>
AuthorDate: Wed Oct 28 02:03:12 2026 +0800
HDDS-17464. Parse open key DB key into a structured OpenKeyDBKey (#11782)
---
.../apache/hadoop/ozone/om/OMMetadataManager.java | 13 ----
.../org/apache/hadoop/ozone/om/OpenKeyDBKey.java | 78 ++++++++++++++++++++++
.../apache/hadoop/ozone/om/TestOpenKeyDBKey.java | 60 +++++++++++++++++
.../hadoop/ozone/om/OmMetadataManagerImpl.java | 35 ++++------
.../om/request/util/OMMultipartUploadUtils.java | 37 ----------
5 files changed, 150 insertions(+), 73 deletions(-)
diff --git
a/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OMMetadataManager.java
b/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OMMetadataManager.java
index 20f5949f1a0..803b47a2603 100644
---
a/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OMMetadataManager.java
+++
b/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OMMetadataManager.java
@@ -17,8 +17,6 @@
package org.apache.hadoop.ozone.om;
-import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX;
-
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.nio.file.Path;
@@ -220,17 +218,6 @@ default String getOpenKey(String volume, String bucket,
String key, long id) {
*/
String getOpenKey(String volume, String bucket, String key, String clientId);
- /**
- * Returns client ID in Long of an OpenKeyTable DB Key String.
- * @param dbOpenKeyName An OpenKeyTable DB Key String.
- * @return Client ID (Long)
- */
- static long getClientIDFromOpenKeyDBKey(String dbOpenKeyName) {
- final int lastPrefix = dbOpenKeyName.lastIndexOf(OM_KEY_PREFIX);
- final String clientIdString = dbOpenKeyName.substring(lastPrefix + 1);
- return Long.parseLong(clientIdString);
- }
-
/**
* Given a volume, check if it is empty, i.e there are no buckets inside it.
*
diff --git
a/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OpenKeyDBKey.java
b/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OpenKeyDBKey.java
new file mode 100644
index 00000000000..8d60455ca29
--- /dev/null
+++
b/hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/OpenKeyDBKey.java
@@ -0,0 +1,78 @@
+/*
+ * 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.ozone.om;
+
+import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * A DB key of openKeyTable or openFileTable, parsed into the DB key of the
key and the suffix after it.
+ * <p>
+ * The DB keys of the two tables are different:
+ * <ul>
+ * <li>openKeyTable: /{volumeName}/{bucketName}/{keyName}/{suffix}</li>
+ * <li>openFileTable:
/{volumeId}/{bucketId}/{parentId}/{fileName}/{suffix}</li>
+ * </ul>
+ * The suffix is the client ID, or the upload ID for the open key of a
multipart upload.
+ * Upload ID format: uploadId = UUID + "-" + UniqueId#next
+ * <p>
+ * The open key of a part being written ends with a client ID, so {@link
#isUpload()} is false for it.
+ */
+public final class OpenKeyDBKey {
+ private final String keyTableDBKey;
+ private final String suffix;
+ private final boolean isUpload;
+
+ /**
+ * @param dbKey a DB key of openKeyTable or openFileTable
+ * @throws IllegalArgumentException if dbKey has no "/"
+ */
+ public OpenKeyDBKey(String dbKey) {
+ final int lastPrefix = dbKey.lastIndexOf(OM_KEY_PREFIX);
+ Preconditions.checkArgument(lastPrefix >= 0, "Not an open key DB key: %s",
dbKey);
+ this.keyTableDBKey = dbKey.substring(0, lastPrefix);
+ this.suffix = dbKey.substring(lastPrefix + 1);
+ // Similar to the logic of UUID#fromString, but use 6 since there is
another "-" between the UUID and the UniqueId
+ this.isUpload = !suffix.isEmpty() && suffix.split("-").length == 6;
+ }
+
+ /** @return the DB key without the suffix, i.e. the DB key of keyTable or
fileTable. */
+ public String getKeyTableDBKey() {
+ return keyTableDBKey;
+ }
+
+ /** @return true if the suffix is an upload ID. */
+ public boolean isUpload() {
+ return isUpload;
+ }
+
+ /** @return the upload ID, or null if the suffix is a client ID. */
+ public String getUploadId() {
+ return isUpload ? suffix : null;
+ }
+
+ /**
+ * @return the client ID.
+ * @throws IllegalStateException if the suffix is an upload ID
+ */
+ public long getClientId() {
+ Preconditions.checkState(!isUpload, "No client ID in the open key of
upload %s", suffix);
+ return Long.parseLong(suffix);
+ }
+}
diff --git
a/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/TestOpenKeyDBKey.java
b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/TestOpenKeyDBKey.java
new file mode 100644
index 00000000000..91c445185df
--- /dev/null
+++
b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/TestOpenKeyDBKey.java
@@ -0,0 +1,60 @@
+/*
+ * 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.ozone.om;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test {@link OpenKeyDBKey}.
+ */
+public class TestOpenKeyDBKey {
+
+ @Test
+ public void testClientId() {
+ // The key name may contain "/", only the part after the last "/" is the
suffix
+ OpenKeyDBKey openKey = new OpenKeyDBKey("/vol/bucket/dir/key/1000");
+ assertFalse(openKey.isUpload());
+ assertEquals(1000L, openKey.getClientId());
+ assertNull(openKey.getUploadId());
+ assertEquals("/vol/bucket/dir/key", openKey.getKeyTableDBKey());
+ }
+
+ @Test
+ public void testUploadId() {
+ String uploadId = "c1f1d5a2-3b4e-4c5d-8e9f-0a1b2c3d4e5f-123456789";
+ // openKeyTable and openFileTable
+ for (String keyTableDBKey : new String[] {"/vol/bucket/key",
"/1/2/3/file"}) {
+ OpenKeyDBKey openKey = new OpenKeyDBKey(keyTableDBKey + "/" + uploadId);
+ assertTrue(openKey.isUpload());
+ assertEquals(uploadId, openKey.getUploadId());
+ assertEquals(keyTableDBKey, openKey.getKeyTableDBKey());
+ assertThrows(IllegalStateException.class, openKey::getClientId);
+ }
+ }
+
+ @Test
+ public void testNotOpenKeyDBKey() {
+ assertThrows(IllegalArgumentException.class, () -> new
OpenKeyDBKey("1000"));
+ }
+}
diff --git
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
index 7a653f78aa1..d77c756726d 100644
---
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
+++
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
@@ -1050,11 +1050,10 @@ public ListOpenFilesResult listOpenFiles(BucketLayout
bucketLayout,
while (currentCount < maxKeys && openKeyIter.hasNext()) {
kv = openKeyIter.next();
if (kv != null && kv.getKey().startsWith(dbOpenKeyPrefix)) {
- String dbKey = kv.getKey();
+ final OpenKeyDBKey openKey = new OpenKeyDBKey(kv.getKey());
// The open key of a multipart upload ends with the upload ID and
has no client ID, so it is listed with
// client ID 0, which no real client has
- final boolean isUpload =
OMMultipartUploadUtils.getUploadIdFromDbKey(dbKey) != null;
- long clientID = isUpload ? 0 :
OMMetadataManager.getClientIDFromOpenKeyDBKey(dbKey);
+ long clientID = openKey.isUpload() ? 0 : openKey.getClientId();
OmKeyInfo omKeyInfo = kv.getValue();
// Note with HDDS-10077, there is no need to check KeyTable for
hsync metadata
openKeySessionList.add(
@@ -1415,28 +1414,23 @@ private PersistedUserVolumeInfo getVolumesByUser(String
userNameKey)
/**
* Decide whether the open key is a multipart upload related key.
* @param openKeyInfo open key related to multipart upload
- * @param openDbKey db key of open key related to multipart upload, which
- * might have different format depending on the bucket
- * layout
+ * @param openKey db key of open key related to multipart upload
* @return true open key is a multipart upload related key.
* false otherwise.
*/
- private boolean isOpenMultipartKey(OmKeyInfo openKeyInfo, String openDbKey)
+ private boolean isOpenMultipartKey(OmKeyInfo openKeyInfo, OpenKeyDBKey
openKey)
throws IOException {
if (OMMultipartUploadUtils.isMultipartKeySet(openKeyInfo)) {
return true;
}
- String multipartUploadId =
- OMMultipartUploadUtils.getUploadIdFromDbKey(openDbKey);
-
- if (StringUtils.isEmpty(multipartUploadId)) {
+ if (!openKey.isUpload()) {
return false;
}
String multipartInfoDbKey = getMultipartKey(openKeyInfo.getVolumeName(),
openKeyInfo.getBucketName(), openKeyInfo.getKeyName(),
- multipartUploadId);
+ openKey.getUploadId());
// In addition to checking isMultipartKey flag in the open key info,
// multipartInfoTable needs to be checked to handle multipart upload
@@ -1484,24 +1478,19 @@ public ExpiredOpenKeys getExpiredOpenKeys(Duration
expireThreshold,
while (num < count && keyValueTableIterator.hasNext()) {
KeyValue<String, OmKeyInfo> openKeyValue =
keyValueTableIterator.next();
String dbOpenKeyName = openKeyValue.getKey();
-
- final int lastPrefix = dbOpenKeyName.lastIndexOf(OM_KEY_PREFIX);
- final String dbKeyName = dbOpenKeyName.substring(0, lastPrefix);
+ final OpenKeyDBKey openKey = new OpenKeyDBKey(dbOpenKeyName);
OmKeyInfo openKeyInfo = openKeyValue.getValue();
- if (isOpenMultipartKey(openKeyInfo, dbOpenKeyName)) {
+ if (isOpenMultipartKey(openKeyInfo, openKey)) {
continue;
}
if (openKeyInfo.getCreationTime() <= expiredCreationTimestamp ||
openKeyInfo.getModificationTime() <= expiredLeaseTimestamp) {
- final String clientIdString
- = dbOpenKeyName.substring(lastPrefix + 1);
-
- final boolean isHsync = Optional.of(openKeyInfo)
+ final boolean isHsync = !openKey.isUpload() &&
Optional.of(openKeyInfo)
.map(WithMetadata::getMetadata)
.map(meta -> meta.get(OzoneConsts.HSYNC_CLIENT_ID))
- .filter(id -> id.equals(clientIdString))
+ .filter(id -> id.equals(String.valueOf(openKey.getClientId())))
.isPresent();
if ((!isHsync && openKeyInfo.getCreationTime() <=
expiredCreationTimestamp) ||
@@ -1514,7 +1503,7 @@ public ExpiredOpenKeys getExpiredOpenKeys(Duration
expireThreshold,
} else if (isHsync && openKeyInfo.getModificationTime() <=
expiredLeaseTimestamp &&
!openKeyInfo.getMetadata().containsKey(OzoneConsts.LEASE_RECOVERY)) {
// add hsync'ed keys
- final OmKeyInfo info = kt.get(dbKeyName);
+ final OmKeyInfo info = kt.get(openKey.getKeyTableDBKey());
// Set keyName from openFileTable which contains keyName with full
path like(/a/b/c/d/e/file1),
// which is required in commit key request.
// Whereas fileTable contains only leaf in keyName(like file1) and
so cannot be used in commit request.
@@ -1534,7 +1523,7 @@ public ExpiredOpenKeys getExpiredOpenKeys(Duration
expireThreshold,
OzoneManagerProtocolClientSideTranslatorPB.setReplicationConfig(
info.getReplicationConfig(), keyArgs);
- expiredKeys.addHsyncKey(keyArgs, Long.parseLong(clientIdString));
+ expiredKeys.addHsyncKey(keyArgs, openKey.getClientId());
num++;
}
}
diff --git
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/util/OMMultipartUploadUtils.java
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/util/OMMultipartUploadUtils.java
index 71781360baa..13f8df7e864 100644
---
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/util/OMMultipartUploadUtils.java
+++
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/util/OMMultipartUploadUtils.java
@@ -28,7 +28,6 @@
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
-import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hdds.client.ReplicationConfig;
import org.apache.hadoop.hdds.utils.UniqueId;
import org.apache.hadoop.hdds.utils.db.Table;
@@ -72,42 +71,6 @@ public static void validateUploadId(String uploadId) throws
OMException {
}
}
- /**
- * Get multipart upload ID from the DB key of multipart upload
- * form openKeyTable/openFileTable.
- *
- * The DB keys of openKeyTable and openFileTable are different:
- * openKeyTable: /{volumeName}/{bucketName}/{keyName}/{uploadId}
- * openFileTable: /{volumeId}/{bucketId}/{parentId}/{fileName}/{uploadId}
- *
- *
- * Despite the difference, both have the uploadId as the suffix of the DB
- * key, we can extract this suffix to get the upload ID from the DB key.
- *
- * Upload ID format: uploadId = UUIDv4 + "-" + UniqueId#next
- *
- * @param key DB key
- * @return upload ID if uploadId can be extracted from openDBKey
- * otherwise null
- */
- public static String getUploadIdFromDbKey(String key) {
- String[] split = key.split(OM_KEY_PREFIX);
- if (split.length < 5) {
- return null;
- }
-
- String uploadId = split[split.length - 1];
-
- // Similar to the logic of UUID#fromString, but use 6 since there is
- // another "-" between the UUID and the UniqueId
- if (StringUtils.isEmpty(uploadId) ||
- uploadId.split("-").length != 6) {
- return null;
- }
-
- return uploadId;
- }
-
/**
* Get the multipart open key based on the bucket layout.
* @throws IOException
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]