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