From d45f09257947c46b3caac8cbc03db179a2dc7288 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 13 Nov 2015 16:58:46 -0800 Subject: [PATCH] Add TimerContext to wrap timer metrics TimerContext wraps the context used in Timer{0|1|2|3} and allows to get the start time and elapsed time. Update the replication plugin to use it. Change-Id: I194b9a7bd6a00bc2841ac37ca8654f0b4a2dda00 --- .../com/google/gerrit/metrics/Timer0.java | 14 ++--- .../com/google/gerrit/metrics/Timer1.java | 8 +-- .../com/google/gerrit/metrics/Timer2.java | 8 +-- .../com/google/gerrit/metrics/Timer3.java | 9 +-- .../google/gerrit/metrics/TimerContext.java | 59 +++++++++++++++++++ plugins/replication | 2 +- 6 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 gerrit-server/src/main/java/com/google/gerrit/metrics/TimerContext.java diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer0.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer0.java index fc8cef3e76..ff0735e838 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer0.java +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer0.java @@ -31,22 +31,22 @@ import java.util.concurrent.TimeUnit; * */ public abstract class Timer0 implements RegistrationHandle { - public class Context implements AutoCloseable { - private final long startNanos; + public static class Context extends TimerContext { + private final Timer0 timer; - Context() { - this.startNanos = System.nanoTime(); + Context(Timer0 timer) { + this.timer = timer; } @Override - public void close() { - record(System.nanoTime() - startNanos, NANOSECONDS); + public void record(long elapsed) { + timer.record(elapsed, NANOSECONDS); } } /** Begin a timer for the current block, value will be recorded when closed. */ public Context start() { - return new Context(); + return new Context(this); } /** Record a value in the distribution. */ diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer1.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer1.java index 88576f26c3..e0bc4fcb96 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer1.java +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer1.java @@ -33,21 +33,19 @@ import java.util.concurrent.TimeUnit; * @param type of the field. */ public abstract class Timer1 implements RegistrationHandle { - public static class Context implements AutoCloseable { + public static class Context extends TimerContext { private final Timer1 timer; private final Object field1; - private final long startNanos; @SuppressWarnings("unchecked") Context(Timer1 timer, F1 field1) { this.timer = (Timer1) timer; this.field1 = field1; - this.startNanos = System.nanoTime(); } @Override - public void close() { - timer.record(field1, System.nanoTime() - startNanos, NANOSECONDS); + public void record(long elapsed) { + timer.record(field1, elapsed, NANOSECONDS); } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer2.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer2.java index f4ffebd801..5bfaba0b6e 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer2.java +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer2.java @@ -34,23 +34,21 @@ import java.util.concurrent.TimeUnit; * @param type of the field. */ public abstract class Timer2 implements RegistrationHandle { - public static class Context implements AutoCloseable { + public static class Context extends TimerContext { private final Timer2 timer; private final Object field1; private final Object field2; - private final long startNanos; @SuppressWarnings("unchecked") Context(Timer2 timer, F1 field1, F2 field2) { this.timer = (Timer2) timer; this.field1 = field1; this.field2 = field2; - this.startNanos = System.nanoTime(); } @Override - public void close() { - timer.record(field1, field2, System.nanoTime() - startNanos, NANOSECONDS); + public void record(long elapsed) { + timer.record(field1, field2, elapsed, NANOSECONDS); } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer3.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer3.java index 60f0d5a884..c564d42e1b 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer3.java +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/Timer3.java @@ -35,12 +35,11 @@ import java.util.concurrent.TimeUnit; * @param type of the field. */ public abstract class Timer3 implements RegistrationHandle { - public static class Context implements AutoCloseable { + public static class Context extends TimerContext { private final Timer3 timer; private final Object field1; private final Object field2; private final Object field3; - private final long startNanos; @SuppressWarnings("unchecked") Context(Timer3 timer, F1 f1, F2 f2, F3 f3) { @@ -48,13 +47,11 @@ public abstract class Timer3 implements RegistrationHandle { this.field1 = f1; this.field2 = f2; this.field3 = f3; - this.startNanos = System.nanoTime(); } @Override - public void close() { - timer.record(field1, field2, field3, - System.nanoTime() - startNanos, NANOSECONDS); + public void record(long elapsed) { + timer.record(field1, field2, field3, elapsed, NANOSECONDS); } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/metrics/TimerContext.java b/gerrit-server/src/main/java/com/google/gerrit/metrics/TimerContext.java new file mode 100644 index 0000000000..32ef753cb5 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/metrics/TimerContext.java @@ -0,0 +1,59 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.metrics; + +abstract class TimerContext implements AutoCloseable { + private final long startNanos; + private boolean stopped; + + TimerContext() { + this.startNanos = System.nanoTime(); + } + + /** + * Record the elapsed time to the timer. + * + * @param elapsed Elapsed time in nanoseconds. + */ + public abstract void record(long elapsed); + + /** Get the start time in system time nanoseconds. */ + public long getStartTime() { + return startNanos; + } + + /** + * Stop the timer and record the elapsed time. + * + * @return the elapsed time in nanoseconds. + * @throws IllegalStateException if the timer is already stopped. + */ + public long stop() { + if (!stopped) { + stopped = true; + long elapsed = System.nanoTime() - startNanos; + record(elapsed); + return elapsed; + } + throw new IllegalStateException("Already stopped"); + } + + @Override + public void close() { + if (!stopped) { + stop(); + } + } +} diff --git a/plugins/replication b/plugins/replication index 168032cb1e..4ab29b755a 160000 --- a/plugins/replication +++ b/plugins/replication @@ -1 +1 @@ -Subproject commit 168032cb1efadb83db5da4fd0060c94c9e05ccfa +Subproject commit 4ab29b755a147a69d88bf000e276a7a2eaa6403b