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.cleaner;
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.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.io.HFileLink;
31  import org.apache.hadoop.hbase.util.FSUtils;
32  
33  /**
34   * HFileLink cleaner that determines if a hfile should be deleted.
35   * HFiles can be deleted only if there're no links to them.
36   *
37   * When a HFileLink is created a back reference file is created in:
38   *      /hbase/archive/table/region/cf/.links-hfile/ref-region.ref-table
39   * To check if the hfile can be deleted the back references folder must be empty.
40   */
41  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
42  public class HFileLinkCleaner extends BaseHFileCleanerDelegate {
43    private static final Log LOG = LogFactory.getLog(HFileLinkCleaner.class);
44  
45    private FileSystem fs = null;
46  
47    @Override
48    public synchronized boolean isFileDeletable(FileStatus fStat) {
49      if (this.fs == null) return false;
50      Path filePath = fStat.getPath();
51      // HFile Link is always deletable
52      if (HFileLink.isHFileLink(filePath)) return true;
53  
54      // If the file is inside a link references directory, means that it is a back ref link.
55      // The back ref can be deleted only if the referenced file doesn't exists.
56      Path parentDir = filePath.getParent();
57      if (HFileLink.isBackReferencesDir(parentDir)) {
58        try {
59          Path hfilePath = HFileLink.getHFileFromBackReference(getConf(), filePath);
60          return !fs.exists(hfilePath);
61        } catch (IOException e) {
62          LOG.error("Couldn't verify if the referenced file still exists, keep it just in case");
63          return false;
64        }
65      }
66  
67      // HFile is deletable only if has no links
68      try {
69        Path backRefDir = HFileLink.getBackReferencesDir(parentDir, filePath.getName());
70        return FSUtils.listStatus(fs, backRefDir) == null;
71      } catch (IOException e) {
72        LOG.error("Couldn't get the references, not deleting file, just in case");
73        return false;
74      }
75    }
76  
77    @Override
78    public void setConf(Configuration conf) {
79      super.setConf(conf);
80  
81      // setup filesystem
82      try {
83        this.fs = FileSystem.get(this.getConf());
84      } catch (IOException e) {
85        LOG.error("Couldn't instantiate the file system, not deleting file, just in case");
86      }
87    }
88  }