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