View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * @param startHour inclusive
42     * @param endHour exclusive
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     * @return whether {@code targetHour} is off-peak hour
71     */
72    public abstract boolean isOffPeakHour(int targetHour);
73  
74    /**
75     * @return whether it is off-peak hour
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       * @param startHour inclusive
85       * @param endHour exclusive
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 }