ibessonov commented on code in PR #1236:
URL: https://github.com/apache/ignite-3/pull/1236#discussion_r1003115338


##########
modules/raft-client/src/main/java/org/apache/ignite/raft/client/service/RaftGroupService.java:
##########
@@ -239,18 +237,6 @@ public interface RaftGroupService {
      */
     <R> CompletableFuture<R> run(Command cmd);
 
-    /**
-     * Runs a read command on a given peer.
-     *
-     * <p>Read commands can see stale data (in the past).
-     *
-     * @param peer Peer id.
-     * @param cmd  The command.
-     * @param <R>  Execution result type.
-     * @return A future with the execution result.
-     */
-    <R> CompletableFuture<R> run(Peer peer, ReadCommand cmd);

Review Comment:
   This is interesting, how are we getting rid of read commands? Is there 
another way to run them? Or this API is unused?



##########
modules/raft/src/integrationTest/java/org/apache/ignite/internal/raft/ItLearnersTest.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.raft;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.raft.server.RaftGroupEventsListener.noopLsnr;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.hlc.HybridClock;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
+import org.apache.ignite.internal.raft.server.RaftGroupOptions;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.raft.client.Peer;
+import org.apache.ignite.raft.client.ReadCommand;
+import org.apache.ignite.raft.client.WriteCommand;
+import org.apache.ignite.raft.client.service.CommandClosure;
+import org.apache.ignite.raft.client.service.RaftGroupListener;
+import org.apache.ignite.raft.client.service.RaftGroupService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests for the Raft Learner functionality.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class ItLearnersTest extends IgniteAbstractTest {
+    private static final String RAFT_GROUP_NAME = "test";
+
+    private static final List<NetworkAddress> ADDRS = List.of(
+            new NetworkAddress("localhost", 5000),
+            new NetworkAddress("localhost", 5001),
+            new NetworkAddress("localhost", 5002)
+    );
+
+    @InjectConfiguration
+    private static RaftConfiguration raftConfiguration;
+
+    private final List<RaftNode> nodes = new ArrayList<>(ADDRS.size());
+
+    /** Mock Raft node. */
+    private class RaftNode implements AutoCloseable {
+        final ClusterService clusterService;
+
+        final Loza loza;
+
+        RaftNode(ClusterService clusterService) {
+            this.clusterService = clusterService;
+
+            Path raftDir = 
workDir.resolve(clusterService.localConfiguration().getName());
+
+            loza = new Loza(clusterService, raftConfiguration, raftDir, new 
HybridClock());
+        }
+
+        ClusterNode localMember() {
+            return clusterService.topologyService().localMember();
+        }
+
+        Peer asPeer() {
+            return new Peer(localMember().address());
+        }
+
+        void start() {
+            clusterService.start();
+            loza.start();
+        }
+
+        @Override
+        public void close() throws Exception {
+            closeAll(
+                    loza == null ? null : () -> 
loza.stopRaftGroup(RAFT_GROUP_NAME),
+                    loza == null ? null : loza::beforeNodeStop,
+                    clusterService == null ? null : 
clusterService::beforeNodeStop,
+                    loza == null ? null : loza::stop,
+                    clusterService == null ? null : clusterService::stop
+            );
+        }
+    }
+
+    @BeforeEach
+    void setUp(TestInfo testInfo, @InjectConfiguration RaftConfiguration 
raftConfiguration) {

Review Comment:
   There are two RAFT configurations in the test, field and parameter, maybe 
you forgot to delete one of them



##########
modules/raft/src/integrationTest/java/org/apache/ignite/internal/raft/ItLearnersTest.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.raft;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.raft.server.RaftGroupEventsListener.noopLsnr;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.hlc.HybridClock;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
+import org.apache.ignite.internal.raft.server.RaftGroupOptions;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.raft.client.Peer;
+import org.apache.ignite.raft.client.ReadCommand;
+import org.apache.ignite.raft.client.WriteCommand;
+import org.apache.ignite.raft.client.service.CommandClosure;
+import org.apache.ignite.raft.client.service.RaftGroupListener;
+import org.apache.ignite.raft.client.service.RaftGroupService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests for the Raft Learner functionality.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class ItLearnersTest extends IgniteAbstractTest {
+    private static final String RAFT_GROUP_NAME = "test";
+
+    private static final List<NetworkAddress> ADDRS = List.of(
+            new NetworkAddress("localhost", 5000),
+            new NetworkAddress("localhost", 5001),
+            new NetworkAddress("localhost", 5002)
+    );
+
+    @InjectConfiguration
+    private static RaftConfiguration raftConfiguration;
+
+    private final List<RaftNode> nodes = new ArrayList<>(ADDRS.size());
+
+    /** Mock Raft node. */
+    private class RaftNode implements AutoCloseable {
+        final ClusterService clusterService;
+
+        final Loza loza;
+
+        RaftNode(ClusterService clusterService) {
+            this.clusterService = clusterService;
+
+            Path raftDir = 
workDir.resolve(clusterService.localConfiguration().getName());
+
+            loza = new Loza(clusterService, raftConfiguration, raftDir, new 
HybridClock());
+        }
+
+        ClusterNode localMember() {
+            return clusterService.topologyService().localMember();
+        }
+
+        Peer asPeer() {
+            return new Peer(localMember().address());
+        }
+
+        void start() {
+            clusterService.start();
+            loza.start();
+        }
+
+        @Override
+        public void close() throws Exception {
+            closeAll(
+                    loza == null ? null : () -> 
loza.stopRaftGroup(RAFT_GROUP_NAME),
+                    loza == null ? null : loza::beforeNodeStop,
+                    clusterService == null ? null : 
clusterService::beforeNodeStop,
+                    loza == null ? null : loza::stop,
+                    clusterService == null ? null : clusterService::stop
+            );
+        }
+    }
+
+    @BeforeEach
+    void setUp(TestInfo testInfo, @InjectConfiguration RaftConfiguration 
raftConfiguration) {
+        var nodeFinder = new StaticNodeFinder(ADDRS);
+
+        ADDRS.stream()
+                .map(addr -> clusterService(testInfo, addr.port(), nodeFinder))
+                .map(RaftNode::new)
+                .forEach(nodes::add);
+
+        nodes.parallelStream().forEach(RaftNode::start);
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        closeAll(nodes);
+    }
+
+    /**
+     * Tests that it is possible to replicate and read data from learners.
+     */
+    @Test
+    public void testReadWriteLearners() throws Exception {
+        List<TestRaftGroupListener> listeners = IntStream.range(0, 
nodes.size())
+                .mapToObj(i -> new TestRaftGroupListener())
+                .collect(toList());
+
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = 
IntStream.range(0, nodes.size())
+                .mapToObj(i -> startRaftGroup(nodes.get(i), listeners.get(i), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Check that learners and peers have been set correctly.
+        services.forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+
+        listeners.forEach(listener -> assertThat(listener.storage, 
is(empty())));
+
+        // Test writing data.
+        CompletableFuture<?> writeFuture = services.get(0)
+                .thenCompose(s -> s.run(new 
TestWriteCommand("foo")).thenApply(v -> s))
+                .thenCompose(s -> s.run(new TestWriteCommand("bar")));
+
+        assertThat(writeFuture, willCompleteSuccessfully());
+
+        for (TestRaftGroupListener listener : listeners) {
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("foo"));
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("bar"));
+        }
+    }
+
+    /**
+     * Tests {@link RaftGroupService#addLearners} functionality.
+     */
+    @Test
+    public void testAddLearners() {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        CompletableFuture<RaftGroupService> service1 =
+                startRaftGroup(follower, new TestRaftGroupListener(), 
List.of(follower), List.of());
+
+        assertThat(service1.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+        assertThat(service1.thenApply(RaftGroupService::learners), 
willBe(empty()));
+
+        CompletableFuture<Void> addLearners = service1
+                .thenCompose(s -> 
s.addLearners(Arrays.asList(toPeerArray(learners))));
+
+        assertThat(addLearners, willCompleteSuccessfully());
+
+        CompletableFuture<RaftGroupService> service2 =
+                startRaftGroup(nodes.get(1), new TestRaftGroupListener(), 
List.of(follower), learners);
+
+        // Check that learners and peers have been set correctly.
+        Stream.of(service1, service2).forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+    }
+
+    /**
+     * Tests that if the only follower is stopped, then the majority is lost.
+     */
+    @Test
+    public void testLostLeadership() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(0, null).close();
+
+        assertThat(services.get(1).thenCompose(s -> s.run(new 
TestWriteCommand("foo"))), willThrow(TimeoutException.class));
+    }
+
+    /**
+     * Tests that even if all learners are stopped, then the majority is not 
lost.
+     */
+    @Test
+    public void testLostLearners() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.

Review Comment:
   Comment got duplicated



##########
modules/raft/src/integrationTest/java/org/apache/ignite/internal/raft/ItLearnersTest.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.raft;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.raft.server.RaftGroupEventsListener.noopLsnr;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.hlc.HybridClock;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
+import org.apache.ignite.internal.raft.server.RaftGroupOptions;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.raft.client.Peer;
+import org.apache.ignite.raft.client.ReadCommand;
+import org.apache.ignite.raft.client.WriteCommand;
+import org.apache.ignite.raft.client.service.CommandClosure;
+import org.apache.ignite.raft.client.service.RaftGroupListener;
+import org.apache.ignite.raft.client.service.RaftGroupService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests for the Raft Learner functionality.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class ItLearnersTest extends IgniteAbstractTest {
+    private static final String RAFT_GROUP_NAME = "test";
+
+    private static final List<NetworkAddress> ADDRS = List.of(
+            new NetworkAddress("localhost", 5000),
+            new NetworkAddress("localhost", 5001),
+            new NetworkAddress("localhost", 5002)
+    );
+
+    @InjectConfiguration
+    private static RaftConfiguration raftConfiguration;
+
+    private final List<RaftNode> nodes = new ArrayList<>(ADDRS.size());
+
+    /** Mock Raft node. */
+    private class RaftNode implements AutoCloseable {
+        final ClusterService clusterService;
+
+        final Loza loza;
+
+        RaftNode(ClusterService clusterService) {
+            this.clusterService = clusterService;
+
+            Path raftDir = 
workDir.resolve(clusterService.localConfiguration().getName());
+
+            loza = new Loza(clusterService, raftConfiguration, raftDir, new 
HybridClock());
+        }
+
+        ClusterNode localMember() {
+            return clusterService.topologyService().localMember();
+        }
+
+        Peer asPeer() {
+            return new Peer(localMember().address());
+        }
+
+        void start() {
+            clusterService.start();
+            loza.start();
+        }
+
+        @Override
+        public void close() throws Exception {
+            closeAll(
+                    loza == null ? null : () -> 
loza.stopRaftGroup(RAFT_GROUP_NAME),
+                    loza == null ? null : loza::beforeNodeStop,
+                    clusterService == null ? null : 
clusterService::beforeNodeStop,
+                    loza == null ? null : loza::stop,
+                    clusterService == null ? null : clusterService::stop
+            );
+        }
+    }
+
+    @BeforeEach
+    void setUp(TestInfo testInfo, @InjectConfiguration RaftConfiguration 
raftConfiguration) {
+        var nodeFinder = new StaticNodeFinder(ADDRS);
+
+        ADDRS.stream()
+                .map(addr -> clusterService(testInfo, addr.port(), nodeFinder))
+                .map(RaftNode::new)
+                .forEach(nodes::add);
+
+        nodes.parallelStream().forEach(RaftNode::start);
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        closeAll(nodes);
+    }
+
+    /**
+     * Tests that it is possible to replicate and read data from learners.
+     */
+    @Test
+    public void testReadWriteLearners() throws Exception {
+        List<TestRaftGroupListener> listeners = IntStream.range(0, 
nodes.size())
+                .mapToObj(i -> new TestRaftGroupListener())
+                .collect(toList());
+
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = 
IntStream.range(0, nodes.size())
+                .mapToObj(i -> startRaftGroup(nodes.get(i), listeners.get(i), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Check that learners and peers have been set correctly.
+        services.forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+
+        listeners.forEach(listener -> assertThat(listener.storage, 
is(empty())));
+
+        // Test writing data.
+        CompletableFuture<?> writeFuture = services.get(0)
+                .thenCompose(s -> s.run(new 
TestWriteCommand("foo")).thenApply(v -> s))
+                .thenCompose(s -> s.run(new TestWriteCommand("bar")));
+
+        assertThat(writeFuture, willCompleteSuccessfully());
+
+        for (TestRaftGroupListener listener : listeners) {
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("foo"));
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("bar"));
+        }
+    }
+
+    /**
+     * Tests {@link RaftGroupService#addLearners} functionality.
+     */
+    @Test
+    public void testAddLearners() {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        CompletableFuture<RaftGroupService> service1 =
+                startRaftGroup(follower, new TestRaftGroupListener(), 
List.of(follower), List.of());
+
+        assertThat(service1.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+        assertThat(service1.thenApply(RaftGroupService::learners), 
willBe(empty()));
+
+        CompletableFuture<Void> addLearners = service1
+                .thenCompose(s -> 
s.addLearners(Arrays.asList(toPeerArray(learners))));
+
+        assertThat(addLearners, willCompleteSuccessfully());
+
+        CompletableFuture<RaftGroupService> service2 =
+                startRaftGroup(nodes.get(1), new TestRaftGroupListener(), 
List.of(follower), learners);
+
+        // Check that learners and peers have been set correctly.
+        Stream.of(service1, service2).forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+    }
+
+    /**
+     * Tests that if the only follower is stopped, then the majority is lost.
+     */
+    @Test
+    public void testLostLeadership() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(0, null).close();
+
+        assertThat(services.get(1).thenCompose(s -> s.run(new 
TestWriteCommand("foo"))), willThrow(TimeoutException.class));
+    }
+
+    /**
+     * Tests that even if all learners are stopped, then the majority is not 
lost.
+     */
+    @Test
+    public void testLostLearners() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(1, null).close();
+        nodes.set(2, null).close();
+
+        
assertThat(services.get(0).thenCompose(RaftGroupService::refreshLeader), 
willCompleteSuccessfully());
+    }
+
+    private CompletableFuture<RaftGroupService> startRaftGroup(
+            RaftNode raftNode,
+            RaftGroupListener listener,
+            List<RaftNode> peers,
+            List<RaftNode> learners
+    ) {
+        try {
+            CompletableFuture<RaftGroupService> future = 
raftNode.loza.prepareRaftGroup(
+                    RAFT_GROUP_NAME,
+                    
peers.stream().map(RaftNode::localMember).collect(toList()),
+                    
learners.stream().map(RaftNode::localMember).collect(toList()),
+                    () -> listener,
+                    () -> noopLsnr,
+                    RaftGroupOptions.defaults()
+            );
+
+            return future.thenApply(s -> {
+                s.timeout(100);
+
+                return s;
+            });
+        } catch (NodeStoppingException e) {
+            throw new RuntimeException(e);

Review Comment:
   Why not `fail(...)`?



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/Loza.java:
##########
@@ -305,25 +280,58 @@ public CompletableFuture<RaftGroupService> 
startRaftGroupService(
             throw new NodeStoppingException();
         }
 
-        List<Peer> peers = nodes.stream().map(n -> new 
Peer(n.address())).collect(Collectors.toList());
-
         try {
-            return RaftGroupServiceImpl.start(
-                    grpId,
-                    clusterNetSvc,
-                    FACTORY,
-                    RETRY_TIMEOUT,
-                    RPC_TIMEOUT,
-                    peers,
-                    true,
-                    DELAY,
-                    executor
-            );
+            return startRaftGroupServiceInternal(grpId, nodesToPeers(nodes), 
List.of());
         } finally {
             busyLock.leaveBusy();
         }
     }
 
+    private void startRaftGroupNodeInternal(
+            String grpId,
+            List<Peer> peers,
+            List<Peer> learners,
+            RaftGroupListener lsnr,
+            RaftGroupEventsListener raftGrpEvtsLsnr,
+            RaftGroupOptions groupOptions
+    ) {
+        assert !peers.isEmpty();
+
+        LOG.info("Start new raft node for group={} with initial peers={}", 
grpId, peers);

Review Comment:
   Should we put it into `if (infoEnabled())`? That's usually a good practice



##########
modules/raft/src/integrationTest/java/org/apache/ignite/internal/raft/ItLearnersTest.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.raft;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.raft.server.RaftGroupEventsListener.noopLsnr;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.hlc.HybridClock;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
+import org.apache.ignite.internal.raft.server.RaftGroupOptions;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.raft.client.Peer;
+import org.apache.ignite.raft.client.ReadCommand;
+import org.apache.ignite.raft.client.WriteCommand;
+import org.apache.ignite.raft.client.service.CommandClosure;
+import org.apache.ignite.raft.client.service.RaftGroupListener;
+import org.apache.ignite.raft.client.service.RaftGroupService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests for the Raft Learner functionality.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class ItLearnersTest extends IgniteAbstractTest {
+    private static final String RAFT_GROUP_NAME = "test";
+
+    private static final List<NetworkAddress> ADDRS = List.of(
+            new NetworkAddress("localhost", 5000),
+            new NetworkAddress("localhost", 5001),
+            new NetworkAddress("localhost", 5002)
+    );
+
+    @InjectConfiguration
+    private static RaftConfiguration raftConfiguration;
+
+    private final List<RaftNode> nodes = new ArrayList<>(ADDRS.size());
+
+    /** Mock Raft node. */
+    private class RaftNode implements AutoCloseable {
+        final ClusterService clusterService;
+
+        final Loza loza;
+
+        RaftNode(ClusterService clusterService) {
+            this.clusterService = clusterService;
+
+            Path raftDir = 
workDir.resolve(clusterService.localConfiguration().getName());
+
+            loza = new Loza(clusterService, raftConfiguration, raftDir, new 
HybridClock());
+        }
+
+        ClusterNode localMember() {
+            return clusterService.topologyService().localMember();
+        }
+
+        Peer asPeer() {
+            return new Peer(localMember().address());
+        }
+
+        void start() {
+            clusterService.start();
+            loza.start();
+        }
+
+        @Override
+        public void close() throws Exception {
+            closeAll(
+                    loza == null ? null : () -> 
loza.stopRaftGroup(RAFT_GROUP_NAME),
+                    loza == null ? null : loza::beforeNodeStop,
+                    clusterService == null ? null : 
clusterService::beforeNodeStop,
+                    loza == null ? null : loza::stop,
+                    clusterService == null ? null : clusterService::stop
+            );
+        }
+    }
+
+    @BeforeEach
+    void setUp(TestInfo testInfo, @InjectConfiguration RaftConfiguration 
raftConfiguration) {
+        var nodeFinder = new StaticNodeFinder(ADDRS);
+
+        ADDRS.stream()
+                .map(addr -> clusterService(testInfo, addr.port(), nodeFinder))
+                .map(RaftNode::new)
+                .forEach(nodes::add);
+
+        nodes.parallelStream().forEach(RaftNode::start);
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        closeAll(nodes);
+    }
+
+    /**
+     * Tests that it is possible to replicate and read data from learners.
+     */
+    @Test
+    public void testReadWriteLearners() throws Exception {
+        List<TestRaftGroupListener> listeners = IntStream.range(0, 
nodes.size())
+                .mapToObj(i -> new TestRaftGroupListener())
+                .collect(toList());
+
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = 
IntStream.range(0, nodes.size())
+                .mapToObj(i -> startRaftGroup(nodes.get(i), listeners.get(i), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Check that learners and peers have been set correctly.
+        services.forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+
+        listeners.forEach(listener -> assertThat(listener.storage, 
is(empty())));
+
+        // Test writing data.
+        CompletableFuture<?> writeFuture = services.get(0)
+                .thenCompose(s -> s.run(new 
TestWriteCommand("foo")).thenApply(v -> s))
+                .thenCompose(s -> s.run(new TestWriteCommand("bar")));
+
+        assertThat(writeFuture, willCompleteSuccessfully());
+
+        for (TestRaftGroupListener listener : listeners) {
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("foo"));
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("bar"));
+        }
+    }
+
+    /**
+     * Tests {@link RaftGroupService#addLearners} functionality.
+     */
+    @Test
+    public void testAddLearners() {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        CompletableFuture<RaftGroupService> service1 =
+                startRaftGroup(follower, new TestRaftGroupListener(), 
List.of(follower), List.of());
+
+        assertThat(service1.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+        assertThat(service1.thenApply(RaftGroupService::learners), 
willBe(empty()));
+
+        CompletableFuture<Void> addLearners = service1
+                .thenCompose(s -> 
s.addLearners(Arrays.asList(toPeerArray(learners))));
+
+        assertThat(addLearners, willCompleteSuccessfully());
+
+        CompletableFuture<RaftGroupService> service2 =
+                startRaftGroup(nodes.get(1), new TestRaftGroupListener(), 
List.of(follower), learners);
+
+        // Check that learners and peers have been set correctly.
+        Stream.of(service1, service2).forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+    }
+
+    /**
+     * Tests that if the only follower is stopped, then the majority is lost.
+     */
+    @Test
+    public void testLostLeadership() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(0, null).close();
+
+        assertThat(services.get(1).thenCompose(s -> s.run(new 
TestWriteCommand("foo"))), willThrow(TimeoutException.class));
+    }
+
+    /**
+     * Tests that even if all learners are stopped, then the majority is not 
lost.
+     */
+    @Test
+    public void testLostLearners() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(1, null).close();
+        nodes.set(2, null).close();
+
+        
assertThat(services.get(0).thenCompose(RaftGroupService::refreshLeader), 
willCompleteSuccessfully());
+    }
+
+    private CompletableFuture<RaftGroupService> startRaftGroup(
+            RaftNode raftNode,
+            RaftGroupListener listener,
+            List<RaftNode> peers,
+            List<RaftNode> learners
+    ) {
+        try {
+            CompletableFuture<RaftGroupService> future = 
raftNode.loza.prepareRaftGroup(

Review Comment:
   There should be a version of this method that takes listeners directly, not 
their suppliers. Or maybe I'm wrong



##########
modules/raft/src/integrationTest/java/org/apache/ignite/internal/raft/ItLearnersTest.java:
##########
@@ -0,0 +1,338 @@
+/*
+ * 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.raft;
+
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.raft.server.RaftGroupEventsListener.noopLsnr;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAll;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.hlc.HybridClock;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.raft.configuration.RaftConfiguration;
+import org.apache.ignite.internal.raft.server.RaftGroupOptions;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.lang.NodeStoppingException;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.raft.client.Peer;
+import org.apache.ignite.raft.client.ReadCommand;
+import org.apache.ignite.raft.client.WriteCommand;
+import org.apache.ignite.raft.client.service.CommandClosure;
+import org.apache.ignite.raft.client.service.RaftGroupListener;
+import org.apache.ignite.raft.client.service.RaftGroupService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests for the Raft Learner functionality.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public class ItLearnersTest extends IgniteAbstractTest {
+    private static final String RAFT_GROUP_NAME = "test";
+
+    private static final List<NetworkAddress> ADDRS = List.of(
+            new NetworkAddress("localhost", 5000),
+            new NetworkAddress("localhost", 5001),
+            new NetworkAddress("localhost", 5002)
+    );
+
+    @InjectConfiguration
+    private static RaftConfiguration raftConfiguration;
+
+    private final List<RaftNode> nodes = new ArrayList<>(ADDRS.size());
+
+    /** Mock Raft node. */
+    private class RaftNode implements AutoCloseable {
+        final ClusterService clusterService;
+
+        final Loza loza;
+
+        RaftNode(ClusterService clusterService) {
+            this.clusterService = clusterService;
+
+            Path raftDir = 
workDir.resolve(clusterService.localConfiguration().getName());
+
+            loza = new Loza(clusterService, raftConfiguration, raftDir, new 
HybridClock());
+        }
+
+        ClusterNode localMember() {
+            return clusterService.topologyService().localMember();
+        }
+
+        Peer asPeer() {
+            return new Peer(localMember().address());
+        }
+
+        void start() {
+            clusterService.start();
+            loza.start();
+        }
+
+        @Override
+        public void close() throws Exception {
+            closeAll(
+                    loza == null ? null : () -> 
loza.stopRaftGroup(RAFT_GROUP_NAME),
+                    loza == null ? null : loza::beforeNodeStop,
+                    clusterService == null ? null : 
clusterService::beforeNodeStop,
+                    loza == null ? null : loza::stop,
+                    clusterService == null ? null : clusterService::stop
+            );
+        }
+    }
+
+    @BeforeEach
+    void setUp(TestInfo testInfo, @InjectConfiguration RaftConfiguration 
raftConfiguration) {
+        var nodeFinder = new StaticNodeFinder(ADDRS);
+
+        ADDRS.stream()
+                .map(addr -> clusterService(testInfo, addr.port(), nodeFinder))
+                .map(RaftNode::new)
+                .forEach(nodes::add);
+
+        nodes.parallelStream().forEach(RaftNode::start);
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        closeAll(nodes);
+    }
+
+    /**
+     * Tests that it is possible to replicate and read data from learners.
+     */
+    @Test
+    public void testReadWriteLearners() throws Exception {
+        List<TestRaftGroupListener> listeners = IntStream.range(0, 
nodes.size())
+                .mapToObj(i -> new TestRaftGroupListener())
+                .collect(toList());
+
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = 
IntStream.range(0, nodes.size())
+                .mapToObj(i -> startRaftGroup(nodes.get(i), listeners.get(i), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Check that learners and peers have been set correctly.
+        services.forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+
+        listeners.forEach(listener -> assertThat(listener.storage, 
is(empty())));
+
+        // Test writing data.
+        CompletableFuture<?> writeFuture = services.get(0)
+                .thenCompose(s -> s.run(new 
TestWriteCommand("foo")).thenApply(v -> s))
+                .thenCompose(s -> s.run(new TestWriteCommand("bar")));
+
+        assertThat(writeFuture, willCompleteSuccessfully());
+
+        for (TestRaftGroupListener listener : listeners) {
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("foo"));
+            assertThat(listener.storage.poll(1, TimeUnit.SECONDS), is("bar"));
+        }
+    }
+
+    /**
+     * Tests {@link RaftGroupService#addLearners} functionality.
+     */
+    @Test
+    public void testAddLearners() {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        CompletableFuture<RaftGroupService> service1 =
+                startRaftGroup(follower, new TestRaftGroupListener(), 
List.of(follower), List.of());
+
+        assertThat(service1.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+        assertThat(service1.thenApply(RaftGroupService::learners), 
willBe(empty()));
+
+        CompletableFuture<Void> addLearners = service1
+                .thenCompose(s -> 
s.addLearners(Arrays.asList(toPeerArray(learners))));
+
+        assertThat(addLearners, willCompleteSuccessfully());
+
+        CompletableFuture<RaftGroupService> service2 =
+                startRaftGroup(nodes.get(1), new TestRaftGroupListener(), 
List.of(follower), learners);
+
+        // Check that learners and peers have been set correctly.
+        Stream.of(service1, service2).forEach(service -> {
+            CompletableFuture<RaftGroupService> refreshMembers = service
+                    .thenCompose(s -> s.refreshMembers(true).thenApply(v -> 
s));
+
+            assertThat(refreshMembers.thenApply(RaftGroupService::leader), 
willBe(follower.asPeer()));
+            assertThat(refreshMembers.thenApply(RaftGroupService::peers), 
will(contains(follower.asPeer())));
+            assertThat(refreshMembers.thenApply(RaftGroupService::learners), 
will(containsInAnyOrder(toPeerArray(learners))));
+        });
+    }
+
+    /**
+     * Tests that if the only follower is stopped, then the majority is lost.
+     */
+    @Test
+    public void testLostLeadership() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(0, null).close();
+
+        assertThat(services.get(1).thenCompose(s -> s.run(new 
TestWriteCommand("foo"))), willThrow(TimeoutException.class));
+    }
+
+    /**
+     * Tests that even if all learners are stopped, then the majority is not 
lost.
+     */
+    @Test
+    public void testLostLearners() throws Exception {
+        RaftNode follower = nodes.get(0);
+        List<RaftNode> learners = nodes.subList(1, nodes.size());
+
+        List<CompletableFuture<RaftGroupService>> services = nodes.stream()
+                .map(node -> startRaftGroup(node, new TestRaftGroupListener(), 
List.of(follower), learners))
+                .collect(toList());
+
+        // Wait for the leader to be elected.
+        // Wait for the leader to be elected.
+        services.forEach(service -> {
+            assertThat(service.thenCompose(s -> s.refreshLeader().thenApply(v 
-> s.leader())), willBe(follower.asPeer()));
+        });
+
+        nodes.set(1, null).close();
+        nodes.set(2, null).close();
+
+        
assertThat(services.get(0).thenCompose(RaftGroupService::refreshLeader), 
willCompleteSuccessfully());
+    }
+
+    private CompletableFuture<RaftGroupService> startRaftGroup(
+            RaftNode raftNode,
+            RaftGroupListener listener,
+            List<RaftNode> peers,
+            List<RaftNode> learners
+    ) {
+        try {
+            CompletableFuture<RaftGroupService> future = 
raftNode.loza.prepareRaftGroup(
+                    RAFT_GROUP_NAME,
+                    
peers.stream().map(RaftNode::localMember).collect(toList()),
+                    
learners.stream().map(RaftNode::localMember).collect(toList()),
+                    () -> listener,
+                    () -> noopLsnr,
+                    RaftGroupOptions.defaults()
+            );
+
+            return future.thenApply(s -> {
+                s.timeout(100);

Review Comment:
   Please add a comment for this line. I believe it is here to make tests faster



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