You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/07/25 15:30:52 UTC

(pulsar) 03/08: [fix][meta] Complete handleMetadataEvent future exceptionally when the initial get fails (#26199)

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 864ed57905e7869c509566179db9bfa64d0e8887
Author: SongOf <46...@users.noreply.github.com>
AuthorDate: Sat Jul 25 20:40:47 2026 +0800

    [fix][meta] Complete handleMetadataEvent future exceptionally when the initial get fails (#26199)
    
    Co-authored-by: maxlisongsong <ma...@didiglobal.com>
    (cherry picked from commit bc56ba8d30b2a96648a041359579b1e1138fb6f3)
---
 .../metadata/impl/AbstractMetadataStore.java       | 10 ++++---
 .../impl/MetadataEventSynchronizerTest.java        | 31 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/AbstractMetadataStore.java b/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/AbstractMetadataStore.java
index 53ccf09e297d..3cd680514df3 100644
--- a/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/AbstractMetadataStore.java
+++ b/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/AbstractMetadataStore.java
@@ -167,14 +167,14 @@ public abstract class AbstractMetadataStore implements MetadataStoreExtended, Co
     @Override
     public CompletableFuture<Void> handleMetadataEvent(MetadataEvent event) {
         CompletableFuture<Void> result = new CompletableFuture<>();
-        get(event.getPath()).thenApply(res -> {
+        get(event.getPath()).thenAccept(res -> {
             Set<CreateOption> options = event.getOptions() != null ? event.getOptions()
                     : Collections.emptySet();
             if (res.isPresent()) {
                 GetResult existingValue = res.get();
                 if (shouldIgnoreEvent(event, existingValue)) {
                     result.complete(null);
-                    return result;
+                    return;
                 }
             }
             // else update the event
@@ -196,7 +196,11 @@ public abstract class AbstractMetadataStore implements MetadataStoreExtended, Co
                 }
                 return false;
             });
-            return result;
+        }).exceptionally(ex -> {
+            Throwable cause = FutureUtil.unwrapCompletionException(ex);
+            log.warn("Failed to handle metadata event {}", event.getPath(), cause);
+            result.completeExceptionally(cause);
+            return null;
         });
         return result;
     }
diff --git a/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/MetadataEventSynchronizerTest.java b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/MetadataEventSynchronizerTest.java
index 3b07e0b3c2bd..90c17b7c5ae4 100644
--- a/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/MetadataEventSynchronizerTest.java
+++ b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/MetadataEventSynchronizerTest.java
@@ -20,13 +20,22 @@ package org.apache.pulsar.metadata.impl;
 
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
 import java.nio.charset.StandardCharsets;
+import java.util.HashSet;
 import java.util.Optional;
 import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 import lombok.Cleanup;
+import org.apache.pulsar.metadata.api.GetResult;
+import org.apache.pulsar.metadata.api.MetadataEvent;
 import org.apache.pulsar.metadata.api.MetadataStore;
 import org.apache.pulsar.metadata.api.MetadataStoreConfig;
+import org.apache.pulsar.metadata.api.MetadataStoreException;
 import org.apache.pulsar.metadata.api.MetadataStoreFactory;
+import org.apache.pulsar.metadata.api.NotificationType;
 import org.awaitility.Awaitility;
 import org.testng.annotations.Test;
 
@@ -75,6 +84,28 @@ public class MetadataEventSynchronizerTest {
         });
     }
 
+    @Test
+    public void testHandleMetadataEventCompletesWhenGetFails() throws Exception {
+        @Cleanup
+        LocalMemoryMetadataStore store = new LocalMemoryMetadataStore("memory:local",
+                MetadataStoreConfig.builder().build()) {
+            @Override
+            public CompletableFuture<Optional<GetResult>> storeGet(String path) {
+                return CompletableFuture.failedFuture(
+                        new MetadataStoreException("injected storeGet failure"));
+            }
+        };
+
+        MetadataEvent event = new MetadataEvent("/test", "value".getBytes(StandardCharsets.UTF_8),
+                new HashSet<>(), null, System.currentTimeMillis(), "test-cluster", NotificationType.Modified);
+
+        CompletableFuture<Void> result = store.handleMetadataEvent(event);
+        // The future must not hang when the initial get() fails: it should complete exceptionally
+        ExecutionException ex = expectThrows(ExecutionException.class, () -> result.get(5, TimeUnit.SECONDS));
+        assertTrue(ex.getCause() instanceof MetadataStoreException,
+                "expected MetadataStoreException cause but got: " + ex.getCause());
+    }
+
     @Test
     public void testPathValid() {
         assertFalse(AbstractMetadataStore.isValidPath(null));