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: once delete GlobalSession locks for db mode when commit success #1574

Merged
merged 9 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 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,6 +15,7 @@
*/
package io.seata.server.lock;

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

import io.seata.common.util.CollectionUtils;
Expand All @@ -23,6 +24,7 @@
import io.seata.core.lock.Locker;
import io.seata.core.lock.RowLock;
import io.seata.server.session.BranchSession;
import io.seata.server.session.GlobalSession;

/**
* The type Default lock manager.
Expand Down Expand Up @@ -53,18 +55,37 @@ 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 releaseDBGlobalSessionLock(GlobalSession globalSession) throws TransactionException {
List<RowLock> locks = new ArrayList<>();
ArrayList<BranchSession> branchSessions = globalSession.getBranchSessions();
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);
zjinlei marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

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
11 changes: 11 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,16 @@ public interface LockManager {
*/
boolean releaseLock(BranchSession branchSession) throws TransactionException;

/**
* Un lock boolean.
* only used by db mode
*
* @param globalSession the global session
* @return the boolean
* @throws TransactionException the transaction exception
*/
boolean releaseDBGlobalSessionLock(GlobalSession globalSession) throws TransactionException;

/**
* Is lockable boolean.
*
Expand Down
23 changes: 21 additions & 2 deletions server/src/main/java/io/seata/server/session/GlobalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
import java.util.concurrent.locks.ReentrantLock;

import io.seata.common.XID;
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.exception.TransactionExceptionCode;
import io.seata.core.model.BranchStatus;
import io.seata.core.model.BranchType;
import io.seata.core.model.GlobalStatus;
import io.seata.core.store.StoreMode;
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 +79,11 @@ public class GlobalSession implements SessionLifecycle, SessionStorable {

private GlobalSessionLock globalSessionLock = new GlobalSessionLock();

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

/**
* Add boolean.
*
Expand Down Expand Up @@ -173,8 +183,13 @@ public void end() throws TransactionException {
}

public void clean() throws TransactionException {
for (BranchSession branchSession : branchSessions) {
branchSession.unlock();
String storeMode = CONFIG.getConfig(ConfigurationKeys.STORE_MODE);
if (StoreMode.DB.name().equalsIgnoreCase(storeMode)) {
LockerFactory.getLockManager().releaseDBGlobalSessionLock(this);
} else {
for (BranchSession branchSession : branchSessions) {
branchSession.unlock();
}
}

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

V call() throws TransactionException;
}

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