View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.fs.FileStatus;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * Log cleaner that uses the timestamp of the hlog to determine if it should
32   * be deleted. By default they are allowed to live for 10 minutes.
33   */
34  public class TimeToLiveLogCleaner implements LogCleanerDelegate {
35    static final Log LOG = LogFactory.getLog(TimeToLiveLogCleaner.class.getName());
36    private Configuration conf;
37    // Configured time a log can be kept after it was closed
38    private long ttl;
39    private boolean stopped = false;
40  
41    @Override
42    public boolean isLogDeletable(Path filePath) {
43      long time = 0;
44      long currentTime = System.currentTimeMillis();
45      try {
46        FileStatus fStat = filePath.getFileSystem(conf).getFileStatus(filePath);
47        time = fStat.getModificationTime();
48      } catch (IOException e) {
49        LOG.error("Unable to get modification time of file " + filePath.getName() +
50        ", not deleting it.", e);
51        return false;
52      }
53      long life = currentTime - time;
54      if (life < 0) {
55        LOG.warn("Found a log newer than current time, " +
56            "probably a clock skew");
57        return false;
58      }
59      return life > ttl;
60    }
61  
62    @Override
63    public void setConf(Configuration conf) {
64      this.conf = conf;
65      this.ttl = conf.getLong("hbase.master.logcleaner.ttl", 600000);
66    }
67  
68    @Override
69    public Configuration getConf() {
70      return conf;
71    }
72  
73    @Override
74    public void stop(String why) {
75      this.stopped = true;
76    }
77  
78    @Override
79    public boolean isStopped() {
80      return this.stopped;
81    }
82  }