1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.classification.InterfaceStability;
25 import org.apache.hadoop.hbase.Stoppable;
26
27
28
29
30
31
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Stable
35 public class Sleeper {
36 private final Log LOG = LogFactory.getLog(this.getClass().getName());
37 private final int period;
38 private final Stoppable stopper;
39 private static final long MINIMAL_DELTA_FOR_LOGGING = 10000;
40
41 private final Object sleepLock = new Object();
42 private boolean triggerWake = false;
43
44
45
46
47
48
49 public Sleeper(final int sleep, final Stoppable stopper) {
50 this.period = sleep;
51 this.stopper = stopper;
52 }
53
54
55
56
57 public void sleep() {
58 sleep(System.currentTimeMillis());
59 }
60
61
62
63
64
65 public void skipSleepCycle() {
66 synchronized (sleepLock) {
67 triggerWake = true;
68 sleepLock.notifyAll();
69 }
70 }
71
72
73
74
75
76
77 public void sleep(final long startTime) {
78 if (this.stopper.isStopped()) {
79 return;
80 }
81 long now = System.currentTimeMillis();
82 long waitTime = this.period - (now - startTime);
83 if (waitTime > this.period) {
84 LOG.warn("Calculated wait time > " + this.period +
85 "; setting to this.period: " + System.currentTimeMillis() + ", " +
86 startTime);
87 waitTime = this.period;
88 }
89 while (waitTime > 0) {
90 long woke = -1;
91 try {
92 synchronized (sleepLock) {
93 if (triggerWake) break;
94 sleepLock.wait(waitTime);
95 }
96 woke = System.currentTimeMillis();
97 long slept = woke - now;
98 if (slept - this.period > MINIMAL_DELTA_FOR_LOGGING) {
99 LOG.warn("We slept " + slept + "ms instead of " + this.period +
100 "ms, this is likely due to a long " +
101 "garbage collecting pause and it's usually bad, see " +
102 "http://hbase.apache.org/book.html#trouble.rs.runtime.zkexpired");
103 }
104 } catch(InterruptedException iex) {
105
106
107 if (this.stopper.isStopped()) {
108 return;
109 }
110 }
111
112 woke = (woke == -1)? System.currentTimeMillis(): woke;
113 waitTime = this.period - (woke - startTime);
114 }
115 triggerWake = false;
116 }
117 }