#1345 - Refactor io.ebean.meta.MetaInfoManager API (query execution metrics). Breaking change for people collecting query execution metrics.

This commit is contained in:
Rob Bygrave
2018-03-10 02:51:51 +13:00
parent eda6350d89
commit 85bfe3d03e
79 changed files with 928 additions and 466 deletions
@@ -1,7 +1,7 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetricType;
import io.ebeaninternal.metric.MetricFactory;
import io.ebeaninternal.metric.QueryPlanCollector;
import io.ebeaninternal.metric.QueryPlanMetric;
import io.ebeaninternal.metric.TimedMetric;
import io.ebeaninternal.metric.TimedMetricMap;
@@ -12,22 +12,18 @@ import io.ebeaninternal.metric.TimedMetricMap;
public class DMetricFactory implements MetricFactory {
@Override
public TimedMetricMap createTimedMetricMap(String name) {
return new DTimedMetricMap(name);
public TimedMetricMap createTimedMetricMap(MetricType metricType, String name) {
return new DTimedMetricMap(metricType, name);
}
@Override
public TimedMetric createTimedMetric(String name) {
return new DTimedMetric(name);
public TimedMetric createTimedMetric(MetricType metricType, String name) {
return new DTimedMetric(metricType, name);
}
@Override
public QueryPlanMetric createQueryPlanMetric(Class<?> type, String label, String sql) {
return new DQueryPlanMetric(new DQueryPlanMeta(type, label, sql), createTimedMetric(label));
public QueryPlanMetric createQueryPlanMetric(MetricType metricType, Class<?> type, String label, String sql) {
return new DQueryPlanMetric(new DQueryPlanMeta(type, label, sql), new DTimedMetric(metricType, label));
}
@Override
public QueryPlanCollector createCollector(boolean reset) {
return new DQueryPlanCollector(reset);
}
}
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.profile;
import io.ebean.ProfileLocation;
import io.ebean.meta.MetricType;
import io.ebean.service.SpiProfileLocationFactory;
import io.ebeaninternal.metric.MetricFactory;
import io.ebeaninternal.metric.TimedMetric;
@@ -18,7 +19,7 @@ public class DProfileLocationFactory implements SpiProfileLocationFactory {
@Override
public ProfileLocation create(int lineNumber, String label) {
TimedMetric timedMetric = MetricFactory.get().createTimedMetric("txn.named." + label);
TimedMetric timedMetric = MetricFactory.get().createTimedMetric(MetricType.TXN, "txn.named." + label);
DTimedProfileLocation loc = new DTimedProfileLocation(lineNumber, label, timedMetric);
TimedProfileLocationRegistry.register(loc);
@@ -1,7 +1,8 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetaQueryMetric;
import io.ebeaninternal.metric.QueryPlanCollector;
import io.ebean.meta.MetricType;
import io.ebean.meta.MetricVisitor;
import io.ebeaninternal.metric.QueryPlanMetric;
import io.ebeaninternal.metric.TimedMetric;
import io.ebeaninternal.metric.TimedMetricStats;
@@ -9,18 +10,18 @@ import io.ebeaninternal.metric.TimedMetricStats;
class DQueryPlanMetric implements QueryPlanMetric {
private final DQueryPlanMeta meta;
private final TimedMetric metric;
private final DTimedMetric metric;
DQueryPlanMetric(DQueryPlanMeta meta, TimedMetric metric) {
DQueryPlanMetric(DQueryPlanMeta meta, DTimedMetric metric) {
this.meta = meta;
this.metric = metric;
}
@Override
public void collect(QueryPlanCollector collector) {
TimedMetricStats stats = metric.collect(collector.isReset());
public void visit(MetricVisitor visitor) {
TimedMetricStats stats = metric.collect(visitor.isReset());
if (stats != null) {
collector.add(new Stats(meta, stats));
visitor.visitQuery(new Stats(meta, stats));
}
}
@@ -44,6 +45,11 @@ class DQueryPlanMetric implements QueryPlanMetric {
return meta + " " + stats + " sql:" + getSql();
}
@Override
public MetricType getMetricType() {
return stats.getMetricType();
}
@Override
public Class<?> getType() {
return meta.getType();
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetricType;
import io.ebeaninternal.metric.TimedMetricStats;
/**
@@ -7,6 +8,8 @@ import io.ebeaninternal.metric.TimedMetricStats;
*/
class DTimeMetricStats implements TimedMetricStats {
private final MetricType metricType;
private final String name;
private String location;
@@ -21,7 +24,8 @@ class DTimeMetricStats implements TimedMetricStats {
private final long beanCount;
DTimeMetricStats(String name, long collectionStart, long count, long total, long max, long beanCount) {
DTimeMetricStats(MetricType metricType, String name, long collectionStart, long count, long total, long max, long beanCount) {
this.metricType = metricType;
this.name = name;
this.startTime = collectionStart;
this.count = count;
@@ -53,6 +57,11 @@ class DTimeMetricStats implements TimedMetricStats {
this.location = location;
}
@Override
public MetricType getMetricType() {
return metricType;
}
@Override
public String getName() {
return name;
@@ -1,9 +1,9 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetaTimedMetric;
import io.ebean.meta.MetricType;
import io.ebean.meta.MetricVisitor;
import io.ebeaninternal.metric.TimedMetric;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
@@ -16,6 +16,8 @@ import java.util.concurrent.atomic.LongAdder;
*/
class DTimedMetric implements TimedMetric {
private final MetricType metricType;
private final String name;
private final LongAdder beanCount = new LongAdder();
@@ -28,7 +30,8 @@ class DTimedMetric implements TimedMetric {
private final AtomicLong startTime = new AtomicLong(System.currentTimeMillis());
DTimedMetric(String name) {
DTimedMetric(MetricType metricType, String name) {
this.metricType = metricType;
this.name = name;
}
@@ -54,11 +57,23 @@ class DTimedMetric implements TimedMetric {
return count.sum() == 0;
}
/**
* Reset all the internal counters and start time.
*/
@Override
public void collect(boolean reset, List<MetaTimedMetric> result) {
DTimeMetricStats metric = collect(reset);
public void reset() {
startTime.set(System.currentTimeMillis());
max.reset();
count.reset();
total.reset();
beanCount.reset();
}
@Override
public void visit(MetricVisitor visitor) {
DTimeMetricStats metric = collect(visitor.isReset());
if (metric != null) {
result.add(metric);
visitor.visitTimed(metric);
}
}
@@ -78,7 +93,7 @@ class DTimedMetric implements TimedMetric {
/**
* Return the current statistics resetting the internal values if reset is true.
*/
public DTimeMetricStats getStatistics(boolean reset) {
private DTimeMetricStats getStatistics(boolean reset) {
if (reset) {
// Note these values are not guaranteed to be consistent wrt each other
@@ -88,21 +103,11 @@ class DTimedMetric implements TimedMetric {
final long totalVal = total.sumThenReset();
final long countVal = count.sumThenReset();
final long startTimeVal = startTime.getAndSet(System.currentTimeMillis());
return new DTimeMetricStats(name, startTimeVal, countVal, totalVal, maxVal, beans);
return new DTimeMetricStats(metricType, name, startTimeVal, countVal, totalVal, maxVal, beans);
} else {
return new DTimeMetricStats(name, startTime.get(), count.sum(), total.sum(), max.get(), beanCount.sum());
return new DTimeMetricStats(metricType, name, startTime.get(), count.sum(), total.sum(), max.get(), beanCount.sum());
}
}
/**
* Reset all the internal counters and start time.
*/
public void reset() {
startTime.set(System.currentTimeMillis());
max.reset();
count.reset();
total.reset();
}
}
@@ -1,30 +1,38 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetaTimedMetric;
import io.ebean.meta.MetricType;
import io.ebean.meta.MetricVisitor;
import io.ebeaninternal.metric.TimedMetricMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
class DTimedMetricMap implements TimedMetricMap {
private final MetricType metricType;
private final String name;
private final ConcurrentHashMap<String, DTimedMetric> map = new ConcurrentHashMap<>();
DTimedMetricMap(String name) {
DTimedMetricMap(MetricType metricType, String name) {
this.metricType = metricType;
this.name = name;
}
@Override
public void add(String key, long exeMicros) {
map.computeIfAbsent(key, (k)-> new DTimedMetric(name + key)).add(exeMicros);
map.computeIfAbsent(key, (k) -> new DTimedMetric(metricType, name + key)).add(exeMicros);
}
@Override
public void collect(boolean reset, List<MetaTimedMetric> list) {
public void add(String key, long exeMicros, int rows) {
map.computeIfAbsent(key, (k) -> new DTimedMetric(metricType, name + key)).add(exeMicros, rows);
}
@Override
public void visit(MetricVisitor visitor) {
for (DTimedMetric value : map.values()) {
value.collect(reset, list);
value.visit(visitor);
}
}
}
@@ -1,11 +1,9 @@
package io.ebeaninternal.server.profile;
import io.ebean.meta.MetaTimedMetric;
import io.ebean.meta.MetricVisitor;
import io.ebeaninternal.metric.TimedMetric;
import io.ebeaninternal.metric.TimedMetricStats;
import java.util.List;
/**
* Default profile location that uses stack trace.
*/
@@ -37,13 +35,11 @@ class DTimedProfileLocation extends DProfileLocation implements TimedProfileLoca
}
@Override
public void collect(boolean reset, List<MetaTimedMetric> list) {
TimedMetricStats collect = timedMetric.collect(reset);
public void visit(MetricVisitor visitor) {
TimedMetricStats collect = timedMetric.collect(visitor.isReset());
if (collect != null) {
collect.setLocation(obtain());
list.add(collect);
visitor.visitTimed(collect);
}
}
}
@@ -1,11 +1,9 @@
package io.ebeaninternal.server.profile;
import io.ebean.ProfileLocation;
import io.ebean.meta.MetaTimedMetric;
import io.ebean.meta.MetricVisitor;
import io.ebeaninternal.metric.TimedMetric;
import java.util.List;
/**
* ProfileLocation that collects timing metrics.
*/
@@ -22,7 +20,7 @@ public interface TimedProfileLocation extends ProfileLocation {
TimedMetric getMetric();
/**
* Collect the metrics adding to the given list if the metrics are non empty.
* Visit the non empty metrics.
*/
void collect(boolean reset, List<MetaTimedMetric> list);
void visit(MetricVisitor visitor);
}