1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.apache.hadoop.hbase.regionserver.compactions;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.conf.Configuration;
24  
25  @InterfaceAudience.Private
26  public abstract class OffPeakHours {
27    private static final Log LOG = LogFactory.getLog(OffPeakHours.class);
28  
29    public static final OffPeakHours DISABLED = new OffPeakHours() {
30      @Override public boolean isOffPeakHour() { return false; }
31      @Override public boolean isOffPeakHour(int targetHour) { return false; }
32    };
33  
34    public static OffPeakHours getInstance(Configuration conf) {
35      int startHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, -1);
36      int endHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, -1);
37      return getInstance(startHour, endHour);
38    }
39  
40    
41  
42  
43  
44    public static OffPeakHours getInstance(int startHour, int endHour) {
45      if (startHour == -1 && endHour == -1) {
46        return DISABLED;
47      }
48  
49      if (! isValidHour(startHour) || ! isValidHour(endHour)) {
50        if (LOG.isWarnEnabled()) {
51          LOG.warn("Ignoring invalid start/end hour for peak hour : start = " +
52              startHour + " end = " + endHour +
53              ". Valid numbers are [0-23]");
54        }
55        return DISABLED;
56      }
57  
58      if (startHour == endHour) {
59        return DISABLED;
60      }
61  
62      return new OffPeakHoursImpl(startHour, endHour);
63    }
64  
65    private static boolean isValidHour(int hour) {
66      return 0 <= hour && hour <= 23;
67    }
68  
69    
70  
71  
72    public abstract boolean isOffPeakHour(int targetHour);
73  
74    
75  
76  
77    public abstract boolean isOffPeakHour();
78  
79    private static class OffPeakHoursImpl extends OffPeakHours {
80      final int startHour;
81      final int endHour;
82  
83      
84  
85  
86  
87      OffPeakHoursImpl(int startHour, int endHour) {
88        this.startHour = startHour;
89        this.endHour = endHour;
90      }
91  
92      @Override
93      public boolean isOffPeakHour() {
94        return isOffPeakHour(CurrentHourProvider.getCurrentHour());
95      }
96  
97      @Override
98      public boolean isOffPeakHour(int targetHour) {
99        if (startHour <= endHour) {
100         return startHour <= targetHour && targetHour < endHour;
101       }
102       return targetHour < endHour || startHour <= targetHour;
103     }
104   }
105 }