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.master.snapshot;
19  
20  import java.io.IOException;
21  import java.util.Collection;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
31  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
32  import org.apache.hadoop.hbase.util.FSUtils;
33  
34  /**
35   * Implementation of a file cleaner that checks if a hfile is still used by snapshots of HBase
36   * tables.
37   */
38  @InterfaceAudience.Private
39  @InterfaceStability.Evolving
40  public class SnapshotHFileCleaner extends BaseHFileCleanerDelegate {
41    private static final Log LOG = LogFactory.getLog(SnapshotHFileCleaner.class);
42  
43    /**
44     * Conf key for the frequency to attempt to refresh the cache of hfiles currently used in
45     * snapshots (ms)
46     */
47    public static final String HFILE_CACHE_REFRESH_PERIOD_CONF_KEY =
48        "hbase.master.hfilecleaner.plugins.snapshot.period";
49  
50    /** Refresh cache, by default, every 5 minutes */
51    private static final long DEFAULT_HFILE_CACHE_REFRESH_PERIOD = 300000;
52  
53    /** File cache for HFiles in the completed and currently running snapshots */
54    private SnapshotFileCache cache;
55  
56    @Override
57    public synchronized boolean isFileDeletable(Path filePath) {
58      try {
59        return !cache.contains(filePath.getName());
60      } catch (IOException e) {
61        LOG.error("Exception while checking if:" + filePath + " was valid, keeping it just in case.",
62          e);
63        return false;
64      }
65    }
66  
67    @Override
68    public void setConf(Configuration conf) {
69      super.setConf(conf);
70      try {
71        long cacheRefreshPeriod = conf.getLong(HFILE_CACHE_REFRESH_PERIOD_CONF_KEY,
72          DEFAULT_HFILE_CACHE_REFRESH_PERIOD);
73        final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
74        Path rootDir = FSUtils.getRootDir(conf);
75        cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
76            "snapshot-hfile-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
77              public Collection<String> filesUnderSnapshot(final Path snapshotDir)
78                  throws IOException {
79                return SnapshotReferenceUtil.getHFileNames(fs, snapshotDir);
80              }
81            });
82      } catch (IOException e) {
83        LOG.error("Failed to create cleaner util", e);
84      }
85    }
86  
87    @Override
88    public void stop(String why) {
89      this.cache.stop(why);
90    }
91  
92    @Override
93    public boolean isStopped() {
94      return this.cache.isStopped();
95    }
96  
97    /**
98     * Exposed for Testing!
99     * @return the cache of all hfiles
100    */
101   public SnapshotFileCache getFileCacheForTesting() {
102     return this.cache;
103   }
104 }