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