rpuch commented on code in PR #1461: URL: https://github.com/apache/ignite-3/pull/1461#discussion_r1058306819
########## 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) { Review Comment: This method should never return `null`, added a check -- 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]
