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;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileStatus;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.master.cleaner.BaseHFileCleanerDelegate;
027import org.apache.hadoop.hbase.util.CommonFSUtils;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.zookeeper.KeeperException;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * {@link BaseHFileCleanerDelegate} that only cleans HFiles that don't belong to a table that is
035 * currently being archived.
036 * <p>
037 * This only works properly if the
038 * {@link org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner} is also enabled (it always
039 * should be), since it may take a little time for the ZK notification to propagate, in which case
040 * we may accidentally delete some files.
041 */
042@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
043public class LongTermArchivingHFileCleaner extends BaseHFileCleanerDelegate {
044
045  private static final Logger LOG = LoggerFactory.getLogger(LongTermArchivingHFileCleaner.class);
046
047  TableHFileArchiveTracker archiveTracker;
048  private FileSystem fs;
049
050  @Override
051  public boolean isFileDeletable(FileStatus fStat) {
052    try {
053      // if its a directory, then it can be deleted
054      if (fStat.isDirectory()) {
055        return true;
056      }
057
058      Path file = fStat.getPath();
059      // check to see if
060      FileStatus[] deleteStatus = CommonFSUtils.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) {
064        return true;
065      }
066
067      // otherwise, we need to check the file's table and see its being archived
068      Path family = file.getParent();
069      Path region = family.getParent();
070      Path table = region.getParent();
071
072      String tableName = table.getName();
073      boolean ret = !archiveTracker.keepHFiles(tableName);
074      LOG
075        .debug("Archiver says to [" + (ret ? "delete" : "keep") + "] files for table:" + tableName);
076      return ret;
077    } catch (IOException e) {
078      LOG.error("Failed to lookup status of:" + fStat.getPath() + ", keeping it just incase.", e);
079      return false;
080    }
081  }
082
083  @Override
084  public void setConf(Configuration config) {
085    // setup our own zookeeper connection
086    // Make my own Configuration. Then I'll have my own connection to zk that
087    // I can close myself when comes time.
088    Configuration conf = new Configuration(config);
089    super.setConf(conf);
090    try {
091      this.fs = FileSystem.get(conf);
092      this.archiveTracker = TableHFileArchiveTracker.create(conf);
093      this.archiveTracker.start();
094    } catch (KeeperException e) {
095      LOG.error("Error while configuring " + this.getClass().getName(), e);
096    } catch (IOException e) {
097      LOG.error("Error while configuring " + this.getClass().getName(), e);
098    }
099  }
100
101  @Override
102  public void stop(String reason) {
103    if (this.isStopped()) {
104      return;
105    }
106
107    super.stop(reason);
108    if (this.archiveTracker != null) {
109      LOG.info("Stopping " + this.archiveTracker);
110      this.archiveTracker.stop();
111    }
112  }
113}