Search in sources :

Example 1 with RaftClient

use of io.atomix.protocols.raft.RaftClient in project atomix by atomix.

the class RaftPerformanceTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    Member member = nextNode();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        MessagingService messagingService = new NettyMessagingService("test", member.address(), new MessagingConfig()).start().join();
        protocol = new RaftClientMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(member.id());
    }
    RaftClient client = RaftClient.builder().withMemberId(member.id()).withPartitionId(PartitionId.from("test", 1)).withProtocol(protocol).withThreadModel(ThreadModel.SHARED_THREAD_POOL).build();
    client.connect(members.stream().map(Member::id).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) Member(io.atomix.cluster.Member) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService)

Example 2 with RaftClient

use of io.atomix.protocols.raft.RaftClient in project atomix by atomix.

the class RaftFuzzTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    MemberId memberId = nextNodeId();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        Address address = Address.from(++port);
        MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
        addressMap.put(memberId, address);
        protocol = new RaftClientMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(memberId);
    }
    RaftClient client = RaftClient.builder().withMemberId(memberId).withProtocol(protocol).build();
    client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) MemberId(io.atomix.cluster.MemberId) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Address(io.atomix.utils.net.Address) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService)

Example 3 with RaftClient

use of io.atomix.protocols.raft.RaftClient in project atomix by atomix.

the class RaftFuzzTest method runFuzzTest.

/**
 * Runs a single fuzz test.
 */
private void runFuzzTest() throws Exception {
    reset();
    createServers(randomNumber(5) + 3);
    final Object lock = new Object();
    final AtomicLong index = new AtomicLong();
    final Map<Integer, Long> indexes = new HashMap<>();
    ThreadContext context = new SingleThreadContext("fuzz-test");
    int clients = randomNumber(10) + 1;
    for (int i = 0; i < clients; i++) {
        ReadConsistency consistency = randomConsistency();
        RaftClient client = createClient();
        SessionClient proxy = createProxy(client, consistency);
        Scheduler scheduler = new SingleThreadContext("fuzz-test-" + i);
        final int clientId = i;
        scheduler.schedule(Duration.ofMillis((100 * clients) + (randomNumber(50) - 25)), Duration.ofMillis((100 * clients) + (randomNumber(50) - 25)), () -> {
            long lastLinearizableIndex = index.get();
            int type = randomNumber(4);
            switch(type) {
                case 0:
                    proxy.execute(operation(PUT, CLIENT_SERIALIZER.encode(Maps.immutableEntry(randomKey(), randomString(1024 * 16))))).<Long>thenApply(CLIENT_SERIALIZER::decode).thenAccept(result -> {
                        synchronized (lock) {
                            if (result < lastLinearizableIndex) {
                                System.out.println(result + " is less than last linearizable index " + lastLinearizableIndex);
                                System.exit(1);
                            } else if (result > index.get()) {
                                index.set(result);
                            }
                            Long lastSequentialIndex = indexes.get(clientId);
                            if (lastSequentialIndex == null) {
                                indexes.put(clientId, result);
                            } else if (result < lastSequentialIndex) {
                                System.out.println(result + " is less than last sequential index " + lastSequentialIndex);
                                System.exit(1);
                            } else {
                                indexes.put(clientId, lastSequentialIndex);
                            }
                        }
                    });
                    break;
                case 1:
                    proxy.execute(operation(GET, CLIENT_SERIALIZER.encode(randomKey())));
                    break;
                case 2:
                    proxy.execute(operation(REMOVE, CLIENT_SERIALIZER.encode(randomKey()))).<Long>thenApply(CLIENT_SERIALIZER::decode).thenAccept(result -> {
                        synchronized (lock) {
                            if (result < lastLinearizableIndex) {
                                System.out.println(result + " is less than last linearizable index " + lastLinearizableIndex);
                                System.exit(1);
                            } else if (result > index.get()) {
                                index.set(result);
                            }
                            Long lastSequentialIndex = indexes.get(clientId);
                            if (lastSequentialIndex == null) {
                                indexes.put(clientId, result);
                            } else if (result < lastSequentialIndex) {
                                System.out.println(result + " is less than last sequential index " + lastSequentialIndex);
                                System.exit(1);
                            } else {
                                indexes.put(clientId, lastSequentialIndex);
                            }
                        }
                    });
                    break;
                case 3:
                    proxy.execute(operation(INDEX)).<Long>thenApply(CLIENT_SERIALIZER::decode).thenAccept(result -> {
                        synchronized (lock) {
                            switch(consistency) {
                                case LINEARIZABLE:
                                case LINEARIZABLE_LEASE:
                                    if (result < lastLinearizableIndex) {
                                        System.out.println(result + " is less than last linearizable index " + lastLinearizableIndex);
                                        System.exit(1);
                                    } else if (result > index.get()) {
                                        index.set(result);
                                    }
                                case SEQUENTIAL:
                                    Long lastSequentialIndex = indexes.get(clientId);
                                    if (lastSequentialIndex == null) {
                                        indexes.put(clientId, result);
                                    } else if (result < lastSequentialIndex) {
                                        System.out.println(result + " is less than last sequential index " + lastSequentialIndex);
                                        System.exit(1);
                                    } else {
                                        indexes.put(clientId, lastSequentialIndex);
                                    }
                            }
                        }
                    });
            }
        });
    }
    scheduleRestarts(context);
    Thread.sleep(Duration.ofMinutes(15).toMillis());
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SessionClient(io.atomix.primitive.session.SessionClient) Scheduler(io.atomix.utils.concurrent.Scheduler) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) ReadConsistency(io.atomix.protocols.raft.ReadConsistency) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) RaftClient(io.atomix.protocols.raft.RaftClient)

Aggregations

RaftClient (io.atomix.protocols.raft.RaftClient)3 MessagingConfig (io.atomix.cluster.messaging.MessagingConfig)2 MessagingService (io.atomix.cluster.messaging.MessagingService)2 NettyMessagingService (io.atomix.cluster.messaging.impl.NettyMessagingService)2 RaftClientProtocol (io.atomix.protocols.raft.protocol.RaftClientProtocol)2 RaftClientMessagingProtocol (io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol)2 Member (io.atomix.cluster.Member)1 MemberId (io.atomix.cluster.MemberId)1 ManagedMessagingService (io.atomix.cluster.messaging.ManagedMessagingService)1 SessionClient (io.atomix.primitive.session.SessionClient)1 ReadConsistency (io.atomix.protocols.raft.ReadConsistency)1 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)1 DefaultRaftMember (io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)1 Scheduler (io.atomix.utils.concurrent.Scheduler)1 SingleThreadContext (io.atomix.utils.concurrent.SingleThreadContext)1 ThreadContext (io.atomix.utils.concurrent.ThreadContext)1 Address (io.atomix.utils.net.Address)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1