1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.apache.hadoop.hbase.util;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.util.concurrent.atomic.AtomicBoolean;
26
27 /**
28 * Sleeper for current thread.
29 * Sleeps for passed period. Also checks passed boolean and if interrupted,
30 * will return if the flag is set (rather than go back to sleep until its
31 * sleep time is up).
32 */
33 public class Sleeper {
34 private final Log LOG = LogFactory.getLog(this.getClass().getName());
35 private final int period;
36 private final AtomicBoolean stop;
37 private static final long MINIMAL_DELTA_FOR_LOGGING = 10000;
38
39 private final Object sleepLock = new Object();
40 private boolean triggerWake = false;
41
42 /**
43 * @param sleep sleep time in milliseconds
44 * @param stop flag for when we stop
45 */
46 public Sleeper(final int sleep, final AtomicBoolean stop) {
47 this.period = sleep;
48 this.stop = stop;
49 }
50
51 /**
52 * Sleep for period.
53 */
54 public void sleep() {
55 sleep(System.currentTimeMillis());
56 }
57
58 /**
59 * If currently asleep, stops sleeping; if not asleep, will skip the next
60 * sleep cycle.
61 */
62 public void skipSleepCycle() {
63 synchronized (sleepLock) {
64 triggerWake = true;
65 sleepLock.notify();
66 }
67 }
68
69 /**
70 * Sleep for period adjusted by passed <code>startTime<code>
71 * @param startTime Time some task started previous to now. Time to sleep
72 * will be docked current time minus passed <code>startTime<code>.
73 */
74 public void sleep(final long startTime) {
75 if (this.stop.get()) {
76 return;
77 }
78 long now = System.currentTimeMillis();
79 long waitTime = this.period - (now - startTime);
80 if (waitTime > this.period) {
81 LOG.warn("Calculated wait time > " + this.period +
82 "; setting to this.period: " + System.currentTimeMillis() + ", " +
83 startTime);
84 waitTime = this.period;
85 }
86 while (waitTime > 0) {
87 long woke = -1;
88 try {
89 synchronized (sleepLock) {
90 if (triggerWake) break;
91 sleepLock.wait(waitTime);
92 }
93 woke = System.currentTimeMillis();
94 long slept = woke - now;
95 if (slept - this.period > MINIMAL_DELTA_FOR_LOGGING) {
96 LOG.warn("We slept " + slept + "ms instead of " + this.period +
97 "ms, this is likely due to a long " +
98 "garbage collecting pause and it's usually bad, " +
99 "see http://wiki.apache.org/hadoop/Hbase/Troubleshooting#A9");
100 }
101 } catch(InterruptedException iex) {
102 // We we interrupted because we're meant to stop? If not, just
103 // continue ignoring the interruption
104 if (this.stop.get()) {
105 return;
106 }
107 }
108 // Recalculate waitTime.
109 woke = (woke == -1)? System.currentTimeMillis(): woke;
110 waitTime = this.period - (woke - startTime);
111 }
112 triggerWake = false;
113 }
114 }