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.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.fs.FileStatus;
25  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
27  
28  /**
29   * Log cleaner that uses the timestamp of the hlog to determine if it should
30   * be deleted. By default they are allowed to live for 10 minutes.
31   */
32  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
33  public class TimeToLiveLogCleaner extends BaseLogCleanerDelegate {
34    static final Log LOG = LogFactory.getLog(TimeToLiveLogCleaner.class.getName());
35    // Configured time a log can be kept after it was closed
36    private long ttl;
37    private boolean stopped = false;
38  
39    @Override
40    public boolean isLogDeletable(FileStatus fStat) {
41      long currentTime = EnvironmentEdgeManager.currentTime();
42      long time = fStat.getModificationTime();
43      long life = currentTime - time;
44      
45      if (LOG.isTraceEnabled()) {
46        LOG.trace("Log life:" + life + ", ttl:" + ttl + ", current:" + currentTime + ", from: "
47            + time);
48      }
49      if (life < 0) {
50        LOG.warn("Found a log (" + fStat.getPath() + ") newer than current time (" + currentTime
51            + " < " + time + "), probably a clock skew");
52        return false;
53      }
54      return life > ttl;
55    }
56  
57    @Override
58    public void setConf(Configuration conf) {
59      super.setConf(conf);
60      this.ttl = conf.getLong("hbase.master.logcleaner.ttl", 600000);
61    }
62  
63  
64    @Override
65    public void stop(String why) {
66      this.stopped = true;
67    }
68  
69    @Override
70    public boolean isStopped() {
71      return this.stopped;
72    }
73  }