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.BackupManager;
32 import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.hbase.classification.InterfaceStability;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
38
39
40
41
42
43 @InterfaceStability.Evolving
44 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
45 public class BackupLogCleaner extends BaseLogCleanerDelegate {
46 private static final Log LOG = LogFactory.getLog(BackupLogCleaner.class);
47
48 private boolean stopped = false;
49
50 public BackupLogCleaner() {
51 }
52
53 @Override
54 public Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
55
56
57 if (this.getConf() == null|| !BackupManager.isBackupEnabled(getConf())) {
58 if (LOG.isDebugEnabled()) {
59 LOG.debug("Backup is not enabled. Check your "
60 + HConstants.BACKUP_ENABLE_KEY + " setting");
61 }
62 return files;
63 }
64
65 List<FileStatus> list = new ArrayList<FileStatus>();
66
67
68
69 try (final Connection conn = ConnectionFactory.createConnection(getConf());
70 final BackupSystemTable table = new BackupSystemTable(conn)) {
71
72 if (!table.hasBackupSessions()) {
73 LOG.debug("BackupLogCleaner has no backup sessions");
74 return files;
75 }
76
77 for(FileStatus file: files){
78 String wal = file.getPath().toString();
79 boolean logInSystemTable = table.isWALFileDeletable(wal);
80 if(LOG.isDebugEnabled()) {
81 if(logInSystemTable) {
82 LOG.debug("Found log file in hbase:backup, deleting: " + wal);
83 list.add(file);
84 } else {
85 LOG.debug("Didn't find this log in hbase:backup, keeping: " + wal);
86 }
87 }
88 }
89 return list;
90 } catch (IOException e) {
91 LOG.error("Failed to get hbase:backup table, therefore will keep all files", e);
92
93 return new ArrayList<FileStatus>();
94 }
95 }
96
97 @Override
98 public void setConf(Configuration config) {
99 super.setConf(config);
100
101 if (!config.getBoolean(HConstants.BACKUP_ENABLE_KEY, HConstants.BACKUP_ENABLE_DEFAULT)) {
102 LOG.warn("Backup is disabled - allowing all wals to be deleted");
103 return;
104 }
105 }
106
107 @Override
108 public void stop(String why) {
109 if (this.stopped) {
110 return;
111 }
112 this.stopped = true;
113 LOG.info("Stopping BackupLogCleaner");
114 }
115
116 @Override
117 public boolean isStopped() {
118 return this.stopped;
119 }
120
121 }