diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java b/src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java index ece5d7be..2552678c 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java @@ -35,7 +35,7 @@ public class ClientOptions { "127.0.0.1:34601,127.0.0.1:34602,127.0.0.1:34603"; public static final Duration DEFAULT_OPERATION_TIMEOUT = Duration.ofMillis(1000); public static final int DEFAULT_ASYNC_WORKERS = Runtime.getRuntime().availableProcessors(); - public static final boolean DEFAULT_ENABLE_PERF_COUNTER = false; + public static final boolean DEFAULT_ENABLE_PERF_COUNTER = true; public static final String DEFAULT_FALCON_PERF_COUNTER_TAGS = ""; public static final Duration DEFAULT_FALCON_PUSH_INTERVAL = Duration.ofSeconds(10); public static final boolean DEFAULT_ENABLE_WRITE_LIMIT = true; @@ -195,7 +195,7 @@ public Builder asyncWorkers(int asyncWorkers) { /** * Whether to enable performance statistics. If true, the client will periodically report * metrics to local falcon agent (currently we only support falcon as monitoring system). - * Defaults to {@literal false}, see {@link #DEFAULT_ENABLE_PERF_COUNTER}. + * Defaults to {@literal true}, see {@link #DEFAULT_ENABLE_PERF_COUNTER}. * * @param enablePerfCounter enablePerfCounter * @return {@code this} @@ -317,7 +317,7 @@ public int getAsyncWorkers() { /** * Whether to enable performance statistics. If true, the client will periodically report metrics * to local falcon agent (currently we only support falcon as monitoring system). Defaults to - * {@literal false}. + * {@literal true}. * * @return whether to enable performance statistics. */ diff --git a/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java b/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java index e41ec295..8ecb5508 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java +++ b/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java @@ -15,11 +15,15 @@ /** Created by weijiesun on 18-3-9. */ public final class MetricsPool { + + private final String defaultTags; + public MetricsPool(String host, String tags, int reportStepSec) { theMetric = new FalconMetric(); theMetric.endpoint = host; theMetric.step = reportStepSec; theMetric.tags = tags; + defaultTags = tags; } public void setMeter(String counterName, long count) { @@ -35,23 +39,9 @@ public void genJsonsFromMeter(String name, Meter meter, StringBuilder output) theMetric.counterType = "GAUGE"; theMetric.metric = name + ".cps-1sec"; + theMetric.tags = getTableTag(name); theMetric.value = meter.getMeanRate(); oneMetricToJson(theMetric, output); - output.append(','); - - theMetric.metric = name + ".cps-1min"; - theMetric.value = meter.getOneMinuteRate(); - oneMetricToJson(theMetric, output); - output.append(','); - - theMetric.metric = name + ".cps-5min"; - theMetric.value = meter.getFiveMinuteRate(); - oneMetricToJson(theMetric, output); - output.append(','); - - theMetric.metric = name + ".cps-15min"; - theMetric.value = meter.getFifteenMinuteRate(); - oneMetricToJson(theMetric, output); } public void genJsonsFromHistogram(String name, Histogram hist, StringBuilder output) @@ -59,24 +49,16 @@ public void genJsonsFromHistogram(String name, Histogram hist, StringBuilder out theMetric.counterType = "GAUGE"; Snapshot s = hist.getSnapshot(); - theMetric.metric = name + ".p50"; - theMetric.value = s.getMedian(); - oneMetricToJson(theMetric, output); - output.append(','); - theMetric.metric = name + ".p99"; + theMetric.tags = getTableTag(name); theMetric.value = s.get99thPercentile(); oneMetricToJson(theMetric, output); output.append(','); theMetric.metric = name + ".p999"; + theMetric.tags = getTableTag(name); theMetric.value = s.get999thPercentile(); oneMetricToJson(theMetric, output); - output.append(','); - - theMetric.metric = name + ".max"; - theMetric.value = s.getMax(); - oneMetricToJson(theMetric, output); } public static void oneMetricToJson(FalconMetric metric, StringBuilder output) @@ -98,27 +80,38 @@ public String metricsToJson() throws JSONException { StringBuilder builder = new StringBuilder(); builder.append('['); SortedMap meters = registry.getMeters(); - boolean start = true; for (Map.Entry entry : meters.entrySet()) { - if (!start) { - builder.append(','); - } genJsonsFromMeter(entry.getKey(), entry.getValue(), builder); - start = false; + builder.append(','); } for (Map.Entry entry : registry.getHistograms().entrySet()) { - if (!start) { - builder.append(','); - } genJsonsFromHistogram(entry.getKey(), entry.getValue(), builder); - start = false; + builder.append(','); + } + + if (builder.charAt(builder.length() - 1) == ',') { + builder.deleteCharAt(builder.length() - 1); } + builder.append("]"); return builder.toString(); } + private String getTableTag(String counterName) { + if (defaultTags.contains("table=")) { + return defaultTags; + } + String[] result = counterName.split("@"); + if (result.length >= 2) { + return defaultTags.equals("") + ? ("table=" + result[1]) + : (defaultTags + ",table=" + result[1]); + } + return defaultTags; + } + static final class FalconMetric { public String endpoint; // metric host public String metric; // metric name diff --git a/src/main/java/com/xiaomi/infra/pegasus/rpc/ClusterOptions.java b/src/main/java/com/xiaomi/infra/pegasus/rpc/ClusterOptions.java index efaef7bf..c3a6fb46 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/rpc/ClusterOptions.java +++ b/src/main/java/com/xiaomi/infra/pegasus/rpc/ClusterOptions.java @@ -19,7 +19,7 @@ public class ClusterOptions { String.valueOf(Runtime.getRuntime().availableProcessors()); public static final String PEGASUS_ENABLE_PERF_COUNTER_KEY = "enable_perf_counter"; - public static final String PEGASUS_ENABLE_PERF_COUNTER_DEF = "false"; + public static final String PEGASUS_ENABLE_PERF_COUNTER_DEF = "true"; public static final String PEGASUS_PERF_COUNTER_TAGS_KEY = "perf_counter_tags"; public static final String PEGASUS_PERF_COUNTER_TAGS_DEF = ""; diff --git a/src/main/java/com/xiaomi/infra/pegasus/rpc/async/ClientRequestRound.java b/src/main/java/com/xiaomi/infra/pegasus/rpc/async/ClientRequestRound.java index ece35831..2818f511 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/rpc/async/ClientRequestRound.java +++ b/src/main/java/com/xiaomi/infra/pegasus/rpc/async/ClientRequestRound.java @@ -34,12 +34,15 @@ public ClientRequestRound( Table.ClientOPCallback cb, boolean enableCounter, long timeoutInMilliseconds) { - this( - op, - cb, - enableCounter, - System.nanoTime() + timeoutInMilliseconds * 1000000L, - timeoutInMilliseconds); + this.operator = op; + this.callback = cb; + this.timeoutMs = timeoutInMilliseconds; + + this.enableCounter = enableCounter; + this.createNanoTime = System.nanoTime(); + this.expireNanoTime = createNanoTime + timeoutInMilliseconds; + this.isCompleted = false; + this.backupRequestTask = null; } public ClientRequestRound( @@ -48,14 +51,8 @@ public ClientRequestRound( boolean enableCounter, long expireNanoTime, long timeoutInMilliseconds) { - this.operator = op; - this.callback = cb; - this.timeoutMs = timeoutInMilliseconds; - - this.enableCounter = enableCounter; + this(op, cb, enableCounter, timeoutInMilliseconds); this.expireNanoTime = expireNanoTime; - this.isCompleted = false; - this.backupRequestTask = null; } public com.xiaomi.infra.pegasus.operator.client_operator getOperator() { diff --git a/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java b/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java index 435fe5a9..88c0676f 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java +++ b/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java @@ -33,7 +33,7 @@ public void genJsonsFromMeter() throws Exception { pool.genJsonsFromMeter("TestName", m, builder); JSONArray array = new JSONArray("[" + builder.toString() + "]"); - Assert.assertEquals(4, array.length()); + Assert.assertEquals(1, array.length()); String[] metrics = { "TestName.cps-1sec", "TestName.cps-1min", "TestName.cps-5min", "TestName.cps-15min" @@ -62,9 +62,9 @@ public void genJsonFromHistogram() throws Exception { pool.genJsonsFromHistogram("TestHist", h, builder); JSONArray array = new JSONArray("[" + builder.toString() + "]"); - Assert.assertEquals(4, array.length()); + Assert.assertEquals(2, array.length()); - String[] metrics = {"TestHist.p50", "TestHist.p99", "TestHist.p999", "TestHist.max"}; + String[] metrics = {"TestHist.p99", "TestHist.p999"}; for (int i = 0; i < array.length(); ++i) { JSONObject j = array.getJSONObject(i); @@ -119,17 +119,24 @@ public void metricsToJson() throws Exception { String tags = "what=you,like=another"; MetricsPool pool = new MetricsPool(host, tags, 20); - pool.setMeter("aaa", 1); + pool.setMeter("aaa@temp", 1); pool.setMeter("aaa", 2); - for (int i = 0; i < 10000; ++i) pool.setHistorgram("ccc", i); + for (int i = 0; i < 10000; ++i) { + pool.setHistorgram("ccc", i); + pool.setHistorgram("ccc@temp", i); + } JSONArray array = new JSONArray(pool.metricsToJson()); - Assert.assertEquals(8, array.length()); + Assert.assertEquals(6, array.length()); for (int i = 0; i < array.length(); ++i) { JSONObject j = array.getJSONObject(i); - Assert.assertEquals(tags, j.getString("tags")); + if (j.getString("metric").contains("@")) { + Assert.assertEquals(tags + ",table=temp", j.getString("tags")); + } else { + Assert.assertEquals(tags, j.getString("tags")); + } Assert.assertEquals("GAUGE", j.getString("counterType")); Assert.assertEquals(20, j.getInt("step")); Assert.assertEquals(host, j.getString("endpoint"));