in server/src/main/java/org/apache/seata/server/storage/redis/lock/RedisLocker.java [91:192]
public boolean acquireLock(List<RowLock> rowLocks, boolean autoCommit, boolean skipCheckLock) {
if (CollectionUtils.isEmpty(rowLocks)) {
return true;
}
try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
String needLockXid = rowLocks.get(0).getXid();
Long branchId = rowLocks.get(0).getBranchId();
List<LockDO> needLockDOS = convertToLockDO(rowLocks);
if (needLockDOS.size() > 1) {
needLockDOS =
needLockDOS.stream().filter(LambdaUtils.distinctByKey(LockDO::getRowKey)).collect(Collectors.toList());
}
List<String> needLockKeys = new ArrayList<>();
needLockDOS.forEach(lockDO -> needLockKeys.add(buildLockKey(lockDO.getRowKey())));
Map<String, LockDO> needAddLock = new HashMap<>(needLockKeys.size(), 1);
if (!skipCheckLock) {
Pipeline pipeline1 = jedis.pipelined();
needLockKeys.stream().forEachOrdered(needLockKey -> {
pipeline1.hget(needLockKey, XID);
if (!autoCommit) {
pipeline1.hget(needLockKey, STATUS);
}
});
List<List<String>> existedLockInfos =
Lists.partition((List<String>) (List) pipeline1.syncAndReturnAll(), autoCommit ? 1 : 2);
// When the local transaction and the global transaction are enabled,
// the branch registration fails to acquire the global lock,
// the lock holder is in the second-stage rollback,
// and the branch registration fails to be retried quickly,
// because the retry with the local transaction does not release the database lock ,
// resulting in a two-phase rollback wait.
// Therefore, if a global lock is found in the Rollbacking state,
// the fail-fast code is returned directly.
if (!autoCommit) {
boolean hasRollBackingLock = existedLockInfos.parallelStream().anyMatch(
result -> StringUtils.equals(result.get(1), String.valueOf(LockStatus.Rollbacking.getCode())));
if (hasRollBackingLock) {
throw new StoreException(new BranchTransactionException(LockKeyConflictFailFast));
}
}
// The logic is executed here, there must be a lock without Rollbacking status when autoCommit equals false
for (int i = 0; i < needLockKeys.size(); i++) {
List<String> results = existedLockInfos.get(i);
String existedLockXid = CollectionUtils.isEmpty(results) ? null : existedLockInfos.get(i).get(0);
if (StringUtils.isEmpty(existedLockXid)) {
// If empty,we need to lock this row
needAddLock.put(needLockKeys.get(i), needLockDOS.get(i));
} else {
if (!StringUtils.equals(existedLockXid, needLockXid)) {
// If not equals,means the rowkey is holding by another global transaction
logGlobalLockConflictInfo(needLockXid, needLockKeys.get(i), existedLockXid);
return false;
}
}
}
if (needAddLock.isEmpty()) {
return true;
}
}
Pipeline pipeline = jedis.pipelined();
List<String> readyKeys = new ArrayList<>(needAddLock.keySet());
needAddLock.forEach((key, value) -> {
pipeline.hsetnx(key, XID, value.getXid());
pipeline.hsetnx(key, TRANSACTION_ID, value.getTransactionId().toString());
pipeline.hsetnx(key, BRANCH_ID, value.getBranchId().toString());
pipeline.hset(key, ROW_KEY, value.getRowKey());
pipeline.hset(key, RESOURCE_ID, value.getResourceId());
pipeline.hset(key, TABLE_NAME, value.getTableName());
pipeline.hset(key, PK, value.getPk());
});
List<Integer> results = (List<Integer>) (List) pipeline.syncAndReturnAll();
List<List<Integer>> partitions = Lists.partition(results, 7);
ArrayList<String> success = new ArrayList<>(partitions.size());
Integer status = SUCCEED;
for (int i = 0; i < partitions.size(); i++) {
if (Objects.equals(partitions.get(i).get(0), FAILED)) {
status = FAILED;
} else {
success.add(readyKeys.get(i));
}
}
// If someone has failed,all the lockkey which has been added need to be delete.
if (FAILED.equals(status)) {
if (success.size() > 0) {
jedis.del(success.toArray(new String[0]));
}
return false;
}
String xidLockKey = buildXidLockKey(needLockXid);
StringJoiner lockKeysString = new StringJoiner(ROW_LOCK_KEY_SPLIT_CHAR);
needLockKeys.forEach(lockKeysString::add);
jedis.hset(xidLockKey, branchId.toString(), lockKeysString.toString());
return true;
}
}