You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/08/16 15:15:09 UTC

[GitHub] [pulsar] gaoran10 commented on a change in pull request #11564: Add compacted topic metrics for TopicStats in CLI

gaoran10 commented on a change in pull request #11564:
URL: https://github.com/apache/pulsar/pull/11564#discussion_r689607897



##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorMXBeanImpl.java
##########
@@ -0,0 +1,125 @@
+/**
+ * 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.compaction;
+
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.LongAdder;
+import org.apache.pulsar.broker.service.BrokerService;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+
+public class CompactorMXBeanImpl implements CompactorMXBean {
+
+    private final ConcurrentHashMap<String, CompactRecord> compactRecordOps = new ConcurrentHashMap<>();
+
+    private final Optional<BrokerService> brokerService;
+    private final ScheduledExecutorService scheduler;
+
+    public CompactorMXBeanImpl(Optional<BrokerService> brokerService, ScheduledExecutorService scheduler) {
+        this.brokerService = brokerService;
+        this.scheduler = scheduler;
+        this.scheduler.scheduleAtFixedRate(this::cleanup, 2, 2, TimeUnit.HOURS);
+    }
+
+    public void cleanup() {
+        if (compactRecordOps.isEmpty()) {
+            return;
+        }
+        Set<String> topics = new HashSet<>();
+        if (brokerService.isPresent()) {
+            brokerService.get().forEachTopic(t -> {
+                if (t instanceof PersistentTopic) {
+                    topics.add(t.getName());
+                }
+            });
+        }
+        if (!topics.isEmpty()) {
+            compactRecordOps.forEach((k, v) -> {
+                if (!topics.contains(k)) {
+                    compactRecordOps.remove(k);
+                }
+            });
+        }
+    }
+
+    public void addCompactRemovedEvent(String topic) {
+        compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord()).addCompactRemovedEvent();
+    }
+
+    public void addCompactStartOp(String topic) {
+        compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord()).reset();
+    }
+
+    public void addCompactEndOp(String topic, boolean succeed) {
+        CompactRecord compactRecord = compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord());

Review comment:
       It seems that if the `CompactRecord` of the topic does not exist, it's an unusual situation, the stat is invalid, maybe we could log a warn log instead of recording the stat. One possible scenario is that the topic was deleted.

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
##########
@@ -1903,9 +1905,28 @@ public TopicStatsImpl getStats(boolean getPreciseBacklog, boolean subscriptionBa
         stats.lastOffloadLedgerId = ledger.getLastOffloadedLedgerId();
         stats.lastOffloadSuccessTimeStamp = ledger.getLastOffloadedSuccessTimestamp();
         stats.lastOffloadFailureTimeStamp = ledger.getLastOffloadedFailureTimestamp();
+        stats.compact.lastCompactRemovedEventCount = getCompactorMXBean().map(stat ->
+                stat.getLastCompactRemovedEventCount(topic)).orElse(0L);
+        stats.compact.lastCompactSucceedTimestamp = getCompactorMXBean().map(stat ->

Review comment:
       A trivial question, maybe we could reuse the `getCompactorMXBean()` result, call the method `getCompactorMXBean()` one time.

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/compaction/TwoPhaseCompactor.java
##########
@@ -145,14 +149,17 @@ private void phaseOneLoop(RawReader reader,
                     Pair<String, Integer> keyAndSize = extractKeyAndSize(m);
                     if (keyAndSize != null) {
                         if (keyAndSize.getRight() > 0) {
-                            latestForKey.put(keyAndSize.getLeft(), id);
+                            MessageId old = latestForKey.put(keyAndSize.getLeft(), id);
+                            replaceMessage = old != null;
                         } else {
                             deletedMessage = true;
                             latestForKey.remove(keyAndSize.getLeft());
                         }
                     }
                 }
-
+                if (replaceMessage || deletedMessage) {
+                    mxBean.addCompactRemovedEvent(reader.getTopic());
+                }

Review comment:
       Maybe the `replaceMessage` could be re-modified. For example, in a batch loop, at first, it could be true, when meeting a new key it will change to false.
   
   I'm not sure about the meaning of the `compactRemoveEvent`, this event indicates one batch compact event or message compact events.

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorMXBeanImpl.java
##########
@@ -0,0 +1,125 @@
+/**
+ * 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.compaction;
+
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.LongAdder;
+import org.apache.pulsar.broker.service.BrokerService;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+
+public class CompactorMXBeanImpl implements CompactorMXBean {
+
+    private final ConcurrentHashMap<String, CompactRecord> compactRecordOps = new ConcurrentHashMap<>();
+
+    private final Optional<BrokerService> brokerService;
+    private final ScheduledExecutorService scheduler;
+
+    public CompactorMXBeanImpl(Optional<BrokerService> brokerService, ScheduledExecutorService scheduler) {
+        this.brokerService = brokerService;
+        this.scheduler = scheduler;
+        this.scheduler.scheduleAtFixedRate(this::cleanup, 2, 2, TimeUnit.HOURS);
+    }
+
+    public void cleanup() {
+        if (compactRecordOps.isEmpty()) {
+            return;
+        }
+        Set<String> topics = new HashSet<>();
+        if (brokerService.isPresent()) {
+            brokerService.get().forEachTopic(t -> {
+                if (t instanceof PersistentTopic) {
+                    topics.add(t.getName());
+                }
+            });
+        }
+        if (!topics.isEmpty()) {
+            compactRecordOps.forEach((k, v) -> {
+                if (!topics.contains(k)) {
+                    compactRecordOps.remove(k);
+                }
+            });
+        }
+    }
+
+    public void addCompactRemovedEvent(String topic) {
+        compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord()).addCompactRemovedEvent();
+    }
+
+    public void addCompactStartOp(String topic) {
+        compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord()).reset();
+    }
+
+    public void addCompactEndOp(String topic, boolean succeed) {
+        CompactRecord compactRecord = compactRecordOps.computeIfAbsent(topic, k -> new CompactRecord());
+        compactRecord.lastCompactDurationTimeInMills = TimeUnit.NANOSECONDS.toMillis(
+                System.nanoTime() - compactRecord.lastCompactStartTimeOp);
+        compactRecord.lastCompactRemovedEventCount = compactRecord.lastCompactRemovedEventCountOp.longValue();
+        if (succeed) {
+            compactRecord.lastCompactSucceedTimestamp = System.currentTimeMillis();
+        } else {
+            compactRecord.lastCompactFailedTimestamp = System.currentTimeMillis();
+        }
+    }
+
+    @Override
+    public long getLastCompactRemovedEventCount(String topic) {
+        return compactRecordOps.getOrDefault(topic, new CompactRecord()).lastCompactRemovedEventCount;
+    }
+
+    @Override
+    public long getLastCompactSucceedTimestamp(String topic) {
+        return compactRecordOps.getOrDefault(topic, new CompactRecord()).lastCompactSucceedTimestamp;
+    }
+
+    @Override
+    public long getLastCompactFailedTimestamp(String topic) {
+        return compactRecordOps.getOrDefault(topic, new CompactRecord()).lastCompactFailedTimestamp;
+    }
+
+    @Override
+    public long getLastCompactDurationTimeInMills(String topic) {
+        return compactRecordOps.getOrDefault(topic, new CompactRecord()).lastCompactDurationTimeInMills;
+    }
+
+    static class CompactRecord {
+
+        private long lastCompactRemovedEventCount = 0L;
+        private long lastCompactSucceedTimestamp = 0L;
+        private long lastCompactFailedTimestamp = 0L;
+        private long lastCompactDurationTimeInMills = 0L;
+
+        //

Review comment:
       Could you clean up the comment?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org