1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.backup.master;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileStatus;
29 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.classification.InterfaceStability;
34 import org.apache.hadoop.hbase.client.Connection;
35 import org.apache.hadoop.hbase.client.ConnectionFactory;
36 import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
37
38
39
40
41
42 @InterfaceStability.Evolving
43 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
44 public class BackupLogCleaner extends BaseLogCleanerDelegate {
45 private static final Log LOG = LogFactory.getLog(BackupLogCleaner.class);
46
47 private boolean stopped = false;
48
49 public BackupLogCleaner() {
50 }
51
52 @Override
53 public Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
54
55
56 if (this.getConf() == null) {
57 return files;
58 }
59
60 List<FileStatus> list = new ArrayList<FileStatus>();
61
62
63
64 try (final Connection conn = ConnectionFactory.createConnection(getConf());
65 final BackupSystemTable table = new BackupSystemTable(conn)) {
66
67 if (!table.hasBackupSessions()) {
68 LOG.debug("BackupLogCleaner has no backup sessions");
69 return files;
70 }
71
72 for(FileStatus file: files){
73 String wal = file.getPath().toString();
74 boolean logInSystemTable = table.isWALFileDeletable(wal);
75 if(LOG.isDebugEnabled()) {
76 if(logInSystemTable) {
77 LOG.debug("Found log file in hbase:backup, deleting: " + wal);
78 list.add(file);
79 } else {
80 LOG.debug("Didn't find this log in hbase:backup, keeping: " + wal);
81 }
82 }
83 }
84 return list;
85 } catch (IOException e) {
86 LOG.error("Failed to get hbase:backup table, therefore will keep all files", e);
87
88 return new ArrayList<FileStatus>();
89 }
90 }
91
92 @Override
93 public void setConf(Configuration config) {
94
95 if (!config.getBoolean(HConstants.BACKUP_ENABLE_KEY, HConstants.BACKUP_ENABLE_DEFAULT)) {
96 LOG.warn("Backup is disabled - allowing all wals to be deleted");
97 return;
98 }
99 super.setConf(config);
100 }
101
102 @Override
103 public void stop(String why) {
104 if (this.stopped) {
105 return;
106 }
107 this.stopped = true;
108 LOG.info("Stopping BackupLogCleaner");
109 }
110
111 @Override
112 public boolean isStopped() {
113 return this.stopped;
114 }
115
116 }