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  import java.util.Collections;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileStatus;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
34  import org.apache.hadoop.hbase.master.HMaster;
35  import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
36  import org.apache.hadoop.hbase.snapshot.CorruptedSnapshotException;
37  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  
40  /**
41   * Implementation of a file cleaner that checks if a hfile is still used by snapshots of HBase
42   * tables.
43   */
44  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
45  @InterfaceStability.Evolving
46  public class SnapshotHFileCleaner extends BaseHFileCleanerDelegate {
47    private static final Log LOG = LogFactory.getLog(SnapshotHFileCleaner.class);
48  
49    /**
50     * Conf key for the frequency to attempt to refresh the cache of hfiles currently used in
51     * snapshots (ms)
52     */
53    public static final String HFILE_CACHE_REFRESH_PERIOD_CONF_KEY =
54        "hbase.master.hfilecleaner.plugins.snapshot.period";
55  
56    /** Refresh cache, by default, every 5 minutes */
57    private static final long DEFAULT_HFILE_CACHE_REFRESH_PERIOD = 300000;
58  
59    /** File cache for HFiles in the completed and currently running snapshots */
60    private SnapshotFileCache cache;
61  
62    private HMaster master;
63  
64    @Override
65    public synchronized Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
66      try {
67        return cache.getUnreferencedFiles(files, master.getSnapshotManager());
68      } catch (CorruptedSnapshotException cse) {
69        LOG.debug("Corrupted in-progress snapshot file exception, ignored ", cse);
70      } catch (IOException e) {
71        LOG.error("Exception while checking if files were valid, keeping them just in case.", e);
72      }
73      return Collections.emptyList();
74    }
75  
76    @Override
77    public void init(Map<String, Object> params) {
78      if (params.containsKey(HMaster.MASTER)) {
79        this.master = (HMaster) params.get(HMaster.MASTER);
80      }
81    }
82  
83    @Override
84    protected boolean isFileDeletable(FileStatus fStat) {
85      return false;
86    }
87  
88    public void setConf(final Configuration conf) {
89      super.setConf(conf);
90      try {
91        long cacheRefreshPeriod = conf.getLong(HFILE_CACHE_REFRESH_PERIOD_CONF_KEY,
92          DEFAULT_HFILE_CACHE_REFRESH_PERIOD);
93        final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
94        Path rootDir = FSUtils.getRootDir(conf);
95        cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
96            "snapshot-hfile-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
97              public Collection<String> filesUnderSnapshot(final Path snapshotDir)
98                  throws IOException {
99                return SnapshotReferenceUtil.getHFileNames(conf, fs, snapshotDir);
100             }
101           });
102     } catch (IOException e) {
103       LOG.error("Failed to create cleaner util", e);
104     }
105   }
106 
107 
108   @Override
109   public void stop(String why) {
110     this.cache.stop(why);
111   }
112 
113   @Override
114   public boolean isStopped() {
115     return this.cache.isStopped();
116   }
117 
118   /**
119    * Exposed for Testing!
120    * @return the cache of all hfiles
121    */
122   public SnapshotFileCache getFileCacheForTesting() {
123     return this.cache;
124   }
125 }