Use generic types for Context classes in metric timers

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Ia28988a916ae67f7508bfebdff4b495e3762055d
This commit is contained in:
Edwin Kempin
2019-06-28 16:11:16 +02:00
parent aadd006c8c
commit d0019c4e06
12 changed files with 51 additions and 49 deletions

View File

@@ -37,13 +37,12 @@ import java.util.concurrent.TimeUnit;
public abstract class Timer1<F1> implements RegistrationHandle { public abstract class Timer1<F1> implements RegistrationHandle {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Context extends TimerContext { public static class Context<F1> extends TimerContext {
private final Timer1<Object> timer; private final Timer1<F1> timer;
private final Object fieldValue; private final F1 fieldValue;
@SuppressWarnings("unchecked") Context(Timer1<F1> timer, F1 fieldValue) {
<F1> Context(Timer1<F1> timer, F1 fieldValue) { this.timer = timer;
this.timer = (Timer1<Object>) timer;
this.fieldValue = fieldValue; this.fieldValue = fieldValue;
} }
@@ -67,8 +66,8 @@ public abstract class Timer1<F1> implements RegistrationHandle {
* @param fieldValue bucket to record the timer * @param fieldValue bucket to record the timer
* @return timer context * @return timer context
*/ */
public Context start(F1 fieldValue) { public Context<F1> start(F1 fieldValue) {
return new Context(this, fieldValue); return new Context<>(this, fieldValue);
} }
/** /**

View File

@@ -38,14 +38,13 @@ import java.util.concurrent.TimeUnit;
public abstract class Timer2<F1, F2> implements RegistrationHandle { public abstract class Timer2<F1, F2> implements RegistrationHandle {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Context extends TimerContext { public static class Context<F1, F2> extends TimerContext {
private final Timer2<Object, Object> timer; private final Timer2<F1, F2> timer;
private final Object fieldValue1; private final F1 fieldValue1;
private final Object fieldValue2; private final F2 fieldValue2;
@SuppressWarnings("unchecked") Context(Timer2<F1, F2> timer, F1 fieldValue1, F2 fieldValue2) {
<F1, F2> Context(Timer2<F1, F2> timer, F1 fieldValue1, F2 fieldValue2) { this.timer = timer;
this.timer = (Timer2<Object, Object>) timer;
this.fieldValue1 = fieldValue1; this.fieldValue1 = fieldValue1;
this.fieldValue2 = fieldValue2; this.fieldValue2 = fieldValue2;
} }
@@ -73,8 +72,8 @@ public abstract class Timer2<F1, F2> implements RegistrationHandle {
* @param fieldValue2 bucket to record the timer * @param fieldValue2 bucket to record the timer
* @return timer context * @return timer context
*/ */
public Context start(F1 fieldValue1, F2 fieldValue2) { public Context<F1, F2> start(F1 fieldValue1, F2 fieldValue2) {
return new Context(this, fieldValue1, fieldValue2); return new Context<>(this, fieldValue1, fieldValue2);
} }
/** /**

View File

@@ -39,15 +39,14 @@ import java.util.concurrent.TimeUnit;
public abstract class Timer3<F1, F2, F3> implements RegistrationHandle { public abstract class Timer3<F1, F2, F3> implements RegistrationHandle {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static class Context extends TimerContext { public static class Context<F1, F2, F3> extends TimerContext {
private final Timer3<Object, Object, Object> timer; private final Timer3<F1, F2, F3> timer;
private final Object fieldValue1; private final F1 fieldValue1;
private final Object fieldValue2; private final F2 fieldValue2;
private final Object fieldValue3; private final F3 fieldValue3;
@SuppressWarnings("unchecked") Context(Timer3<F1, F2, F3> timer, F1 f1, F2 f2, F3 f3) {
<F1, F2, F3> Context(Timer3<F1, F2, F3> timer, F1 f1, F2 f2, F3 f3) { this.timer = timer;
this.timer = (Timer3<Object, Object, Object>) timer;
this.fieldValue1 = f1; this.fieldValue1 = f1;
this.fieldValue2 = f2; this.fieldValue2 = f2;
this.fieldValue3 = f3; this.fieldValue3 = f3;
@@ -79,8 +78,8 @@ public abstract class Timer3<F1, F2, F3> implements RegistrationHandle {
* @param fieldValue3 bucket to record the timer * @param fieldValue3 bucket to record the timer
* @return timer context * @return timer context
*/ */
public Context start(F1 fieldValue1, F2 fieldValue2, F3 fieldValue3) { public Context<F1, F2, F3> start(F1 fieldValue1, F2 fieldValue2, F3 fieldValue3) {
return new Context(this, fieldValue1, fieldValue2, fieldValue3); return new Context<>(this, fieldValue1, fieldValue2, fieldValue3);
} }
/** /**

View File

@@ -143,7 +143,7 @@ public class UiActions {
String name = e.getExportName().substring(d + 1); String name = e.getExportName().substring(d + 1);
UiAction.Description dsc; UiAction.Description dsc;
try (Timer1.Context ignored = uiActionLatency.start(name)) { try (Timer1.Context<String> ignored = uiActionLatency.start(name)) {
dsc = ((UiAction<R>) view).getDescription(resource); dsc = ((UiAction<R>) view).getDescription(resource);
} }

View File

@@ -139,7 +139,7 @@ public abstract class AbstractChangeNotes<T> {
if (args.failOnLoadForTest.get()) { if (args.failOnLoadForTest.get()) {
throw new StorageException("Reading from NoteDb is disabled"); throw new StorageException("Reading from NoteDb is disabled");
} }
try (Timer1.Context timer = args.metrics.readLatency.start(CHANGES); try (Timer1.Context<NoteDbTable> timer = args.metrics.readLatency.start(CHANGES);
Repository repo = args.repoManager.openRepository(getProjectName()); Repository repo = args.repoManager.openRepository(getProjectName());
// Call openHandle even if reading is disabled, to trigger // Call openHandle even if reading is disabled, to trigger
// auto-rebuilding before this object may get passed to a ChangeUpdate. // auto-rebuilding before this object may get passed to a ChangeUpdate.

View File

@@ -187,7 +187,7 @@ class ChangeNotesParser {
walk.reset(); walk.reset();
walk.markStart(walk.parseCommit(tip)); walk.markStart(walk.parseCommit(tip));
try (Timer1.Context timer = metrics.parseLatency.start(CHANGES)) { try (Timer1.Context<NoteDbTable> timer = metrics.parseLatency.start(CHANGES)) {
ChangeNotesCommit commit; ChangeNotesCommit commit;
while ((commit = walk.next()) != null) { while ((commit = walk.next()) != null) {
parse(commit); parse(commit);

View File

@@ -267,7 +267,7 @@ public class NoteDbUpdateManager implements AutoCloseable {
* @throws IOException if a storage layer error occurs. * @throws IOException if a storage layer error occurs.
*/ */
private void stage() throws IOException { private void stage() throws IOException {
try (Timer1.Context timer = metrics.stageUpdateLatency.start(CHANGES)) { try (Timer1.Context<NoteDbTable> timer = metrics.stageUpdateLatency.start(CHANGES)) {
if (isEmpty()) { if (isEmpty()) {
return; return;
} }
@@ -302,7 +302,7 @@ public class NoteDbUpdateManager implements AutoCloseable {
executed = true; executed = true;
return null; return null;
} }
try (Timer1.Context timer = metrics.updateLatency.start(CHANGES)) { try (Timer1.Context<NoteDbTable> timer = metrics.updateLatency.start(CHANGES)) {
stage(); stage();
// ChangeUpdates must execute before ChangeDraftUpdates. // ChangeUpdates must execute before ChangeDraftUpdates.
// //

View File

@@ -100,25 +100,29 @@ public class Sequences {
} }
public int nextAccountId() { public int nextAccountId() {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.ACCOUNTS, false)) { try (Timer2.Context<SequenceType, Boolean> timer =
nextIdLatency.start(SequenceType.ACCOUNTS, false)) {
return accountSeq.next(); return accountSeq.next();
} }
} }
public int nextChangeId() { public int nextChangeId() {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.CHANGES, false)) { try (Timer2.Context<SequenceType, Boolean> timer =
nextIdLatency.start(SequenceType.CHANGES, false)) {
return changeSeq.next(); return changeSeq.next();
} }
} }
public ImmutableList<Integer> nextChangeIds(int count) { public ImmutableList<Integer> nextChangeIds(int count) {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.CHANGES, count > 1)) { try (Timer2.Context<SequenceType, Boolean> timer =
nextIdLatency.start(SequenceType.CHANGES, count > 1)) {
return changeSeq.next(count); return changeSeq.next(count);
} }
} }
public int nextGroupId() { public int nextGroupId() {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.GROUPS, false)) { try (Timer2.Context<SequenceType, Boolean> timer =
nextIdLatency.start(SequenceType.GROUPS, false)) {
return groupSeq.next(); return groupSeq.next();
} }
} }

View File

@@ -140,7 +140,7 @@ public class PluginContext<T> {
exportNameField); exportNameField);
} }
Timer3.Context startLatency(Extension<?> extension) { Timer3.Context<String, String, String> startLatency(Extension<?> extension) {
return latency.start( return latency.start(
extension.getPluginName(), extension.getPluginName(),
extension.get().getClass().getName(), extension.get().getClass().getName(),
@@ -198,7 +198,7 @@ public class PluginContext<T> {
return; return;
} }
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
extensionImplConsumer.run(extensionImpl); extensionImplConsumer.run(extensionImpl);
} catch (Throwable e) { } catch (Throwable e) {
pluginMetrics.incrementErrorCount(extension); pluginMetrics.incrementErrorCount(extension);
@@ -227,7 +227,7 @@ public class PluginContext<T> {
} }
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
extensionConsumer.run(extension); extensionConsumer.run(extension);
} catch (Throwable e) { } catch (Throwable e) {
pluginMetrics.incrementErrorCount(extension); pluginMetrics.incrementErrorCount(extension);
@@ -261,7 +261,7 @@ public class PluginContext<T> {
} }
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
extensionImplConsumer.run(extensionImpl); extensionImplConsumer.run(extensionImpl);
} catch (Throwable e) { } catch (Throwable e) {
Throwables.throwIfInstanceOf(e, exceptionClass); Throwables.throwIfInstanceOf(e, exceptionClass);
@@ -298,7 +298,7 @@ public class PluginContext<T> {
} }
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
extensionConsumer.run(extension); extensionConsumer.run(extension);
} catch (Throwable e) { } catch (Throwable e) {
Throwables.throwIfInstanceOf(e, exceptionClass); Throwables.throwIfInstanceOf(e, exceptionClass);
@@ -324,7 +324,7 @@ public class PluginContext<T> {
Extension<T> extension, Extension<T> extension,
ExtensionImplFunction<T, R> extensionImplFunction) { ExtensionImplFunction<T, R> extensionImplFunction) {
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
return extensionImplFunction.call(extension.get()); return extensionImplFunction.call(extension.get());
} }
} }
@@ -349,7 +349,7 @@ public class PluginContext<T> {
Class<X> exceptionClass) Class<X> exceptionClass)
throws X { throws X {
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
try { try {
return checkedExtensionImplFunction.call(extension.get()); return checkedExtensionImplFunction.call(extension.get());
} catch (Exception e) { } catch (Exception e) {
@@ -378,7 +378,7 @@ public class PluginContext<T> {
Extension<T> extension, Extension<T> extension,
ExtensionFunction<Extension<T>, R> extensionFunction) { ExtensionFunction<Extension<T>, R> extensionFunction) {
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
return extensionFunction.call(extension); return extensionFunction.call(extension);
} }
} }
@@ -404,7 +404,7 @@ public class PluginContext<T> {
Class<X> exceptionClass) Class<X> exceptionClass)
throws X { throws X {
try (TraceContext traceContext = newTrace(extension); try (TraceContext traceContext = newTrace(extension);
Timer3.Context ctx = pluginMetrics.startLatency(extension)) { Timer3.Context<String, String, String> ctx = pluginMetrics.startLatency(extension)) {
try { try {
return checkedExtensionFunction.call(extension); return checkedExtensionFunction.call(extension);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -354,14 +354,15 @@ public class ProjectState {
* cached. Callers should try to cache this result per-request as much as possible. * cached. Callers should try to cache this result per-request as much as possible.
*/ */
public List<SectionMatcher> getAllSections() { public List<SectionMatcher> getAllSections() {
try (Timer1.Context ignored = computationLatency.start("getAllSections")) { try (Timer1.Context<String> ignored = computationLatency.start("getAllSections")) {
if (isAllProjects) { if (isAllProjects) {
return getLocalAccessSections(); return getLocalAccessSections();
} }
List<SectionMatcher> all = new ArrayList<>(); List<SectionMatcher> all = new ArrayList<>();
Iterable<ProjectState> tree = tree(); Iterable<ProjectState> tree = tree();
try (Timer1.Context ignored2 = computationLatency.start("getAllSections-parsing-only")) { try (Timer1.Context<String> ignored2 =
computationLatency.start("getAllSections-parsing-only")) {
for (ProjectState s : tree) { for (ProjectState s : tree) {
all.addAll(s.getLocalAccessSections()); all.addAll(s.getLocalAccessSections());
} }