From ef389a60ceea5beaf34de6a6292748922ab18774 Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Wed, 5 Apr 2023 13:54:46 +0200 Subject: [PATCH] convert exceptions into CacheException prior calling to NearlineRequest#failed --- .../nearline/cta/CtaNearlineStorage.java | 36 +++++++++++-------- .../nearline/cta/CtaNearlineStorageTest.java | 9 ++--- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/dcache/nearline/cta/CtaNearlineStorage.java b/src/main/java/org/dcache/nearline/cta/CtaNearlineStorage.java index 0da4c52..0c49c95 100644 --- a/src/main/java/org/dcache/nearline/cta/CtaNearlineStorage.java +++ b/src/main/java/org/dcache/nearline/cta/CtaNearlineStorage.java @@ -10,6 +10,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.protobuf.Empty; import cta.admin.CtaAdmin.Version; +import diskCacheV111.util.CacheException; import io.grpc.ChannelCredentials; import io.grpc.ConnectivityState; import io.grpc.Deadline; @@ -206,9 +207,7 @@ public void onNext(CreateResponse createResponse) { @Override public void onError(Throwable t) { LOGGER.error("Failed to submit create request {}", t.getMessage()); - Exception e = - t instanceof Exception ? Exception.class.cast(t) : new Exception(t); - fr.failed(e); + fr.failed(asCacheException(t)); } @Override @@ -220,7 +219,7 @@ public void onCompleted() { } catch (ExecutionException | InterruptedException e) { Throwable t = Throwables.getRootCause(e); LOGGER.error("Failed to activate flush request: {}", t.getMessage()); - fr.failed(e); + fr.failed(asCacheException(t)); } } @@ -290,9 +289,7 @@ public void cancel() { @Override public void onError(Throwable t) { LOGGER.error("Failed to submit archive request {}", t.getMessage()); - Exception e = - t instanceof Exception ? Exception.class.cast(t) : new Exception(t); - r.failed(e); + r.failed(asCacheException(t)); } @Override @@ -343,7 +340,7 @@ public void completed(Set checksums) { Throwable t = Throwables.getRootCause(e); LOGGER.error("Failed to activate/allocate space for retrieve request: {}", t.getMessage()); - r.failed(e); + r.failed(asCacheException(t)); continue; } @@ -374,9 +371,7 @@ public void cancel() { @Override public void onError(Throwable t) { LOGGER.error("Failed to submit stage request {}", t.getMessage()); - Exception e = - t instanceof Exception ? Exception.class.cast(t) : new Exception(t); - r.failed(e); + r.failed(asCacheException(t)); } @Override @@ -409,9 +404,7 @@ public void onNext(Empty value) { @Override public void onError(Throwable t) { LOGGER.error("Failed to submit stage request {}", t.getMessage()); - Exception e = - t instanceof Exception ? Exception.class.cast(t) : new Exception(t); - r.failed(e); + r.failed(asCacheException(t)); } @Override @@ -575,4 +568,19 @@ int getPendingRequestsCount() { PendingRequest getRequest(String id) { return pendingRequests.get(id); } + + + /** + * Convert a given Throwable into CacheException to ensure serialization. + * @param e origianl exception. + * @return corresponding cache exception + */ + private CacheException asCacheException(Throwable e) { + + if (e.getClass().isAssignableFrom(CacheException.class)) { + return CacheException.class.cast(e); + } + + return new CacheException(CacheException.UNEXPECTED_SYSTEM_EXCEPTION, e.toString()); + } } diff --git a/src/test/java/org/dcache/nearline/cta/CtaNearlineStorageTest.java b/src/test/java/org/dcache/nearline/cta/CtaNearlineStorageTest.java index cdb4f25..0da7689 100644 --- a/src/test/java/org/dcache/nearline/cta/CtaNearlineStorageTest.java +++ b/src/test/java/org/dcache/nearline/cta/CtaNearlineStorageTest.java @@ -22,6 +22,7 @@ import static org.mockito.Mockito.when; import com.google.common.util.concurrent.Futures; +import diskCacheV111.util.CacheException; import diskCacheV111.vehicles.GenericStorageInfo; import eu.emi.security.authn.x509.impl.CertificateUtils; import eu.emi.security.authn.x509.impl.CertificateUtils.Encoding; @@ -199,7 +200,7 @@ public void testWithoutTLS() { var request = mockedFlushRequest(); driver.flush(Set.of(request)); waitToComplete(); - verify(request).failed(any(io.grpc.StatusRuntimeException.class)); + verify(request).failed(any(CacheException.class)); } @Test @@ -637,7 +638,7 @@ public void testFailOnLostStageMessage() { driver.stage(Set.of(request)); cta.waitToReply(4); - verify(request, times(1)).failed(any(io.grpc.StatusRuntimeException.class)); + verify(request, times(1)).failed(any(CacheException.class)); } @@ -653,7 +654,7 @@ public void testFailOnLostFlushMessage() { driver.flush(Set.of(request)); cta.waitToReply(4); - verify(request, times(1)).failed(any(io.grpc.StatusRuntimeException.class)); + verify(request, times(1)).failed(any(CacheException.class)); } @Test @@ -668,7 +669,7 @@ public void testFailOnLostDeleteMessage() { driver.remove(Set.of(request)); cta.waitToReply(4); - verify(request, times(1)).failed(any(io.grpc.StatusRuntimeException.class)); + verify(request, times(1)).failed(any(CacheException.class)); } void waitToComplete() {