bitflicker64 commented on code in PR #887:
URL: 
https://github.com/apache/hugegraph-toolchain/pull/887#discussion_r4444257939


##########
hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/op/PdClusterStatistics.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.hugegraph.service.op;
+
+import java.time.Clock;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.function.Supplier;
+
+import org.apache.hugegraph.service.op.OperationsModels.SourceStatus;
+
+/** A single, rate-limited PD aggregation shared by all overview callers. */
+final class PdClusterStatistics implements AutoCloseable {
+
+    private final Supplier<Map<String, Long>> fetch;
+    private final Clock clock;
+    private final long intervalMillis;
+    private final ExecutorService executor;
+    private Map<String, Long> facts = Collections.emptyMap();
+    private Long lastAttempt;
+    private Long lastSuccess;
+    private boolean inFlight;
+    private boolean failed;
+    private boolean closed;
+
+    PdClusterStatistics(Supplier<Map<String, Long>> fetch, Clock clock,
+                        long intervalMillis) {
+        if (intervalMillis <= 0) {
+            throw new IllegalArgumentException("interval must be positive");
+        }
+        this.fetch = fetch;
+        this.clock = clock;
+        this.intervalMillis = intervalMillis;
+        this.executor = Executors.newSingleThreadExecutor(runnable -> {
+            Thread thread = new Thread(runnable, 
"hubble-pd-cluster-statistics");
+            thread.setDaemon(true);
+            return thread;
+        });
+    }
+
+    synchronized Result sample() {
+        long now = this.clock.millis();
+        if (!this.closed && !this.inFlight &&
+            (this.lastAttempt == null || now < this.lastAttempt ||
+             now - this.lastAttempt >= this.intervalMillis)) {
+            this.inFlight = true;
+            this.lastAttempt = now;
+            this.executor.execute(this::collect);
+        }
+        if (this.lastSuccess == null) {
+            return new Result(Collections.emptyMap(),
+                    new SourceStatus("UNAVAILABLE", "UNKNOWN", 
this.lastAttempt,
+                                     null, false, false,
+                                     this.inFlight ? "collecting" : 
"collection_failed"));
+        }
+        boolean stale = this.failed || now < this.lastSuccess ||
+                        now - this.lastSuccess >= this.intervalMillis;

Review Comment:
   Important: staleness here depends on age, not failure, so opening the 
Overview more than 60 seconds after the last sample returns the old values as 
`stale` and `PARTIAL` while the refresh runs, and `Overview.js` never polls 
again, leaving `Stale ยท Unavailable` on screen though PD never failed. Mark 
retained values stale only after a refresh fails, and while a refresh is in 
flight report a distinct reason that `Overview.js` polls on like `collecting`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to