All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.client.cache.impl.CacheStatsHandler Maven / Gradle / Ivy

There is a newer version: 3.12.13
Show newest version
/*
 * Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.client.cache.impl;

import com.hazelcast.core.ExecutionCallback;
import com.hazelcast.spi.serialization.SerializationService;

/**
 * Helper class which contains specific statistic calculations per context
 */
final class CacheStatsHandler {

    private final SerializationService serializationService;
    private final ClientCacheStatisticsImpl statistics;

    CacheStatsHandler(SerializationService serializationService) {
        this.serializationService = serializationService;
        this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis());
    }

    ClientCacheStatisticsImpl getStatistics() {
        return statistics;
    }

    void onReplace(boolean isGet, long startNanos, Object response) {
        if (isGet) {
            statistics.addGetTimeNanos(System.nanoTime() - startNanos);
            if (response != null) {
                statistics.increaseCacheHits();
                statistics.increaseCachePuts();
                statistics.addPutTimeNanos(System.nanoTime() - startNanos);
            } else {
                statistics.increaseCacheMisses();
            }
        } else {
            if (Boolean.TRUE.equals(response)) {
                statistics.increaseCacheHits();
                statistics.increaseCachePuts();
                statistics.addPutTimeNanos(System.nanoTime() - startNanos);
            } else {
                statistics.increaseCacheMisses();
            }
        }
    }

     ExecutionCallback newOnReplaceCallback(final long startNanos) {
        return new ExecutionCallback() {
            @Override
            public void onResponse(T response) {
                onReplace(true, startNanos, toObject(response));
            }

            @Override
            public void onFailure(Throwable t) {
            }
        };
    }

    void onPutIfAbsent(long startNanos, boolean saved) {
        if (saved) {
            statistics.increaseCachePuts();
            statistics.increaseCacheMisses();
            statistics.addPutTimeNanos(System.nanoTime() - startNanos);
        }
    }

    ExecutionCallback newOnPutIfAbsentCallback(final long startNanos) {
        return new ExecutionCallback() {
            @Override
            public void onResponse(Boolean responseData) {
                Object response = toObject(responseData);
                onPutIfAbsent(startNanos, (Boolean) response);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        };
    }

    void onPut(boolean isGet, long startNanos, boolean cacheHit) {
        statistics.increaseCachePuts();
        statistics.addPutTimeNanos(System.nanoTime() - startNanos);
        if (isGet) {
            statistics.addGetTimeNanos(System.nanoTime() - startNanos);
            if (cacheHit) {
                statistics.increaseCacheHits();
            } else {
                statistics.increaseCacheMisses();
            }
        }
    }

     OneShotExecutionCallback newOnPutCallback(final boolean isGet, final long startNanos) {
        return new OneShotExecutionCallback() {
            @Override
            protected void onResponseInternal(T responseData) {
                onPut(isGet, startNanos, responseData != null);
            }

            @Override
            protected void onFailureInternal(Throwable t) {
            }
        };
    }

    void onRemove(boolean isGet, long startNanos, Object response) {
        if (isGet) {
            statistics.addGetTimeNanos(System.nanoTime() - startNanos);
            if (response != null) {
                statistics.increaseCacheHits();
                statistics.increaseCacheRemovals();
                statistics.addRemoveTimeNanos(System.nanoTime() - startNanos);
            } else {
                statistics.increaseCacheMisses();
            }
        } else {
            if (Boolean.TRUE.equals(toObject(response))) {
                statistics.increaseCacheRemovals();
                statistics.addRemoveTimeNanos(System.nanoTime() - startNanos);
            }
        }
    }

     ExecutionCallback newOnRemoveCallback(final boolean isGet, final long startNanos) {
        return new ExecutionCallback() {
            public void onResponse(T response) {
                onRemove(isGet, startNanos, response);
            }

            public void onFailure(Throwable t) {
            }
        };
    }

    void onGet(long startNanos, boolean responseReceived) {
        if (responseReceived) {
            statistics.increaseCacheHits();
        } else {
            statistics.increaseCacheMisses();
        }
        statistics.addGetTimeNanos(System.nanoTime() - startNanos);
    }

     ExecutionCallback newOnGetCallback(final long startNanos) {
        return new ExecutionCallback() {
            @Override
            public void onResponse(T response) {
                onGet(startNanos, response != null);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        };
    }

    void onBatchRemove(long startNanos, int batchSize) {
        // Actually we don't know how many of them are really removed or not.
        // We just assume that if there is no exception, all of them are removed.
        // Otherwise (if there is an exception), we don't update any cache stats about remove.
        statistics.increaseCacheRemovals(batchSize);
        statistics.addRemoveTimeNanos(System.nanoTime() - startNanos);
    }

    void onBatchGet(long startNanos, int batchSize) {
        statistics.increaseCacheHits(batchSize);
        statistics.addGetTimeNanos(System.nanoTime() - startNanos);
    }

    void onBatchPut(long startNanos, int batchSize) {
        // we don't know how many of keys are actually loaded, so we assume that all of them are loaded
        // and calculate statistics based on this assumption
        statistics.increaseCachePuts(batchSize);
        statistics.addPutTimeNanos(System.nanoTime() - startNanos);
    }

    void clear() {
        statistics.clear();
    }

    private Object toObject(Object responseData) {
        return serializationService.toObject(responseData);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy