You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "valepakh (via GitHub)" <gi...@apache.org> on 2023/05/03 10:19:13 UTC

[GitHub] [ignite-3] valepakh commented on a diff in pull request #2008: IGNITE-19316 Fix ItIgnitePicocliCommandsTest

valepakh commented on code in PR #2008:
URL: https://github.com/apache/ignite-3/pull/2008#discussion_r1183494595


##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/registry/impl/MetricRegistryImpl.java:
##########
@@ -49,23 +52,19 @@ public Set<String> metricSources() {
      */
     @Override
     public void onConnect(SessionInfo sessionInfo) {
-        CompletableFuture.runAsync(() -> {
-            try {
-                //TODO https://issues.apache.org/jira/browse/IGNITE-17416
-                metricSourceListCall.execute(new UrlCallInput(sessionInfo.nodeUrl()))
-                        .body()
-                        .forEach(source -> metricSources.add(source.getName()));
-            } catch (Exception ignored) {
-                // no-op
-            }
-        });
+        metricSourcesRef = new LazyObjectRef<>(() -> fetchMetricSources(sessionInfo));
+    }
+
+    @NotNull

Review Comment:
   ```suggestion
   ```



##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/registry/impl/LazyObjectRef.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.ignite.internal.cli.core.repl.registry.impl;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
+import org.apache.ignite.internal.cli.logger.CliLoggers;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.jetbrains.annotations.Nullable;
+
+/** Lazy async reference that will fetch the value from the source until the value is not null. */
+public final class LazyObjectRef<R> {
+
+    private static final IgniteLogger LOG = CliLoggers.forClass(LazyObjectRef.class);
+
+    private final Supplier<R> source;
+
+    private final AtomicReference<R> ref = new AtomicReference<>(null);
+
+    private final AtomicReference<Boolean> execInProgress = new AtomicReference<>(false);
+
+    public LazyObjectRef(Supplier<R> source) {
+        this.source = source;
+        fetchFrom(source);
+    }
+
+    private void fetchFrom(Supplier<R> source) {
+        execInProgress.set(true);
+
+        CompletableFuture.supplyAsync(source)
+                .thenAccept(ref::set)
+                .whenComplete((v, t) -> {
+                    if (t != null) {
+                        LOG.warn("Got exception when fetch from source", t);
+                    }
+                    execInProgress.set(false);
+                });
+    }
+
+    /** Returns null if the fetching in progress or the value from the source. */

Review Comment:
   ```suggestion
       /** Returns {@code null} if the fetching is in progress or the value returned from the source is {@code null}. */
   ```



##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/core/repl/registry/impl/ClusterConfigRegistryImpl.java:
##########
@@ -34,25 +32,23 @@ public class ClusterConfigRegistryImpl implements ClusterConfigRegistry, AsyncSe
 
     private final ClusterConfigShowCall clusterConfigShowCall;
 
-    private final AtomicReference<Config> config = new AtomicReference<>(null);
+    private LazyObjectRef<Config> configRef;
 
     public ClusterConfigRegistryImpl(ClusterConfigShowCall clusterConfigShowCall) {
         this.clusterConfigShowCall = clusterConfigShowCall;
     }
 
     @Override
     public void onConnect(SessionInfo sessionInfo) {
-        CompletableFuture.runAsync(() -> {
-            try {
-                config.set(ConfigFactory.parseString(
-                        clusterConfigShowCall.execute(
-                                ClusterConfigShowCallInput.builder().clusterUrl(sessionInfo.nodeUrl()).build()
-                        ).body().getValue()
-                ));
-            } catch (Exception ignored) {
-                // no-op
-            }
-        });
+        configRef = new LazyObjectRef<>(() -> fetchConfig(sessionInfo));

Review Comment:
   Should we null the reference in `onDisconnect` here and in the `NodeConfigRegistryImpl`?



-- 
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: notifications-unsubscribe@ignite.apache.org

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