Search in sources :

Example 1 with Scheduled

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

the class RaftProxyManager method close.

/**
 * Closes the session manager.
 *
 * @return A completable future to be completed once the session manager is closed.
 */
public CompletableFuture<Void> close() {
    if (open.compareAndSet(true, false)) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        threadContext.execute(() -> {
            synchronized (this) {
                for (Scheduled keepAliveFuture : keepAliveTimers.values()) {
                    keepAliveFuture.cancel();
                }
                protocol.unregisterHeartbeatHandler();
            }
            future.complete(null);
        });
        return future;
    }
    return CompletableFuture.completedFuture(null);
}
Also used : Scheduled(io.atomix.utils.concurrent.Scheduled) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 2 with Scheduled

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

the class RaftProxyManager method scheduleKeepAlive.

/**
 * Schedules a keep-alive request.
 */
private synchronized void scheduleKeepAlive(long lastKeepAliveTime, long timeout, long delta) {
    Scheduled keepAliveFuture = keepAliveTimers.remove(timeout);
    if (keepAliveFuture != null) {
        keepAliveFuture.cancel();
    }
    // Schedule the keep alive for 3/4 the timeout minus the delta from the last keep-alive request.
    keepAliveTimers.put(timeout, threadContext.schedule(Duration.ofMillis(Math.max(Math.max((long) (timeout * TIMEOUT_FACTOR) - delta, timeout - MIN_TIMEOUT_DELTA - delta), 0)), () -> {
        if (open.get()) {
            keepAliveSessions(lastKeepAliveTime, timeout);
        }
    }));
}
Also used : Scheduled(io.atomix.utils.concurrent.Scheduled)

Example 3 with Scheduled

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

the class DistributedLockService method releaseSession.

private void releaseSession(Session session) {
    if (lock != null && lock.session == session.sessionId().id()) {
        lock = queue.poll();
        while (lock != null) {
            if (lock.session == session.sessionId().id()) {
                lock = queue.poll();
            } else {
                Scheduled timer = timers.remove(lock.index);
                if (timer != null) {
                    timer.cancel();
                }
                Session lockSession = getSessions().getSession(lock.session);
                if (lockSession == null || lockSession.getState() == Session.State.EXPIRED || lockSession.getState() == Session.State.CLOSED) {
                    lock = queue.poll();
                } else {
                    lockSession.publish(DistributedLockEvents.LOCK, SERIALIZER::encode, new LockEvent(lock.id, lock.index));
                    break;
                }
            }
        }
    }
}
Also used : Scheduled(io.atomix.utils.concurrent.Scheduled) Session(io.atomix.primitive.session.Session)

Example 4 with Scheduled

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

the class DistributedLockService method unlock.

/**
 * Applies an unlock commit.
 */
protected void unlock(Commit<Unlock> commit) {
    if (lock != null) {
        if (lock.session != commit.session().sessionId().id()) {
            return;
        }
        lock = queue.poll();
        while (lock != null) {
            Scheduled timer = timers.remove(lock.index);
            if (timer != null) {
                timer.cancel();
            }
            Session session = getSessions().getSession(lock.session);
            if (session == null || session.getState() == Session.State.EXPIRED || session.getState() == Session.State.CLOSED) {
                lock = queue.poll();
            } else {
                session.publish(DistributedLockEvents.LOCK, SERIALIZER::encode, new LockEvent(lock.id, commit.index()));
                break;
            }
        }
    }
}
Also used : Scheduled(io.atomix.utils.concurrent.Scheduled) Session(io.atomix.primitive.session.Session)

Example 5 with Scheduled

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

the class RaftFuzzTest method reset.

/**
 * Shuts down clients and servers.
 */
private void reset() throws Exception {
    for (Scheduled shutdownTimer : shutdownTimers.values()) {
        shutdownTimer.cancel();
    }
    shutdownTimers.clear();
    for (Scheduled restartTimer : restartTimers.values()) {
        restartTimer.cancel();
    }
    restartTimers.clear();
    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/fuzz-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;
            }
        });
    }
    members = new ArrayList<>();
    port = 5000;
    clients = new ArrayList<>();
    servers = new ArrayList<>();
    protocolFactory = new LocalRaftProtocolFactory(protocolSerializer);
}
Also used : Scheduled(io.atomix.utils.concurrent.Scheduled) Path(java.nio.file.Path) LocalRaftProtocolFactory(io.atomix.protocols.raft.protocol.LocalRaftProtocolFactory) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

Scheduled (io.atomix.utils.concurrent.Scheduled)5 Session (io.atomix.primitive.session.Session)2 LocalRaftProtocolFactory (io.atomix.protocols.raft.protocol.LocalRaftProtocolFactory)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 CompletableFuture (java.util.concurrent.CompletableFuture)1