通过client的源码分析,我们发现每次建立连接前需要先找到rowkey所属region的regionserver。本篇来分析一下这个找到regionserver的整个流程。
从代码connection.getRegionLocator(tableName)开始,跟踪下来最后调用的是如下代码
// ConnectionManager.java
public HRegionLocation getRegionLocation(final TableName tableName,
final byte [] row, boolean reload)
throws IOException {
return reload? relocateRegion(tableName, row): locateRegion(tableName, row);
}
这里根据reload的方式来确定是从远程获取location信息还是从本地获取。
// ConnectionManager.java
public RegionLocations locateRegion(final TableName tableName,
final byte [] row, boolean useCache, boolean retry, int replicaId)
throws IOException {
if (this.closed) throw new IOException(toString() + " closed");
if (tableName== null || tableName.getName().length == 0) {
throw new IllegalArgumentException(
"table name cannot be null or zero length");
}
if (tableName.equals(TableName.META_TABLE_NAME)) {
return locateMeta(tableName, useCache, replicaId);
} else {
// Region not in the cache - have to go to the meta RS
return locateRegionInMeta(tableName, row, useCache, retry, replicaId);
}
}
再跟踪代码下来,会根据表的类型选择是获取meta表的location还是从meta表中获取对应region的location。
// ConnectionManager.java
private RegionLocations locateMeta(final TableName tableName,
boolean useCache, int replicaId) throws IOException {
// HBASE-10785: We cache the location of the META itself, so that we are not overloading
// zookeeper with one request for every region lookup. We cache the META with empty row
// key in MetaCache.
byte[] metaCacheKey = HConstants.EMPTY_START_ROW; // use by