Search in sources :

Example 1 with RaftStorage

use of io.atomix.protocols.raft.storage.RaftStorage in project atomix by atomix.

the class RaftTest method testSnapshotSentOnDataLoss.

@Test
public void testSnapshotSentOnDataLoss() throws Throwable {
    final List<RaftMember> members = Lists.newArrayList(createMember(), createMember(), createMember());
    final Map<MemberId, RaftStorage> storages = members.stream().map(RaftMember::memberId).collect(Collectors.toMap(Function.identity(), this::createStorage));
    final Map<MemberId, RaftServer> servers = storages.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::createServer));
    // wait for cluster to start
    startCluster(servers);
    // fill two segments then compact so we have at least one snapshot
    final RaftClient client = createClient(members);
    final TestPrimitive primitive = createPrimitive(client);
    fillSegment(primitive);
    fillSegment(primitive);
    Futures.allOf(servers.values().stream().map(RaftServer::compact)).thenRun(this::resume);
    await(30000);
    // partition into leader/followers
    final Map<Boolean, List<RaftMember>> collect = members.stream().collect(Collectors.partitioningBy(m -> servers.get(m.memberId()).isLeader()));
    final RaftMember leader = collect.get(true).get(0);
    final RaftStorage leaderStorage = storages.get(leader.memberId());
    final RaftMember slave = collect.get(false).get(0);
    final RaftStorage slaveStorage = storages.get(slave.memberId());
    // shutdown client + primitive
    primitive.close().thenCompose(nothing -> client.close()).thenRun(this::resume);
    await(30000);
    // shutdown other node
    final RaftMember other = collect.get(false).get(1);
    servers.get(other.memberId()).shutdown().thenRun(this::resume);
    await(30000);
    // shutdown slave and recreate from scratch
    final RaftServer slaveServer = recreateServerWithDataLoss(leader, slave, servers.get(slave.memberId()), slaveStorage);
    assertEquals(leaderStorage.openSnapshotStore().getCurrentSnapshotIndex(), slaveStorage.openSnapshotStore().getCurrentSnapshotIndex());
    // and again a second time to ensure the snapshot index of the member is reset
    recreateServerWithDataLoss(leader, slave, slaveServer, slaveStorage);
    assertEquals(leaderStorage.openSnapshotStore().getCurrentSnapshotIndex(), slaveStorage.openSnapshotStore().getCurrentSnapshotIndex());
}
Also used : RaftClusterEvent(io.atomix.protocols.raft.cluster.RaftClusterEvent) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) TimeoutException(java.util.concurrent.TimeoutException) AsyncPrimitive(io.atomix.primitive.AsyncPrimitive) SessionId(io.atomix.primitive.session.SessionId) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) PrimitiveInfo(io.atomix.primitive.PrimitiveInfo) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) DefaultOperationId(io.atomix.primitive.operation.impl.DefaultOperationId) SessionMetadata(io.atomix.primitive.session.SessionMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrimitiveConfig(io.atomix.primitive.config.PrimitiveConfig) After(org.junit.After) Duration(java.time.Duration) Map(java.util.Map) Session(io.atomix.primitive.session.Session) Path(java.nio.file.Path) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ProxyClient(io.atomix.primitive.proxy.ProxyClient) InitializeEntry(io.atomix.protocols.raft.storage.log.entry.InitializeEntry) PrimitiveBuilder(io.atomix.primitive.PrimitiveBuilder) PrimitiveState(io.atomix.primitive.PrimitiveState) Event(io.atomix.primitive.event.Event) Set(java.util.Set) RaftStorage(io.atomix.protocols.raft.storage.RaftStorage) Instant(java.time.Instant) CommandEntry(io.atomix.protocols.raft.storage.log.entry.CommandEntry) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) PrimitiveRegistry(io.atomix.primitive.PrimitiveRegistry) Matchers.any(org.mockito.Matchers.any) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) PrimitiveService(io.atomix.primitive.service.PrimitiveService) ServiceConfig(io.atomix.primitive.service.ServiceConfig) KeepAliveEntry(io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry) MetadataEntry(io.atomix.protocols.raft.storage.log.entry.MetadataEntry) BackupOutput(io.atomix.primitive.service.BackupOutput) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) PrimitiveException(io.atomix.primitive.PrimitiveException) Serializer(io.atomix.utils.serializer.Serializer) QueryEntry(io.atomix.protocols.raft.storage.log.entry.QueryEntry) Mockito.mock(org.mockito.Mockito.mock) BackupInput(io.atomix.primitive.service.BackupInput) IntStream(java.util.stream.IntStream) SyncPrimitive(io.atomix.primitive.SyncPrimitive) OperationType(io.atomix.primitive.operation.OperationType) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) StorageLevel(io.atomix.storage.StorageLevel) HashSet(java.util.HashSet) PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) Lists(com.google.common.collect.Lists) ConfigurationEntry(io.atomix.protocols.raft.storage.log.entry.ConfigurationEntry) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) OpenSessionEntry(io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry) MemberId(io.atomix.cluster.MemberId) TestRaftProtocolFactory(io.atomix.protocols.raft.protocol.TestRaftProtocolFactory) Futures(io.atomix.utils.concurrent.Futures) Before(org.junit.Before) AbstractAsyncPrimitive(io.atomix.primitive.AbstractAsyncPrimitive) CloseSessionEntry(io.atomix.protocols.raft.storage.log.entry.CloseSessionEntry) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Command(io.atomix.primitive.operation.Command) File(java.io.File) Query(io.atomix.primitive.operation.Query) Configuration(io.atomix.protocols.raft.storage.system.Configuration) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractPrimitiveService(io.atomix.primitive.service.AbstractPrimitiveService) Namespace(io.atomix.utils.serializer.Namespace) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) PrimitiveType(io.atomix.primitive.PrimitiveType) PrimitiveProtocol(io.atomix.primitive.protocol.PrimitiveProtocol) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) SessionClient(io.atomix.primitive.session.SessionClient) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) MemberId(io.atomix.cluster.MemberId) RaftStorage(io.atomix.protocols.raft.storage.RaftStorage) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

Example 2 with RaftStorage

use of io.atomix.protocols.raft.storage.RaftStorage in project atomix by atomix.

the class RaftServiceManagerTest method setupContext.

@Before
public void setupContext() throws IOException {
    deleteStorage();
    RaftStorage storage = RaftStorage.builder().withPrefix("test").withDirectory(PATH.toFile()).withNamespace(NAMESPACE).build();
    PrimitiveTypeRegistry registry = new PrimitiveTypeRegistry() {

        @Override
        public Collection<PrimitiveType> getPrimitiveTypes() {
            return Collections.singleton(new TestType());
        }

        @Override
        public PrimitiveType getPrimitiveType(String typeName) {
            return new TestType();
        }
    };
    raft = new RaftContext("test", MemberId.from("test-1"), mock(ClusterMembershipService.class), mock(RaftServerProtocol.class), storage, registry, ThreadModel.SHARED_THREAD_POOL.factory("raft-server-test-%d", 1, LoggerFactory.getLogger(RaftServer.class)), true);
    snapshotTaken = new AtomicBoolean();
    snapshotInstalled = new AtomicBoolean();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftStorage(io.atomix.protocols.raft.storage.RaftStorage) RaftServer(io.atomix.protocols.raft.RaftServer) PrimitiveTypeRegistry(io.atomix.primitive.PrimitiveTypeRegistry) PrimitiveType(io.atomix.primitive.PrimitiveType) Before(org.junit.Before)

Aggregations

PrimitiveType (io.atomix.primitive.PrimitiveType)2 RaftStorage (io.atomix.protocols.raft.storage.RaftStorage)2 Before (org.junit.Before)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)1 MemberId (io.atomix.cluster.MemberId)1 AbstractAsyncPrimitive (io.atomix.primitive.AbstractAsyncPrimitive)1 AsyncPrimitive (io.atomix.primitive.AsyncPrimitive)1 PrimitiveBuilder (io.atomix.primitive.PrimitiveBuilder)1 PrimitiveException (io.atomix.primitive.PrimitiveException)1 PrimitiveInfo (io.atomix.primitive.PrimitiveInfo)1 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)1 PrimitiveRegistry (io.atomix.primitive.PrimitiveRegistry)1 PrimitiveState (io.atomix.primitive.PrimitiveState)1 PrimitiveTypeRegistry (io.atomix.primitive.PrimitiveTypeRegistry)1 SyncPrimitive (io.atomix.primitive.SyncPrimitive)1 PrimitiveConfig (io.atomix.primitive.config.PrimitiveConfig)1 Event (io.atomix.primitive.event.Event)1 Command (io.atomix.primitive.operation.Command)1