Search in sources :

Example 1 with SingleThreadContext

use of io.atomix.utils.concurrent.SingleThreadContext in project atomix by atomix.

the class RaftTest method clearTests.

@Before
@After
public void clearTests() throws Exception {
    clients.forEach(c -> {
        try {
            c.close().get(10, TimeUnit.SECONDS);
        } catch (Exception e) {
        }
    });
    servers.forEach(s -> {
        try {
            if (s.isRunning()) {
                s.shutdown().get(10, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
        }
    });
    Path directory = Paths.get("target/test-logs/");
    if (Files.exists(directory)) {
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    if (context != null) {
        context.close();
    }
    members = new ArrayList<>();
    nextId = 0;
    clients = new ArrayList<>();
    servers = new ArrayList<>();
    context = new SingleThreadContext("raft-test-messaging-%d");
    protocolFactory = new TestRaftProtocolFactory(context);
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) TestRaftProtocolFactory(io.atomix.protocols.raft.protocol.TestRaftProtocolFactory) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before) After(org.junit.After)

Example 2 with SingleThreadContext

use of io.atomix.utils.concurrent.SingleThreadContext 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();
        PrimitiveProxy 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.<Map.Entry<String, String>, Long>invoke(PUT, clientSerializer::encode, Maps.immutableEntry(randomKey(), randomString(1024 * 16)), clientSerializer::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.invoke(GET, clientSerializer::encode, randomKey(), clientSerializer::decode);
                    break;
                case 2:
                    proxy.<String, Long>invoke(REMOVE, clientSerializer::encode, randomKey(), clientSerializer::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.<Long>invoke(INDEX, clientSerializer::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) Scheduler(io.atomix.utils.concurrent.Scheduler) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) Endpoint(io.atomix.messaging.Endpoint) PrimitiveProxy(io.atomix.primitive.proxy.PrimitiveProxy) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

SingleThreadContext (io.atomix.utils.concurrent.SingleThreadContext)2 Endpoint (io.atomix.messaging.Endpoint)1 PrimitiveProxy (io.atomix.primitive.proxy.PrimitiveProxy)1 TestRaftProtocolFactory (io.atomix.protocols.raft.protocol.TestRaftProtocolFactory)1 Scheduler (io.atomix.utils.concurrent.Scheduler)1 ThreadContext (io.atomix.utils.concurrent.ThreadContext)1 IOException (java.io.IOException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 After (org.junit.After)1 Before (org.junit.Before)1