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.master.cleaner;
019
020import org.apache.yetus.audience.InterfaceAudience;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
027
028/**
029 * HFile cleaner that uses the timestamp of the hfile to determine if it should be deleted. By
030 * default they are allowed to live for {@value #DEFAULT_TTL}
031 */
032@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
033public class TimeToLiveHFileCleaner extends BaseHFileCleanerDelegate {
034
035  private static final Logger LOG = LoggerFactory.getLogger(TimeToLiveHFileCleaner.class.getName());
036  public static final String TTL_CONF_KEY = "hbase.master.hfilecleaner.ttl";
037  // default ttl = 5 minutes
038  public static final long DEFAULT_TTL = 60000 * 5;
039  // Configured time a hfile can be kept after it was moved to the archive
040  private long ttl;
041
042  @Override
043  public void setConf(Configuration conf) {
044    this.ttl = conf.getLong(TTL_CONF_KEY, DEFAULT_TTL);
045    super.setConf(conf);
046  }
047
048  @Override
049  public boolean isFileDeletable(FileStatus fStat) {
050    long currentTime = EnvironmentEdgeManager.currentTime();
051    long time = fStat.getModificationTime();
052    long life = currentTime - time;
053    if (LOG.isTraceEnabled()) {
054      LOG.trace("HFile life:" + life + ", ttl:" + ttl + ", current:" + currentTime + ", from: "
055          + time);
056    }
057    if (life < 0) {
058      LOG.warn("Found a hfile (" + fStat.getPath() + ") newer than current time (" + currentTime
059          + " < " + time + "), probably a clock skew");
060      return false;
061    }
062    return life > ttl;
063  }
064}