Search in sources :

Example 1 with PartitionSpecificRunnableAdaptor

use of com.hazelcast.cp.internal.util.PartitionSpecificRunnableAdaptor in project hazelcast by hazelcast.

the class RaftSessionService method getInactiveSessions.

private Map<CPGroupId, Collection<Long>> getInactiveSessions() {
    Map<CPGroupId, Collection<Long>> response = new ConcurrentHashMap<>();
    Semaphore semaphore = new Semaphore(0);
    OperationServiceImpl operationService = nodeEngine.getOperationService();
    Collection<RaftSessionRegistry> registries = new ArrayList<>(this.registries.values());
    for (RaftSessionRegistry registry : registries) {
        CPGroupId groupId = registry.groupId();
        operationService.execute(new PartitionSpecificRunnableAdaptor(() -> {
            Set<Long> activeSessionIds = new HashSet<>();
            for (SessionAwareService service : nodeEngine.getServices(SessionAwareService.class)) {
                activeSessionIds.addAll(service.getAttachedSessions(groupId));
            }
            Set<Long> inactiveSessionIds = new HashSet<>();
            for (CPSession session : registry.getSessions()) {
                if (!activeSessionIds.contains(session.id()) && session.creationTime() + getSessionTTLMillis() < Clock.currentTimeMillis()) {
                    inactiveSessionIds.add(session.id());
                }
            }
            if (inactiveSessionIds.size() > 0) {
                response.put(groupId, inactiveSessionIds);
            }
            semaphore.release();
        }, nodeEngine.getPartitionService().getPartitionId(groupId)));
    }
    try {
        semaphore.tryAcquire(registries.size(), COLLECT_INACTIVE_SESSIONS_TASK_TIMEOUT_SECONDS, SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return response;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) CPGroupId(com.hazelcast.cp.CPGroupId) PartitionSpecificRunnableAdaptor(com.hazelcast.cp.internal.util.PartitionSpecificRunnableAdaptor) Collections.unmodifiableCollection(java.util.Collections.unmodifiableCollection) Collection(java.util.Collection) CPSession(com.hazelcast.cp.session.CPSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)

Aggregations

CPGroupId (com.hazelcast.cp.CPGroupId)1 PartitionSpecificRunnableAdaptor (com.hazelcast.cp.internal.util.PartitionSpecificRunnableAdaptor)1 CPSession (com.hazelcast.cp.session.CPSession)1 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections.unmodifiableCollection (java.util.Collections.unmodifiableCollection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Semaphore (java.util.concurrent.Semaphore)1