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 if (HFileLink.isHFileLink(path) || StoreFileInfo.isHFile(path)) {
128 HFileLink link = HFileLink
129 .build(conf, regionInfoForFs.getTable(), regionInfoForFs.getEncodedName(), familyName,
130 path.getName());
131 return new StoreFileInfo(conf, fs, link.getFileStatus(fs), link);
132 } else if (StoreFileInfo.isReference(path)) {
133 Reference reference = Reference.read(fs, path);
134 Path referencePath = StoreFileInfo.getReferredToFile(path);
135 if (HFileLink.isHFileLink(referencePath)) {
136
137 HFileLink link = HFileLink.buildFromHFileLinkPattern(conf, referencePath);
138 return new StoreFileInfo(conf, fs, link.getFileStatus(fs), reference, link);
139 } else {
140
141 HFileLink link = HFileLink
142 .build(conf, regionInfoForFs.getTable(), regionInfoForFs.getEncodedName(), familyName,
143 path.getName());
144 return new StoreFileInfo(conf, fs, link.getFileStatus(fs), reference);
145 }
146 } else {
147 throw new IOException("path=" + path + " doesn't look like a valid StoreFile");
148 }
149 }
150
151
152
153
154
155
156 public static void setupRegionReplicaReplication(Configuration conf) throws IOException {
157 if (!isRegionReplicaReplicationEnabled(conf)) {
158 return;
159 }
160 ReplicationAdmin repAdmin = new ReplicationAdmin(conf);
161 try {
162 if (repAdmin.getPeerConfig(REGION_REPLICA_REPLICATION_PEER) == null) {
163 ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
164 peerConfig.setClusterKey(ZKConfig.getZooKeeperClusterKey(conf));
165 peerConfig.setReplicationEndpointImpl(RegionReplicaReplicationEndpoint.class.getName());
166 repAdmin.addPeer(REGION_REPLICA_REPLICATION_PEER, peerConfig, null);
167 }
168 } catch (ReplicationException ex) {
169 throw new IOException(ex);
170 } finally {
171 repAdmin.close();
172 }
173 }
174
175 public static boolean isRegionReplicaReplicationEnabled(Configuration conf) {
176 return conf.getBoolean(REGION_REPLICA_REPLICATION_CONF_KEY,
177 DEFAULT_REGION_REPLICA_REPLICATION);
178 }
179
180 public static boolean isRegionReplicaWaitForPrimaryFlushEnabled(Configuration conf) {
181 return conf.getBoolean(REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
182 DEFAULT_REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH);
183 }
184
185 public static boolean isRegionReplicaStoreFileRefreshEnabled(Configuration conf) {
186 return conf.getBoolean(REGION_REPLICA_STORE_FILE_REFRESH,
187 DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH);
188 }
189
190 public static double getRegionReplicaStoreFileRefreshMultiplier(Configuration conf) {
191 return conf.getDouble(REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER,
192 DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER);
193 }
194
195
196
197
198 public static String getReplicationPeerId() {
199 return REGION_REPLICA_REPLICATION_PEER;
200 }
201
202 }