PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/monitor/OperationsCounterSupport.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 126 lines | 88 code | 22 blank | 16 comment | 10 complexity | 46a3300093c34741370cc8f7a1eaf3fd MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.impl.monitor;
 18
 19import com.hazelcast.util.Clock;
 20import com.hazelcast.monitor.LocalInstanceOperationStats;
 21
 22import java.util.ArrayList;
 23import java.util.List;
 24import java.util.concurrent.atomic.AtomicLong;
 25
 26abstract class OperationsCounterSupport<T extends LocalInstanceOperationStats> {
 27
 28    final long interval;
 29    final Object lock = new Object();
 30    long startTime = now();
 31    long endTime = Long.MAX_VALUE;
 32    transient volatile T published = null;
 33
 34    final List listOfSubCounters = new ArrayList();
 35
 36    public OperationsCounterSupport() {
 37        this(5000);
 38    }
 39
 40    public OperationsCounterSupport(long interval) {
 41        super();
 42        this.interval = interval;
 43    }
 44
 45    public final T getPublishedStats() {
 46        //noinspection DoubleCheckedLocking
 47        if (published == null) {
 48            synchronized (lock) {
 49                if (published == null) {
 50                    published = getThis();
 51                }
 52            }
 53        }
 54        if (published.getPeriodEnd() < now() - interval) {
 55            return getEmpty();
 56        }
 57        return published;
 58    }
 59
 60    final void publishSubResult() {
 61        long subInterval = interval / 5;
 62        if (now() - startTime > subInterval) {
 63            synchronized (lock) {
 64                if (now() - startTime >= subInterval) {
 65                    OperationsCounterSupport<T> copy = getAndReset();
 66                    if (listOfSubCounters.size() == 5) {
 67                        listOfSubCounters.remove(0);
 68                    }
 69                    listOfSubCounters.add(copy);
 70                    this.published = aggregateSubCounterStats();
 71                }
 72            }
 73        }
 74    }
 75
 76    abstract T getThis();
 77
 78    abstract T getEmpty();
 79
 80    abstract OperationsCounterSupport<T> getAndReset();
 81
 82    abstract T aggregateSubCounterStats();
 83
 84    final long now() {
 85        return Clock.currentTimeMillis();
 86    }
 87
 88    class OperationCounter {
 89        final AtomicLong count;
 90        final AtomicLong totalLatency;
 91
 92        public OperationCounter() {
 93            this(0, 0);
 94        }
 95
 96        public OperationCounter(long c, long l) {
 97            this.count = new AtomicLong(c);
 98            totalLatency = new AtomicLong(l);
 99        }
100
101        public OperationCounter copyAndReset() {
102            OperationCounter copy = new OperationCounter(count.get(),
103                    totalLatency.get());
104            this.count.set(0);
105            this.totalLatency.set(0);
106            return copy;
107        }
108
109        public void set(OperationCounter now) {
110            this.count.set(now.count.get());
111            this.totalLatency.set(now.totalLatency.get());
112        }
113
114        public void count(long elapsed) {
115            this.count.incrementAndGet();
116            this.totalLatency.addAndGet(elapsed);
117        }
118
119        @Override
120        public String toString() {
121            long count = this.count.get();
122            return "OperationStat{" + "count=" + count + ", averageLatency="
123                    + ((count == 0) ? 0 : totalLatency.get() / count) + '}';
124        }
125    }
126}