Search in sources :

Example 1 with Role

use of io.atomix.raft.RaftServer.Role in project zeebe by camunda.

the class RaftTest method shouldFailOverOnLeaderDisconnect.

@Test
public void shouldFailOverOnLeaderDisconnect() throws Throwable {
    final List<RaftServer> servers = createServers(3);
    final RaftServer leader = getLeader(servers).orElseThrow();
    final MemberId leaderId = leader.getContext().getCluster().getLocalMember().memberId();
    final CountDownLatch newLeaderElected = new CountDownLatch(1);
    final AtomicReference<MemberId> newLeaderId = new AtomicReference<>();
    servers.forEach(s -> s.addRoleChangeListener((role, term) -> {
        if (role == Role.LEADER && !s.equals(leader)) {
            newLeaderId.set(s.getContext().getCluster().getLocalMember().memberId());
            newLeaderElected.countDown();
        }
    }));
    // when
    protocolFactory.partition(leaderId);
    // then
    assertThat(newLeaderElected.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(leaderId).isNotEqualTo(newLeaderId.get());
}
Also used : Arrays(java.util.Arrays) Role(io.atomix.raft.RaftServer.Role) ZeebeLogAppender(io.atomix.raft.zeebe.ZeebeLogAppender) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IndexedRaftLogEntry(io.atomix.raft.storage.log.IndexedRaftLogEntry) ByteBuffer(java.nio.ByteBuffer) RaftLog(io.atomix.raft.storage.log.RaftLog) BooleanSupplier(java.util.function.BooleanSupplier) Assert.assertThat(org.junit.Assert.assertThat) RaftRoleMetrics(io.atomix.raft.metrics.RaftRoleMetrics) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) Is.is(org.hamcrest.core.Is.is) AssertionsForClassTypes.assertThatThrownBy(org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) RaftMember(io.atomix.raft.cluster.RaftMember) Builder(io.atomix.raft.RaftServer.Builder) FailureListener(io.camunda.zeebe.util.health.FailureListener) Collectors(java.util.stream.Collectors) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) RaftStorage(io.atomix.raft.storage.RaftStorage) CountDownLatch(java.util.concurrent.CountDownLatch) TestSnapshotStore(io.atomix.raft.snapshot.TestSnapshotStore) List(java.util.List) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RaftLogReader(io.atomix.raft.storage.log.RaftLogReader) TestRaftProtocolFactory(io.atomix.raft.protocol.TestRaftProtocolFactory) LogCorrupter(io.camunda.zeebe.journal.file.LogCorrupter) InitialEntry(io.atomix.raft.storage.log.entry.InitialEntry) TestRaftServerProtocol(io.atomix.raft.protocol.TestRaftServerProtocol) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Mockito.timeout(org.mockito.Mockito.timeout) TestMember(io.atomix.raft.primitive.TestMember) RaftPartitionConfig(io.atomix.raft.partition.RaftPartitionConfig) MemberId(io.atomix.cluster.MemberId) LeaderRole(io.atomix.raft.roles.LeaderRole) Before(org.junit.Before) Test(org.junit.Test) Maps(com.google.common.collect.Maps) CorruptedLogException(io.camunda.zeebe.journal.file.record.CorruptedLogException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) HealthReport(io.camunda.zeebe.util.health.HealthReport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Ignore(org.junit.Ignore) TemporaryFolder(org.junit.rules.TemporaryFolder) MemberId(io.atomix.cluster.MemberId) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Role

use of io.atomix.raft.RaftServer.Role in project zeebe by camunda.

the class RaftRolesTest method testStepDownInRoleChangedListener.

@Test
public void testStepDownInRoleChangedListener() throws Exception {
    // given
    final CompletableFuture<Void> roleChanged = new CompletableFuture<>();
    final CountDownLatch followerLatch = new CountDownLatch(2);
    final List<Role> roles = new ArrayList<>();
    startSingleNodeSinglePartitionWithPartitionConsumer(partition -> {
        final RaftPartition raftPartition = (RaftPartition) partition;
        raftPartition.addRoleChangeListener((role, term) -> {
            roles.add(role);
            if (!roleChanged.isDone() && role == Role.LEADER) {
                roleChanged.complete(null);
                // when
                raftPartition.stepDown();
            } else if (role == Role.FOLLOWER) {
                followerLatch.countDown();
            }
        });
    }).join();
    // then
    roleChanged.get(60, TimeUnit.SECONDS);
    followerLatch.await(10, TimeUnit.SECONDS);
    // single node becomes directly leader again
    assertThat(roles).containsSequence(Role.INACTIVE, Role.LEADER, Role.LEADER);
}
Also used : Role(io.atomix.raft.RaftServer.Role) AtomixClusterBuilder(io.atomix.cluster.AtomixClusterBuilder) AtomixClusterRule(io.atomix.cluster.AtomixClusterRule) Role(io.atomix.raft.RaftServer.Role) DefaultPartitionService(io.atomix.primitive.partition.impl.DefaultPartitionService) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomixCluster(io.atomix.cluster.AtomixCluster) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) Partition(io.atomix.primitive.partition.Partition) RaftPartition(io.atomix.raft.partition.RaftPartition) LangUtil(org.agrona.LangUtil) RaftPartitionGroup(io.atomix.raft.partition.RaftPartitionGroup) NoopSnapshotStoreFactory(io.atomix.cluster.NoopSnapshotStoreFactory) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) RaftPartition(io.atomix.raft.partition.RaftPartition) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with Role

use of io.atomix.raft.RaftServer.Role in project zeebe by zeebe-io.

the class RaftTest method testRoleChangeNotificationAfterInitialEntryOnLeader.

@Test
public void testRoleChangeNotificationAfterInitialEntryOnLeader() throws Throwable {
    // given
    final List<RaftServer> servers = createServers(3);
    final RaftServer previousLeader = getLeader(servers).get();
    final long previousLeaderTerm = previousLeader.getTerm();
    final CountDownLatch transitionCompleted = new CountDownLatch(1);
    servers.forEach(server -> server.addRoleChangeListener((role, term) -> {
        if (term > previousLeaderTerm) {
            assertLastReadInitialEntry(role, term, server, transitionCompleted);
        }
    }));
    // when
    previousLeader.stepDown();
    // then
    assertThat(transitionCompleted.await(1000, TimeUnit.SECONDS)).isTrue();
}
Also used : Arrays(java.util.Arrays) Role(io.atomix.raft.RaftServer.Role) ZeebeLogAppender(io.atomix.raft.zeebe.ZeebeLogAppender) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IndexedRaftLogEntry(io.atomix.raft.storage.log.IndexedRaftLogEntry) ByteBuffer(java.nio.ByteBuffer) RaftLog(io.atomix.raft.storage.log.RaftLog) Assert.assertThat(org.junit.Assert.assertThat) RaftRoleMetrics(io.atomix.raft.metrics.RaftRoleMetrics) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) Is.is(org.hamcrest.core.Is.is) AssertionsForClassTypes.assertThatThrownBy(org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) RaftMember(io.atomix.raft.cluster.RaftMember) Builder(io.atomix.raft.RaftServer.Builder) FailureListener(io.camunda.zeebe.util.health.FailureListener) Collectors(java.util.stream.Collectors) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) RaftStorage(io.atomix.raft.storage.RaftStorage) CountDownLatch(java.util.concurrent.CountDownLatch) TestSnapshotStore(io.atomix.raft.snapshot.TestSnapshotStore) List(java.util.List) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RaftLogReader(io.atomix.raft.storage.log.RaftLogReader) TestRaftProtocolFactory(io.atomix.raft.protocol.TestRaftProtocolFactory) InitialEntry(io.atomix.raft.storage.log.entry.InitialEntry) TestRaftServerProtocol(io.atomix.raft.protocol.TestRaftServerProtocol) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Mockito.timeout(org.mockito.Mockito.timeout) TestMember(io.atomix.raft.primitive.TestMember) RaftPartitionConfig(io.atomix.raft.partition.RaftPartitionConfig) MemberId(io.atomix.cluster.MemberId) LeaderRole(io.atomix.raft.roles.LeaderRole) Before(org.junit.Before) Files(java.nio.file.Files) Test(org.junit.Test) Maps(com.google.common.collect.Maps) CorruptedLogException(io.camunda.zeebe.journal.file.record.CorruptedLogException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) HealthReport(io.camunda.zeebe.util.health.HealthReport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Ignore(org.junit.Ignore) TemporaryFolder(org.junit.rules.TemporaryFolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Role

use of io.atomix.raft.RaftServer.Role in project zeebe by camunda-cloud.

the class RaftTest method testRoleChangeNotificationAfterInitialEntryOnLeader.

@Test
public void testRoleChangeNotificationAfterInitialEntryOnLeader() throws Throwable {
    // given
    final List<RaftServer> servers = createServers(3);
    final RaftServer previousLeader = getLeader(servers).get();
    final long previousLeaderTerm = previousLeader.getTerm();
    final CountDownLatch transitionCompleted = new CountDownLatch(1);
    servers.forEach(server -> server.addRoleChangeListener((role, term) -> {
        if (term > previousLeaderTerm) {
            assertLastReadInitialEntry(role, term, server, transitionCompleted);
        }
    }));
    // when
    previousLeader.stepDown();
    // then
    assertThat(transitionCompleted.await(1000, TimeUnit.SECONDS)).isTrue();
}
Also used : Arrays(java.util.Arrays) Role(io.atomix.raft.RaftServer.Role) ZeebeLogAppender(io.atomix.raft.zeebe.ZeebeLogAppender) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IndexedRaftLogEntry(io.atomix.raft.storage.log.IndexedRaftLogEntry) ByteBuffer(java.nio.ByteBuffer) RaftLog(io.atomix.raft.storage.log.RaftLog) Assert.assertThat(org.junit.Assert.assertThat) RaftRoleMetrics(io.atomix.raft.metrics.RaftRoleMetrics) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) Is.is(org.hamcrest.core.Is.is) AssertionsForClassTypes.assertThatThrownBy(org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) RaftMember(io.atomix.raft.cluster.RaftMember) Builder(io.atomix.raft.RaftServer.Builder) FailureListener(io.camunda.zeebe.util.health.FailureListener) Collectors(java.util.stream.Collectors) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) RaftStorage(io.atomix.raft.storage.RaftStorage) CountDownLatch(java.util.concurrent.CountDownLatch) TestSnapshotStore(io.atomix.raft.snapshot.TestSnapshotStore) List(java.util.List) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RaftLogReader(io.atomix.raft.storage.log.RaftLogReader) TestRaftProtocolFactory(io.atomix.raft.protocol.TestRaftProtocolFactory) InitialEntry(io.atomix.raft.storage.log.entry.InitialEntry) TestRaftServerProtocol(io.atomix.raft.protocol.TestRaftServerProtocol) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Mockito.timeout(org.mockito.Mockito.timeout) TestMember(io.atomix.raft.primitive.TestMember) RaftPartitionConfig(io.atomix.raft.partition.RaftPartitionConfig) MemberId(io.atomix.cluster.MemberId) LeaderRole(io.atomix.raft.roles.LeaderRole) Before(org.junit.Before) Files(java.nio.file.Files) Test(org.junit.Test) Maps(com.google.common.collect.Maps) CorruptedLogException(io.camunda.zeebe.journal.file.record.CorruptedLogException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) HealthReport(io.camunda.zeebe.util.health.HealthReport) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Ignore(org.junit.Ignore) TemporaryFolder(org.junit.rules.TemporaryFolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with Role

use of io.atomix.raft.RaftServer.Role in project zeebe by camunda-cloud.

the class RaftServerDisconnectTest method shouldFailOverOnLeaderDisconnect.

@Test
public void shouldFailOverOnLeaderDisconnect() throws Throwable {
    final RaftServer leader = raftRule.getLeader().orElseThrow();
    final MemberId leaderId = leader.getContext().getCluster().getLocalMember().memberId();
    final CountDownLatch newLeaderElected = new CountDownLatch(1);
    final AtomicReference<MemberId> newLeaderId = new AtomicReference<>();
    raftRule.getServers().forEach(s -> s.addRoleChangeListener((role, term) -> {
        if (role == Role.LEADER && !s.equals(leader)) {
            newLeaderId.set(s.getContext().getCluster().getLocalMember().memberId());
            newLeaderElected.countDown();
        }
    }));
    // when
    raftRule.partition(leader);
    // then
    assertThat(newLeaderElected.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(leaderId).isNotEqualTo(newLeaderId.get());
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Role(io.atomix.raft.RaftServer.Role) Rule(org.junit.Rule) Parameter(org.junit.runners.Parameterized.Parameter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MemberId(io.atomix.cluster.MemberId) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Awaitility(org.awaitility.Awaitility) MemberId(io.atomix.cluster.MemberId) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Role (io.atomix.raft.RaftServer.Role)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 Test (org.junit.Test)14 TimeUnit (java.util.concurrent.TimeUnit)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Rule (org.junit.Rule)11 LeaderRole (io.atomix.raft.roles.LeaderRole)9 File (java.io.File)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 Collectors (java.util.stream.Collectors)9 MemberId (io.atomix.cluster.MemberId)8 Maps (com.google.common.collect.Maps)6 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)6 Builder (io.atomix.raft.RaftServer.Builder)6 RaftMember (io.atomix.raft.cluster.RaftMember)6 RaftRoleMetrics (io.atomix.raft.metrics.RaftRoleMetrics)6