Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -150,13 +150,7 @@ public void initialize(URI uri, Configuration configuration)
if (abfsConfiguration.getCreateRemoteFileSystemDuringInitialization()) {
TracingContext tracingContext = new TracingContext(clientCorrelationId,
fileSystemId, FSOperationType.CREATE_FILESYSTEM, tracingHeaderFormat, listener);
if (this.tryGetFileStatus(new Path(AbfsHttpConstants.ROOT_PATH), tracingContext) == null) {
try {
this.createFileSystem(tracingContext);
} catch (AzureBlobFileSystemException ex) {
checkException(null, ex, AzureServiceErrorCode.FILE_SYSTEM_ALREADY_EXISTS);
}
}
this.createFileSystemIfNotExist(tracingContext);
}

LOG.trace("Initiate check for delegation token manager");
Expand Down Expand Up @@ -1411,6 +1405,30 @@ AzureBlobFileSystemStore getAbfsStore() {
return abfsStore;
}

@VisibleForTesting
void setAbfsStore(AzureBlobFileSystemStore abfsStore) {
this.abfsStore = abfsStore;
}

@VisibleForTesting
void createFileSystemIfNotExist(TracingContext tracingContext) throws IOException {
if (this.tryGetFileStatus(new Path(AbfsHttpConstants.ROOT_PATH), tracingContext) == null) {
try {
this.createFileSystem(tracingContext);
} catch (IOException ex) {
if (ex instanceof AzureBlobFileSystemException) {

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.

better to add a catch(AzureBlobFileSystemException) clause

checkException(null, (AzureBlobFileSystemException) ex,
AzureServiceErrorCode.FILE_SYSTEM_ALREADY_EXISTS);
} else if (ex.getCause() != null && ex.getCause() instanceof AzureBlobFileSystemException) {

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.

instanceof always returns false if source is null, so the null check here can be removed

checkException(null, (AzureBlobFileSystemException) ex.getCause(),
AzureServiceErrorCode.FILE_SYSTEM_ALREADY_EXISTS);
} else {
throw ex;
}
}
}
}

@VisibleForTesting
AbfsClient getAbfsClient() {
return abfsStore.getClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
package org.apache.hadoop.fs.azurebfs;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException;
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AzureBlobFileSystemException;
import org.apache.hadoop.fs.azurebfs.contracts.services.AzureServiceErrorCode;
import org.apache.hadoop.fs.azurebfs.utils.TracingContext;
import org.junit.Test;

import org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys;
Expand Down Expand Up @@ -49,4 +56,39 @@ public void ensureFilesystemWillNotBeCreatedIfCreationConfigIsNotSet() throws Ex
final AzureBlobFileSystem fs = this.createFileSystem();
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
}

@Test
public void ensureFilesystemWillBeCreatedIfCreationConfigIsSet() throws Exception {
final AzureBlobFileSystem fs = createFileSystem();

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.

use try with resources to close afterwards

Configuration config = getRawConfiguration();
fs.initialize(FileSystem.getDefaultUri(config), config);

// Make sure createFileSystemIfNotExists is working as intended.
final MockAzureBlobFileSystemStore store = new MockAzureBlobFileSystemStore(config);
fs.setAbfsStore(store);
fs.createFileSystemIfNotExist(getTestTracingContext(fs, true));
assert(store.isCreateFileSystemCalled);

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.

assertj assertions with useful error text

}

/**
* Mock AzureBlobFileSystemStore to simulate container already exists
* exception when calling createFileSystem command.
*/
static class MockAzureBlobFileSystemStore extends AzureBlobFileSystemStore {
boolean isCreateFileSystemCalled = false;

public MockAzureBlobFileSystemStore(Configuration config) throws IOException {
super(FileSystem.getDefaultUri(config), true, config, null);
}

@Override
public void createFilesystem(TracingContext tracingContext) throws AzureBlobFileSystemException {
isCreateFileSystemCalled = true;
// Make sure createFileSystemIfNotExists works when the filesystem/container already exists.
throw new AbfsRestOperationException(
AzureServiceErrorCode.FILE_SYSTEM_ALREADY_EXISTS.getStatusCode(),
AzureServiceErrorCode.FILE_SYSTEM_ALREADY_EXISTS.getErrorCode(),
"This container is already exists", null);
}
}
}