* @param oldTid 所要检查的行存入数据库的旧事务id
* @param currentTransaction 当前事务
* @return true 有效
*/
public boolean isValid(String hostAndPort, long oldTid, Transaction currentTransaction) {
TransactionStatusCache cache = hostAndPortMap.get(hostAndPort);
if (cache == null) {
cache = newCache(hostAndPort);
}
long commitTimestamp = cache.get(oldTid);
//1.上一次已经查过了,已确认过是条无效的记录
if (commitTimestamp == -2)
return false;
//2. 是有效的事务记录,再进一步判断是否小于等于当前事务的开始时间戳
if (commitTimestamp != -1)
return commitTimestamp <= currentTransaction.getTransactionId();
String oldTransactionName = Transaction.getTransactionName(hostAndPort, oldTid);
Get get = new Get(Bytes.toBytes(oldTransactionName));
try {
Result r = table.get(get);
if (r != null && !r.isEmpty()) {
boolean isFullSuccessful = true;
commitTimestamp = Bytes.toLong(r.getValue(MetaDataAdmin.DEFAULT_COLUMN_FAMILY, COMMIT_TIMESTAMP));
String[] allLocalTransactionNames = Bytes.toString(
r.getValue(MetaDataAdmin.DEFAULT_COLUMN_FAMILY, ALL_LOCAL_TRANSACTION_NAMES)).split(",");
for (String localTransactionName : allLocalTransactionNames) {
if (!oldTransactionName.equals(localTransactionName)) {
get = new Get(Bytes.toBytes(localTransactionName));
if (!table.exists(get)) {
isFullSuccessful = false;
break;
}
}
}
if (isFullSuccessful)
cache.set(oldTid, commitTimestamp);
if (commitTimestamp <= currentTransaction.getTransactionId()) {
if (!isFullSuccessful)
currentTransaction.addHalfSuccessfulTransaction(oldTid);
return true;
} else {
return false;
}
} else {
cache.set(oldTid, -2);
return false;
}
} catch (IOException e) {
throw DbException.convert(e);
}