在前面的文章裏粗略地分析了hbase的put的源碼,但是對提交那塊分析的不夠深入.........現在把提交那塊拿出來再分析一下。
書接上文,前面說了在HconnectionManager 其中有一步是提交請求,調用瞭如下代碼
private <R> Callable<MultiResponse> createCallable(final HRegionLocation loc,
final MultiAction<R> multi, final byte [] tableName) {
final HConnection connection = this;
return new Callable<MultiResponse>() {
public MultiResponse call() throws IOException {
return getRegionServerWithoutRetries(
new ServerCallable<MultiResponse>(connection, tableName, null) {
public MultiResponse call() throws IOException {
return server.multi(multi);
}
@Override
public void connect(boolean reload) throws IOException {
server =
connection.getHRegionConnection(loc.getHostname(), loc.getPort());
}
}
);
}
};
}
跟進這個server.multi(multi); 方法,來到HRegionServer的multi這個方法
public <R> MultiResponse multi(MultiAction<R> multi) throws IOException {
checkOpen();
MultiResponse response = new MultiResponse();
for (Map.Entry<byte[], List<Action<R>>> e : multi.actions.entrySet()) {
byte[] regionName = e.getKey();
List<Action<R>> actionsForRegion = e.getValue();
// sort based on the row id - this helps in the case where we reach the
// end of a region, so that we don't have to try the rest of the
// actions in the list.
Collections.sort(actionsForRegion);
Row action;
List<Action<R>> puts = new ArrayList<Action<R>>();
for (Action<R> a : actionsForRegion) {
action = a.getAction();
int originalIndex = a.getOriginalIndex();
try {
if (action instanceof Delete) {
delete(regionName, (Delete) action);
response.add(regionName, originalIndex, new Result());
} else if (action instanceof Get) {
response.add(regionName, originalIndex, get(regionName, (Get) action));
} else if (action instanceof Put) {
puts.add(a); // wont throw.
} else if (action instanceof Exec) {
ExecResult result = execCoprocessor(regionName, (Exec)action);
response.add(regionName, new Pair<Integer, Object>(
a.getOriginalIndex(), result.getValue()
));
} else {
LOG.debug("Error: invalid Action, row must be a Get, Delete, " +
"Put or Exec.");
throw new DoNotRetryIOException("Invalid Action, row must be a " +
"Get, Delete or Put.");
}
} catch (IOException ex) {
response.add(regionName, originalIndex, ex);
}
}
// We do the puts with result.put so we can get the batching efficiency
// we so need. All this data munging doesn't seem great, but at least
// we arent copying bytes or anything.
if (!puts.isEmpty()) {
try {
HRegion region = getRegion(regionName);
if (!region.getRegionInfo().isMetaTable()) {
this.cacheFlusher.reclaimMemStoreMemory();
}
List<Pair<Put,Integer>> putsWithLocks =
Lists.newArrayListWithCapacity(puts.size());
for (Action<R> a : puts) {
Put p = (Put) a.getAction();
Integer lock;
try {
lock = getLockFromId(p.getLockId());
} catch (UnknownRowLockException ex) {
response.add(regionName, a.getOriginalIndex(), ex);
continue;
}
putsWithLocks.add(new Pair<Put, Integer>(p, lock));
}
this.requestCount.addAndGet(puts.size());
OperationStatus[] codes =
region.put(putsWithLocks.toArray(new Pair[]{}));
for( int i = 0 ; i < codes.length ; i++) {
OperationStatus code = codes[i];
Action<R> theAction = puts.get(i);
Object result = null;
if (code.getOperationStatusCode() == OperationStatusCode.SUCCESS) {
result = new Result();
} else if (code.getOperationStatusCode()
== OperationStatusCode.BAD_FAMILY) {
result = new NoSuchColumnFamilyException(code.getExceptionMsg());
}
// FAILURE && NOT_RUN becomes null, aka: need to run again.
response.add(regionName, theAction.getOriginalIndex(), result);
}
} catch (IOException ioe) {
// fail all the puts with the ioe in question.
for (Action<R> a: puts) {
response.add(regionName, a.getOriginalIndex(), ioe);
}
}
}
}
return response;
}
又是一個冗長的方法(囧......)
首先處理action 還是按region來處理的(一個region 一個 region順序來處理的)。
在一個region範圍內,首先把所有action 按照row-id 排序,並排除掉不屬於該region的action。
接下來判斷 action 的類型 , 如果是put類型的話 就放到新new的一個數組裏。
程序再往後,對裝有put的list做處理。
首先
List<Pair<Put,Integer>> putsWithLocks = Lists.newArrayListWithCapacity(puts.size());
先new 了一個存有put和鎖的數組,針對於每一個put都去調用 getLockFromId 這個方法,獲得鎖(Integer類型),放入list中。
然後在requestcout裏計數
this.requestCount.addAndGet(puts.size());
再往後就看到了提交層.......
OperationStatus[] codes =
region.put(putsWithLocks.toArray(new Pair[]{}));
接下來的工作就簡單了,針對返回的codes 逐一進行遍歷,
if (code.getOperationStatusCode() == OperationStatusCode.SUCCESS) {
result = new Result();
} else if (code.getOperationStatusCode()
== OperationStatusCode.BAD_FAMILY) {
result = new NoSuchColumnFamilyException(code.getExceptionMsg());
}
// FAILURE && NOT_RUN becomes null, aka: need to run again.
response.add(regionName, theAction.getOriginalIndex(), result);
如果成功了就new一個新的result放進去,如果是BAD_FAMILY這個錯,就new一個Exception ,否則的話就是null。 這樣上面這層就能按照返回類型進行重試判斷了。
最核心的提交層又沒分析........等下次吧!