tkalkirill commented on code in PR #1461:
URL: https://github.com/apache/ignite-3/pull/1461#discussion_r1058239596


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/Cluster.java:
##########
@@ -0,0 +1,346 @@
+/*
+ * 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;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName;
+import static 
org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.IgnitionManager;
+import org.apache.ignite.internal.app.IgniteImpl;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.raft.RaftNodeId;
+import org.apache.ignite.internal.raft.server.impl.JraftServerImpl;
+import 
org.apache.ignite.internal.table.distributed.replicator.TablePartitionId;
+import org.apache.ignite.lang.IgniteStringFormatter;
+import org.apache.ignite.raft.jraft.RaftGroupService;
+import org.apache.ignite.raft.jraft.util.concurrent.ConcurrentHashSet;
+import org.apache.ignite.sql.Session;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Cluster of nodes used for testing.
+ */
+@SuppressWarnings("resource")
+public class Cluster {
+    private static final IgniteLogger LOG = Loggers.forClass(Cluster.class);
+
+    /** Base port number. */
+    private static final int BASE_PORT = 3344;
+
+    private static final String CONNECT_NODE_ADDR = "\"localhost:" + BASE_PORT 
+ '\"';
+
+    /** Timeout for SQL queries (in milliseconds). */
+    private static final int QUERY_TIMEOUT_MS = 10_000;
+
+    private final TestInfo testInfo;
+
+    private final Path workDir;
+
+    private final String nodeBootstrapConfig;
+
+    /** Cluster nodes. */
+    private final List<IgniteImpl> nodes = new CopyOnWriteArrayList<>();
+
+    private volatile boolean started = false;
+
+    /** Indices of nodes that have been knocked out. */
+    private final Set<Integer> knockedOutIndices = new ConcurrentHashSet<>();
+
+    /**
+     * Creates a new instance.
+     */
+    public Cluster(TestInfo testInfo, Path workDir, String 
nodeBootstrapConfig) {
+        this.testInfo = testInfo;
+        this.workDir = workDir;
+        this.nodeBootstrapConfig = nodeBootstrapConfig;
+    }
+
+    /**
+     * Starts the cluster with the given number of nodes and initializes it.
+     *
+     * @param nodeCount Number of nodes in the cluster.
+     */
+    public void startAndInit(int nodeCount) {
+        if (started) {
+            throw new IllegalStateException("The cluster is already started");
+        }
+
+        List<CompletableFuture<IgniteImpl>> futures = IntStream.range(0, 
nodeCount)
+                .mapToObj(this::startClusterNode)
+                .collect(toList());
+
+        String metaStorageAndCmgNodeName = testNodeName(testInfo, 0);
+
+        IgnitionManager.init(metaStorageAndCmgNodeName, 
List.of(metaStorageAndCmgNodeName), "cluster");
+
+        for (CompletableFuture<IgniteImpl> future : futures) {
+            assertThat(future, willCompleteSuccessfully());
+
+            nodes.add(future.join());
+        }
+
+        started = true;
+    }
+
+    private CompletableFuture<IgniteImpl> startClusterNode(int nodeIndex) {
+        String nodeName = testNodeName(testInfo, nodeIndex);
+
+        String config = IgniteStringFormatter.format(nodeBootstrapConfig, 
BASE_PORT + nodeIndex, CONNECT_NODE_ADDR);
+
+        return IgnitionManager.start(nodeName, config, 
workDir.resolve(nodeName))
+                .thenApply(IgniteImpl.class::cast);
+    }
+
+    /**
+     * Returns an Ignite node (a member of the cluster) by its index.
+     */
+    public IgniteImpl node(int index) {
+        return nodes.get(index);
+    }
+
+    /**
+     * Returns a node that is not stopped and not knocked out (so it can be 
used to interact with the cluster).
+     */
+    public IgniteImpl entryNode() {
+        return IntStream.range(0, nodes.size())
+                .filter(index -> nodes.get(index) != null)
+                .filter(index -> !knockedOutIndices.contains(index))
+                .mapToObj(nodes::get)
+                .findAny()
+                .orElseThrow(() -> new IllegalStateException("There is no 
single alive node that would not be knocked out"));
+    }
+
+    /**
+     * Starts a new node with the given index.
+     *
+     * @param index Node index.
+     * @return Started node (if the cluster is already initialized, the node 
is returned when it joins the cluster; if it
+     *     is not initialized, the node is returned in a state in which it is 
ready to join the cluster).
+     */
+    public IgniteImpl startNode(int index) {
+        IgniteImpl newIgniteNode;
+
+        try {
+            newIgniteNode = startClusterNode(index).get(10, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+
+            throw new RuntimeException(e);
+        } catch (ExecutionException | TimeoutException e) {
+            throw new RuntimeException(e);
+        }
+
+        nodes.set(index, newIgniteNode);
+
+        return newIgniteNode;
+    }
+
+    /**
+     * Stops a node by index.
+     *
+     * @param index Node index in the cluster.
+     */
+    public void stopNode(int index) {

Review Comment:
   What happens if there is no node?



##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/Cluster.java:
##########
@@ -0,0 +1,346 @@
+/*
+ * 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;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName;
+import static 
org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.IgnitionManager;
+import org.apache.ignite.internal.app.IgniteImpl;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.raft.RaftNodeId;
+import org.apache.ignite.internal.raft.server.impl.JraftServerImpl;
+import 
org.apache.ignite.internal.table.distributed.replicator.TablePartitionId;
+import org.apache.ignite.lang.IgniteStringFormatter;
+import org.apache.ignite.raft.jraft.RaftGroupService;
+import org.apache.ignite.raft.jraft.util.concurrent.ConcurrentHashSet;
+import org.apache.ignite.sql.Session;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Cluster of nodes used for testing.
+ */
+@SuppressWarnings("resource")
+public class Cluster {
+    private static final IgniteLogger LOG = Loggers.forClass(Cluster.class);
+
+    /** Base port number. */
+    private static final int BASE_PORT = 3344;
+
+    private static final String CONNECT_NODE_ADDR = "\"localhost:" + BASE_PORT 
+ '\"';
+
+    /** Timeout for SQL queries (in milliseconds). */
+    private static final int QUERY_TIMEOUT_MS = 10_000;
+
+    private final TestInfo testInfo;
+
+    private final Path workDir;
+
+    private final String nodeBootstrapConfig;
+
+    /** Cluster nodes. */
+    private final List<IgniteImpl> nodes = new CopyOnWriteArrayList<>();
+
+    private volatile boolean started = false;
+
+    /** Indices of nodes that have been knocked out. */
+    private final Set<Integer> knockedOutIndices = new ConcurrentHashSet<>();
+
+    /**
+     * Creates a new instance.
+     */
+    public Cluster(TestInfo testInfo, Path workDir, String 
nodeBootstrapConfig) {
+        this.testInfo = testInfo;
+        this.workDir = workDir;
+        this.nodeBootstrapConfig = nodeBootstrapConfig;
+    }
+
+    /**
+     * Starts the cluster with the given number of nodes and initializes it.
+     *
+     * @param nodeCount Number of nodes in the cluster.
+     */
+    public void startAndInit(int nodeCount) {
+        if (started) {
+            throw new IllegalStateException("The cluster is already started");
+        }
+
+        List<CompletableFuture<IgniteImpl>> futures = IntStream.range(0, 
nodeCount)
+                .mapToObj(this::startClusterNode)
+                .collect(toList());
+
+        String metaStorageAndCmgNodeName = testNodeName(testInfo, 0);
+
+        IgnitionManager.init(metaStorageAndCmgNodeName, 
List.of(metaStorageAndCmgNodeName), "cluster");
+
+        for (CompletableFuture<IgniteImpl> future : futures) {
+            assertThat(future, willCompleteSuccessfully());
+
+            nodes.add(future.join());
+        }
+
+        started = true;
+    }
+
+    private CompletableFuture<IgniteImpl> startClusterNode(int nodeIndex) {
+        String nodeName = testNodeName(testInfo, nodeIndex);
+
+        String config = IgniteStringFormatter.format(nodeBootstrapConfig, 
BASE_PORT + nodeIndex, CONNECT_NODE_ADDR);
+
+        return IgnitionManager.start(nodeName, config, 
workDir.resolve(nodeName))
+                .thenApply(IgniteImpl.class::cast);
+    }
+
+    /**
+     * Returns an Ignite node (a member of the cluster) by its index.
+     */
+    public IgniteImpl node(int index) {
+        return nodes.get(index);
+    }
+
+    /**
+     * Returns a node that is not stopped and not knocked out (so it can be 
used to interact with the cluster).
+     */
+    public IgniteImpl entryNode() {
+        return IntStream.range(0, nodes.size())
+                .filter(index -> nodes.get(index) != null)
+                .filter(index -> !knockedOutIndices.contains(index))
+                .mapToObj(nodes::get)
+                .findAny()
+                .orElseThrow(() -> new IllegalStateException("There is no 
single alive node that would not be knocked out"));
+    }
+
+    /**
+     * Starts a new node with the given index.
+     *
+     * @param index Node index.
+     * @return Started node (if the cluster is already initialized, the node 
is returned when it joins the cluster; if it
+     *     is not initialized, the node is returned in a state in which it is 
ready to join the cluster).
+     */
+    public IgniteImpl startNode(int index) {
+        IgniteImpl newIgniteNode;
+
+        try {
+            newIgniteNode = startClusterNode(index).get(10, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+
+            throw new RuntimeException(e);
+        } catch (ExecutionException | TimeoutException e) {
+            throw new RuntimeException(e);
+        }
+
+        nodes.set(index, newIgniteNode);
+
+        return newIgniteNode;
+    }
+
+    /**
+     * Stops a node by index.
+     *
+     * @param index Node index in the cluster.
+     */
+    public void stopNode(int index) {
+        IgnitionManager.stop(nodes.get(index).name());
+
+        nodes.set(index, null);
+    }
+
+    /**
+     * Restarts a node by index.
+     *
+     * @param index Node index in the cluster.
+     * @return New node.
+     */
+    public IgniteImpl restartNode(int index) {

Review Comment:
   What happens if there is no node?



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

Reply via email to