Skip to content

Commit

Permalink
optimize: once delete GlobalSession locks for db mode when commit suc…
Browse files Browse the repository at this point in the history
…cess (apache#1574)
  • Loading branch information
zjinlei committed Sep 7, 2019
1 parent b350b36 commit e191caa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
51 changes: 46 additions & 5 deletions server/src/main/java/io/seata/server/lock/DefaultLockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
*/
package io.seata.server.lock;

import java.util.ArrayList;
import java.util.List;

import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import io.seata.config.Configuration;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
import io.seata.core.exception.TransactionException;
import io.seata.core.lock.Locker;
import io.seata.core.lock.RowLock;
import io.seata.core.store.StoreMode;
import io.seata.server.session.BranchSession;
import io.seata.server.session.GlobalSession;

/**
* The type Default lock manager.
Expand All @@ -34,6 +40,11 @@ public class DefaultLockManager extends AbstractLockManager {

private static Locker locker = null;

/**
* The constant CONFIG.
*/
protected static final Configuration CONFIG = ConfigurationFactory.getInstance();

@Override
public boolean acquireLock(BranchSession branchSession) throws TransactionException {
String lockKey = branchSession.getLockKey();
Expand All @@ -53,18 +64,48 @@ public boolean acquireLock(BranchSession branchSession) throws TransactionExcept
@Override
public boolean releaseLock(BranchSession branchSession) throws TransactionException {
List<RowLock> locks = collectRowLocks(branchSession);
if (CollectionUtils.isEmpty(locks)) {
//no lock
return true;
}
try {
return getLocker(branchSession).releaseLock(locks);
return this.doReleaseLock(locks, branchSession);
} catch (Exception t) {
LOGGER.error("unLock error, branchSession:" + branchSession, t);
return false;
}
}

@Override
public boolean releaseGlobalSessionLock(GlobalSession globalSession) throws TransactionException {
ArrayList<BranchSession> branchSessions = globalSession.getBranchSessions();
String storeMode = CONFIG.getConfig(ConfigurationKeys.STORE_MODE);
if (StoreMode.DB.name().equalsIgnoreCase(storeMode)) {
List<RowLock> locks = new ArrayList<>();
for (BranchSession branchSession : branchSessions) {
locks.addAll(collectRowLocks(branchSession));
}
try {
return this.doReleaseLock(locks, null);
} catch (Exception t) {
LOGGER.error("unLock globalSession error, xid:{}", globalSession.getXid(), t);
return false;
}
} else {
boolean releaseLockResult = true;
for (BranchSession branchSession : branchSessions) {
if (!this.releaseLock(branchSession)) {
releaseLockResult = false;
}
}
return releaseLockResult;
}
}

private boolean doReleaseLock(List<RowLock> locks, BranchSession branchSession) {
if (CollectionUtils.isEmpty(locks)) {
//no lock
return true;
}
return getLocker(branchSession).releaseLock(locks);
}

@Override
public boolean isLockable(String xid, String resourceId, String lockKey) throws TransactionException {
List<RowLock> locks = collectRowLocks(lockKey, resourceId, xid);
Expand Down
10 changes: 10 additions & 0 deletions server/src/main/java/io/seata/server/lock/LockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.seata.core.exception.TransactionException;
import io.seata.server.session.BranchSession;
import io.seata.server.session.GlobalSession;

/**
* The interface Lock manager.
Expand All @@ -43,6 +44,15 @@ public interface LockManager {
*/
boolean releaseLock(BranchSession branchSession) throws TransactionException;

/**
* Un lock boolean.
*
* @param globalSession the global session
* @return the boolean
* @throws TransactionException the transaction exception
*/
boolean releaseGlobalSessionLock(GlobalSession globalSession) throws TransactionException;

/**
* Is lockable boolean.
*
Expand Down
10 changes: 7 additions & 3 deletions server/src/main/java/io/seata/server/session/GlobalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.seata.core.model.BranchType;
import io.seata.core.model.GlobalStatus;
import io.seata.server.UUIDGenerator;
import io.seata.server.lock.LockerFactory;
import io.seata.server.store.SessionStorable;
import io.seata.server.store.StoreConfig;
import org.slf4j.Logger;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class GlobalSession implements SessionLifecycle, SessionStorable {

private GlobalSessionLock globalSessionLock = new GlobalSessionLock();


/**
* Add boolean.
*
Expand Down Expand Up @@ -173,9 +175,7 @@ public void end() throws TransactionException {
}

public void clean() throws TransactionException {
for (BranchSession branchSession : branchSessions) {
branchSession.unlock();
}
LockerFactory.getLockManager().releaseGlobalSessionLock(this);

}

Expand Down Expand Up @@ -633,4 +633,8 @@ public interface LockCallable<V> {

V call() throws TransactionException;
}

public ArrayList<BranchSession> getBranchSessions() {
return branchSessions;
}
}

0 comments on commit e191caa

Please sign in to comment.