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 d5cea6766e2 HDDS-17198. Reject tenant creation on a volume already 
used by another tenant (#11847)
d5cea6766e2 is described below

commit d5cea6766e2bc155b65fedafa69ce11d2278287f
Author: Chi-Hsuan Huang <[email protected]>
AuthorDate: Fri Nov 6 14:53:48 2026 +0800

    HDDS-17198. Reject tenant creation on a volume already used by another 
tenant (#11847)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../request/s3/tenant/OMTenantCreateRequest.java   | 13 ++++++
 .../s3/tenant/TestOMTenantCreateRequest.java       | 54 ++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/s3/tenant/OMTenantCreateRequest.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/s3/tenant/OMTenantCreateRequest.java
index 792ba749db4..313cae88e17 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/s3/tenant/OMTenantCreateRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/s3/tenant/OMTenantCreateRequest.java
@@ -20,6 +20,7 @@
 import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.TENANT_ALREADY_EXISTS;
 import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.USER_NOT_FOUND;
 import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_ALREADY_EXISTS;
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_IS_REFERENCED;
 import static 
org.apache.hadoop.ozone.om.lock.OzoneManagerLock.LeveledResource.USER_LOCK;
 import static 
org.apache.hadoop.ozone.om.lock.OzoneManagerLock.LeveledResource.VOLUME_LOCK;
 import static 
org.apache.hadoop.ozone.om.upgrade.OMLayoutFeature.MULTITENANCY_SCHEMA;
@@ -172,6 +173,14 @@ public OMRequest preExecute(OzoneManager ozoneManager) 
throws IOException {
       throw new OMException("Volume already exists", VOLUME_ALREADY_EXISTS);
     }
 
+    // A volume can only be used by one tenant
+    if (forceCreationWhenVolumeExists) {
+      final OmVolumeArgs existingVolumeArgs = 
ozoneManager.getMetadataManager().getVolumeTable().get(dbVolumeKey);
+      if (existingVolumeArgs != null && existingVolumeArgs.getRefCount() != 
0L) {
+        throw new OMException("Volume '" + volumeName + "' is already used by 
a tenant", VOLUME_IS_REFERENCED);
+      }
+    }
+
     // TODO: Refactor this and OMVolumeCreateRequest to improve 
maintainability.
     final VolumeInfo volumeInfo = VolumeInfo.newBuilder()
         .setVolume(volumeName)
@@ -270,6 +279,10 @@ public OMClientResponse 
validateAndUpdateCache(OzoneManager ozoneManager, Execut
       if (omMetadataManager.getVolumeTable().isExist(dbVolumeKey)) {
         LOG.debug("volume: '{}' already exists", volumeName);
         if (forceCreationWhenVolumeExists) {
+          // A volume can only be used by one tenant
+          if (getVolumeInfo(omMetadataManager, volumeName).getRefCount() != 
0L) {
+            throw new OMException("Volume '" + volumeName + "' is already used 
by a tenant", VOLUME_IS_REFERENCED);
+          }
           LOG.warn("forceCreationWhenVolumeExists = true. Resuming "
               + "tenant creation despite volume '{}' existence", volumeName);
           skipVolumeCreation = true;
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/s3/tenant/TestOMTenantCreateRequest.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/s3/tenant/TestOMTenantCreateRequest.java
index 85cc1896fc7..9397a0083c0 100644
--- 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/s3/tenant/TestOMTenantCreateRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/s3/tenant/TestOMTenantCreateRequest.java
@@ -18,16 +18,23 @@
 package org.apache.hadoop.ozone.om.request.s3.tenant;
 
 import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_ALREADY_EXISTS;
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_IS_REFERENCED;
+import static 
org.apache.hadoop.ozone.om.lock.OzoneManagerLock.LeveledResource.VOLUME_LOCK;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.framework;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.io.IOException;
@@ -253,6 +260,39 @@ public void testValidateAndUpdateCacheWhenVolumeExists() 
throws Exception {
     assertEquals(Status.OK, omResponse.getStatus());
     assertNotNull(omMetadataManager.getVolumeTable().get(
         omMetadataManager.getVolumeKey(tenantId)));
+    assertEquals(1L, 
omMetadataManager.getVolumeTable().get(omMetadataManager.getVolumeKey(tenantId)).getRefCount());
+  }
+
+  @Test
+  public void testRejectForceCreationOnVolumeUsedByAnotherTenant() throws 
Exception {
+    acceptTenantIdCreationHelper("tenanta");
+
+    OMException omException = assertThrows(OMException.class, () -> 
doPreExecute("tenantb", "tenanta", true));
+    assertEquals(VOLUME_IS_REFERENCED, omException.getResult());
+    verify(ozoneManager.getMultiTenantManager().getAuthorizerOp(), 
never()).createTenant(eq("tenantb"), any(), any());
+
+    // Without force, the existing volume check still comes first
+    omException = assertThrows(OMException.class, () -> 
doPreExecute("tenantb", "tenanta", false));
+    assertEquals(VOLUME_ALREADY_EXISTS, omException.getResult());
+  }
+
+  @Test
+  public void 
testRejectForceCreationWhenVolumeBecomesReferencedAfterPreExecute() throws 
Exception {
+    OMRequestTestUtils.addVolumeToDB("vol", "username", omMetadataManager);
+
+    // Both requests pass preExecute before either is applied
+    OMTenantCreateRequest request1 = doPreExecute("tenant1", "vol", true);
+    OMTenantCreateRequest request2 = doPreExecute("tenant2", "vol", true);
+
+    assertEquals(Status.OK, request1.validateAndUpdateCache(ozoneManager, 
2L).getOMResponse().getStatus());
+
+    OMResponse omResponse = request2.validateAndUpdateCache(ozoneManager, 
3L).getOMResponse();
+    assertEquals(Status.VOLUME_IS_REFERENCED, omResponse.getStatus());
+    assertEquals("Volume 'vol' is already used by a tenant", 
omResponse.getMessage());
+    assertEquals(1L, 
omMetadataManager.getVolumeTable().get(omMetadataManager.getVolumeKey("vol")).getRefCount());
+    assertFalse(omMetadataManager.getTenantStateTable().isExist("tenant2"));
+    
assertFalse(omMetadataManager.getLock().isWriteLockedByCurrentThread(VOLUME_LOCK,
 "vol"));
+    verify(ozoneManager.getMultiTenantManager().getAuthorizerLock(), 
times(2)).unlockWriteInOMRequest();
   }
 
   @Test
@@ -326,6 +366,20 @@ private void doPreExecute(String tenantId) throws 
Exception {
     omTenantCreateRequest.preExecute(ozoneManager);
   }
 
+  // Runs preExecute for a tenant on the given volume and returns the request 
to apply
+  private OMTenantCreateRequest doPreExecute(String tenantId, String 
volumeName,
+      boolean forceCreationWhenVolumeExists) throws Exception {
+    OMRequest originalRequest = 
OMRequestTestUtils.createTenantRequest(tenantId, forceCreationWhenVolumeExists);
+    originalRequest = originalRequest.toBuilder()
+        
.setCreateTenantRequest(originalRequest.getCreateTenantRequest().toBuilder().setVolumeName(volumeName))
+        .build();
+
+    OMTenantCreateRequest omTenantCreateRequest = spy(new 
OMTenantCreateRequest(originalRequest));
+    doReturn("username").when(omTenantCreateRequest).getUserName();
+
+    return new 
OMTenantCreateRequest(omTenantCreateRequest.preExecute(ozoneManager));
+  }
+
   @Test
   public void preExecutePermissionDeniedWhenAclEnabled() throws Exception {
     when(ozoneManager.getAclsEnabled()).thenReturn(true);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to