/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. package com.hazelcast.impl.monitor;
  17. import com.hazelcast.util.Clock;
  18. import com.hazelcast.monitor.LocalInstanceOperationStats;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.concurrent.atomic.AtomicLong;
  22. abstract class OperationsCounterSupport<T extends LocalInstanceOperationStats> {
  23. final long interval;
  24. final Object lock = new Object();
  25. long startTime = now();
  26. long endTime = Long.MAX_VALUE;
  27. transient volatile T published = null;
  28. final List listOfSubCounters = new ArrayList();
  29. public OperationsCounterSupport() {
  30. this(5000);
  31. }
  32. public OperationsCounterSupport(long interval) {
  33. super();
  34. this.interval = interval;
  35. }
  36. public final T getPublishedStats() {
  37. //noinspection DoubleCheckedLocking
  38. if (published == null) {
  39. synchronized (lock) {
  40. if (published == null) {
  41. published = getThis();
  42. }
  43. }
  44. }
  45. if (published.getPeriodEnd() < now() - interval) {
  46. return getEmpty();
  47. }
  48. return published;
  49. }
  50. final void publishSubResult() {
  51. long subInterval = interval / 5;
  52. if (now() - startTime > subInterval) {
  53. synchronized (lock) {
  54. if (now() - startTime >= subInterval) {
  55. OperationsCounterSupport<T> copy = getAndReset();
  56. if (listOfSubCounters.size() == 5) {
  57. listOfSubCounters.remove(0);
  58. }
  59. listOfSubCounters.add(copy);
  60. this.published = aggregateSubCounterStats();
  61. }
  62. }
  63. }
  64. }
  65. abstract T getThis();
  66. abstract T getEmpty();
  67. abstract OperationsCounterSupport<T> getAndReset();
  68. abstract T aggregateSubCounterStats();
  69. final long now() {
  70. return Clock.currentTimeMillis();
  71. }
  72. class OperationCounter {
  73. final AtomicLong count;
  74. final AtomicLong totalLatency;
  75. public OperationCounter() {
  76. this(0, 0);
  77. }
  78. public OperationCounter(long c, long l) {
  79. this.count = new AtomicLong(c);
  80. totalLatency = new AtomicLong(l);
  81. }
  82. public OperationCounter copyAndReset() {
  83. OperationCounter copy = new OperationCounter(count.get(),
  84. totalLatency.get());
  85. this.count.set(0);
  86. this.totalLatency.set(0);
  87. return copy;
  88. }
  89. public void set(OperationCounter now) {
  90. this.count.set(now.count.get());
  91. this.totalLatency.set(now.totalLatency.get());
  92. }
  93. public void count(long elapsed) {
  94. this.count.incrementAndGet();
  95. this.totalLatency.addAndGet(elapsed);
  96. }
  97. @Override
  98. public String toString() {
  99. long count = this.count.get();
  100. return "OperationStat{" + "count=" + count + ", averageLatency="
  101. + ((count == 0) ? 0 : totalLatency.get() / count) + '}';
  102. }
  103. }
  104. }