001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas;
018
019import java.util.HashSet;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map.Entry;
023import java.util.Set;
024import java.util.concurrent.TimeUnit;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.ScheduledChore;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.regionserver.MetricsRegionServer;
030import org.apache.hadoop.hbase.regionserver.Region;
031import org.apache.hadoop.hbase.regionserver.RegionServerServices;
032import org.apache.yetus.audience.InterfaceAudience;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * A Chore which sends the region size reports on this RegionServer to the Master.
039 */
040@InterfaceAudience.Private
041public class RegionSizeReportingChore extends ScheduledChore {
042  private static final Logger LOG = LoggerFactory.getLogger(RegionSizeReportingChore.class);
043
044  static final String REGION_SIZE_REPORTING_CHORE_PERIOD_KEY =
045      "hbase.regionserver.quotas.region.size.reporting.chore.period";
046  static final int REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT = 1000 * 60;
047
048  static final String REGION_SIZE_REPORTING_CHORE_DELAY_KEY =
049      "hbase.regionserver.quotas.region.size.reporting.chore.delay";
050  static final long REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT = 1000 * 30;
051
052  static final String REGION_SIZE_REPORTING_CHORE_TIMEUNIT_KEY =
053      "hbase.regionserver.quotas.region.size.reporting.chore.timeunit";
054  static final String REGION_SIZE_REPORTING_CHORE_TIMEUNIT_DEFAULT = TimeUnit.MILLISECONDS.name();
055
056  private final RegionServerServices rsServices;
057  private final MetricsRegionServer metrics;
058
059  public RegionSizeReportingChore(RegionServerServices rsServices) {
060    super(
061        RegionSizeReportingChore.class.getSimpleName(), rsServices,
062        getPeriod(rsServices.getConfiguration()), getInitialDelay(rsServices.getConfiguration()),
063        getTimeUnit(rsServices.getConfiguration()));
064    this.rsServices = rsServices;
065    this.metrics = rsServices.getMetrics();
066  }
067
068  @Override
069  protected void chore() {
070    final long start = System.nanoTime();
071    try {
072      _chore();
073    } finally {
074      if (metrics != null) {
075        metrics.incrementRegionSizeReportingChoreTime(
076            TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS));
077      }
078    }
079  }
080
081  void _chore() {
082    final RegionServerSpaceQuotaManager quotaManager =
083        rsServices.getRegionServerSpaceQuotaManager();
084    // Get the HRegionInfo for each online region
085    HashSet<RegionInfo> onlineRegionInfos = getOnlineRegionInfos(rsServices.getRegions());
086    RegionSizeStore store = quotaManager.getRegionSizeStore();
087    // Remove all sizes for non-online regions
088    removeNonOnlineRegions(store, onlineRegionInfos);
089    rsServices.reportRegionSizesForQuotas(store);
090  }
091
092  HashSet<RegionInfo> getOnlineRegionInfos(List<? extends Region> onlineRegions) {
093    HashSet<RegionInfo> regionInfos = new HashSet<>();
094    onlineRegions.forEach((region) -> regionInfos.add(region.getRegionInfo()));
095    return regionInfos;
096  }
097
098  void removeNonOnlineRegions(RegionSizeStore store, Set<RegionInfo> onlineRegions) {
099    // We have to remove regions which are no longer online from the store, otherwise they will
100    // continue to be sent to the Master which will prevent size report expiration.
101    if (onlineRegions.isEmpty()) {
102      // Easy-case, no online regions means no size reports
103      store.clear();
104      return;
105    }
106
107    Iterator<Entry<RegionInfo,RegionSize>> iter = store.iterator();
108    int numEntriesRemoved = 0;
109    while (iter.hasNext()) {
110      Entry<RegionInfo,RegionSize> entry = iter.next();
111      RegionInfo regionInfo = entry.getKey();
112      if (!onlineRegions.contains(regionInfo)) {
113        numEntriesRemoved++;
114        iter.remove();
115      }
116    }
117    if (LOG.isTraceEnabled()) {
118      LOG.trace("Removed " + numEntriesRemoved + " region sizes before reporting to Master "
119          + "because they are for non-online regions.");
120    }
121  }
122
123  /**
124   * Extracts the period for the chore from the configuration.
125   *
126   * @param conf The configuration object.
127   * @return The configured chore period or the default value.
128   */
129  static int getPeriod(Configuration conf) {
130    return conf.getInt(
131        REGION_SIZE_REPORTING_CHORE_PERIOD_KEY, REGION_SIZE_REPORTING_CHORE_PERIOD_DEFAULT);
132  }
133
134  /**
135   * Extracts the initial delay for the chore from the configuration.
136   *
137   * @param conf The configuration object.
138   * @return The configured chore initial delay or the default value.
139   */
140  static long getInitialDelay(Configuration conf) {
141    return conf.getLong(
142        REGION_SIZE_REPORTING_CHORE_DELAY_KEY, REGION_SIZE_REPORTING_CHORE_DELAY_DEFAULT);
143  }
144
145  /**
146   * Extracts the time unit for the chore period and initial delay from the configuration. The
147   * configuration value for {@link #REGION_SIZE_REPORTING_CHORE_TIMEUNIT_KEY} must correspond to a
148   * {@link TimeUnit} value.
149   *
150   * @param conf The configuration object.
151   * @return The configured time unit for the chore period and initial delay or the default value.
152   */
153  static TimeUnit getTimeUnit(Configuration conf) {
154    return TimeUnit.valueOf(conf.get(REGION_SIZE_REPORTING_CHORE_TIMEUNIT_KEY,
155        REGION_SIZE_REPORTING_CHORE_TIMEUNIT_DEFAULT));
156  }
157}