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