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