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.regionserver;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.fs.FileSystem;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.HConstants;
26 import org.apache.hadoop.hbase.HRegionInfo;
27 import org.apache.hadoop.hbase.HServerInfo;
28 import org.apache.hadoop.hbase.KeyValue;
29 import org.apache.hadoop.hbase.regionserver.wal.HLog;
30 import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
31 import org.apache.hadoop.hbase.regionserver.wal.LogEntryVisitor;
32 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
33 import org.apache.hadoop.hbase.replication.ReplicationZookeeperWrapper;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
36
37 import java.io.IOException;
38 import java.util.NavigableMap;
39 import java.util.TreeMap;
40 import java.util.concurrent.atomic.AtomicBoolean;
41
42
43
44
45
46 public class Replication implements LogEntryVisitor {
47
48 private final boolean replication;
49 private final ReplicationSourceManager replicationManager;
50 private boolean replicationMaster;
51 private final AtomicBoolean replicating = new AtomicBoolean(true);
52 private final ReplicationZookeeperWrapper zkHelper;
53 private final Configuration conf;
54 private final AtomicBoolean stopRequested;
55 private ReplicationSink replicationSink;
56
57
58
59
60
61
62
63
64
65
66 public Replication(Configuration conf, HServerInfo hsi,
67 FileSystem fs, Path logDir, Path oldLogDir,
68 AtomicBoolean stopRequested) throws IOException {
69 this.conf = conf;
70 this.stopRequested = stopRequested;
71 this.replication =
72 conf.getBoolean(HConstants.REPLICATION_ENABLE_KEY, false);
73 if (replication) {
74 this.zkHelper = new ReplicationZookeeperWrapper(
75 ZooKeeperWrapper.getInstance(conf, hsi.getServerName()), conf,
76 this.replicating, hsi.getServerName());
77 this.replicationMaster = zkHelper.isReplicationMaster();
78 this.replicationManager = this.replicationMaster ?
79 new ReplicationSourceManager(zkHelper, conf, stopRequested,
80 fs, this.replicating, logDir, oldLogDir) : null;
81 } else {
82 replicationManager = null;
83 zkHelper = null;
84 }
85 }
86
87
88
89
90 public void join() {
91 if (this.replication) {
92 if (this.replicationMaster) {
93 this.replicationManager.join();
94 }
95 this.zkHelper.deleteOwnRSZNode();
96 }
97 }
98
99
100
101
102
103
104 public void replicateLogEntries(HLog.Entry[] entries) throws IOException {
105 if (this.replication && !this.replicationMaster) {
106 this.replicationSink.replicateEntries(entries);
107 }
108 }
109
110
111
112
113
114
115 public void startReplicationServices() throws IOException {
116 if (this.replication) {
117 if (this.replicationMaster) {
118 this.replicationManager.init();
119 } else {
120 this.replicationSink =
121 new ReplicationSink(this.conf, this.stopRequested);
122 }
123 }
124 }
125
126
127
128
129
130 public ReplicationSourceManager getReplicationManager() {
131 return replicationManager;
132 }
133
134 @Override
135 public void visitLogEntryBeforeWrite(HRegionInfo info, HLogKey logKey,
136 WALEdit logEdit) {
137 NavigableMap<byte[], Integer> scopes =
138 new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
139 byte[] family;
140 for (KeyValue kv : logEdit.getKeyValues()) {
141 family = kv.getFamily();
142 int scope = info.getTableDesc().getFamily(family).getScope();
143 if (scope != HConstants.REPLICATION_SCOPE_LOCAL &&
144 !scopes.containsKey(family)) {
145 scopes.put(family, scope);
146 }
147 }
148 if (!scopes.isEmpty()) {
149 logEdit.setScopes(scopes);
150 }
151 }
152
153
154
155
156
157 public void addLogEntryVisitor(HLog hlog) {
158 if (replication) {
159 hlog.addLogEntryVisitor(this);
160 }
161 }
162 }