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.backup.example;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
31  import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
32  import org.apache.hadoop.hbase.util.FSUtils;
33  import org.apache.zookeeper.KeeperException;
34  
35  /**
36   * {@link BaseHFileCleanerDelegate} that only cleans HFiles that don't belong to a table that is
37   * currently being archived.
38   * <p>
39   * This only works properly if the {@link TimeToLiveHFileCleaner} is also enabled (it always should
40   * be), since it may take a little time for the ZK notification to propagate, in which case we may
41   * accidentally delete some files.
42   */
43  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
44  public class LongTermArchivingHFileCleaner extends BaseHFileCleanerDelegate {
45  
46    private static final Log LOG = LogFactory.getLog(LongTermArchivingHFileCleaner.class);
47  
48    TableHFileArchiveTracker archiveTracker;
49    private FileSystem fs;
50  
51    @Override
52    public boolean isFileDeletable(FileStatus fStat) {
53      try {
54        // if its a directory, then it can be deleted
55        if (fStat.isDirectory()) return true;
56        
57        Path file = fStat.getPath();
58        // check to see if
59        FileStatus[] deleteStatus = FSUtils.listStatus(this.fs, file, null);
60        // if the file doesn't exist, then it can be deleted (but should never
61        // happen since deleted files shouldn't get passed in)
62        if (deleteStatus == null) return true;
63  
64        // otherwise, we need to check the file's table and see its being archived
65        Path family = file.getParent();
66        Path region = family.getParent();
67        Path table = region.getParent();
68  
69        String tableName = table.getName();
70        boolean ret = !archiveTracker.keepHFiles(tableName);
71        LOG.debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
72        return ret;
73      } catch (IOException e) {
74        LOG.error("Failed to lookup status of:" + fStat.getPath() + ", keeping it just incase.", e);
75        return false;
76      }
77    }
78  
79    @Override
80    public void setConf(Configuration config) {
81      // setup our own zookeeper connection
82      // Make my own Configuration. Then I'll have my own connection to zk that
83      // I can close myself when comes time.
84      Configuration conf = new Configuration(config);
85      super.setConf(conf);
86      try {
87        this.fs = FileSystem.get(conf);
88        this.archiveTracker = TableHFileArchiveTracker.create(conf);
89        this.archiveTracker.start();
90      } catch (KeeperException e) {
91        LOG.error("Error while configuring " + this.getClass().getName(), e);
92      } catch (IOException e) {
93        LOG.error("Error while configuring " + this.getClass().getName(), e);
94      }
95    }
96  
97    @Override
98    public void stop(String reason) {
99      if (this.isStopped()) return;
100     super.stop(reason);
101     if (this.archiveTracker != null) {
102       LOG.info("Stopping " + this.archiveTracker);
103       this.archiveTracker.stop();
104     }
105 
106   }
107 
108 }