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    static final Log LOG = LogFactory.getLog(TimeToLiveLogCleaner.class.getName());
33    private Configuration conf;
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(Path filePath) {
40      long time = 0;
41      long currentTime = System.currentTimeMillis();
42      String[] parts = filePath.getName().split("\\.");
43      try {
44        time = Long.parseLong(parts[parts.length-1]);
45      } catch (NumberFormatException e) {
46        LOG.error("Unable to parse the timestamp in " + filePath.getName() +
47            ", deleting it since it's invalid and may not be a hlog", e);
48        return true;
49      }
50      long life = currentTime - time;
51      if (life < 0) {
52        LOG.warn("Found a log newer than current time, " +
53            "probably a clock skew");
54        return false;
55      }
56      return life > ttl;
57    }
58  
59    @Override
60    public void setConf(Configuration conf) {
61      this.conf = conf;
62      this.ttl = conf.getLong("hbase.master.logcleaner.ttl", 600000);
63    }
64  
65    @Override
66    public Configuration getConf() {
67      return conf;
68    }
69  
70    @Override
71    public void stop(String why) {
72      this.stopped = true;
73    }
74  
75    @Override
76    public boolean isStopped() {
77      return this.stopped;
78    }
79  }