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.FileStatus;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
32  import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
33  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
34  import org.apache.hadoop.hbase.util.FSUtils;
35  
36  /**
37   * Implementation of a log cleaner that checks if a log is still used by
38   * snapshots of HBase tables.
39   */
40  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
41  @InterfaceStability.Evolving
42  public class SnapshotLogCleaner extends BaseLogCleanerDelegate {
43    private static final Log LOG = LogFactory.getLog(SnapshotLogCleaner.class);
44  
45    /**
46     * Conf key for the frequency to attempt to refresh the cache of hfiles currently used in
47     * snapshots (ms)
48     */
49    static final String HLOG_CACHE_REFRESH_PERIOD_CONF_KEY =
50        "hbase.master.hlogcleaner.plugins.snapshot.period";
51  
52    /** Refresh cache, by default, every 5 minutes */
53    private static final long DEFAULT_HLOG_CACHE_REFRESH_PERIOD = 300000;
54  
55    private SnapshotFileCache cache;
56  
57    @Override
58    public synchronized boolean isFileDeletable(FileStatus fStat) {
59      try {
60        if (null == cache) return false;
61        return !cache.contains(fStat.getPath().getName());
62      } catch (IOException e) {
63        LOG.error("Exception while checking if:" + fStat.getPath()
64            + " was valid, keeping it just in case.", e);
65        return false;
66      }
67    }
68  
69    /**
70     * This method should only be called <b>once</b>, as it starts a thread to keep the cache
71     * up-to-date.
72     * <p>
73     * {@inheritDoc}
74     */
75    @Override
76    public void setConf(Configuration conf) {
77      super.setConf(conf);
78      try {
79        long cacheRefreshPeriod = conf.getLong(
80          HLOG_CACHE_REFRESH_PERIOD_CONF_KEY, DEFAULT_HLOG_CACHE_REFRESH_PERIOD);
81        final FileSystem fs = FSUtils.getCurrentFileSystem(conf);
82        Path rootDir = FSUtils.getRootDir(conf);
83        cache = new SnapshotFileCache(fs, rootDir, cacheRefreshPeriod, cacheRefreshPeriod,
84            "snapshot-log-cleaner-cache-refresher", new SnapshotFileCache.SnapshotFileInspector() {
85              public Collection<String> filesUnderSnapshot(final Path snapshotDir)
86                  throws IOException {
87                return SnapshotReferenceUtil.getHLogNames(fs, snapshotDir);
88              }
89            });
90      } catch (IOException e) {
91        LOG.error("Failed to create snapshot log cleaner", e);
92      }
93    }
94  
95    @Override
96    public void stop(String why) {
97      this.cache.stop(why);
98    }
99  
100   @Override
101   public boolean isStopped() {
102     return this.cache.isStopped();
103   }
104 }