1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.fs.FileSystem;
25 import org.apache.hadoop.fs.Path;
26 import org.apache.hadoop.hbase.HRegionInfo;
27 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
28 import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
29 import org.apache.hadoop.hbase.io.HFileLink;
30 import org.apache.hadoop.hbase.io.Reference;
31 import org.apache.hadoop.hbase.regionserver.HRegion;
32 import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
33 import org.apache.hadoop.hbase.replication.ReplicationException;
34 import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
35 import org.apache.hadoop.hbase.replication.regionserver.RegionReplicaReplicationEndpoint;
36 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
37
38
39
40
41 public class ServerRegionReplicaUtil extends RegionReplicaUtil {
42
43
44
45
46
47
48
49
50
51
52
53 public static final String REGION_REPLICA_REPLICATION_CONF_KEY
54 = "hbase.region.replica.replication.enabled";
55 private static final boolean DEFAULT_REGION_REPLICA_REPLICATION = false;
56 private static final String REGION_REPLICA_REPLICATION_PEER = "region_replica_replication";
57
58
59
60
61
62
63
64 public static final String REGION_REPLICA_STORE_FILE_REFRESH
65 = "hbase.region.replica.storefile.refresh";
66 private static final boolean DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH = true;
67
68
69
70
71
72
73 public static final String REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER
74 = "hbase.region.replica.storefile.refresh.memstore.multiplier";
75 private static final double DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER = 4;
76
77
78
79
80
81 public static HRegionInfo getRegionInfoForFs(HRegionInfo regionInfo) {
82 if (regionInfo == null) {
83 return null;
84 }
85 return RegionReplicaUtil.getRegionInfoForDefaultReplica(regionInfo);
86 }
87
88
89
90
91
92
93 public static boolean isReadOnly(HRegion region) {
94 return region.getTableDesc().isReadOnly()
95 || !isDefaultReplica(region.getRegionInfo());
96 }
97
98
99
100
101
102
103
104
105
106 public static boolean shouldReplayRecoveredEdits(HRegion region) {
107 return isDefaultReplica(region.getRegionInfo());
108 }
109
110
111
112
113
114
115
116
117 public static StoreFileInfo getStoreFileInfo(Configuration conf, FileSystem fs,
118 HRegionInfo regionInfo, HRegionInfo regionInfoForFs, String familyName, Path path)
119 throws IOException {
120
121
122 if (regionInfo.equals(regionInfoForFs)) {
123 return new StoreFileInfo(conf, fs, path);
124 }
125
126
127 HFileLink link = HFileLink.build(conf, regionInfoForFs.getTable(),
128 regionInfoForFs.getEncodedName(), familyName, path.getName());
129
130 if (StoreFileInfo.isReference(path)) {
131 Reference reference = Reference.read(fs, path);
132 return new StoreFileInfo(conf, fs, link.getFileStatus(fs), reference);
133 }
134
135 return new StoreFileInfo(conf, fs, link.getFileStatus(fs), link);
136 }
137
138
139
140
141
142
143 public static void setupRegionReplicaReplication(Configuration conf) throws IOException {
144 if (!isRegionReplicaReplicationEnabled(conf)) {
145 return;
146 }
147 ReplicationAdmin repAdmin = new ReplicationAdmin(conf);
148 try {
149 if (repAdmin.getPeerConfig(REGION_REPLICA_REPLICATION_PEER) == null) {
150 ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
151 peerConfig.setClusterKey(ZKConfig.getZooKeeperClusterKey(conf));
152 peerConfig.setReplicationEndpointImpl(RegionReplicaReplicationEndpoint.class.getName());
153 repAdmin.addPeer(REGION_REPLICA_REPLICATION_PEER, peerConfig, null);
154 }
155 } catch (ReplicationException ex) {
156 throw new IOException(ex);
157 } finally {
158 repAdmin.close();
159 }
160 }
161
162 public static boolean isRegionReplicaReplicationEnabled(Configuration conf) {
163 return conf.getBoolean(REGION_REPLICA_REPLICATION_CONF_KEY,
164 DEFAULT_REGION_REPLICA_REPLICATION);
165 }
166
167 public static boolean isRegionReplicaWaitForPrimaryFlushEnabled(Configuration conf) {
168 return conf.getBoolean(REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
169 DEFAULT_REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH);
170 }
171
172 public static boolean isRegionReplicaStoreFileRefreshEnabled(Configuration conf) {
173 return conf.getBoolean(REGION_REPLICA_STORE_FILE_REFRESH,
174 DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH);
175 }
176
177 public static double getRegionReplicaStoreFileRefreshMultiplier(Configuration conf) {
178 return conf.getDouble(REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER,
179 DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER);
180 }
181
182
183
184
185 public static String getReplicationPeerId() {
186 return REGION_REPLICA_REPLICATION_PEER;
187 }
188
189 }