下面列出了怎么用com.codahale.metrics.SlidingTimeWindowReservoir的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testCustomReservoir() throws Exception {
System.setProperty("timer.reservoir", UniformReservoir.class.getName());
System.setProperty("histogram.size", "2048");
System.setProperty("histogram.window", "600");
System.setProperty("histogram.reservoir", SlidingTimeWindowReservoir.class.getName());
NodeConfig cfg = loadNodeConfig();
SolrMetricManager mgr = new SolrMetricManager(cfg.getSolrResourceLoader(), cfg.getMetricsConfig());
assertTrue(mgr.getCounterSupplier() instanceof MetricSuppliers.DefaultCounterSupplier);
assertTrue(mgr.getMeterSupplier() instanceof MetricSuppliers.DefaultMeterSupplier);
assertTrue(mgr.getTimerSupplier() instanceof MetricSuppliers.DefaultTimerSupplier);
assertTrue(mgr.getHistogramSupplier() instanceof MetricSuppliers.DefaultHistogramSupplier);
Reservoir rsv = ((MetricSuppliers.DefaultTimerSupplier)mgr.getTimerSupplier()).getReservoir();
assertTrue(rsv instanceof UniformReservoir);
rsv = ((MetricSuppliers.DefaultHistogramSupplier)mgr.getHistogramSupplier()).getReservoir();
assertTrue(rsv instanceof SlidingTimeWindowReservoir);
}
/** add a metric (time) to a specific resource - for example
* type = PostgresClient.get
* time = single operation time in nanoseconds
*/
public static void addStatElement(String type, long time){
// unsynchronized but fast check
if (!registeredStatRequesters.contains(type)) {
// prevent race condition - registering the same type twice will throw an exception
synchronized(registeredStatRequesters) {
// synchronized check
if (!registeredStatRequesters.contains(type)) {
METRICS.register(type, new Histogram(new SlidingTimeWindowReservoir(60,
TimeUnit.SECONDS)));
registeredStatRequesters.add(type);
}
}
}
METRICS.histogram(type).update(time/1000000);
}
@Setup(Level.Iteration)
public void setup() {
registry = new MetricRegistry();
histogram = registry.histogram("histogram");
histogramSlidingTimeWindow =
registry.register("slidingTimeWindowHistogram",
new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS)));
histogramUniform =
registry.register("uniformHistogram",
new Histogram(new UniformReservoir()));
}
public static Timer createTimer(MetricRegistry metrics, String name, final String pipelineName, final String pipelineRev) {
return create(
metrics,
new Timer(new SlidingTimeWindowReservoir(60, TimeUnit.SECONDS)),
metricName(name, TIMER_SUFFIX),
pipelineName,
pipelineRev
);
}
/**
* Constructor used internally.
*/
private TaskExecutor(int taskExecutorThreadPoolSize, int coreRetryThreadPoolSize, long retryIntervalInSeconds,
int queuedTaskTimeMaxSize, long queuedTaskTimeMaxAge, int timerWindowSize) {
Preconditions.checkArgument(taskExecutorThreadPoolSize > 0, "Task executor thread pool size should be positive");
Preconditions.checkArgument(retryIntervalInSeconds > 0, "Task retry interval should be positive");
Preconditions.checkArgument(queuedTaskTimeMaxSize > 0, "Queued task time max size should be positive");
Preconditions.checkArgument(queuedTaskTimeMaxAge > 0, "Queued task time max age should be positive");
// Currently a fixed-size thread pool is used to execute tasks. We probably need to revisit this later.
this.taskExecutor = ExecutorsUtils.loggingDecorator(Executors.newScheduledThreadPool(
taskExecutorThreadPoolSize,
ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("TaskExecutor-%d"))));
this.retryIntervalInSeconds = retryIntervalInSeconds;
this.queuedTaskTimeMaxSize = queuedTaskTimeMaxSize;
this.queuedTaskTimeMaxAge = queuedTaskTimeMaxAge;
this.taskCreateAndRunTimer = new Timer(new SlidingTimeWindowReservoir(timerWindowSize, TimeUnit.MINUTES));
this.forkExecutor = ExecutorsUtils.loggingDecorator(
new ThreadPoolExecutor(
// The core thread pool size is equal to that of the task
// executor as there's at least one fork per task
taskExecutorThreadPoolSize,
// The fork executor thread pool size is essentially unbounded. This is to make sure all forks of
// a task get a thread to run so all forks of the task are making progress. This is necessary since
// otherwise the parent task will be blocked if the record queue (bounded) of some fork is full and
// that fork has not yet started to run because of no available thread. The task cannot proceed in
// this case because it has to make sure every records go to every forks.
Integer.MAX_VALUE,
0L,
TimeUnit.MILLISECONDS,
// The work queue is a SynchronousQueue. This essentially forces a new thread to be created for each fork.
new SynchronousQueue<Runnable>(),
ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("ForkExecutor-%d"))));
}
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram, long windowSize, TimeUnit unit) {
super(new SlidingTimeWindowReservoir(windowSize, unit));
this.name = name;
Optional<MetricContext> parentContext = context.getParent();
if (parentContext.isPresent()) {
this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name, windowSize, unit));
} else {
this.parentHistogram = Optional.absent();
}
this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram);
}
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer, long windowSize, TimeUnit unit) {
super(new SlidingTimeWindowReservoir(windowSize, unit));
this.name = name;
Optional<MetricContext> parentContext = context.getParent();
if (parentContext.isPresent()) {
this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name, windowSize, unit));
} else {
this.parentTimer = Optional.absent();
}
this.timer = new WeakReference<>(contextAwareTimer);
}
public Histogram getSlidingTimeWindowHistogram(MetricRegistry registry, String name, final long window,
final TimeUnit windowUnit) {
return getOrAdd(registry, name, new MetricBuilder<Histogram>() {
@Override
public Histogram newMetric() {
return new Histogram(new SlidingTimeWindowReservoir(window, windowUnit));
}
@Override
public boolean isInstance(Metric metric) {
return Histogram.class.isInstance(metric);
}
});
}
public Timer getSlidingTimeWindowTimer(MetricRegistry registry, String name, final long window,
final TimeUnit windowUnit) {
return getOrAdd(registry, name, new MetricBuilder<Timer>() {
@Override
public Timer newMetric() {
return new Timer(new SlidingTimeWindowReservoir(window, windowUnit));
}
@Override
public boolean isInstance(Metric metric) {
return Timer.class.isInstance(metric);
}
});
}
@SuppressWarnings({"unchecked"})
private static final Reservoir getReservoir(SolrResourceLoader loader, PluginInfo info) {
if (info == null) {
return new ExponentiallyDecayingReservoir();
}
Clock clk = getClock(info, CLOCK);
String clazz = ExponentiallyDecayingReservoir.class.getName();
int size = -1;
double alpha = -1;
long window = -1;
if (info.initArgs != null) {
if (info.initArgs.get(RESERVOIR) != null) {
String val = String.valueOf(info.initArgs.get(RESERVOIR)).trim();
if (!val.isEmpty()) {
clazz = val;
}
}
Number n = (Number)info.initArgs.get(RESERVOIR_SIZE);
if (n != null) {
size = n.intValue();
}
n = (Number)info.initArgs.get(RESERVOIR_EDR_ALPHA);
if (n != null) {
alpha = n.doubleValue();
}
n = (Number)info.initArgs.get(RESERVOIR_WINDOW);
if (n != null) {
window = n.longValue();
}
}
if (size <= 0) {
size = DEFAULT_SIZE;
}
if (alpha <= 0) {
alpha = DEFAULT_ALPHA;
}
// special case for core implementations
if (clazz.equals(EDR_CLAZZ)) {
return new ExponentiallyDecayingReservoir(size, alpha, clk);
} else if (clazz.equals(UNI_CLAZZ)) {
return new UniformReservoir(size);
} else if (clazz.equals(STW_CLAZZ)) {
if (window <= 0) {
window = DEFAULT_WINDOW; // 5 minutes, comparable to EDR
}
return new SlidingTimeWindowReservoir(window, TimeUnit.SECONDS);
} else if (clazz.equals(SW_CLAZZ)) {
return new SlidingWindowReservoir(size);
} else { // custom reservoir
Reservoir reservoir;
try {
reservoir = loader.newInstance(clazz, Reservoir.class);
if (reservoir instanceof PluginInfoInitialized) {
((PluginInfoInitialized)reservoir).init(info);
} else {
SolrPluginUtils.invokeSetters(reservoir, info.initArgs, true);
}
return reservoir;
} catch (Exception e) {
log.warn("Error initializing custom Reservoir implementation (will use default): {}", info, e);
return new ExponentiallyDecayingReservoir(size, alpha, clk);
}
}
}
@Override
public Histogram newMetric() {
return new Histogram(new SlidingTimeWindowReservoir(
TimeUnit.MINUTES.toMinutes(HISTOGRAM_TTL_MINUTES), TimeUnit.MINUTES));
}
ContextAwareHistogram(MetricContext context, String name, long windowSize, TimeUnit unit) {
super(new SlidingTimeWindowReservoir(windowSize, unit));
this.innerHistogram = new InnerHistogram(context, name, this, windowSize, unit);
this.context = context;
}
ContextAwareTimer(MetricContext context, String name, long windowSize, TimeUnit unit) {
super(new SlidingTimeWindowReservoir(windowSize, unit));
this.innerTimer = new InnerTimer(context, name, this, windowSize, unit);
this.context = context;
}