Search in sources :

Example 1 with TcpClientConnection

use of com.hazelcast.client.impl.connection.tcp.TcpClientConnection in project hazelcast by hazelcast.

the class ClientStatisticsService method schedulePeriodicStatisticsSendTask.

/**
 * @param periodSeconds the interval at which the statistics collection and send is being run
 */
private void schedulePeriodicStatisticsSendTask(long periodSeconds) {
    ClientMetricCollector clientMetricCollector = new ClientMetricCollector();
    CompositeMetricsCollector compositeMetricsCollector = new CompositeMetricsCollector(clientMetricCollector, publisherMetricsCollector);
    client.getTaskScheduler().scheduleWithRepetition(() -> {
        long collectionTimestamp = System.currentTimeMillis();
        metricsRegistry.collect(compositeMetricsCollector);
        publisherMetricsCollector.publishCollectedMetrics();
        TcpClientConnection connection = getConnection();
        if (connection == null) {
            logger.finest("Cannot send client statistics to the server. No connection found.");
            return;
        }
        final StringBuilder clientAttributes = new StringBuilder();
        periodicStats.fillMetrics(collectionTimestamp, clientAttributes, connection);
        addNearCacheStats(clientAttributes);
        byte[] metricsBlob = clientMetricCollector.getBlob();
        sendStats(collectionTimestamp, clientAttributes.toString(), metricsBlob, connection);
    }, 0, periodSeconds, SECONDS);
}
Also used : CompositeMetricsCollector(com.hazelcast.internal.metrics.impl.CompositeMetricsCollector) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection)

Example 2 with TcpClientConnection

use of com.hazelcast.client.impl.connection.tcp.TcpClientConnection in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method getLocalClient.

@Override
public Client getLocalClient() {
    final ClientConnectionManager cm = client.getConnectionManager();
    final TcpClientConnection connection = (TcpClientConnection) cm.getRandomConnection();
    InetSocketAddress inetSocketAddress = connection != null ? connection.getLocalSocketAddress() : null;
    UUID clientUuid = cm.getClientUuid();
    return new ClientImpl(clientUuid, inetSocketAddress, client.getName(), labels);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClientImpl(com.hazelcast.client.impl.ClientImpl) UUID(java.util.UUID) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection)

Example 3 with TcpClientConnection

use of com.hazelcast.client.impl.connection.tcp.TcpClientConnection in project hazelcast by hazelcast.

the class ClientStatisticsTest method testStatisticsCollectionNonDefaultPeriod.

@Test
public void testStatisticsCollectionNonDefaultPeriod() {
    HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();
    final HazelcastClientInstanceImpl client = createHazelcastClient();
    final ClientEngineImpl clientEngine = getClientEngineImpl(hazelcastInstance);
    long clientConnectionTime = System.currentTimeMillis();
    // wait enough time for statistics collection
    waitForFirstStatisticsCollection(client, clientEngine);
    Map<String, String> stats = getStats(client, clientEngine);
    String connStat = stats.get("clusterConnectionTimestamp");
    assertNotNull(format("clusterConnectionTimestamp should not be null (%s)", stats), connStat);
    Long connectionTimeStat = Long.valueOf(connStat);
    assertNotNull(format("connectionTimeStat should not be null (%s)", stats), connStat);
    TcpClientConnection aConnection = (TcpClientConnection) client.getConnectionManager().getActiveConnections().iterator().next();
    String expectedClientAddress = aConnection.getLocalSocketAddress().getAddress().getHostAddress();
    assertEquals(expectedClientAddress, stats.get("clientAddress"));
    assertEquals(BuildInfoProvider.getBuildInfo().getVersion(), stats.get("clientVersion"));
    assertEquals(client.getName(), stats.get("clientName"));
    // time measured by us after client connection should be greater than the connection time reported by the statistics
    assertTrue(format("connectionTimeStat was %d, clientConnectionTime was %d (%s)", connectionTimeStat, clientConnectionTime, stats), clientConnectionTime >= connectionTimeStat);
    String mapHits = stats.get(MAP_HITS_KEY);
    assertNull(format("%s should be null (%s)", MAP_HITS_KEY, stats), mapHits);
    String cacheHits = stats.get(CACHE_HITS_KEY);
    assertNull(format("%s should be null (%s)", CACHE_HITS_KEY, stats), cacheHits);
    String lastStatisticsCollectionTimeString = stats.get("lastStatisticsCollectionTime");
    final long lastCollectionTime = Long.parseLong(lastStatisticsCollectionTimeString);
    // this creates empty map statistics
    client.getMap(MAP_NAME);
    // wait enough time for statistics collection
    waitForNextStatsCollection(client, clientEngine, lastStatisticsCollectionTimeString);
    assertTrueEventually(() -> {
        Map<String, String> stats12 = getStats(client, clientEngine);
        String mapHits12 = stats12.get(MAP_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", MAP_HITS_KEY, stats12), mapHits12);
        assertEquals(format("Expected 0 map hits (%s)", stats12), "0", mapHits12);
        String cacheHits12 = stats12.get(CACHE_HITS_KEY);
        assertNull(format("%s should be null (%s)", CACHE_HITS_KEY, stats12), cacheHits12);
        // verify that collection is periodic
        verifyThatCollectionIsPeriodic(stats12, lastCollectionTime);
    });
    // produce map and cache stat
    produceSomeStats(hazelcastInstance, client);
    assertTrueEventually(() -> {
        Map<String, String> stats1 = getStats(client, clientEngine);
        String mapHits1 = stats1.get(MAP_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", MAP_HITS_KEY, stats1), mapHits1);
        assertEquals(format("Expected 1 map hits (%s)", stats1), "1", mapHits1);
        String cacheHits1 = stats1.get(CACHE_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", CACHE_HITS_KEY, stats1), cacheHits1);
        assertEquals(format("Expected 1 cache hits (%s)", stats1), "1", cacheHits1);
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientEngineImpl(com.hazelcast.client.impl.ClientEngineImpl) Accessors.getClientEngineImpl(com.hazelcast.test.Accessors.getClientEngineImpl) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TcpClientConnection (com.hazelcast.client.impl.connection.tcp.TcpClientConnection)3 ClientEngineImpl (com.hazelcast.client.impl.ClientEngineImpl)1 ClientImpl (com.hazelcast.client.impl.ClientImpl)1 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)1 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 CompositeMetricsCollector (com.hazelcast.internal.metrics.impl.CompositeMetricsCollector)1 Accessors.getClientEngineImpl (com.hazelcast.test.Accessors.getClientEngineImpl)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 InetSocketAddress (java.net.InetSocketAddress)1 UUID (java.util.UUID)1 Test (org.junit.Test)1