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.hadoop.hbase.wal.AbstractFSWALProvider;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileStatus;
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
028
029/**
030 * Log cleaner that uses the timestamp of the wal to determine if it should
031 * be deleted. By default they are allowed to live for 10 minutes.
032 */
033@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
034public class TimeToLiveLogCleaner extends BaseLogCleanerDelegate {
035  private static final Logger LOG = LoggerFactory.getLogger(TimeToLiveLogCleaner.class.getName());
036  public static final String TTL_CONF_KEY = "hbase.master.logcleaner.ttl";
037  // default ttl = 10 minutes
038  public static final long DEFAULT_TTL = 600_000L;
039  // Configured time a log can be kept after it was closed
040  private long ttl;
041  private boolean stopped = false;
042
043  @Override
044  public boolean isFileDeletable(FileStatus fStat) {
045    // Files are validated for the second time here,
046    // if it causes a bottleneck this logic needs refactored
047    if (!AbstractFSWALProvider.validateWALFilename(fStat.getPath().getName())) {
048      return true;
049    }
050
051    long currentTime = EnvironmentEdgeManager.currentTime();
052    long time = fStat.getModificationTime();
053    long life = currentTime - time;
054
055    if (LOG.isTraceEnabled()) {
056      LOG.trace("Log life:" + life + ", ttl:" + ttl + ", current:" + currentTime + ", from: "
057          + time);
058    }
059    if (life < 0) {
060      LOG.warn("Found a log (" + fStat.getPath() + ") newer than current time (" + currentTime
061          + " < " + time + "), probably a clock skew");
062      return false;
063    }
064    return life > ttl;
065  }
066
067  @Override
068  public void setConf(Configuration conf) {
069    super.setConf(conf);
070    this.ttl = conf.getLong(TTL_CONF_KEY, DEFAULT_TTL);
071  }
072
073  @Override
074  public void stop(String why) {
075    this.stopped = true;
076  }
077
078  @Override
079  public boolean isStopped() {
080    return this.stopped;
081  }
082}