1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.compactions;
20
21 import java.util.Calendar;
22 import java.util.GregorianCalendar;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.conf.Configuration;
28
29
30
31
32
33
34 @InterfaceAudience.Private
35 public class OffPeakCompactions {
36 private static final Log LOG = LogFactory.getLog(OffPeakCompactions.class);
37 private final static Calendar calendar = new GregorianCalendar();
38 private int offPeakStartHour;
39 private int offPeakEndHour;
40
41
42
43
44
45
46 private static long numOutstanding = 0;
47
48
49
50
51 private static final Object compactionCountLock = new Object();
52
53 public OffPeakCompactions(Configuration conf) {
54 offPeakStartHour = conf.getInt("hbase.offpeak.start.hour", -1);
55 offPeakEndHour = conf.getInt("hbase.offpeak.end.hour", -1);
56 if (!isValidHour(offPeakStartHour) || !isValidHour(offPeakEndHour)) {
57 if (!(offPeakStartHour == -1 && offPeakEndHour == -1)) {
58 LOG.warn("Ignoring invalid start/end hour for peak hour : start = " +
59 this.offPeakStartHour + " end = " + this.offPeakEndHour +
60 ". Valid numbers are [0-23]");
61 }
62 this.offPeakStartHour = this.offPeakEndHour = -1;
63 }
64 }
65
66
67
68
69
70 public boolean tryStartOffPeakRequest() {
71 if (!isOffPeakHour()) return false;
72 synchronized(compactionCountLock) {
73 if (numOutstanding == 0) {
74 numOutstanding++;
75 return true;
76 }
77 }
78 return false;
79 }
80
81
82
83
84
85 public void endOffPeakRequest() {
86 long newValueToLog = -1;
87 synchronized(compactionCountLock) {
88 newValueToLog = --numOutstanding;
89 }
90 LOG.info("Compaction done, numOutstandingOffPeakCompactions is now " + newValueToLog);
91 }
92
93
94
95
96 private boolean isOffPeakHour() {
97 int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
98
99 if (this.offPeakStartHour == this.offPeakEndHour) {
100 return false;
101 }
102 if (this.offPeakStartHour < this.offPeakEndHour) {
103 return (currentHour >= this.offPeakStartHour && currentHour < this.offPeakEndHour);
104 }
105 return (currentHour >= this.offPeakStartHour || currentHour < this.offPeakEndHour);
106 }
107
108 private static boolean isValidHour(int hour) {
109 return (hour >= 0 && hour <= 23);
110 }
111 }