Skip to content

Commit

Permalink
Remove IOException wrappers from ObjectIO impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimas-b committed Jul 15, 2024
1 parent 43687ba commit 5897903
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,28 @@
import java.util.stream.Collectors;
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.storage.uri.StorageUri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AdlsObjectIO implements ObjectIO {
private static final Logger LOGGER = LoggerFactory.getLogger(AdlsObjectIO.class);

private final AdlsClientSupplier clientSupplier;

public AdlsObjectIO(AdlsClientSupplier clientSupplier) {
this.clientSupplier = clientSupplier;
}

@Override
public void ping(StorageUri uri) throws IOException {
public void ping(StorageUri uri) {
AdlsLocation location = adlsLocation(uri);

DataLakeFileSystemClient fileSystem = clientSupplier.fileSystemClient(location);
try {
fileSystem.getAccessPolicy();
} catch (Exception e) {
throw new IOException(e);
}
fileSystem.getAccessPolicy();
}

@Override
public InputStream readObject(StorageUri uri) throws IOException {
DataLakeFileClient file = clientSupplier.fileClientForLocation(uri);
DataLakeFileInputStreamOptions options = new DataLakeFileInputStreamOptions();
clientSupplier.adlsOptions().readBlockSize().ifPresent(options::setBlockSize);
try {
return file.openInputStream(options).getInputStream();
} catch (BlobStorageException e) {
if (e.getStatusCode() == 404) {
throw new IOException(e.getServiceMessage(), e);
}
throw e;
}
return file.openInputStream(options).getInputStream();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobWriteOption;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
Expand All @@ -35,11 +34,8 @@
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.catalog.secrets.KeySecret;
import org.projectnessie.storage.uri.StorageUri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GcsObjectIO implements ObjectIO {
private static final Logger LOGGER = LoggerFactory.getLogger(GcsObjectIO.class);

private final GcsStorageSupplier storageSupplier;

Expand All @@ -48,16 +44,12 @@ public GcsObjectIO(GcsStorageSupplier storageSupplier) {
}

@Override
public void ping(StorageUri uri) throws IOException {
public void ping(StorageUri uri) {
GcsLocation location = gcsLocation(uri);
GcsBucketOptions bucketOptions = storageSupplier.bucketOptions(location);
@SuppressWarnings("resource")
Storage client = storageSupplier.forLocation(bucketOptions);
try {
client.get(BlobId.of(uri.requiredAuthority(), uri.requiredPath()));
} catch (RuntimeException e) {
throw new IOException(e);
}
client.get(BlobId.of(uri.requiredAuthority(), uri.requiredPath()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,24 @@ public S3ObjectIO(S3ClientSupplier s3clientSupplier) {
}

@Override
public void ping(StorageUri uri) throws IOException {
public void ping(StorageUri uri) {
S3Client s3client = s3clientSupplier.getClient(uri);
try {
s3client.headBucket(b -> b.bucket(uri.requiredAuthority()));
} catch (RuntimeException e) {
throw new IOException(e);
}
s3client.headBucket(b -> b.bucket(uri.requiredAuthority()));
}

@Override
public InputStream readObject(StorageUri uri) throws IOException {
public InputStream readObject(StorageUri uri) {
checkArgument(uri != null, "Invalid location: null");
String scheme = uri.scheme();
checkArgument(isS3scheme(scheme), "Invalid S3 scheme: %s", uri);

S3Client s3client = s3clientSupplier.getClient(uri);

try {
return s3client.getObject(
GetObjectRequest.builder()
.bucket(uri.requiredAuthority())
.key(withoutLeadingSlash(uri))
.build());
} catch (Exception e) {
throw new IOException(e);
}
return s3client.getObject(
GetObjectRequest.builder()
.bucket(uri.requiredAuthority())
.key(withoutLeadingSlash(uri))
.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.projectnessie.objectstoragemock.HeapStorageBucket.newHeapStorageBucket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.projectnessie.catalog.files.api.BackendErrorCode;
import org.projectnessie.catalog.files.api.BackendErrorStatus;
import org.projectnessie.catalog.files.api.BackendExceptionMapper;
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.objectstoragemock.Bucket;
import org.projectnessie.objectstoragemock.MockObject;
Expand All @@ -36,10 +40,16 @@
@ExtendWith(SoftAssertionsExtension.class)
public abstract class AbstractClients {
@InjectSoftAssertions protected SoftAssertions soft;
protected BackendExceptionMapper exceptionMapper;

public static final String BUCKET_1 = "bucket1";
public static final String BUCKET_2 = "bucket2";

@BeforeEach
protected void buildExceptionMapper() {
exceptionMapper = addExceptionHandlers(BackendExceptionMapper.builder()).build();
}

@SuppressWarnings("resource")
@Test
public void writeReadDelete() throws Exception {
Expand Down Expand Up @@ -71,7 +81,10 @@ public void writeReadDelete() throws Exception {
objectIO.deleteObjects(List.of(uri));

soft.assertThatThrownBy(() -> objectIO.readObject(uri).readAllBytes())
.isInstanceOf(IOException.class);
.extracting(e -> exceptionMapper.analyze(e))
.extracting(Optional::get)
.extracting(BackendErrorStatus::statusCode)
.isEqualTo(BackendErrorCode.NOT_FOUND);
}
}

Expand Down Expand Up @@ -131,6 +144,9 @@ public void twoBucketsTwoServers() throws Exception {
}
}

protected abstract BackendExceptionMapper.Builder addExceptionHandlers(
BackendExceptionMapper.Builder builder);

protected abstract StorageUri buildURI(String bucket, String key);

protected abstract ObjectIO buildObjectIO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import org.projectnessie.catalog.files.AbstractClients;
import org.projectnessie.catalog.files.api.BackendExceptionMapper;
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.catalog.secrets.SecretsProvider;
import org.projectnessie.objectstoragemock.ObjectStorageMock;
Expand All @@ -33,6 +34,12 @@ protected StorageUri buildURI(String bucket, String key) {
return StorageUri.of(String.format("abfs://%s@storageAccount/%s", bucket, key));
}

@Override
protected BackendExceptionMapper.Builder addExceptionHandlers(
BackendExceptionMapper.Builder builder) {
return builder.addAnalyzer(AdlsExceptionMapper.INSTANCE);
}

@Override
protected ObjectIO buildObjectIO(
ObjectStorageMock.MockServer server1, ObjectStorageMock.MockServer server2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import org.projectnessie.catalog.files.AbstractClients;
import org.projectnessie.catalog.files.api.BackendExceptionMapper;
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.catalog.secrets.SecretsProvider;
import org.projectnessie.objectstoragemock.ObjectStorageMock;
Expand All @@ -33,6 +34,12 @@ protected StorageUri buildURI(String bucket, String key) {
return StorageUri.of(String.format("gs://%s/%s", bucket, key));
}

@Override
protected BackendExceptionMapper.Builder addExceptionHandlers(
BackendExceptionMapper.Builder builder) {
return builder.addAnalyzer(GcsExceptionMapper.INSTANCE);
}

@Override
protected ObjectIO buildObjectIO(
ObjectStorageMock.MockServer server1, ObjectStorageMock.MockServer server2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.projectnessie.catalog.files.AbstractClients;
import org.projectnessie.catalog.files.api.BackendExceptionMapper;
import org.projectnessie.catalog.files.api.ObjectIO;
import org.projectnessie.catalog.secrets.SecretsProvider;
import org.projectnessie.objectstoragemock.ObjectStorageMock;
Expand Down Expand Up @@ -97,6 +98,12 @@ protected StorageUri buildURI(String bucket, String key) {
return StorageUri.of(String.format("s3://%s/%s", bucket, key));
}

@Override
protected BackendExceptionMapper.Builder addExceptionHandlers(
BackendExceptionMapper.Builder builder) {
return builder.addAnalyzer(S3ExceptionMapper.INSTANCE);
}

@Test
public void invalidTrustStore(@TempDir Path tempDir) throws Exception {
Path file = tempDir.resolve("my.trust.store");
Expand Down

0 comments on commit 5897903

Please sign in to comment.