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.replication.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.Path;
26 import org.apache.hadoop.hbase.master.HMaster;
27 import org.apache.hadoop.hbase.master.LogCleanerDelegate;
28 import org.apache.hadoop.hbase.replication.ReplicationZookeeperWrapper;
29 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
30 import org.apache.zookeeper.WatchedEvent;
31 import org.apache.zookeeper.Watcher;
32
33 import java.io.IOException;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.concurrent.atomic.AtomicBoolean;
38
39
40
41
42
43 public class ReplicationLogCleaner implements LogCleanerDelegate, Watcher {
44
45 private static final Log LOG =
46 LogFactory.getLog(ReplicationLogCleaner.class);
47 private Configuration conf;
48 private ReplicationZookeeperWrapper zkHelper;
49 private Set<String> hlogs = new HashSet<String>();
50
51
52
53
54 public ReplicationLogCleaner() {}
55
56 @Override
57 public boolean isLogDeletable(Path filePath) {
58 String log = filePath.getName();
59
60
61 if (this.hlogs.contains(log)) {
62 return false;
63 }
64
65
66
67
68 return !refreshHLogsAndSearch(log);
69 }
70
71
72
73
74
75
76
77
78 private boolean refreshHLogsAndSearch(String searchedLog) {
79 this.hlogs.clear();
80 final boolean lookForLog = searchedLog != null;
81 List<String> rss = zkHelper.getListOfReplicators(this);
82 if (rss == null) {
83 LOG.debug("Didn't find any region server that replicates, deleting: " +
84 searchedLog);
85 return false;
86 }
87 for (String rs: rss) {
88 List<String> listOfPeers = zkHelper.getListPeersForRS(rs, this);
89
90 if (listOfPeers == null) {
91 continue;
92 }
93 for (String id : listOfPeers) {
94 List<String> peersHlogs = zkHelper.getListHLogsForPeerForRS(rs, id, this);
95 if (peersHlogs != null) {
96 this.hlogs.addAll(peersHlogs);
97 }
98
99 if(lookForLog && this.hlogs.contains(searchedLog)) {
100 LOG.debug("Found log in ZK, keeping: " + searchedLog);
101 return true;
102 }
103 }
104 }
105 LOG.debug("Didn't find this log in ZK, deleting: " + searchedLog);
106 return false;
107 }
108
109 @Override
110 public void setConf(Configuration conf) {
111 this.conf = conf;
112 try {
113 this.zkHelper = new ReplicationZookeeperWrapper(
114 ZooKeeperWrapper.createInstance(this.conf,
115 HMaster.class.getName()),
116 this.conf, new AtomicBoolean(true), null);
117 } catch (IOException e) {
118 LOG.error(e);
119 }
120 refreshHLogsAndSearch(null);
121 }
122
123 @Override
124 public Configuration getConf() {
125 return conf;
126 }
127
128 @Override
129 public void process(WatchedEvent watchedEvent) {}
130 }