From 388cd5adb5c576050cc48ed7b18b9bbfff0c37e1 Mon Sep 17 00:00:00 2001 From: Reid Chan Date: Thu, 7 Dec 2017 19:05:06 +0800 Subject: [PATCH] HBASE-19450 Add log about average execution time for ScheduledChore --- .../org/apache/hadoop/hbase/ScheduledChore.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ScheduledChore.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ScheduledChore.java index 927b2b3736..3a280101c5 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ScheduledChore.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ScheduledChore.java @@ -72,6 +72,12 @@ public abstract class ScheduledChore implements Runnable { private long timeOfThisRun = -1; // system time millis private boolean initialChoreComplete = false; + /** + * Average execution time for this chore + */ + private double averageTime = 0.0; + private long count = 0; + /** * A means by which a ScheduledChore can be stopped. Once a chore recognizes that it has been * stopped, it will cancel itself. This is particularly useful in the case where a single stopper @@ -184,7 +190,7 @@ public abstract class ScheduledChore implements Runnable { if (!initialChoreComplete) { initialChoreComplete = initialChore(); } else { - chore(); + executeChoreAndMeasureTime(); } } catch (Throwable t) { if (LOG.isErrorEnabled()) LOG.error("Caught error", t); @@ -196,6 +202,20 @@ public abstract class ScheduledChore implements Runnable { } } + /** + * Execute chore, and track its average execution time. + */ + private void executeChoreAndMeasureTime() { + long startTime = System.currentTimeMillis(); + chore(); + long executionTime = System.currentTimeMillis() - startTime; + averageTime += (executionTime - averageTime) / (++count); + if (count % 5 == 0 || period >= (int) TimeUnit.MINUTES.toMillis(5)) { + // Show every 5 executions, or every execution if period of this chore is >= 5 minutes. + LOG.info(String.format("%s average execution time: %.2f ms.", getName(), averageTime)); + } + } + /** * Update our time tracking members. Called at the start of an execution of this chore's run() * method so that a correct decision can be made as to whether or not we missed the start time -- 2.15.0