001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup.example;
019
020import java.io.IOException;
021
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseInterfaceAudience;
028import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
029import org.apache.hadoop.hbase.util.FSUtils;
030import org.apache.zookeeper.KeeperException;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * {@link BaseHFileCleanerDelegate} that only cleans HFiles that don't belong to a table that is
036 * currently being archived.
037 * <p>
038 * This only works properly if the 
039 * {@link org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner}
040 *  is also enabled (it always should be), since it may take a little time
041 *  for the ZK notification to propagate, in which case we may accidentally
042 *  delete some files.
043 */
044@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
045public class LongTermArchivingHFileCleaner extends BaseHFileCleanerDelegate {
046
047  private static final Logger LOG = LoggerFactory.getLogger(LongTermArchivingHFileCleaner.class);
048
049  TableHFileArchiveTracker archiveTracker;
050  private FileSystem fs;
051
052  @Override
053  public boolean isFileDeletable(FileStatus fStat) {
054    try {
055      // if its a directory, then it can be deleted
056      if (fStat.isDirectory()) return true;
057      
058      Path file = fStat.getPath();
059      // check to see if
060      FileStatus[] deleteStatus = FSUtils.listStatus(this.fs, file, null);
061      // if the file doesn't exist, then it can be deleted (but should never
062      // happen since deleted files shouldn't get passed in)
063      if (deleteStatus == null) return true;
064
065      // otherwise, we need to check the file's table and see its being archived
066      Path family = file.getParent();
067      Path region = family.getParent();
068      Path table = region.getParent();
069
070      String tableName = table.getName();
071      boolean ret = !archiveTracker.keepHFiles(tableName);
072      LOG.debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
073      return ret;
074    } catch (IOException e) {
075      LOG.error("Failed to lookup status of:" + fStat.getPath() + ", keeping it just incase.", e);
076      return false;
077    }
078  }
079
080  @Override
081  public void setConf(Configuration config) {
082    // setup our own zookeeper connection
083    // Make my own Configuration. Then I'll have my own connection to zk that
084    // I can close myself when comes time.
085    Configuration conf = new Configuration(config);
086    super.setConf(conf);
087    try {
088      this.fs = FileSystem.get(conf);
089      this.archiveTracker = TableHFileArchiveTracker.create(conf);
090      this.archiveTracker.start();
091    } catch (KeeperException e) {
092      LOG.error("Error while configuring " + this.getClass().getName(), e);
093    } catch (IOException e) {
094      LOG.error("Error while configuring " + this.getClass().getName(), e);
095    }
096  }
097
098  @Override
099  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}