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