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;
}
}
}
}
}
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);
}
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);
}
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;
}
}
}
}
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));
}
}
Aggregations