Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: mark the lockholder of branchsession as final #5487

Merged
merged 12 commits into from Apr 7, 2023
1 change: 1 addition & 0 deletions changes/en-us/2.0.0.md
Expand Up @@ -77,6 +77,7 @@ The version is updated as follows:
- [[#5471](https://github.com/seata/seata/pull/5471)] optimize transaction log on client side
- [[#5485](https://github.com/seata/seata/pull/5485)] optimize server log output
- [[#4907](https://github.com/seata/seata/pull/4907)] optimize thread scheduling and code
- [[#5487](https://github.com/seata/seata/pull/5487)] mark the lockholder of branchsession as final

### test:
- [[#5308](https://github.com/seata/seata/pull/5308)] add unit test [FileLoader, ObjectHolder, StringUtils]
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.0.0.md
Expand Up @@ -76,6 +76,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#5471](https://github.com/seata/seata/pull/5471)] 优化客户侧事务日志
- [[#5485](https://github.com/seata/seata/pull/5485)] 优化Server日志输出
- [[#4907](https://github.com/seata/seata/pull/4907)] 调整二阶段result线程池大小及优化代码
- [[#5487](https://github.com/seata/seata/pull/5487)] 将branchsession中的lockholder增加final修饰

### security:
- [[#5172](https://github.com/seata/seata/pull/5172)] 修复一些安全漏洞的版本
Expand Down
26 changes: 14 additions & 12 deletions server/src/main/java/io/seata/server/session/BranchSession.java
Expand Up @@ -20,6 +20,9 @@
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import io.seata.common.util.CollectionUtils;
import io.seata.common.util.CompressUtil;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.BranchStatus;
Expand All @@ -33,7 +36,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import static io.seata.core.model.LockStatus.Locked;

/**
Expand Down Expand Up @@ -72,11 +74,19 @@ public class BranchSession implements Lockable, Comparable<BranchSession>, Sessi

private LockStatus lockStatus = Locked;

private Map<FileLocker.BucketLockMap, Set<String>> lockHolder
= Collections.emptyMap();
private final Map<FileLocker.BucketLockMap, Set<String>> lockHolder;

private final LockManager lockManager = LockerManagerFactory.getLockManager();

public BranchSession() {
lockHolder = new ConcurrentHashMap<>(2);
}

public BranchSession(BranchType branchType) {
this.branchType = branchType;
this.lockHolder = branchType == BranchType.AT ? new ConcurrentHashMap<>(8) : Collections.emptyMap();
}

/**
* Gets application data.
*
Expand Down Expand Up @@ -280,14 +290,6 @@ public Map<FileLocker.BucketLockMap, Set<String>> getLockHolder() {
return lockHolder;
}

/**
* Set lock holder.
*
*/
public void setLockHolder(Map<FileLocker.BucketLockMap, Set<String>> lockHolder) {
this.lockHolder = lockHolder;
}

@Override
public boolean lock() throws TransactionException {
return this.lock(true, false);
Expand All @@ -302,7 +304,7 @@ public boolean lock(boolean autoCommit, boolean skipCheckLock) throws Transactio

@Override
public boolean unlock() throws TransactionException {
if (this.branchType == BranchType.AT) {
if (this.branchType == BranchType.AT && CollectionUtils.isNotEmpty(this.lockHolder)) {
return lockManager.releaseLock(this);
}
return true;
Expand Down
Expand Up @@ -88,12 +88,11 @@ public static BranchSession newBranchByGlobal(GlobalSession globalSession, Branc
*/
public static BranchSession newBranchByGlobal(GlobalSession globalSession, BranchType branchType, String resourceId,
String applicationData, String lockKeys, String clientId) {
BranchSession branchSession = new BranchSession();
BranchSession branchSession = new BranchSession(branchType);

branchSession.setXid(globalSession.getXid());
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setBranchType(branchType);
branchSession.setResourceId(resourceId);
branchSession.setLockKey(lockKeys);
branchSession.setClientId(clientId);
Expand Down
Expand Up @@ -75,7 +75,7 @@ public boolean acquireLock(List<RowLock> rowLocks, boolean autoCommit, boolean s
String resourceId = branchSession.getResourceId();
long transactionId = branchSession.getTransactionId();

Map<BucketLockMap, Set<String>> bucketHolder = new ConcurrentHashMap<>(8);
Map<BucketLockMap, Set<String>> bucketHolder = branchSession.getLockHolder();
Map<String, ConcurrentMap<Integer, BucketLockMap>> dbLockMap = CollectionUtils.computeIfAbsent(
LOCK_MAP, resourceId, key -> new ConcurrentHashMap<>(8));
boolean failFast = false;
Expand All @@ -100,11 +100,8 @@ public boolean acquireLock(List<RowLock> rowLocks, boolean autoCommit, boolean s
} else {
LOGGER.info("Global lock on [" + tableName + ":" + pk + "] is holding by " + previousLockBranchSession.getBranchId());
try {
if (CollectionUtils.isNotEmpty(bucketHolder)) {
branchSession.setLockHolder(bucketHolder);
// Release all acquired locks.
branchSession.unlock();
}
// Release all acquired locks.
branchSession.unlock();
} catch (TransactionException e) {
throw new FrameworkException(e);
}
Expand All @@ -123,9 +120,6 @@ public boolean acquireLock(List<RowLock> rowLocks, boolean autoCommit, boolean s
if (failFast) {
throw new StoreException(new BranchTransactionException(LockKeyConflictFailFast));
}
if (CollectionUtils.isNotEmpty(bucketHolder)) {
branchSession.setLockHolder(bucketHolder);
}
return canLock;
}

Expand Down