Search in sources :

Example 1 with Session

use of io.atomix.primitive.session.Session 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 2 with Session

use of io.atomix.primitive.session.Session in project atomix by atomix.

the class AtomicValueService method backup.

@Override
public void backup(BufferOutput<?> writer) {
    writer.writeInt(value.length).writeBytes(value);
    java.util.Set<Long> sessionIds = new HashSet<>();
    for (Session session : listeners) {
        sessionIds.add(session.sessionId().id());
    }
    writer.writeObject(sessionIds, SERIALIZER::encode);
}
Also used : HashSet(java.util.HashSet) Session(io.atomix.primitive.session.Session)

Example 3 with Session

use of io.atomix.primitive.session.Session in project atomix by atomix.

the class LeaderElectionServiceTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(PrimitiveTypes.leaderElection());
    when(context.serviceName()).thenReturn("test");
    when(context.serviceId()).thenReturn(PrimitiveId.from(1));
    when(context.wallClock()).thenReturn(new WallClock());
    Session session = mock(Session.class);
    when(session.sessionId()).thenReturn(SessionId.from(1));
    LeaderElectionService service = new LeaderElectionService();
    service.init(context);
    byte[] id = "a".getBytes();
    service.run(new DefaultCommit<>(2, RUN, new Run(id), session, System.currentTimeMillis()));
    Buffer buffer = HeapBuffer.allocate();
    service.backup(buffer);
    service = new LeaderElectionService();
    service.init(context);
    service.restore(buffer.flip());
    Leadership<byte[]> value = service.getLeadership();
    assertNotNull(value);
    assertArrayEquals(value.leader().id(), id);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) ServiceContext(io.atomix.primitive.service.ServiceContext) WallClock(io.atomix.utils.time.WallClock) LeaderElectionService(io.atomix.core.election.impl.LeaderElectionService) Run(io.atomix.core.election.impl.LeaderElectionOperations.Run) Session(io.atomix.primitive.session.Session) Test(org.junit.Test)

Example 4 with Session

use of io.atomix.primitive.session.Session 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 Session

use of io.atomix.primitive.session.Session in project atomix by atomix.

the class PrimaryRole method executeQuery.

private CompletableFuture<ExecuteResponse> executeQuery(ExecuteRequest request) {
    // If the session doesn't exist, create and replicate a new session before applying the query.
    Session session = context.getSession(request.session());
    if (session == null) {
        Session newSession = context.createSession(request.session(), request.node());
        long index = context.nextIndex();
        long timestamp = System.currentTimeMillis();
        return replicator.replicate(new ExecuteOperation(index, timestamp, newSession.sessionId().id(), newSession.nodeId(), null)).thenApply(v -> {
            context.setIndex(index);
            context.setTimestamp(timestamp);
            return applyQuery(request, newSession);
        });
    } else {
        return CompletableFuture.completedFuture(applyQuery(request, session));
    }
}
Also used : ExecuteOperation(io.atomix.protocols.backup.protocol.ExecuteOperation) PrimaryBackupSession(io.atomix.protocols.backup.impl.PrimaryBackupSession) Session(io.atomix.primitive.session.Session)

Aggregations

Session (io.atomix.primitive.session.Session)7 Test (org.junit.Test)3 ServiceContext (io.atomix.primitive.service.ServiceContext)2 Buffer (io.atomix.storage.buffer.Buffer)2 HeapBuffer (io.atomix.storage.buffer.HeapBuffer)2 Scheduled (io.atomix.utils.concurrent.Scheduled)2 Run (io.atomix.core.election.impl.LeaderElectionOperations.Run)1 LeaderElectionService (io.atomix.core.election.impl.LeaderElectionService)1 Task (io.atomix.core.queue.Task)1 Add (io.atomix.core.queue.impl.WorkQueueOperations.Add)1 Take (io.atomix.core.queue.impl.WorkQueueOperations.Take)1 WorkQueueService (io.atomix.core.queue.impl.WorkQueueService)1 SessionEvent (io.atomix.primitive.session.SessionEvent)1 PrimaryBackupSession (io.atomix.protocols.backup.impl.PrimaryBackupSession)1 ExecuteOperation (io.atomix.protocols.backup.protocol.ExecuteOperation)1 WallClock (io.atomix.utils.time.WallClock)1 HashSet (java.util.HashSet)1