bitflicker64 commented on code in PR #3221:
URL: https://github.com/apache/hugegraph/pull/3221#discussion_r4052303097


##########
hugegraph-server/hugegraph-hstore/src/main/java/org/apache/hugegraph/backend/store/hstore/HstoreStorageProbe.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.backend.store.hstore;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.grpc.Metapb;
+import org.apache.hugegraph.store.grpc.state.HgStoreStateGrpc;
+import org.apache.hugegraph.store.grpc.state.SubStateReq;
+import org.apache.hugegraph.util.E;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+
+/**
+ * Storage-aware readiness of this server, from this server's point of view:
+ * at least one Store answers a direct, local, read-only gRPC call
+ * (HgStoreState.getScanState, which reads the node's own scan-pool stats and
+ * never touches raft). The Store list comes from PD, but PD is refreshed in
+ * the background and the last known list is used right away, so a PD that is
+ * slow, restarting or down does not change the readiness of a server whose
+ * Stores still answer. Every wait is bounded by one shared time budget, so a
+ * hung PD or Store turns into "not ready" instead of a hung probe. The result
+ * carries no addresses, since it is served without authentication.
+ */
+public final class HstoreStorageProbe {
+
+    public static final String META_STORAGE_READINESS = "storage_readiness";
+
+    private static final ExecutorService EXECUTOR = 
Executors.newCachedThreadPool(

Review Comment:
   โš ๏ธ This ten-line anonymous `ThreadFactory` with its own `AtomicInteger` is 
the thing `BasicThreadFactory.Builder` exists for โ€” and this repo already uses 
it exactly this way, four times, in `hugegraph-common`'s `ExecutorUtil`. 
commons-lang3 is already imported elsewhere in this module 
(`HstoreSessionsImpl`, `HstoreTable`), so no new dependency.
   
   Requested change:
   
   ```java
   private static final ExecutorService EXECUTOR = 
Executors.newCachedThreadPool(
           new 
BasicThreadFactory.Builder().namingPattern("storage-readiness-%d")
                                           .daemon(true).build());
   ```
   
   Same names, same daemon flag, eight lines and one import 
(`java.util.concurrent.ThreadFactory` + `AtomicInteger`) gone.



##########
hugegraph-server/hugegraph-hstore/src/main/java/org/apache/hugegraph/backend/store/hstore/HstoreStorageProbe.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.backend.store.hstore;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.grpc.Metapb;
+import org.apache.hugegraph.store.grpc.state.HgStoreStateGrpc;
+import org.apache.hugegraph.store.grpc.state.SubStateReq;
+import org.apache.hugegraph.util.E;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+
+/**
+ * Storage-aware readiness of this server, from this server's point of view:
+ * at least one Store answers a direct, local, read-only gRPC call
+ * (HgStoreState.getScanState, which reads the node's own scan-pool stats and
+ * never touches raft). The Store list comes from PD, but PD is refreshed in
+ * the background and the last known list is used right away, so a PD that is
+ * slow, restarting or down does not change the readiness of a server whose
+ * Stores still answer. Every wait is bounded by one shared time budget, so a
+ * hung PD or Store turns into "not ready" instead of a hung probe. The result
+ * carries no addresses, since it is served without authentication.
+ */
+public final class HstoreStorageProbe {
+
+    public static final String META_STORAGE_READINESS = "storage_readiness";
+
+    private static final ExecutorService EXECUTOR = 
Executors.newCachedThreadPool(
+            new ThreadFactory() {
+                private final AtomicInteger seq = new AtomicInteger();
+
+                @Override
+                public Thread newThread(Runnable r) {
+                    Thread t = new Thread(r, "storage-readiness-" + 
this.seq.incrementAndGet());
+                    t.setDaemon(true);
+                    return t;
+                }
+            });
+
+    private static final KnownStores KNOWN = new KnownStores();
+    private static final Map<String, ManagedChannel> CHANNELS = new 
ConcurrentHashMap<>();
+
+    private HstoreStorageProbe() {
+    }
+
+    /** The active stores as PD sees them. */
+    public interface StoreLister {
+
+        List<Metapb.Store> activeStores() throws Exception;
+    }
+
+    /** One cheap call to one store; returning (any value) means it answered. 
*/
+    public interface StorePinger {
+
+        void ping(Metapb.Store store, long timeoutMs) throws Exception;
+    }
+
+    /** The last store list PD answered with, shared by consecutive probes. */
+    public static final class KnownStores {
+
+        private volatile List<Metapb.Store> stores = Collections.emptyList();
+        private volatile long at;
+        private volatile Boolean pdOk;
+        private volatile long pdAt;
+
+        public List<Metapb.Store> stores() {
+            return this.stores;
+        }
+
+        public long ageMs() {
+            return this.at == 0L ? -1L : System.currentTimeMillis() - this.at;
+        }
+
+        /** Outcome of the last finished PD refresh, null before the first 
one. */
+        public Boolean pdOk() {
+            return this.pdOk;
+        }
+
+        public long pdAgeMs() {
+            return this.pdAt == 0L ? -1L : System.currentTimeMillis() - 
this.pdAt;
+        }
+
+        public void update(List<Metapb.Store> stores) {
+            this.pdOk = true;
+            this.pdAt = System.currentTimeMillis();
+            if (stores != null && !stores.isEmpty()) {
+                this.stores = Collections.unmodifiableList(new 
ArrayList<>(stores));
+                this.at = this.pdAt;
+            }
+        }
+
+        public void pdFailed() {
+            this.pdOk = false;
+            this.pdAt = System.currentTimeMillis();
+        }
+    }
+
+    public static final class Result {

Review Comment:
   ๐Ÿงน The only production consumer of `Result` is `probe(String, long)` calling 
`.toMap()` on it immediately; outside this file the class appears only in 
`HstoreStorageProbeTest`. So these ~55 lines โ€” eight final fields, an 
eight-positional-arg constructor (`new Result(false, "...", 0, null, false, 
-1L, -1L, 0L)` is hard to read at the four construction sites), five getters โ€” 
exist to make test assertions prettier.
   
   Requested change: have `probe(...)` build the `LinkedHashMap` directly (one 
small private `result(boolean ready, String reason, ...)` helper covers the 
four return points) and let the tests assert on map entries, the way 
`testMapCarriesNoAddresses` already does. Deletes the class and the `.toMap()` 
hops.



##########
hugegraph-server/hugegraph-hstore/src/main/java/org/apache/hugegraph/backend/store/hstore/HstoreStorageProbe.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.backend.store.hstore;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.grpc.Metapb;
+import org.apache.hugegraph.store.grpc.state.HgStoreStateGrpc;
+import org.apache.hugegraph.store.grpc.state.SubStateReq;
+import org.apache.hugegraph.util.E;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+
+/**
+ * Storage-aware readiness of this server, from this server's point of view:
+ * at least one Store answers a direct, local, read-only gRPC call
+ * (HgStoreState.getScanState, which reads the node's own scan-pool stats and
+ * never touches raft). The Store list comes from PD, but PD is refreshed in
+ * the background and the last known list is used right away, so a PD that is
+ * slow, restarting or down does not change the readiness of a server whose
+ * Stores still answer. Every wait is bounded by one shared time budget, so a
+ * hung PD or Store turns into "not ready" instead of a hung probe. The result
+ * carries no addresses, since it is served without authentication.
+ */
+public final class HstoreStorageProbe {
+
+    public static final String META_STORAGE_READINESS = "storage_readiness";
+
+    private static final ExecutorService EXECUTOR = 
Executors.newCachedThreadPool(
+            new ThreadFactory() {
+                private final AtomicInteger seq = new AtomicInteger();
+
+                @Override
+                public Thread newThread(Runnable r) {
+                    Thread t = new Thread(r, "storage-readiness-" + 
this.seq.incrementAndGet());
+                    t.setDaemon(true);
+                    return t;
+                }
+            });
+
+    private static final KnownStores KNOWN = new KnownStores();
+    private static final Map<String, ManagedChannel> CHANNELS = new 
ConcurrentHashMap<>();
+
+    private HstoreStorageProbe() {
+    }
+
+    /** The active stores as PD sees them. */
+    public interface StoreLister {
+
+        List<Metapb.Store> activeStores() throws Exception;
+    }
+
+    /** One cheap call to one store; returning (any value) means it answered. 
*/
+    public interface StorePinger {
+
+        void ping(Metapb.Store store, long timeoutMs) throws Exception;
+    }
+
+    /** The last store list PD answered with, shared by consecutive probes. */
+    public static final class KnownStores {
+
+        private volatile List<Metapb.Store> stores = Collections.emptyList();
+        private volatile long at;
+        private volatile Boolean pdOk;
+        private volatile long pdAt;
+
+        public List<Metapb.Store> stores() {
+            return this.stores;
+        }
+
+        public long ageMs() {
+            return this.at == 0L ? -1L : System.currentTimeMillis() - this.at;
+        }
+
+        /** Outcome of the last finished PD refresh, null before the first 
one. */
+        public Boolean pdOk() {
+            return this.pdOk;
+        }
+
+        public long pdAgeMs() {
+            return this.pdAt == 0L ? -1L : System.currentTimeMillis() - 
this.pdAt;
+        }
+
+        public void update(List<Metapb.Store> stores) {
+            this.pdOk = true;
+            this.pdAt = System.currentTimeMillis();
+            if (stores != null && !stores.isEmpty()) {
+                this.stores = Collections.unmodifiableList(new 
ArrayList<>(stores));
+                this.at = this.pdAt;
+            }
+        }
+
+        public void pdFailed() {
+            this.pdOk = false;
+            this.pdAt = System.currentTimeMillis();
+        }
+    }
+
+    public static final class Result {
+
+        private final boolean ready;
+        private final String reason;
+        private final int activeStores;
+        private final Long answeredStore;
+        private final Boolean pdReachable;
+        private final long pdAgeMs;
+        private final long storesAgeMs;
+        private final long storeMillis;
+
+        Result(boolean ready, String reason, int activeStores, Long 
answeredStore,
+               Boolean pdReachable, long pdAgeMs, long storesAgeMs, long 
storeMillis) {
+            this.ready = ready;
+            this.reason = reason;
+            this.activeStores = activeStores;
+            this.answeredStore = answeredStore;
+            this.pdReachable = pdReachable;
+            this.pdAgeMs = pdAgeMs;
+            this.storesAgeMs = storesAgeMs;
+            this.storeMillis = storeMillis;
+        }
+
+        public boolean ready() {
+            return this.ready;
+        }
+
+        public String reason() {
+            return this.reason;
+        }
+
+        public int activeStores() {
+            return this.activeStores;
+        }
+
+        public Long answeredStore() {
+            return this.answeredStore;
+        }
+
+        public Boolean pdReachable() {
+            return this.pdReachable;
+        }
+
+        public Map<String, Object> toMap() {
+            Map<String, Object> map = new LinkedHashMap<>();
+            map.put("ready", this.ready);
+            map.put("reason", this.reason);
+            map.put("active_stores", this.activeStores);
+            map.put("answered_store", this.answeredStore);
+            map.put("pd_reachable", this.pdReachable);
+            map.put("pd_checked_age_ms", this.pdAgeMs);
+            map.put("stores_age_ms", this.storesAgeMs);
+            map.put("store_millis", this.storeMillis);
+            return map;
+        }
+    }
+
+    /**
+     * Probe through the process-wide PD client and this probe's own plaintext
+     * channels to the stores (the store gRPC server takes no credentials).
+     *
+     * @param graphName the store-side graph name, kept for the meta handler
+     * @param timeoutMs the whole budget for PD plus stores
+     */
+    public static Map<String, Object> probe(String graphName, long timeoutMs) {

Review Comment:
   ๐Ÿงน `graphName` is never read in this method โ€” the javadoc even says "kept for 
the meta handler", which is scaffolding for a use that doesn't exist yet. The 
call site in `HstoreStore` builds `this.namespace + "/" + this.store` just to 
feed it.
   
   Requested change: drop the parameter here and the concat at the call site. 
Re-add it the day something reads it โ€” `pd.getActiveStores(graphName)` exists 
if a per-graph store list is ever wanted.



-- 
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