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;
19  
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.ScheduledChore;
25  import org.apache.hadoop.hbase.Server;
26  import org.apache.hadoop.hbase.Stoppable;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.executor.EventType;
29  
30  import com.google.common.annotations.VisibleForTesting;
31  
32  /**
33   * A chore service that periodically cleans up the compacted files when there are no active readers
34   * using those compacted files and also helps in clearing the block cache with these compacted
35   * file entries
36   */
37  @InterfaceAudience.Private
38  public class CompactedHFilesDischarger extends ScheduledChore {
39    private static final Log LOG = LogFactory.getLog(CompactedHFilesDischarger.class);
40    private RegionServerServices regionServerServices;
41    // Default is to use executor
42    @VisibleForTesting
43    private boolean useExecutor = true;
44  
45    /**
46     * @param period the period of time to sleep between each run
47     * @param stopper the stopper
48     * @param regionServerServices the region server that starts this chore
49     */
50    public CompactedHFilesDischarger(final int period, final Stoppable stopper,
51        final RegionServerServices regionServerServices) {
52      // Need to add the config classes
53      super("CompactedHFilesCleaner", stopper, period);
54      this.regionServerServices = regionServerServices;
55    }
56  
57    /**
58     * @param period the period of time to sleep between each run
59     * @param stopper the stopper
60     * @param regionServerServices the region server that starts this chore
61     * @param useExecutor true if to use the region server's executor service, false otherwise
62     */
63    @VisibleForTesting
64    public CompactedHFilesDischarger(final int period, final Stoppable stopper,
65        final RegionServerServices regionServerServices, boolean useExecutor) {
66      // Need to add the config classes
67      this(period, stopper, regionServerServices);
68      this.useExecutor = useExecutor;
69    }
70  
71    @Override
72    public void chore() {
73      if (regionServerServices == null) return;
74      List<Region> onlineRegions = regionServerServices.getOnlineRegions();
75      if (onlineRegions != null) {
76        for (Region region : onlineRegions) {
77          if (LOG.isTraceEnabled()) {
78            LOG.trace(
79                "Started the compacted hfiles cleaner for the region " + region.getRegionInfo());
80          }
81          for (Store store : region.getStores()) {
82            try {
83              if (useExecutor && regionServerServices != null) {
84                CompactedHFilesDischargeHandler handler = new CompactedHFilesDischargeHandler(
85                    (Server) regionServerServices, EventType.RS_COMPACTED_FILES_DISCHARGER,
86                    (HStore) store);
87                regionServerServices.getExecutorService().submit(handler);
88              } else {
89                // call synchronously if the RegionServerServices are not
90                // available
91                store.closeAndArchiveCompactedFiles();
92              }
93              if (LOG.isTraceEnabled()) {
94                LOG.trace("Completed archiving the compacted files for the region "
95                    + region.getRegionInfo() + " under the store " + store.getColumnFamilyName());
96              }
97            } catch (Exception e) {
98              LOG.error("Exception while trying to close and archive the compacted store "
99                  + "files of the store  " + store.getColumnFamilyName() + " in the" + " region "
100                 + region.getRegionInfo(), e);
101           }
102         }
103         if (LOG.isTraceEnabled()) {
104           LOG.trace(
105               "Completed the compacted hfiles cleaner for the region " + region.getRegionInfo());
106         }
107       }
108     }
109   }
110 }