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 org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.fs.FileStatus;
25  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
26  
27  /**
28   * HFile cleaner that uses the timestamp of the hfile to determine if it should be deleted. By
29   * default they are allowed to live for {@value #DEFAULT_TTL}
30   */
31  @InterfaceAudience.Private
32  public class TimeToLiveHFileCleaner extends BaseHFileCleanerDelegate {
33  
34    public static final Log LOG = LogFactory.getLog(TimeToLiveHFileCleaner.class.getName());
35    public static final String TTL_CONF_KEY = "hbase.master.hfilecleaner.ttl";
36    // default ttl = 5 minutes
37    public static final long DEFAULT_TTL = 60000 * 5;
38    // Configured time a hfile can be kept after it was moved to the archive
39    private long ttl;
40  
41    @Override
42    public void setConf(Configuration conf) {
43      this.ttl = conf.getLong(TTL_CONF_KEY, DEFAULT_TTL);
44      super.setConf(conf);
45    }
46  
47    @Override
48    public boolean isFileDeletable(FileStatus fStat) {
49      long currentTime = EnvironmentEdgeManager.currentTimeMillis();
50      long time = fStat.getModificationTime();
51      long life = currentTime - time;
52      if (LOG.isTraceEnabled()) {
53        LOG.trace("HFile life:" + life + ", ttl:" + ttl + ", current:" + currentTime + ", from: "
54            + time);
55      }
56      if (life < 0) {
57        LOG.warn("Found a hfile (" + fStat.getPath() + ") newer than current time (" + currentTime
58            + " < " + time + "), probably a clock skew");
59        return false;
60      }
61      return life > ttl;
62    }
63  }