Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

refactor: optimize perf-counter #98

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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.
*/
Expand Down
51 changes: 19 additions & 32 deletions src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

/** Created by weijiesun on 18-3-9. */
public final class MetricsPool {

private final String defaultTag;

public MetricsPool(String host, String tags, int reportStepSec) {
theMetric = new FalconMetric();
theMetric.endpoint = host;
theMetric.step = reportStepSec;
theMetric.tags = tags;
defaultTag = tags;
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
}

public void setMeter(String counterName, long count) {
Expand All @@ -35,48 +39,28 @@ 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)
throws JSONException {
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)
Expand All @@ -98,27 +82,30 @@ public String metricsToJson() throws JSONException {
StringBuilder builder = new StringBuilder();
builder.append('[');
SortedMap<String, Meter> meters = registry.getMeters();
boolean start = true;
for (Map.Entry<String, Meter> entry : meters.entrySet()) {
if (!start) {
builder.append(',');
}
genJsonsFromMeter(entry.getKey(), entry.getValue(), builder);
start = false;
}

for (Map.Entry<String, Histogram> entry : registry.getHistograms().entrySet()) {
if (!start) {
builder.append(',');
}
genJsonsFromHistogram(entry.getKey(), entry.getValue(), builder);
start = false;
}
builder.deleteCharAt(builder.length() - 1);
builder.append("]");

return builder.toString();
}

private String getTableTag(String counterName) {
if (defaultTag.contains("table=")) {
return defaultTag;
}
String[] result = counterName.split("@");
if (result.length >= 2) {
return defaultTag.equals("") ? ("table=" + result[1]) : (defaultTag + ",table=" + result[1]);
}
return defaultTag;
}

static final class FalconMetric {
public String endpoint; // metric host
public String metric; // metric name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
this.expireNanoTime = createNanoTime + timeoutInMilliseconds;
this.isCompleted = false;
this.backupRequestTask = null;
}

public ClientRequestRound(
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down