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 11:15:51 UTC

(pulsar) branch master updated: [fix][meta] Record get op stats on the correct completion branch in AbstractMetadataStore (#26201)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 78cc3f41c510 [fix][meta] Record get op stats on the correct completion branch in AbstractMetadataStore (#26201)
78cc3f41c510 is described below

commit 78cc3f41c510b17d5b2569811e62c5744280401e
Author: SongOf <46...@users.noreply.github.com>
AuthorDate: Sat Jul 25 19:15:45 2026 +0800

    [fix][meta] Record get op stats on the correct completion branch in AbstractMetadataStore (#26201)
    
    Co-authored-by: maxlisongsong <ma...@didiglobal.com>
---
 .../metadata/impl/AbstractMetadataStore.java       |   2 +-
 .../impl/AbstractMetadataStoreGetStatsTest.java    | 122 +++++++++++++++++++++
 2 files changed, 123 insertions(+), 1 deletion(-)

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 a67c5e04e3d9..725a740116e4 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
@@ -370,9 +370,9 @@ public abstract class AbstractMetadataStore implements MetadataStoreExtended, Co
         return storeGet(path, opts)
                 .whenComplete((v, t) -> {
                     if (t != null) {
-                        v.ifPresent(getResult -> nodeSizeStats.recordGetRes(path, getResult));
                         metadataStoreStats.recordGetOpsFailed(System.currentTimeMillis() - start);
                     } else {
+                        v.ifPresent(getResult -> nodeSizeStats.recordGetRes(path, getResult));
                         metadataStoreStats.recordGetOpsSucceeded(System.currentTimeMillis() - start);
                     }
                 });
diff --git a/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/AbstractMetadataStoreGetStatsTest.java b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/AbstractMetadataStoreGetStatsTest.java
new file mode 100644
index 000000000000..c5d76b76dde1
--- /dev/null
+++ b/pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/AbstractMetadataStoreGetStatsTest.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.pulsar.metadata.impl;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+import io.opentelemetry.api.OpenTelemetry;
+import io.prometheus.client.CollectorRegistry;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.function.Function;
+import lombok.Cleanup;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.apache.pulsar.metadata.api.GetResult;
+import org.apache.pulsar.metadata.api.MetadataNodeSizeStats;
+import org.apache.pulsar.metadata.api.MetadataStoreException;
+import org.apache.pulsar.metadata.api.Option;
+import org.apache.pulsar.metadata.api.Stat;
+import org.testng.annotations.Test;
+
+/**
+ * Verifies that {@link AbstractMetadataStore#get(String, Set)} records operation stats on the
+ * correct completion branch.
+ */
+public class AbstractMetadataStoreGetStatsTest {
+
+    private static class TestMetadataStore extends AbstractMetadataStore {
+        private volatile Function<String, CompletableFuture<Optional<GetResult>>> getImpl;
+
+        TestMetadataStore(String name, MetadataNodeSizeStats nodeSizeStats) {
+            super(name, OpenTelemetry.noop(), nodeSizeStats, 1);
+        }
+
+        @Override
+        protected CompletableFuture<Optional<GetResult>> storeGet(String path, Set<Option> opts) {
+            return getImpl.apply(path);
+        }
+
+        @Override
+        public CompletableFuture<List<String>> getChildrenFromStore(String path, Set<Option> opts) {
+            return CompletableFuture.completedFuture(List.of());
+        }
+
+        @Override
+        protected CompletableFuture<Boolean> existsFromStore(String path, Set<Option> opts) {
+            return CompletableFuture.completedFuture(false);
+        }
+
+        @Override
+        protected CompletableFuture<Void> storeDelete(String path, Optional<Long> expectedVersion, Set<Option> opts) {
+            return CompletableFuture.completedFuture(null);
+        }
+
+        @Override
+        protected CompletableFuture<Stat> storePut(String path, byte[] data, Optional<Long> optExpectedVersion,
+                                                   Set<Option> opts) {
+            return FutureUtil.failedFuture(new UnsupportedOperationException());
+        }
+    }
+
+    private static double getOpsCount(String storeName, String status) {
+        Double value = CollectorRegistry.defaultRegistry.getSampleValue(
+                "pulsar_metadata_store_ops_latency_ms_count",
+                new String[]{"name", "type", "status"},
+                new String[]{storeName, "get", status});
+        return value == null ? 0.0 : value;
+    }
+
+    @Test
+    public void testGetSuccessRecordsNodeSizeStats() throws Exception {
+        MetadataNodeSizeStats nodeSizeStats = mock(MetadataNodeSizeStats.class);
+        @Cleanup
+        TestMetadataStore store = new TestMetadataStore("get-stats-success", nodeSizeStats);
+        GetResult result = new GetResult(new byte[]{1, 2, 3}, new Stat("/a", 0, 0, 0, false, false));
+        store.getImpl = path -> CompletableFuture.completedFuture(Optional.of(result));
+
+        assertSame(store.get("/a").join().orElse(null), result);
+
+        verify(nodeSizeStats).recordGetRes("/a", result);
+        assertEquals(getOpsCount("get-stats-success", "success"), 1.0);
+    }
+
+    @Test
+    public void testGetFailureRecordsFailedOpAndPropagatesOriginalException() throws Exception {
+        String storeName = "get-stats-failure";
+        @Cleanup
+        TestMetadataStore store = new TestMetadataStore(storeName, mock(MetadataNodeSizeStats.class));
+        MetadataStoreException injected = new MetadataStoreException("injected failure");
+        store.getImpl = path -> FutureUtil.failedFuture(injected);
+
+        CompletionException ex = expectThrows(CompletionException.class, () -> store.get("/a").join());
+
+        assertSame(ex.getCause(), injected);
+        assertTrue(Arrays.stream(injected.getSuppressed()).noneMatch(s -> s instanceof NullPointerException),
+                "stats callback must not throw NPE on the failure path");
+        assertEquals(getOpsCount(storeName, "fail"), 1.0);
+    }
+}