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 com.google.errorprone.annotations.RestrictedApi;
021import java.util.List;
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 file
033 * 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
059   *                             otherwise
060   */
061  public CompactedHFilesDischarger(final int period, final Stoppable stopper,
062    final RegionServerServices regionServerServices, boolean useExecutor) {
063    // Need to add the config classes
064    this(period, stopper, regionServerServices);
065    this.useExecutor = useExecutor;
066  }
067
068  /**
069   * CompactedHFilesDischarger runs asynchronously by default using the hosting RegionServer's
070   * Executor. In tests it can be useful to force a synchronous cleanup. Use this method to set
071   * no-executor before you call run.
072   * @return The old setting for <code>useExecutor</code>
073   */
074  @RestrictedApi(explanation = "Should only be called in tests", link = "",
075      allowedOnPath = ".*/src/test/.*")
076  public boolean setUseExecutor(final boolean useExecutor) {
077    boolean oldSetting = this.useExecutor;
078    this.useExecutor = useExecutor;
079    return oldSetting;
080  }
081
082  @Override
083  public void chore() {
084    // Noop if rss is null. This will never happen in a normal condition except for cases
085    // when the test case is not spinning up a cluster
086    if (regionServerServices == null) return;
087    List<HRegion> onlineRegions = (List<HRegion>) regionServerServices.getRegions();
088    if (onlineRegions == null) return;
089    for (HRegion region : onlineRegions) {
090      if (LOG.isTraceEnabled()) {
091        LOG.trace("Started compacted hfiles cleaner on " + region.getRegionInfo());
092      }
093      for (HStore store : region.getStores()) {
094        try {
095          if (useExecutor && regionServerServices != null) {
096            CompactedHFilesDischargeHandler handler = new CompactedHFilesDischargeHandler(
097              (Server) regionServerServices, EventType.RS_COMPACTED_FILES_DISCHARGER, store);
098            regionServerServices.getExecutorService().submit(handler);
099          } else {
100            // call synchronously if the RegionServerServices are not
101            // available
102            store.closeAndArchiveCompactedFiles();
103          }
104          if (LOG.isTraceEnabled()) {
105            LOG.trace("Completed archiving the compacted files for the region "
106              + region.getRegionInfo() + " under the store " + store.getColumnFamilyName());
107          }
108        } catch (Exception e) {
109          LOG.error("Exception while trying to close and archive the compacted store "
110            + "files of the store  " + store.getColumnFamilyName() + " in the" + " region "
111            + region.getRegionInfo(), e);
112        }
113      }
114      if (LOG.isTraceEnabled()) {
115        LOG
116          .trace("Completed the compacted hfiles cleaner for the region " + region.getRegionInfo());
117      }
118    }
119  }
120}