Skip to content
Merged
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 @@ -33,6 +33,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -88,6 +89,7 @@
import org.apache.hadoop.ozone.om.helpers.QuotaUtil;
import org.apache.hadoop.ozone.om.request.util.OMMultipartUploadUtils;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.ozone.test.GenericTestUtils;
import org.apache.ozone.test.NonHATests;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -414,6 +416,62 @@ public void testMultipartUploadWithMissingParts() throws Exception {
() -> completeMultipartUpload(bucket, keyName, uploadID, partsMap));
}

@Test
public void testFailedCompleteAfterParentDeletionDoesNotLeakNamespaceQuota()

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 is good. I suspect it's possible to make unit test with mocks inside TestS3MultipartUploadCompleteRequest instead.

throws Exception {
OMMetadataManager metadataManager =
cluster().getOzoneManager().getMetadataManager();
String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
assertEquals(BucketLayout.FILE_SYSTEM_OPTIMIZED, metadataManager
.getBucketTable().get(bucketKey).getBucketLayout());

String parentDir = "parentToDelete";
String childKeyName = parentDir + "/" + keyName;

// Initiate the MPU under a parent directory. This creates the parent
// directory and charges the bucket namespace quota by 1.
String uploadID = initiateMultipartUploadWithAsserts(bucket, childKeyName,
RATIS, ONE);
Pair<String, String> partNameAndETag = uploadPart(bucket, childKeyName,
uploadID, 1, "data".getBytes(UTF_8));

// Delete the parent directory, reverting the namespace charge back to 0.
ozClient.getProxy().deleteKey(volumeName, bucketName, parentDir + "/",
false);
GenericTestUtils.waitFor(() -> getDurableUsedNamespace(bucketKey) == 0L,
100, 30_000);

// Complete the MPU with an invalid part ETag. The complete first recreates
// the now-missing parent directory in the cache (charging the namespace by
// 1), then fails validation with INVALID_PART.
TreeMap<Integer, String> partsMap = new TreeMap<>();
partsMap.put(1, partNameAndETag.getValue() + "-invalid");
OzoneTestUtils.expectOmException(OMException.ResultCodes.INVALID_PART,
() -> completeMultipartUpload(bucket, childKeyName, uploadID,
partsMap));

// The failed complete must not leak namespace quota. The cached bucket
// usedNamespace must match the durable value (0). Before the fix, the
// in-place incrUsedNamespace done while recreating the parent was never
// reverted on the failure path, leaving the cached bucket at 1 with no
// backing object.
long durableUsedNamespace = getDurableUsedNamespace(bucketKey);
long liveUsedNamespace =
metadataManager.getBucketTable().get(bucketKey).getUsedNamespace();
assertEquals(0L, durableUsedNamespace);
assertEquals(0L, liveUsedNamespace,
"Failed CompleteMultipartUpload leaked bucket namespace quota");
}

private long getDurableUsedNamespace(String bucketKey) {
try {
return cluster().getOzoneManager().getMetadataManager().getBucketTable()
.getSkipCache(bucketKey).getUsedNamespace();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Test
public void testMultipartPartNumberExceedingAllowedRange() throws Exception {
String uploadID = initiateMultipartUploadWithAsserts(bucket, keyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ public static long sumBlockLengths(OmKeyInfo omKeyInfo) {

/**
* Return bucket info for the specified bucket.
* <p>
* The returned {@link OmBucketInfo} is the cached instance, returned by
* reference. A caller that mutates it (for example quota accounting) before a
* point where the request may still fail must first take a
* {@link OmBucketInfo#copyObject()} and publish that copy only on success,
* otherwise a failed request leaks the mutation into the cache.
*/
@Nullable
public static OmBucketInfo getBucketInfo(OMMetadataManager omMetadataManager,

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 used in 25 places. From what I can tell, at least OMDirectoryCreateRequest has the same potential problem. I think we need more guardrails to prevent this same bug from happening again.

A long term solution could include:

  • Create two separate getters: getBucketInfoForUpdate() and getBucketInfoReadOnly().
  • getBucketInfoForUpdate() returns a copy of the object.
  • Return an immutable/view interface - Define ReadOnlyBucketInfo with getters only.
    getBucketInfoReadOnly() returns ReadOnlyBucketInfo, not OmBucketInfo.
    Mutation methods (incrUsedBytes, setters, etc.) are unavailable at compile time.

@ivandika3 ivandika3 Aug 12, 2026

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.

We can pursue OmBucketInfo immutability https://issues.apache.org/jira/browse/HDDS-10317, mutable OmBucketInfo and OmKeyInfo has been a source of some bugs.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
acquiredLock = getOmLockDetails().isLockAcquired();

validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
// Work on a copy of the cached bucket so the namespace charge for
// recreating missing FSO parent directories (applied before parts are
// validated) is published only on success; a complete that fails with
// INVALID_PART must not leak it into the cache. See getBucketInfo.
OmBucketInfo omBucketInfo = getBucketInfo(omMetadataManager,
volumeName, bucketName);
if (omBucketInfo != null) {
omBucketInfo = omBucketInfo.copyObject();
}

List<OmDirectoryInfo> missingParentInfos;
OMFileRequest.OMPathInfoWithFSO pathInfoFSO = OMFileRequest
Expand Down
Loading