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;
021import org.apache.hadoop.hbase.ScheduledChore;
022import org.apache.hadoop.hbase.Server;
023import org.apache.hadoop.hbase.Stoppable;
024import org.apache.hadoop.hbase.executor.EventType;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * A chore service that periodically cleans up the compacted files when there are no active readers
031 * using those compacted files and also helps in clearing the block cache of these compacted file
032 * entries.
033 */
034@InterfaceAudience.Private
035public class CompactedHFilesDischarger extends ScheduledChore {
036  private static final Logger LOG = LoggerFactory.getLogger(CompactedHFilesDischarger.class);
037  private RegionServerServices regionServerServices;
038  // Default is to use executor
039  private boolean useExecutor = true;
040
041  /**
042   * @param period               the period of time to sleep between each run
043   * @param stopper              the stopper
044   * @param regionServerServices the region server that starts this chore
045   */
046  public CompactedHFilesDischarger(final int period, final Stoppable stopper,
047    final RegionServerServices regionServerServices) {
048    // Need to add the config classes
049    super("CompactedHFilesCleaner", stopper, period);
050    this.regionServerServices = regionServerServices;
051  }
052
053  /**
054   * @param period               the period of time to sleep between each run
055   * @param stopper              the stopper
056   * @param regionServerServices the region server that starts this chore
057   * @param useExecutor          true if to use the region server's executor service, false
058   *                             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 RegionServer's
069   * Executor. In tests it can be useful to force a synchronous cleanup. Use this method to set
070   * 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
113          .trace("Completed the compacted hfiles cleaner for the region " + region.getRegionInfo());
114      }
115    }
116  }
117}