Search in sources :

Example 1 with CPSession

use of com.hazelcast.cp.session.CPSession in project hazelcast by hazelcast.

the class RaftSessionService method provideDynamicMetrics.

@Override
public void provideDynamicMetrics(MetricDescriptor descriptor, MetricsCollectionContext context) {
    MetricDescriptor root = descriptor.withPrefix("cp.session");
    for (RaftSessionRegistry registry : registries.values()) {
        CPGroupId groupId = registry.groupId();
        for (CPSession session : registry.getSessions()) {
            MetricDescriptor desc = root.copy().withDiscriminator("id", session.id() + "@" + groupId.getName()).withTag("sessionId", String.valueOf(session.id())).withTag("group", groupId.getName());
            context.collect(desc.copy().withTag("endpoint", session.endpoint().toString()).withMetric("endpoint"), 0);
            context.collect(desc.copy().withTag("endpointType", session.endpointType().toString()).withMetric("endpointType"), 0);
            context.collect(desc.copy().withMetric("version"), session.version());
            context.collect(desc.copy().withUnit(ProbeUnit.MS).withMetric("creationTime"), session.creationTime());
            context.collect(desc.copy().withUnit(ProbeUnit.MS).withMetric("expirationTime"), session.expirationTime());
        }
    }
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) MetricDescriptor(com.hazelcast.internal.metrics.MetricDescriptor) CPSession(com.hazelcast.cp.session.CPSession)

Example 2 with CPSession

use of com.hazelcast.cp.session.CPSession 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)

Example 3 with CPSession

use of com.hazelcast.cp.session.CPSession in project hazelcast by hazelcast.

the class RaftSessionServiceTest method testSessionCreate.

@Test
public void testSessionCreate() throws ExecutionException, InterruptedException, UnknownHostException {
    SessionResponse response = invocationManager.<SessionResponse>invoke(groupId, newCreateSessionOp()).get();
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService service = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            RaftSessionRegistry registry = service.getSessionRegistryOrNull(groupId);
            assertNotNull(registry);
            CPSession session = registry.getSession(response.getSessionId());
            assertNotNull(session);
            Collection<CPSession> sessions = service.getAllSessions(groupId).get();
            assertThat(sessions, hasItem(session));
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CPSession(com.hazelcast.cp.session.CPSession) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with CPSession

use of com.hazelcast.cp.session.CPSession in project hazelcast by hazelcast.

the class RestCPSubsystemTest method test_forceCloseValidCPSession.

@Test
public void test_forceCloseValidCPSession() throws IOException, ExecutionException, InterruptedException {
    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
    Hazelcast.newHazelcastInstance(config);
    Hazelcast.newHazelcastInstance(config);
    instance1.getCPSubsystem().getLock("lock1").lock();
    instance1.getCPSubsystem().getLock("lock1").unlock();
    Collection<CPSession> sessions1 = instance1.getCPSubsystem().getCPSessionManagementService().getAllSessions(DEFAULT_GROUP_NAME).toCompletableFuture().get();
    assertEquals(1, sessions1.size());
    long sessionId = sessions1.iterator().next().id();
    ConnectionResponse response = new HTTPCommunicator(instance1).forceCloseCPSession(DEFAULT_GROUP_NAME, sessionId, clusterName, null);
    assertEquals(200, response.responseCode);
    Collection<CPSession> sessions2 = instance1.getCPSubsystem().getCPSessionManagementService().getAllSessions(DEFAULT_GROUP_NAME).toCompletableFuture().get();
    assertEquals(0, sessions2.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CPSession(com.hazelcast.cp.session.CPSession) ConnectionResponse(com.hazelcast.internal.ascii.HTTPCommunicator.ConnectionResponse) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 5 with CPSession

use of com.hazelcast.cp.session.CPSession in project hazelcast by hazelcast.

the class HttpGetCommandProcessor method handleGetCPSessions.

private void handleGetCPSessions(final HttpGetCommand command) {
    String uri = command.getURI();
    String prefix = URI_CP_GROUPS_URL + "/";
    int i = uri.indexOf(URI_CP_SESSIONS_SUFFIX);
    String groupName = uri.substring(prefix.length(), i).trim();
    getCpSubsystem().getCPSessionManagementService().getAllSessions(groupName).whenCompleteAsync((sessions, t) -> {
        if (t == null) {
            JsonArray sessionsArr = new JsonArray();
            for (CPSession session : sessions) {
                sessionsArr.add(toJson(session));
            }
            prepareResponse(command, sessionsArr);
            textCommandService.sendResponse(command);
        } else {
            if (peel(t) instanceof IllegalArgumentException) {
                command.send404();
            } else {
                command.send500();
            }
            textCommandService.sendResponse(command);
        }
    });
}
Also used : JsonArray(com.hazelcast.internal.json.JsonArray) CPSession(com.hazelcast.cp.session.CPSession)

Aggregations

CPSession (com.hazelcast.cp.session.CPSession)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 Test (org.junit.Test)3 CPGroupId (com.hazelcast.cp.CPGroupId)2 ConnectionResponse (com.hazelcast.internal.ascii.HTTPCommunicator.ConnectionResponse)2 SlowTest (com.hazelcast.test.annotation.SlowTest)2 PartitionSpecificRunnableAdaptor (com.hazelcast.cp.internal.util.PartitionSpecificRunnableAdaptor)1 JsonArray (com.hazelcast.internal.json.JsonArray)1 MetricDescriptor (com.hazelcast.internal.metrics.MetricDescriptor)1 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)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