Skip to content

Commit 9123ad0

Browse files
author
Anuj Modi
committed
Refactor getAcl
1 parent 6545b7e commit 9123ad0

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystemStore.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,21 @@ private synchronized boolean getNamespaceEnabledInformationFromServer(
395395
try {
396396
LOG.debug("Get root ACL status");
397397
getClient().getAclStatus(AbfsHttpConstants.ROOT_PATH, tracingContext);
398+
// If getAcl succeeds, namespace is enabled.
398399
isNamespaceEnabled = Trilean.getTrilean(true);
399400
} catch (AbfsRestOperationException ex) {
400-
// Get ACL status is a HEAD request, its response doesn't contain
401-
// errorCode
401+
// Get ACL status is a HEAD request, its response doesn't contain errorCode
402402
// So can only rely on its status code to determine its account type.
403-
if (HttpURLConnection.HTTP_BAD_REQUEST != ex.getStatusCode()) {
403+
if (HttpURLConnection.HTTP_BAD_REQUEST == ex.getStatusCode()) {
404+
// If getAcl fails with 400, namespace is disabled.
405+
isNamespaceEnabled = Trilean.getTrilean(false);
406+
} else if (HttpURLConnection.HTTP_NOT_FOUND == ex.getStatusCode()) {
407+
// If getAcl fails with 404, namespace is enabled.
408+
isNamespaceEnabled = Trilean.getTrilean(true);
409+
} else {
410+
// Any other server error, throw exception.
404411
throw ex;
405412
}
406-
isNamespaceEnabled = Trilean.getTrilean(false);
407413
} catch (AzureBlobFileSystemException ex) {
408414
throw ex;
409415
}

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestGetNameSpaceEnabled.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@
2424
import org.junit.Assume;
2525
import org.junit.Test;
2626
import org.assertj.core.api.Assertions;
27+
import org.mockito.Mockito;
2728

2829
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
2930
import org.apache.hadoop.fs.FileSystem;
31+
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException;
3032
import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
3133
import org.apache.hadoop.fs.azurebfs.services.AbfsRestOperation;
3234
import org.apache.hadoop.conf.Configuration;
3335
import org.apache.hadoop.fs.Path;
3436
import org.apache.hadoop.fs.azurebfs.enums.Trilean;
3537
import org.apache.hadoop.fs.azurebfs.utils.TracingContext;
38+
import org.apache.kerby.config.Conf;
3639

3740
import static org.mockito.ArgumentMatchers.any;
3841
import static org.mockito.ArgumentMatchers.anyString;
3942
import static org.mockito.Mockito.doReturn;
43+
import static org.mockito.Mockito.doThrow;
4044
import static org.mockito.Mockito.mock;
4145
import static org.mockito.Mockito.never;
4246
import static org.mockito.Mockito.times;
@@ -48,6 +52,7 @@
4852
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ACCOUNT_IS_HNS_ENABLED;
4953
import static org.apache.hadoop.fs.azurebfs.constants.TestConfigurationKeys.FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT;
5054
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
55+
import static org.mockito.Mockito.when;
5156

5257
/**
5358
* Test getIsNamespaceEnabled call.
@@ -217,4 +222,31 @@ private AbfsClient callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient()
217222
return mockClient;
218223
}
219224

225+
@Test
226+
public void ensureGetAclDetermineHnsStatusAccurately() throws Exception {
227+
ensureGetAclDetermineHnsStatusAccuratelyInternal(400, false);
228+
ensureGetAclDetermineHnsStatusAccuratelyInternal(404, true);
229+
ensureGetAclDetermineHnsStatusAccuratelyInternal(500, false);
230+
ensureGetAclDetermineHnsStatusAccuratelyInternal(503, false);
231+
}
232+
233+
private void ensureGetAclDetermineHnsStatusAccuratelyInternal(int statusCode,
234+
boolean expected) throws Exception {
235+
AzureBlobFileSystemStore store = Mockito.spy(getFileSystem().getAbfsStore());
236+
AbfsClient mockClient = mock(AbfsClient.class);
237+
store.setNamespaceEnabled(Trilean.UNKNOWN);
238+
doReturn(mockClient).when(store).getClient();
239+
AbfsRestOperationException ex = new AbfsRestOperationException(
240+
statusCode, null, Integer.toString(statusCode), null);
241+
doThrow(ex).when(mockClient).getAclStatus(anyString(), any(TracingContext.class));
242+
243+
try {
244+
boolean isHnsEnabled = store.getIsNamespaceEnabled(
245+
getTestTracingContext(getFileSystem(), false));
246+
Assertions.assertThat(isHnsEnabled).isEqualTo(expected);
247+
} catch (AbfsRestOperationException caughtEx) {
248+
Assertions.assertThat(caughtEx.getStatusCode()).isEqualTo(statusCode);
249+
Assertions.assertThat(caughtEx.getErrorMessage()).isEqualTo(ex.getErrorMessage());
250+
}
251+
}
220252
}

0 commit comments

Comments
 (0)