|
| 1 | +package net.spy.memcached.metrics; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | +import java.util.concurrent.atomic.AtomicReference; |
| 8 | +import java.util.concurrent.atomic.AtomicReferenceArray; |
| 9 | + |
| 10 | +import net.spy.memcached.ArcusMBeanServer; |
| 11 | + |
| 12 | +public final class OpLatencyMonitor implements OpLatencyMonitorMBean { |
| 13 | + |
| 14 | + private static final OpLatencyMonitor INSTANCE = new OpLatencyMonitor(); |
| 15 | + private static final long CACHE_DURATION = 2000; // 2초 캐시 |
| 16 | + private static final int WINDOW_SIZE = 10_000; |
| 17 | + |
| 18 | + private final AtomicReferenceArray<Long> latencies = new AtomicReferenceArray<>(WINDOW_SIZE); |
| 19 | + private final AtomicInteger currentIndex = new AtomicInteger(0); |
| 20 | + private final AtomicInteger count = new AtomicInteger(0); |
| 21 | + private final AtomicReference<LatencyMetricsSnapShot> cachedMetrics |
| 22 | + = new AtomicReference<>(LatencyMetricsSnapShot.empty()); |
| 23 | + private final boolean enabled; |
| 24 | + |
| 25 | + private OpLatencyMonitor() { |
| 26 | + if (System.getProperty("arcus.mbean", "false").toLowerCase().equals("false")) { |
| 27 | + enabled = false; |
| 28 | + return; |
| 29 | + } |
| 30 | + enabled = true; |
| 31 | + for (int i = 0; i < WINDOW_SIZE; i++) { |
| 32 | + latencies.set(i, 0L); |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + ArcusMBeanServer mbs = ArcusMBeanServer.getInstance(); |
| 37 | + mbs.registMBean(this, this.getClass().getPackage().getName() |
| 38 | + + ":type=" + this.getClass().getSimpleName()); |
| 39 | + } catch (Exception e) { |
| 40 | + throw new RuntimeException("Failed to register MBean", e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static OpLatencyMonitor getInstance() { |
| 45 | + return INSTANCE; |
| 46 | + } |
| 47 | + |
| 48 | + public void recordLatency(long startNanos) { |
| 49 | + if (!enabled) { |
| 50 | + return; |
| 51 | + } |
| 52 | + long latencyMicros = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos); |
| 53 | + int index = currentIndex.getAndUpdate(i -> (i + 1) % WINDOW_SIZE); |
| 54 | + latencies.lazySet(index, latencyMicros); |
| 55 | + |
| 56 | + if (count.get() < WINDOW_SIZE) { |
| 57 | + count.incrementAndGet(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // 모든 메트릭을 한 번에 계산하고 캐시하는 메서드 |
| 62 | + private LatencyMetricsSnapShot computeMetrics() { |
| 63 | + int currentCount = count.get(); |
| 64 | + if (currentCount == 0) { |
| 65 | + return LatencyMetricsSnapShot.empty(); |
| 66 | + } |
| 67 | + |
| 68 | + // 현재 데이터를 배열로 복사 |
| 69 | + List<Long> sortedLatencies = new ArrayList<>(currentCount); |
| 70 | + int startIndex = currentIndex.get(); |
| 71 | + |
| 72 | + for (int i = 0; i < currentCount; i++) { |
| 73 | + int idx = (startIndex - i + WINDOW_SIZE) % WINDOW_SIZE; |
| 74 | + long value = latencies.get(idx); |
| 75 | + if (value > 0) { |
| 76 | + sortedLatencies.add(value); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (sortedLatencies.isEmpty()) { |
| 81 | + return LatencyMetricsSnapShot.empty(); |
| 82 | + } |
| 83 | + |
| 84 | + sortedLatencies.sort(Long::compareTo); |
| 85 | + |
| 86 | + // 모든 메트릭을 한 번에 계산 |
| 87 | + long avg = sortedLatencies.stream().mapToLong(Long::longValue).sum() / sortedLatencies.size(); |
| 88 | + long min = sortedLatencies.get(0); |
| 89 | + long max = sortedLatencies.get(sortedLatencies.size() - 1); |
| 90 | + long p25 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 25.0) / 100.0) - 1); |
| 91 | + long p50 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 50.0) / 100.0) - 1); |
| 92 | + long p75 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 75.0) / 100.0) - 1); |
| 93 | + |
| 94 | + return new LatencyMetricsSnapShot(avg, min, max, p25, p50, p75); |
| 95 | + } |
| 96 | + |
| 97 | + // 캐시된 메트릭을 가져오거나 필요시 새로 계산 |
| 98 | + private LatencyMetricsSnapShot getMetricsSnapshot() { |
| 99 | + LatencyMetricsSnapShot current = cachedMetrics.get(); |
| 100 | + long now = System.currentTimeMillis(); |
| 101 | + |
| 102 | + // 캐시가 유효한지 확인 |
| 103 | + if (now - current.getTimestamp() < CACHE_DURATION) { |
| 104 | + return current; |
| 105 | + } |
| 106 | + |
| 107 | + // 새로운 메트릭 계산 및 캐시 업데이트 |
| 108 | + LatencyMetricsSnapShot newMetrics = computeMetrics(); |
| 109 | + cachedMetrics.set(newMetrics); |
| 110 | + return newMetrics; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public long getAverageLatencyMicros() { |
| 115 | + return getMetricsSnapshot().getAvgLatency(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public long getMinLatencyMicros() { |
| 120 | + return getMetricsSnapshot().getMinLatency(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public long getMaxLatencyMicros() { |
| 125 | + return getMetricsSnapshot().getMaxLatency(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public long get25thPercentileLatencyMicros() { |
| 130 | + return getMetricsSnapshot().getP25Latency(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public long get50thPercentileLatencyMicros() { |
| 135 | + return getMetricsSnapshot().getP50Latency(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public long get75thPercentileLatencyMicros() { |
| 140 | + return getMetricsSnapshot().getP75Latency(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void resetStatistics() { |
| 145 | + count.set(0); |
| 146 | + currentIndex.set(0); |
| 147 | + cachedMetrics.set(LatencyMetricsSnapShot.empty()); |
| 148 | + } |
| 149 | +} |
0 commit comments