001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.util.List;
021
022import org.apache.hadoop.hbase.ScheduledChore;
023import org.apache.hadoop.hbase.Server;
024import org.apache.hadoop.hbase.Stoppable;
025import org.apache.hadoop.hbase.executor.EventType;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * A chore service that periodically cleans up the compacted files when there are no active readers
032 * using those compacted files and also helps in clearing the block cache of these compacted
033 * file entries.
034 */
035@InterfaceAudience.Private
036public class CompactedHFilesDischarger extends ScheduledChore {
037  private static final Logger LOG = LoggerFactory.getLogger(CompactedHFilesDischarger.class);
038  private RegionServerServices regionServerServices;
039  // Default is to use executor
040  private boolean useExecutor = true;
041
042  /**
043   * @param period the period of time to sleep between each run
044   * @param stopper the stopper
045   * @param regionServerServices the region server that starts this chore
046   */
047  public CompactedHFilesDischarger(final int period, final Stoppable stopper,
048      final RegionServerServices regionServerServices) {
049    // Need to add the config classes
050    super("CompactedHFilesCleaner", stopper, period);
051    this.regionServerServices = regionServerServices;
052  }
053
054  /**
055   * @param period the period of time to sleep between each run
056   * @param stopper the stopper
057   * @param regionServerServices the region server that starts this chore
058   * @param useExecutor true if to use the region server's executor service, false otherwise
059   */
060  public CompactedHFilesDischarger(final int period, final Stoppable stopper,
061      final RegionServerServices regionServerServices, boolean useExecutor) {
062    // Need to add the config classes
063    this(period, stopper, regionServerServices);
064    this.useExecutor = useExecutor;
065  }
066
067  /**
068   * CompactedHFilesDischarger runs asynchronously by default using the hosting
069   * RegionServer's Executor. In tests it can be useful to force a synchronous
070   * cleanup. Use this method to set no-executor before you call run.
071   * @return The old setting for <code>useExecutor</code>
072   */
073  boolean setUseExecutor(final boolean useExecutor) {
074    boolean oldSetting = this.useExecutor;
075    this.useExecutor = useExecutor;
076    return oldSetting;
077  }
078
079  @Override
080  public void chore() {
081    // Noop if rss is null. This will never happen in a normal condition except for cases
082    // when the test case is not spinning up a cluster
083    if (regionServerServices == null) return;
084    List<HRegion> onlineRegions = (List<HRegion>) regionServerServices.getRegions();
085    if (onlineRegions == null) return;
086    for (HRegion region : onlineRegions) {
087      if (LOG.isTraceEnabled()) {
088        LOG.trace("Started compacted hfiles cleaner on " + region.getRegionInfo());
089      }
090      for (HStore store : region.getStores()) {
091        try {
092          if (useExecutor && regionServerServices != null) {
093            CompactedHFilesDischargeHandler handler = new CompactedHFilesDischargeHandler(
094                (Server) regionServerServices, EventType.RS_COMPACTED_FILES_DISCHARGER, store);
095            regionServerServices.getExecutorService().submit(handler);
096          } else {
097            // call synchronously if the RegionServerServices are not
098            // available
099            store.closeAndArchiveCompactedFiles();
100          }
101          if (LOG.isTraceEnabled()) {
102            LOG.trace("Completed archiving the compacted files for the region "
103                + region.getRegionInfo() + " under the store " + store.getColumnFamilyName());
104          }
105        } catch (Exception e) {
106          LOG.error("Exception while trying to close and archive the compacted store "
107              + "files of the store  " + store.getColumnFamilyName() + " in the" + " region "
108              + region.getRegionInfo(), e);
109        }
110      }
111      if (LOG.isTraceEnabled()) {
112        LOG.trace(
113            "Completed the compacted hfiles cleaner for the region " + region.getRegionInfo());
114      }
115    }
116  }
117}