1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileStatus;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.Chore;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.RemoteExceptionHandler;
31 import org.apache.hadoop.hbase.regionserver.wal.HLog;
32
33 import java.io.IOException;
34 import java.util.concurrent.atomic.AtomicBoolean;
35 import java.util.regex.Pattern;
36
37
38
39
40
41
42 public class OldLogsCleaner extends Chore {
43
44 static final Log LOG = LogFactory.getLog(OldLogsCleaner.class.getName());
45
46
47
48 private final int maxDeletedLogs;
49 private final FileSystem fs;
50 private final Path oldLogDir;
51 private final LogCleanerDelegate logCleaner;
52 private final Configuration conf;
53
54
55
56
57
58
59
60
61
62 public OldLogsCleaner(final int p, final AtomicBoolean s,
63 Configuration conf, FileSystem fs,
64 Path oldLogDir) {
65 super("OldLogsCleaner", p, s);
66
67
68 if (conf.getBoolean(HConstants.REPLICATION_ENABLE_KEY, false) &&
69 conf.get("hbase.master.logcleanerplugin.impl") == null) {
70 conf.set("hbase.master.logcleanerplugin.impl",
71 "org.apache.hadoop.hbase.replication.master.ReplicationLogCleaner");
72 }
73 this.maxDeletedLogs =
74 conf.getInt("hbase.master.logcleaner.maxdeletedlogs", 20);
75 this.fs = fs;
76 this.oldLogDir = oldLogDir;
77 this.conf = conf;
78 this.logCleaner = getLogCleaner();
79 }
80
81 private LogCleanerDelegate getLogCleaner() {
82 try {
83 Class c = Class.forName(conf.get("hbase.master.logcleanerplugin.impl",
84 TimeToLiveLogCleaner.class.getCanonicalName()));
85 LogCleanerDelegate cleaner = (LogCleanerDelegate) c.newInstance();
86 cleaner.setConf(conf);
87 return cleaner;
88 } catch (Exception e) {
89 LOG.warn("Passed log cleaner implementation throws errors, " +
90 "defaulting to TimeToLiveLogCleaner", e);
91 return new TimeToLiveLogCleaner();
92 }
93 }
94
95 @Override
96 protected void chore() {
97 try {
98 FileStatus[] files = this.fs.listStatus(this.oldLogDir);
99 int nbDeletedLog = 0;
100 for (FileStatus file : files) {
101 Path filePath = file.getPath();
102 if (HLog.validateHLogFilename(filePath.getName())) {
103 if (logCleaner.isLogDeletable(filePath) ) {
104 this.fs.delete(filePath, true);
105 nbDeletedLog++;
106 }
107 } else {
108 LOG.warn("Found a wrongly formated file: "
109 + file.getPath().getName());
110 this.fs.delete(filePath, true);
111 nbDeletedLog++;
112 }
113 if (nbDeletedLog >= maxDeletedLogs) {
114 break;
115 }
116 }
117 } catch (IOException e) {
118 e = RemoteExceptionHandler.checkIOException(e);
119 LOG.warn("Error while cleaning the logs", e);
120 }
121 }
122 }