From 8109a4bd68e01982f758e24d3b19c6c3df7fd131 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 23:44:40 +0530 Subject: [PATCH] Fix exception handling for global metadata upload (#10889) (#10920) (cherry picked from commit a890e518aea1a706249001c3420c1740bd1a06dd) Signed-off-by: Dhwanil Patel Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../gateway/remote/RemoteClusterStateService.java | 8 ++++++-- .../gateway/remote/RemoteClusterStateServiceTests.java | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/opensearch/gateway/remote/RemoteClusterStateService.java b/server/src/main/java/org/opensearch/gateway/remote/RemoteClusterStateService.java index 329ebd0dcd2b8..57b1b972e08c0 100644 --- a/server/src/main/java/org/opensearch/gateway/remote/RemoteClusterStateService.java +++ b/server/src/main/java/org/opensearch/gateway/remote/RemoteClusterStateService.java @@ -369,6 +369,8 @@ public ClusterMetadataManifest writeIncrementalMetadata( private String writeGlobalMetadata(ClusterState clusterState) throws IOException { AtomicReference result = new AtomicReference(); + AtomicReference exceptionReference = new AtomicReference(); + final BlobContainer globalMetadataContainer = globalMetadataContainer( clusterState.getClusterName().value(), clusterState.metadata().clusterUUID() @@ -381,7 +383,7 @@ private String writeGlobalMetadata(ClusterState clusterState) throws IOException LatchedActionListener completionListener = new LatchedActionListener<>(ActionListener.wrap(resp -> { logger.trace(String.format(Locale.ROOT, "GlobalMetadata uploaded successfully.")); result.set(globalMetadataContainer.path().buildAsString() + globalMetadataFilename); - }, ex -> { throw new GlobalMetadataTransferException(ex.getMessage(), ex); }), latch); + }, ex -> { exceptionReference.set(ex); }), latch); GLOBAL_METADATA_FORMAT.writeAsyncWithUrgentPriority( clusterState.metadata(), @@ -408,7 +410,9 @@ private String writeGlobalMetadata(ClusterState clusterState) throws IOException Thread.currentThread().interrupt(); throw exception; } - + if (exceptionReference.get() != null) { + throw new GlobalMetadataTransferException(exceptionReference.get().getMessage(), exceptionReference.get()); + } return result.get(); } diff --git a/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateServiceTests.java b/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateServiceTests.java index 5a43864f40c0c..ca88653f529f6 100644 --- a/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateServiceTests.java +++ b/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateServiceTests.java @@ -294,7 +294,13 @@ public void testWriteFullMetadataFailureForGlobalMetadata() throws IOException { ArgumentCaptor> actionListenerArgumentCaptor = ArgumentCaptor.forClass(ActionListener.class); doAnswer((i) -> { - actionListenerArgumentCaptor.getValue().onFailure(new RuntimeException("Cannot upload to remote")); + // For async write action listener will be called from different thread, replicating same behaviour here. + new Thread(new Runnable() { + @Override + public void run() { + actionListenerArgumentCaptor.getValue().onFailure(new RuntimeException("Cannot upload to remote")); + } + }).start(); return null; }).when(container).asyncBlobUpload(any(WriteContext.class), actionListenerArgumentCaptor.capture());