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