Skip to content

Commit fd0d14e

Browse files
committed
Changes based on PR review
1 parent ad210ef commit fd0d14e

File tree

7 files changed

+97
-24
lines changed

7 files changed

+97
-24
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,22 @@ void setMaxBackoffIntervalMilliseconds(int maxBackoffInterval) {
15281528
this.maxBackoffInterval = maxBackoffInterval;
15291529
}
15301530

1531+
/**
1532+
* Sets the namespace enabled account flag.
1533+
*
1534+
* @param isNamespaceEnabledAccount boolean value indicating if the account is namespace enabled.
1535+
*/
1536+
void setIsNamespaceEnabledAccount(boolean isNamespaceEnabledAccount) {
1537+
this.isNamespaceEnabled = Trilean.getTrilean(isNamespaceEnabledAccount);
1538+
}
1539+
1540+
/**
1541+
* Sets the namespace enabled account flag for testing purposes.
1542+
*
1543+
* @param isNamespaceEnabledAccount Trilean value indicating if the account is namespace enabled.
1544+
*/
15311545
@VisibleForTesting
1532-
public void setIsNamespaceEnabledAccount(Trilean isNamespaceEnabledAccount) {
1546+
void setIsNamespaceEnabledAccountForTesting(Trilean isNamespaceEnabledAccount) {
15331547
this.isNamespaceEnabled = isNamespaceEnabledAccount;
15341548
}
15351549

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,28 @@ public boolean getIsNamespaceEnabled(TracingContext tracingContext)
394394

395395
private synchronized boolean getNamespaceEnabledInformationFromServer(
396396
final TracingContext tracingContext) throws AzureBlobFileSystemException {
397-
if (abfsConfiguration.getIsNamespaceEnabledAccount() != Trilean.UNKNOWN) {
397+
if (getAbfsConfiguration().getIsNamespaceEnabledAccount() != Trilean.UNKNOWN) {
398398
return isNamespaceEnabled();
399399
}
400400
try {
401401
LOG.debug("Get root ACL status");
402402
getClient(AbfsServiceType.DFS).getAclStatus(AbfsHttpConstants.ROOT_PATH, tracingContext);
403403
// If getAcl succeeds, namespace is enabled.
404-
setNamespaceEnabled(Trilean.TRUE);
404+
setNamespaceEnabled(true);
405405
} catch (AbfsRestOperationException ex) {
406406
// Get ACL status is a HEAD request, its response doesn't contain errorCode
407407
// So can only rely on its status code to determine account type.
408408
if (HttpURLConnection.HTTP_BAD_REQUEST != ex.getStatusCode()) {
409409
// If getAcl fails with anything other than 400, namespace is enabled.
410-
setNamespaceEnabled(Trilean.TRUE);
410+
setNamespaceEnabled(true);
411411
// Continue to throw exception as earlier.
412412
LOG.debug("Failed to get ACL status with non 400. Inferring namespace enabled", ex);
413413
throw ex;
414414
}
415415
// If getAcl fails with 400, namespace is disabled.
416416
LOG.debug("Failed to get ACL status with 400. "
417417
+ "Inferring namespace disabled and ignoring error", ex);
418-
setNamespaceEnabled(Trilean.FALSE);
418+
setNamespaceEnabled(false);
419419
} catch (AzureBlobFileSystemException ex) {
420420
throw ex;
421421
}
@@ -428,7 +428,7 @@ private synchronized boolean getNamespaceEnabledInformationFromServer(
428428
*/
429429
@VisibleForTesting
430430
boolean isNamespaceEnabled() throws TrileanConversionException {
431-
return abfsConfiguration.getIsNamespaceEnabledAccount().toBoolean();
431+
return getAbfsConfiguration().getIsNamespaceEnabledAccount().toBoolean();
432432
}
433433

434434
@VisibleForTesting
@@ -2011,9 +2011,8 @@ DataBlocks.BlockFactory getBlockFactory() {
20112011
return blockFactory;
20122012
}
20132013

2014-
@VisibleForTesting
2015-
void setNamespaceEnabled(Trilean isNamespaceEnabled){
2016-
abfsConfiguration.setIsNamespaceEnabledAccount(isNamespaceEnabled);
2014+
void setNamespaceEnabled(boolean isNamespaceEnabled){
2015+
getAbfsConfiguration().setIsNamespaceEnabledAccount(isNamespaceEnabled);
20172016
}
20182017

20192018
@VisibleForTesting

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,10 +1720,10 @@ protected String getUserAgent() {
17201720
*/
17211721
public boolean getIsNamespaceEnabled() throws AbfsDriverException {
17221722
try {
1723-
return abfsConfiguration.getIsNamespaceEnabledAccount().toBoolean();
1723+
return getAbfsConfiguration().getIsNamespaceEnabledAccount().toBoolean();
17241724
} catch (TrileanConversionException ex) {
17251725
LOG.error("Failed to convert namespace enabled account property to boolean", ex);
1726-
throw new AbfsDriverException("Failed to determine if namespace is enabled", ex);
1726+
throw new AbfsDriverException("Failed to determine account type", ex);
17271727
}
17281728
}
17291729

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ private void setTestUserFs() throws Exception {
102102
conf.setBoolean(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, isHNSEnabled);
103103
this.testUserFs = FileSystem.newInstance(conf);
104104
// Resetting the namespace enabled flag to unknown after file system init.
105-
((AzureBlobFileSystem) testUserFs).getAbfsStore().setNamespaceEnabled(
106-
Trilean.UNKNOWN);
105+
((AzureBlobFileSystem) testUserFs).getAbfsStore()
106+
.getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
107107
}
108108

109109
private void setTestFsConf(final String fsConfKey,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void testGetAclCallOnHnsConfigAbsence() throws Exception {
8282
AzureBlobFileSystemStore store = Mockito.spy(fs.getAbfsStore());
8383
AbfsClient client = Mockito.spy(fs.getAbfsStore().getClient(AbfsServiceType.DFS));
8484
Mockito.doReturn(client).when(store).getClient(AbfsServiceType.DFS);
85-
store.setNamespaceEnabled(Trilean.UNKNOWN);
85+
store.getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
8686

8787
TracingContext tracingContext = getSampleTracingContext(fs, true);
8888
Mockito.doReturn(Mockito.mock(AbfsRestOperation.class))

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

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
3030
import org.apache.hadoop.fs.FileSystem;
3131
import org.apache.hadoop.fs.azurebfs.constants.AbfsServiceType;
32+
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsDriverException;
3233
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException;
3334
import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
3435
import org.apache.hadoop.fs.azurebfs.services.AbfsDfsClient;
@@ -159,7 +160,8 @@ public void testFailedRequestWhenFSNotExist() throws Exception {
159160
+ testUri.substring(testUri.indexOf("@"));
160161
config.setBoolean(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, isUsingXNSAccount);
161162
AzureBlobFileSystem fs = this.getFileSystem(nonExistingFsUrl);
162-
fs.getAbfsStore().setNamespaceEnabled(Trilean.UNKNOWN);
163+
fs.getAbfsStore().getAbfsConfiguration()
164+
.setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
163165

164166
FileNotFoundException ex = intercept(FileNotFoundException.class, ()-> {
165167
fs.getFileStatus(new Path("/")); // Run a dummy FS call
@@ -207,7 +209,8 @@ public void testEnsureGetAclCallIsMadeOnceWhenConfigIsNotPresent()
207209
private void ensureGetAclCallIsMadeOnceForInvalidConf(String invalidConf)
208210
throws Exception {
209211
this.getFileSystem().getAbfsStore()
210-
.setNamespaceEnabled(Trilean.getTrilean(invalidConf));
212+
.getAbfsConfiguration()
213+
.setIsNamespaceEnabledAccountForTesting(Trilean.getTrilean(invalidConf));
211214
AbfsClient mockClient =
212215
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient();
213216
verify(mockClient, times(1))
@@ -217,15 +220,17 @@ private void ensureGetAclCallIsMadeOnceForInvalidConf(String invalidConf)
217220
private void ensureGetAclCallIsNeverMadeForValidConf(String validConf)
218221
throws Exception {
219222
this.getFileSystem().getAbfsStore()
220-
.setNamespaceEnabled(Trilean.getTrilean(validConf));
223+
.getAbfsConfiguration()
224+
.setIsNamespaceEnabledAccountForTesting(Trilean.getTrilean(validConf));
221225
AbfsClient mockClient =
222226
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient();
223227
verify(mockClient, never())
224228
.getAclStatus(anyString(), any(TracingContext.class));
225229
}
226230

227231
private void unsetConfAndEnsureGetAclCallIsMadeOnce() throws IOException {
228-
this.getFileSystem().getAbfsStore().setNamespaceEnabled(Trilean.UNKNOWN);
232+
this.getFileSystem().getAbfsStore()
233+
.getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
229234
AbfsClient mockClient =
230235
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient();
231236
verify(mockClient, times(1))
@@ -262,7 +267,7 @@ private void ensureGetAclDetermineHnsStatusAccuratelyInternal(int statusCode,
262267
boolean expectedValue, boolean isExceptionExpected) throws Exception {
263268
AzureBlobFileSystemStore store = Mockito.spy(getFileSystem().getAbfsStore());
264269
AbfsClient mockClient = mock(AbfsClient.class);
265-
store.setNamespaceEnabled(Trilean.UNKNOWN);
270+
store.getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
266271
doReturn(mockClient).when(store).getClient(AbfsServiceType.DFS);
267272
AbfsRestOperationException ex = new AbfsRestOperationException(
268273
statusCode, null, Integer.toString(statusCode), null);
@@ -354,9 +359,7 @@ public void testAccountSpecificConfig() throws Exception {
354359
*/
355360
@Test
356361
public void testNameSpaceConfig() throws Exception {
357-
Configuration configuration = getRawConfiguration();
358-
configuration.unset(FS_AZURE_ACCOUNT_IS_HNS_ENABLED);
359-
configuration.unset(accountProperty(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, this.getAccountName()));
362+
Configuration configuration = getConfigurationWithoutHnsConfig();
360363
AzureBlobFileSystem abfs = (AzureBlobFileSystem) FileSystem.newInstance(configuration);
361364
AbfsConfiguration abfsConfig = new AbfsConfiguration(configuration, "bogusAccountName");
362365

@@ -414,6 +417,63 @@ public void testNameSpaceConfig() throws Exception {
414417
.isEqualTo(false);
415418
}
416419

420+
/**
421+
* Tests to check that the namespace configuration is set correctly
422+
* during the initialization of the AzureBlobFileSystem.
423+
*
424+
*
425+
* @throws Exception if any error occurs during configuration setup or evaluation
426+
*/
427+
@Test
428+
public void testFsInitShouldSetNamespaceConfig() throws Exception {
429+
// Mock the AzureBlobFileSystem and its dependencies
430+
AzureBlobFileSystem mockFileSystem = Mockito.spy((AzureBlobFileSystem)
431+
FileSystem.newInstance(getConfigurationWithoutHnsConfig()));
432+
AzureBlobFileSystemStore mockStore = Mockito.spy(mockFileSystem.getAbfsStore());
433+
AbfsClient abfsClient = Mockito.spy(mockStore.getClient());
434+
Mockito.doReturn(abfsClient).when(mockStore).getClient();
435+
Mockito.doReturn(abfsClient).when(mockStore).getClient(any());
436+
abfsClient.getIsNamespaceEnabled();
437+
// Verify that getAclStatus is called once during initialization
438+
Mockito.verify(abfsClient, times(0))
439+
.getAclStatus(anyString(), any(TracingContext.class));
440+
441+
mockStore.getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
442+
// In case someone wrongly configured the namespace in between the processing,
443+
// abfsclient.getIsNamespaceEnabled() should throw an exception.
444+
String errorMessage = intercept(AbfsDriverException.class, () -> {
445+
abfsClient.getIsNamespaceEnabled();
446+
}).getMessage();
447+
Assertions.assertThat(errorMessage)
448+
.describedAs("Client should throw exception when namespace is unknown")
449+
.contains("Failed to determine account type");
450+
451+
// In case of unknown namespace, store's getIsNamespaceEnabled should call getAclStatus
452+
// to determine the namespace status.
453+
mockStore.getIsNamespaceEnabled(getTestTracingContext(mockFileSystem, false));
454+
Mockito.verify(abfsClient, times(1))
455+
.getAclStatus(anyString(), any(TracingContext.class));
456+
457+
abfsClient.getIsNamespaceEnabled();
458+
// Verify that client's getNamespaceEnabled will not call getAclStatus again
459+
Mockito.verify(abfsClient, times(1))
460+
.getAclStatus(anyString(), any(TracingContext.class));
461+
}
462+
463+
/**
464+
* Returns the configuration without the HNS config set.
465+
*
466+
* @return Configuration without HNS config
467+
*/
468+
private Configuration getConfigurationWithoutHnsConfig() {
469+
Configuration configuration = getRawConfiguration();
470+
configuration.unset(FS_AZURE_ACCOUNT_IS_HNS_ENABLED);
471+
configuration.unset(accountProperty(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, this.getAccountName()));
472+
configuration.setBoolean(AZURE_CREATE_REMOTE_FILESYSTEM_DURING_INITIALIZATION, true);
473+
configuration.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, getTestUrl());
474+
return configuration;
475+
}
476+
417477
private void assertFileSystemInitWithExpectedHNSSettings(
418478
Configuration configuration, boolean expectedIsHnsEnabledValue) throws IOException {
419479
try (AzureBlobFileSystem fs = (AzureBlobFileSystem) FileSystem.newInstance(configuration)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ public void testExternalOps() throws Exception {
200200
0));
201201

202202
// unset namespaceEnabled to call getAcl -> trigger tracing header validator
203-
fs.getAbfsStore().setNamespaceEnabled(Trilean.UNKNOWN);
203+
fs.getAbfsStore().getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.UNKNOWN);
204204
fs.hasPathCapability(new Path("/"), CommonPathCapabilities.FS_ACLS);
205205

206206
Assume.assumeTrue(getIsNamespaceEnabled(getFileSystem()));
207207
Assume.assumeTrue(getConfiguration().isCheckAccessEnabled());
208208
Assume.assumeTrue(getAuthType() == AuthType.OAuth);
209209

210210
fs.setListenerOperation(FSOperationType.ACCESS);
211-
fs.getAbfsStore().setNamespaceEnabled(Trilean.TRUE);
211+
fs.getAbfsStore().getAbfsConfiguration().setIsNamespaceEnabledAccountForTesting(Trilean.TRUE);
212212
fs.access(new Path("/"), FsAction.READ);
213213
}
214214

0 commit comments

Comments
 (0)