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 org.apache.hadoop.fs.Path;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
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  public class TimeToLiveLogCleaner implements LogCleanerDelegate {
32  
33    static final Log LOG =
34        LogFactory.getLog(TimeToLiveLogCleaner.class.getName());
35    private Configuration conf;
36    // Configured time a log can be kept after it was closed
37    private long ttl;
38  
39    @Override
40    public boolean isLogDeletable(Path filePath) {
41      long time = 0;
42      long currentTime = System.currentTimeMillis();
43      String[] parts = filePath.getName().split("\\.");
44      try {
45        time = Long.parseLong(parts[parts.length-1]);
46      } catch (NumberFormatException e) {
47        LOG.error("Unable to parse the timestamp in " + filePath.getName() +
48            ", deleting it since it's invalid and may not be a hlog", e);
49        return true;
50      }
51      long life = currentTime - time;
52      if (life < 0) {
53        LOG.warn("Found a log newer than current time, " +
54            "probably a clock skew");
55        return false;
56      }
57      return life > ttl;
58    }
59  
60    @Override
61    public void setConf(Configuration conf) {
62      this.conf = conf;
63      this.ttl = conf.getLong("hbase.master.logcleaner.ttl", 600000);
64    }
65  
66    @Override
67    public Configuration getConf() {
68      return conf;
69    }
70  }