-
Notifications
You must be signed in to change notification settings - Fork 49
FEAT: Add Operation throughput and latency metrics by mbean. #841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brido4125
wants to merge
1
commit into
naver:develop
Choose a base branch
from
brido4125:feat/metrics
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/net/spy/memcached/metrics/LatencyMetricsSnapShot.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package net.spy.memcached.metrics; | ||
|
|
||
| class LatencyMetricsSnapShot { | ||
| private static final LatencyMetricsSnapShot EMPTY = new LatencyMetricsSnapShot(0, 0, 0, 0, 0, 0); | ||
|
|
||
| private final long avgLatency; | ||
| private final long minLatency; | ||
| private final long maxLatency; | ||
| private final long p25Latency; | ||
| private final long p50Latency; | ||
| private final long p75Latency; | ||
| private final long timestamp; // μΊμ μμ± μκ° | ||
|
|
||
| LatencyMetricsSnapShot(long avg, long min, long max, long p25, long p50, long p75) { | ||
| this.avgLatency = avg; | ||
| this.minLatency = min; | ||
| this.maxLatency = max; | ||
| this.p25Latency = p25; | ||
| this.p50Latency = p50; | ||
| this.p75Latency = p75; | ||
| this.timestamp = System.currentTimeMillis(); | ||
| } | ||
|
|
||
| public static LatencyMetricsSnapShot empty() { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| public long getAvgLatency() { | ||
| return avgLatency; | ||
| } | ||
|
|
||
| public long getMinLatency() { | ||
| return minLatency; | ||
| } | ||
|
|
||
| public long getMaxLatency() { | ||
| return maxLatency; | ||
| } | ||
|
|
||
| public long getP25Latency() { | ||
| return p25Latency; | ||
| } | ||
|
|
||
| public long getP50Latency() { | ||
| return p50Latency; | ||
| } | ||
|
|
||
| public long getP75Latency() { | ||
| return p75Latency; | ||
| } | ||
|
|
||
| public long getTimestamp() { | ||
| return timestamp; | ||
| } | ||
| } |
149 changes: 149 additions & 0 deletions
149
src/main/java/net/spy/memcached/metrics/OpLatencyMonitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package net.spy.memcached.metrics; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.atomic.AtomicReferenceArray; | ||
|
|
||
| import net.spy.memcached.ArcusMBeanServer; | ||
|
|
||
| public final class OpLatencyMonitor implements OpLatencyMonitorMBean { | ||
|
|
||
| private static final OpLatencyMonitor INSTANCE = new OpLatencyMonitor(); | ||
| private static final long CACHE_DURATION = 2000; // 2μ΄ μΊμ | ||
| private static final int WINDOW_SIZE = 10_000; | ||
|
|
||
| private final AtomicReferenceArray<Long> latencies = new AtomicReferenceArray<>(WINDOW_SIZE); | ||
| private final AtomicInteger currentIndex = new AtomicInteger(0); | ||
| private final AtomicInteger count = new AtomicInteger(0); | ||
| private final AtomicReference<LatencyMetricsSnapShot> cachedMetrics | ||
| = new AtomicReference<>(LatencyMetricsSnapShot.empty()); | ||
| private final boolean enabled; | ||
|
|
||
| private OpLatencyMonitor() { | ||
| if (System.getProperty("arcus.mbean", "false").toLowerCase().equals("false")) { | ||
| enabled = false; | ||
| return; | ||
| } | ||
| enabled = true; | ||
| for (int i = 0; i < WINDOW_SIZE; i++) { | ||
| latencies.set(i, 0L); | ||
| } | ||
|
|
||
| try { | ||
| ArcusMBeanServer mbs = ArcusMBeanServer.getInstance(); | ||
| mbs.registMBean(this, this.getClass().getPackage().getName() | ||
| + ":type=" + this.getClass().getSimpleName()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to register MBean", e); | ||
| } | ||
| } | ||
|
|
||
| public static OpLatencyMonitor getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public void recordLatency(long startNanos) { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
| long latencyMicros = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos); | ||
| int index = currentIndex.getAndUpdate(i -> (i + 1) % WINDOW_SIZE); | ||
| latencies.lazySet(index, latencyMicros); | ||
|
|
||
| if (count.get() < WINDOW_SIZE) { | ||
| count.incrementAndGet(); | ||
| } | ||
| } | ||
|
|
||
| // λͺ¨λ λ©νΈλ¦μ ν λ²μ κ³μ°νκ³ μΊμνλ λ©μλ | ||
| private LatencyMetricsSnapShot computeMetrics() { | ||
| int currentCount = count.get(); | ||
| if (currentCount == 0) { | ||
| return LatencyMetricsSnapShot.empty(); | ||
| } | ||
|
|
||
| // νμ¬ λ°μ΄ν°λ₯Ό λ°°μ΄λ‘ λ³΅μ¬ | ||
| List<Long> sortedLatencies = new ArrayList<>(currentCount); | ||
| int startIndex = currentIndex.get(); | ||
|
|
||
| for (int i = 0; i < currentCount; i++) { | ||
| int idx = (startIndex - i + WINDOW_SIZE) % WINDOW_SIZE; | ||
| long value = latencies.get(idx); | ||
| if (value > 0) { | ||
| sortedLatencies.add(value); | ||
| } | ||
| } | ||
|
|
||
| if (sortedLatencies.isEmpty()) { | ||
| return LatencyMetricsSnapShot.empty(); | ||
| } | ||
|
|
||
| sortedLatencies.sort(Long::compareTo); | ||
|
|
||
| // λͺ¨λ λ©νΈλ¦μ ν λ²μ κ³μ° | ||
| long avg = sortedLatencies.stream().mapToLong(Long::longValue).sum() / sortedLatencies.size(); | ||
| long min = sortedLatencies.get(0); | ||
| long max = sortedLatencies.get(sortedLatencies.size() - 1); | ||
| long p25 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 25.0) / 100.0) - 1); | ||
| long p50 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 50.0) / 100.0) - 1); | ||
| long p75 = sortedLatencies.get((int) Math.ceil((sortedLatencies.size() * 75.0) / 100.0) - 1); | ||
|
|
||
| return new LatencyMetricsSnapShot(avg, min, max, p25, p50, p75); | ||
| } | ||
|
|
||
| // μΊμλ λ©νΈλ¦μ κ°μ Έμ€κ±°λ νμμ μλ‘ κ³μ° | ||
| private LatencyMetricsSnapShot getMetricsSnapshot() { | ||
| LatencyMetricsSnapShot current = cachedMetrics.get(); | ||
| long now = System.currentTimeMillis(); | ||
|
|
||
| // μΊμκ° μ ν¨νμ§ νμΈ | ||
| if (now - current.getTimestamp() < CACHE_DURATION) { | ||
| return current; | ||
| } | ||
|
|
||
| // μλ‘μ΄ λ©νΈλ¦ κ³μ° λ° μΊμ μ λ°μ΄νΈ | ||
| LatencyMetricsSnapShot newMetrics = computeMetrics(); | ||
| cachedMetrics.set(newMetrics); | ||
| return newMetrics; | ||
| } | ||
|
|
||
| @Override | ||
| public long getAverageLatencyMicros() { | ||
| return getMetricsSnapshot().getAvgLatency(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getMinLatencyMicros() { | ||
| return getMetricsSnapshot().getMinLatency(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getMaxLatencyMicros() { | ||
| return getMetricsSnapshot().getMaxLatency(); | ||
| } | ||
|
|
||
| @Override | ||
| public long get25thPercentileLatencyMicros() { | ||
| return getMetricsSnapshot().getP25Latency(); | ||
| } | ||
|
|
||
| @Override | ||
| public long get50thPercentileLatencyMicros() { | ||
| return getMetricsSnapshot().getP50Latency(); | ||
| } | ||
|
|
||
| @Override | ||
| public long get75thPercentileLatencyMicros() { | ||
| return getMetricsSnapshot().getP75Latency(); | ||
| } | ||
|
|
||
| @Override | ||
| public void resetStatistics() { | ||
| count.set(0); | ||
| currentIndex.set(0); | ||
| cachedMetrics.set(LatencyMetricsSnapShot.empty()); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/net/spy/memcached/metrics/OpLatencyMonitorMBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package net.spy.memcached.metrics; | ||
|
|
||
| public interface OpLatencyMonitorMBean { | ||
| long getAverageLatencyMicros(); | ||
|
|
||
| long getMaxLatencyMicros(); | ||
|
|
||
| long getMinLatencyMicros(); | ||
|
|
||
| long get25thPercentileLatencyMicros(); | ||
|
|
||
| long get50thPercentileLatencyMicros(); | ||
|
|
||
| long get75thPercentileLatencyMicros(); | ||
|
|
||
| void resetStatistics(); | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/main/java/net/spy/memcached/metrics/OpThroughputMonitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package net.spy.memcached.metrics; | ||
|
|
||
| import java.util.concurrent.atomic.LongAdder; | ||
|
|
||
| import net.spy.memcached.ArcusMBeanServer; | ||
|
|
||
| public final class OpThroughputMonitor implements OpThroughputMonitorMBean { | ||
| private static final OpThroughputMonitor INSTANCE = new OpThroughputMonitor(); | ||
|
|
||
| private final LongAdder completeOps = new LongAdder(); | ||
| private final LongAdder cancelOps = new LongAdder(); | ||
| private final LongAdder timeOutOps = new LongAdder(); | ||
| private final boolean enabled; | ||
|
|
||
| private OpThroughputMonitor() { | ||
| if (System.getProperty("arcus.mbean", "false").toLowerCase().equals("false")) { | ||
| enabled = false; | ||
| return; | ||
| } | ||
| enabled = true; | ||
| try { | ||
| ArcusMBeanServer mbs = ArcusMBeanServer.getInstance(); | ||
| mbs.registMBean(this, this.getClass().getPackage().getName() | ||
| + ":type=" + this.getClass().getSimpleName()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to register Throughput MBean", e); | ||
| } | ||
| } | ||
|
|
||
| public static OpThroughputMonitor getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public void addCompletedOpCount() { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
| completeOps.increment(); | ||
| } | ||
|
|
||
| public void addCanceledOpCount() { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
| cancelOps.increment(); | ||
| } | ||
|
|
||
| public void addTimeOutedOpCount(int count) { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
| timeOutOps.add(count); | ||
| } | ||
|
|
||
| @Override | ||
| public long getCompletedOps() { | ||
| return completeOps.sum(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getCanceledOps() { | ||
| return cancelOps.sum(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getTimeoutOps() { | ||
| return timeOutOps.sum(); | ||
| } | ||
|
|
||
| @Override | ||
| public void resetStatistics() { | ||
| completeOps.reset(); | ||
| cancelOps.reset(); | ||
| timeOutOps.reset(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/spy/memcached/metrics/OpThroughputMonitorMBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package net.spy.memcached.metrics; | ||
|
|
||
| public interface OpThroughputMonitorMBean { | ||
| long getCompletedOps(); | ||
|
|
||
| long getCanceledOps(); | ||
|
|
||
| long getTimeoutOps(); | ||
|
|
||
| void resetStatistics(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.