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