Skip to content

Commit

Permalink
Add exception mappers to convert storage failures to Iceberg REST cli…
Browse files Browse the repository at this point in the history
…ent exceptions

* Read path for ObjectIO.

* Add AccessCheckHandler to ObjectStorageMock for simulating access failures in tests.

* Add exception mappers to convert storage failures to Iceberg REST client exceptions.

Closes projectnessie#8738
  • Loading branch information
dimas-b committed Jun 10, 2024
1 parent bd23431 commit c37e4ee
Show file tree
Hide file tree
Showing 23 changed files with 586 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@
import java.time.format.DateTimeParseException;
import java.util.Optional;
import java.util.function.Function;
import org.projectnessie.storage.uri.StorageUri;

public class BackendThrottledException extends RetryableException {
public BackendThrottledException(Instant retryNotBefore, Throwable cause) {
super(retryNotBefore, cause);
public BackendThrottledException(
Instant retryNotBefore, String message, StorageUri uri, IOMode ioMode, int httpStatusCode) {
super(retryNotBefore, message, uri, ioMode, httpStatusCode);
}

public BackendThrottledException(Instant retryNotBefore, String message) {
super(retryNotBefore, message);
}

public BackendThrottledException(Instant retryNotBefore, String message, Throwable cause) {
super(retryNotBefore, message, cause);
public BackendThrottledException(
Instant retryNotBefore,
String message,
StorageUri uri,
IOMode ioMode,
int httpStatusCode,
Throwable cause) {
super(retryNotBefore, message, uri, ioMode, httpStatusCode, cause);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@

import java.time.Instant;
import java.util.Optional;
import org.projectnessie.storage.uri.StorageUri;

public class NonRetryableException extends ObjectIOException {
public NonRetryableException(Throwable cause) {
super(cause);
}

public NonRetryableException(String message) {
super(message);
}

public NonRetryableException(String message, Throwable cause) {
super(message, cause);
public NonRetryableException(StorageUri uri, IOMode ioMode, int httpStatusCode, Throwable cause) {
super(cause.getMessage(), uri, ioMode, httpStatusCode, cause);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,44 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Optional;
import org.projectnessie.storage.uri.StorageUri;

public abstract class ObjectIOException extends IOException {
public ObjectIOException(Throwable cause) {
super(cause);
}
private final StorageUri uri;
private final IOMode ioMode;
private final int httpStatusCode;

public ObjectIOException(String message) {
super(message);
public ObjectIOException(String message, StorageUri uri, IOMode ioMode, int httpStatusCode) {
this(message, uri, ioMode, httpStatusCode, null);
}

public ObjectIOException(String message, Throwable cause) {
public ObjectIOException(
String message, StorageUri uri, IOMode ioMode, int httpStatusCode, Throwable cause) {
super(message, cause);
this.uri = uri;
this.ioMode = ioMode;
this.httpStatusCode = httpStatusCode;
}

public abstract boolean isRetryable();

public abstract Optional<Instant> retryNotBefore();

public Optional<StorageUri> uri() {
return Optional.ofNullable(uri);
}

public IOMode ioMode() {
return ioMode;
}

public int httpStatusCode() {
return httpStatusCode;
}

public enum IOMode {
ACCESS,
READ,
WRITE,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.catalog.files.api;

import java.time.Instant;
import javax.annotation.Nullable;
import org.projectnessie.catalog.files.api.ObjectIOException.IOMode;
import org.projectnessie.storage.uri.StorageUri;

public abstract class ObjectIOExceptionMapper {

private final StorageUri uri;
private final IOMode ioMode;

protected ObjectIOExceptionMapper(StorageUri uri, IOMode ioMode) {
this.uri = uri;
this.ioMode = ioMode;
}

public ObjectIOException toStorageFailure(Throwable th) {
while (th != null) {
if (th instanceof ObjectIOException) {
return (ObjectIOException) th;
}

ObjectIOException mapped = maybeMap(th);
if (mapped != null) {
return mapped;
}

th = th.getCause();
}

return new NonRetryableException(uri, ioMode, 500, th);
}

@Nullable
protected abstract ObjectIOException maybeMap(Throwable th);

protected NonRetryableException asNonRetryableException(int httpStatusCode, Throwable cause) {
return new NonRetryableException(uri, ioMode, httpStatusCode, cause);
}

protected BackendThrottledException asThrottledException(
Instant retryNotBefore, String message, int httpStatusCode, Throwable cause) {
return new BackendThrottledException(
retryNotBefore, message, uri, ioMode, httpStatusCode, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2024 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.catalog.files.api;

import java.io.IOException;
import java.io.InputStream;

public class ObjectIOInputStream extends InputStream {

private final InputStream in;
private final ObjectIOExceptionMapper mapper;

public ObjectIOInputStream(InputStream in, ObjectIOExceptionMapper mapper) {
this.in = in;
this.mapper = mapper;
}

@Override
public int read() throws IOException {
try {
return in.read();
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
try {
return in.read(b, off, len);
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}

@Override
public void close() throws IOException {
try {
in.close();
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2024 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.catalog.files.api;

import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Nonnull;

public class ObjectIOOutputStream extends OutputStream {

private final OutputStream out;
private final ObjectIOExceptionMapper mapper;

public ObjectIOOutputStream(OutputStream out, ObjectIOExceptionMapper mapper) {
this.out = out;
this.mapper = mapper;
}

@Override
public void write(int b) throws IOException {
try {
out.write(b);
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}

@Override
public void write(@Nonnull byte[] b, int off, int len) throws IOException {
try {
out.write(b, off, len);
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}

@Override
public void close() throws IOException {
try {
out.close();
} catch (IOException e) {
throw mapper.toStorageFailure(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@

import java.time.Instant;
import java.util.Optional;
import org.projectnessie.storage.uri.StorageUri;

public class RetryableException extends ObjectIOException {
private final Instant retryNotBefore;

public RetryableException(Instant retryNotBefore, Throwable cause) {
super(cause);
public RetryableException(
Instant retryNotBefore, String message, StorageUri uri, IOMode ioMode, int httpStatusCode) {
super(message, uri, ioMode, httpStatusCode);
this.retryNotBefore = retryNotBefore;
}

public RetryableException(Instant retryNotBefore, String message) {
super(message);
this.retryNotBefore = retryNotBefore;
}

public RetryableException(Instant retryNotBefore, String message, Throwable cause) {
super(message, cause);
public RetryableException(
Instant retryNotBefore,
String message,
StorageUri uri,
IOMode ioMode,
int httpStatusCode,
Throwable cause) {
super(message, uri, ioMode, httpStatusCode, cause);
this.retryNotBefore = retryNotBefore;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2024 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.catalog.files.adls;

import com.azure.storage.blob.models.BlobStorageException;
import javax.annotation.Nullable;
import org.projectnessie.catalog.files.api.ObjectIOException;
import org.projectnessie.catalog.files.api.ObjectIOExceptionMapper;
import org.projectnessie.storage.uri.StorageUri;

public class AdlsExceptionMapper extends ObjectIOExceptionMapper {
protected AdlsExceptionMapper(StorageUri uri, ObjectIOException.IOMode ioMode) {
super(uri, ioMode);
}

@Nullable
@Override
protected ObjectIOException maybeMap(Throwable th) {
if (th instanceof BlobStorageException) {
return asNonRetryableException(((BlobStorageException) th).getStatusCode(), th);
}
return null;
}
}
Loading

0 comments on commit c37e4ee

Please sign in to comment.