Skip to content

Commit 79427b8

Browse files
bharathvthangTang
authored andcommitted
HBASE-23281: Track meta region locations in masters (apache#830)
* HBASE-23281: Track meta region changes on masters This patch adds a simple cache that tracks the meta region replica locations. It keeps an eye on the region movements so that the cached locations are not stale. This information is used for servicing client RPCs for connections that use master based registry (HBASE-18095). The RPC end points will be added in a separate patch. Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
1 parent b0e429e commit 79427b8

23 files changed

+586
-83
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -80,6 +80,7 @@
8080
import org.apache.hadoop.hbase.client.Put;
8181
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
8282
import org.apache.hadoop.hbase.client.RegionLoadStats;
83+
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
8384
import org.apache.hadoop.hbase.client.RegionStatesCount;
8485
import org.apache.hadoop.hbase.client.Result;
8586
import org.apache.hadoop.hbase.client.Scan;
@@ -93,6 +94,7 @@
9394
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
9495
import org.apache.hadoop.hbase.filter.Filter;
9596
import org.apache.hadoop.hbase.io.TimeRange;
97+
import org.apache.hadoop.hbase.master.RegionState;
9698
import org.apache.hadoop.hbase.protobuf.ProtobufMagic;
9799
import org.apache.hadoop.hbase.protobuf.ProtobufMessageConverter;
98100
import org.apache.hadoop.hbase.quotas.QuotaScope;
@@ -3067,6 +3069,44 @@ public static ProcedureDescription buildProcedureDescription(String signature, S
30673069
return builder.build();
30683070
}
30693071

3072+
/**
3073+
* Get the Meta region state from the passed data bytes. Can handle both old and new style
3074+
* server names.
3075+
* @param data protobuf serialized data with meta server name.
3076+
* @param replicaId replica ID for this region
3077+
* @return RegionState instance corresponding to the serialized data.
3078+
* @throws DeserializationException if the data is invalid.
3079+
*/
3080+
public static RegionState parseMetaRegionStateFrom(final byte[] data, int replicaId)
3081+
throws DeserializationException {
3082+
RegionState.State state = RegionState.State.OPEN;
3083+
ServerName serverName;
3084+
if (data != null && data.length > 0 && ProtobufUtil.isPBMagicPrefix(data)) {
3085+
try {
3086+
int prefixLen = ProtobufUtil.lengthOfPBMagic();
3087+
ZooKeeperProtos.MetaRegionServer rl =
3088+
ZooKeeperProtos.MetaRegionServer.parser().parseFrom(data, prefixLen,
3089+
data.length - prefixLen);
3090+
if (rl.hasState()) {
3091+
state = RegionState.State.convert(rl.getState());
3092+
}
3093+
HBaseProtos.ServerName sn = rl.getServer();
3094+
serverName = ServerName.valueOf(
3095+
sn.getHostName(), sn.getPort(), sn.getStartCode());
3096+
} catch (InvalidProtocolBufferException e) {
3097+
throw new DeserializationException("Unable to parse meta region location");
3098+
}
3099+
} else {
3100+
// old style of meta region location?
3101+
serverName = parseServerNameFrom(data);
3102+
}
3103+
if (serverName == null) {
3104+
state = RegionState.State.OFFLINE;
3105+
}
3106+
return new RegionState(RegionReplicaUtil.getRegionInfoForReplica(
3107+
RegionInfoBuilder.FIRST_META_REGIONINFO, replicaId), state, serverName);
3108+
}
3109+
30703110
/**
30713111
* Get a ServerName from the passed in data bytes.
30723112
* @param data Data with a serialize server name in it; can handle the old style

hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZNodePaths.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class ZNodePaths {
4141
// TODO: Replace this with ZooKeeper constant when ZOOKEEPER-277 is resolved.
4242
public static final char ZNODE_PATH_SEPARATOR = '/';
4343

44-
private static final String META_ZNODE_PREFIX = "meta-region-server";
44+
public static final String META_ZNODE_PREFIX_CONF_KEY = "zookeeper.znode.metaserver";
45+
public static final String META_ZNODE_PREFIX = "meta-region-server";
4546
private static final String DEFAULT_SNAPSHOT_CLEANUP_ZNODE = "snapshot-cleanup";
4647

4748
// base znode for this cluster
@@ -104,7 +105,7 @@ public class ZNodePaths {
104105
public ZNodePaths(Configuration conf) {
105106
baseZNode = conf.get(ZOOKEEPER_ZNODE_PARENT, DEFAULT_ZOOKEEPER_ZNODE_PARENT);
106107
ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder();
107-
metaZNodePrefix = conf.get("zookeeper.znode.metaserver", META_ZNODE_PREFIX);
108+
metaZNodePrefix = conf.get(META_ZNODE_PREFIX_CONF_KEY, META_ZNODE_PREFIX);
108109
String defaultMetaReplicaZNode = ZNodePaths.joinZNode(baseZNode, metaZNodePrefix);
109110
builder.put(DEFAULT_REPLICA_ID, defaultMetaReplicaZNode);
110111
int numMetaReplicas = conf.getInt(META_REPLICAS_NUM, DEFAULT_META_REPLICA_NUM);
@@ -189,7 +190,19 @@ public String getZNodeForReplica(int replicaId) {
189190
}
190191

191192
/**
192-
* Parse the meta replicaId from the passed znode name.
193+
* Parses the meta replicaId from the passed path.
194+
* @param path the name of the full path which includes baseZNode.
195+
* @return replicaId
196+
*/
197+
public int getMetaReplicaIdFromPath(String path) {
198+
// Extract the znode from path. The prefix is of the following format.
199+
// baseZNode + PATH_SEPARATOR.
200+
int prefixLen = baseZNode.length() + 1;
201+
return getMetaReplicaIdFromZnode(path.substring(prefixLen));
202+
}
203+
204+
/**
205+
* Parse the meta replicaId from the passed znode
193206
* @param znode the name of the znode, does not include baseZNode
194207
* @return replicaId
195208
*/

hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -356,6 +356,12 @@ public void run() {
356356
// manager of assignment nodes in zookeeper
357357
private AssignmentManager assignmentManager;
358358

359+
/**
360+
* Cache for the meta region replica's locations. Also tracks their changes to avoid stale
361+
* cache entries.
362+
*/
363+
private final MetaRegionLocationCache metaRegionLocationCache;
364+
359365
// manager of replication
360366
private ReplicationPeerManager replicationPeerManager;
361367

@@ -508,8 +514,7 @@ public void doGet(HttpServletRequest request,
508514
* {@link #finishActiveMasterInitialization(MonitoredTask)} after the master becomes the
509515
* active one.
510516
*/
511-
public HMaster(final Configuration conf)
512-
throws IOException, KeeperException {
517+
public HMaster(final Configuration conf) throws IOException {
513518
super(conf);
514519
TraceUtil.initTracer(conf);
515520
try {
@@ -522,7 +527,6 @@ public HMaster(final Configuration conf)
522527
} else {
523528
maintenanceMode = false;
524529
}
525-
526530
this.rsFatals = new MemoryBoundedLogMessageBuffer(
527531
conf.getLong("hbase.master.buffer.for.rs.fatals", 1 * 1024 * 1024));
528532
LOG.info("hbase.rootdir={}, hbase.cluster.distributed={}", getDataRootDir(),
@@ -570,8 +574,10 @@ public HMaster(final Configuration conf)
570574

571575
// Some unit tests don't need a cluster, so no zookeeper at all
572576
if (!conf.getBoolean("hbase.testing.nocluster", false)) {
577+
this.metaRegionLocationCache = new MetaRegionLocationCache(this.zooKeeper);
573578
this.activeMasterManager = new ActiveMasterManager(zooKeeper, this.serverName, this);
574579
} else {
580+
this.metaRegionLocationCache = null;
575581
this.activeMasterManager = null;
576582
}
577583
cachedClusterId = new CachedClusterId(conf);
@@ -3838,4 +3844,8 @@ public void runReplicationBarrierCleaner() {
38383844
rbc.chore();
38393845
}
38403846
}
3847+
3848+
public MetaRegionLocationCache getMetaRegionLocationCache() {
3849+
return this.metaRegionLocationCache;
3850+
}
38413851
}

0 commit comments

Comments
 (0)