feat: 2.6.0

1. 分布式锁优化
This commit is contained in:
byteblogs168 2024-01-22 17:59:50 +08:00
parent e630d36a44
commit 00cb6f1fdf
5 changed files with 38 additions and 26 deletions

View File

@ -1,13 +1,12 @@
package com.aizuda.easy.retry.server.common.lock;
import cn.hutool.core.lang.Assert;
import com.aizuda.easy.retry.common.core.context.SpringContext;
import com.aizuda.easy.retry.server.common.cache.CacheLockRecord;
import com.aizuda.easy.retry.server.common.dto.LockConfig;
import com.aizuda.easy.retry.server.common.exception.EasyRetryServerException;
import org.springframework.transaction.support.TransactionTemplate;
import java.time.Duration;
import java.time.LocalDateTime;
/**
* @author www.byteblogs.com
@ -26,40 +25,32 @@ public abstract class AbstractLockProvider implements LockProvider {
LockConfig lockConfig = LockManager.getLockConfig();
String lockName = lockConfig.getLockName();
Assert.notNull(lockAtMost, () -> new EasyRetryServerException("lockAtMost can not be null. lockName:[{}]", lockName));
Assert.isFalse(lockAtMost.isNegative(), () -> new EasyRetryServerException("lockAtMost is negative. lockName:[{}]", lockName));
Assert.notNull(lockAtLeast, () -> new EasyRetryServerException("lockAtLeast can not be null. lockName:[{}]", lockName));
Assert.isFalse(lockAtLeast.compareTo(lockAtMost) > 0, () -> new EasyRetryServerException("lockAtLeast is longer than lockAtMost for lock. lockName:[{}]", lockName));
Assert.notNull(lockAtMost,
() -> new EasyRetryServerException("lockAtMost can not be null. lockName:[{}]", lockName));
Assert.isFalse(lockAtMost.isNegative(),
() -> new EasyRetryServerException("lockAtMost is negative. lockName:[{}]", lockName));
Assert.notNull(lockAtLeast,
() -> new EasyRetryServerException("lockAtLeast can not be null. lockName:[{}]", lockName));
Assert.isFalse(lockAtLeast.compareTo(lockAtMost) > 0,
() -> new EasyRetryServerException("lockAtLeast is longer than lockAtMost for lock. lockName:[{}]",
lockName));
LockManager.setCreateDt(LocalDateTime.now());
LockManager.setLockAtLeast(lockAtLeast);
LockManager.setLockAtMost(lockAtMost);
boolean tryToCreateLockRecord = !CacheLockRecord.lockRecordRecentlyCreated(lockName);
if (tryToCreateLockRecord) {
if (doLock(lockConfig)) {
CacheLockRecord.addLockRecord(lockName);
return true;
}
CacheLockRecord.addLockRecord(lockName);
}
boolean lock;
try {
lock = doLockAfter(lockConfig);
} catch (Exception e) {
if (tryToCreateLockRecord) {
CacheLockRecord.remove(lockName);
}
throw e;
}
return lock;
return doLockAfter(lockConfig);
}
protected boolean doLockAfter(LockConfig lockConfig) {
return renewal(lockConfig);
}
protected abstract boolean doLockAfter(LockConfig lockConfig);
protected boolean doLock(final LockConfig lockConfig) {
return createLock(lockConfig);

View File

@ -11,6 +11,11 @@ import com.aizuda.easy.retry.server.common.lock.persistence.LockStorageFactory;
*/
public class DisposableLockProvider extends AbstractLockProvider {
@Override
protected boolean doLockAfter(final LockConfig lockConfig) {
return Boolean.FALSE;
}
@Override
protected void doUnlock(LockConfig lockConfig) {
doUnlockWithDelete(lockConfig);

View File

@ -37,7 +37,6 @@ public final class LockBuilder {
Assert.notBlank(lockName, () -> new EasyRetryServerException("lockName can not be null."));
LockManager.initialize();
LockManager.setCreateDt(LocalDateTime.now());
LockManager.setLockName(lockName);
if (resident) {
return new ResidentLockProvider();

View File

@ -1,5 +1,6 @@
package com.aizuda.easy.retry.server.common.lock;
import com.aizuda.easy.retry.server.common.cache.CacheLockRecord;
import com.aizuda.easy.retry.server.common.dto.LockConfig;
import com.aizuda.easy.retry.server.common.lock.persistence.LockStorage;
import com.aizuda.easy.retry.server.common.lock.persistence.LockStorageFactory;
@ -11,6 +12,22 @@ import com.aizuda.easy.retry.server.common.lock.persistence.LockStorageFactory;
*/
public class ResidentLockProvider extends AbstractLockProvider {
@Override
protected boolean doLockAfter(final LockConfig lockConfig) {
String lockName = lockConfig.getLockName();
boolean lock;
try {
lock = renewal(lockConfig);
if (lock) {
CacheLockRecord.addLockRecord(lockName);
}
} catch (Exception e) {
CacheLockRecord.remove(lockName);
throw e;
}
return lock;
}
@Override
protected void doUnlock(LockConfig lockConfig) {
@ -22,7 +39,6 @@ public class ResidentLockProvider extends AbstractLockProvider {
lockStorage.releaseLockWithUpdate(lockConfig.getLockName(), lockConfig.getLockAtLeast());
}
@Override
protected boolean createLock(LockConfig lockConfig) {
LockStorage lockStorage = LockStorageFactory.getLockStorage();

View File

@ -88,8 +88,8 @@ public class DistributedLockHandler {
EasyRetryLog.LOCAL.error("lock execute error. lockName:[{}]", lockName, throwable);
} finally {
if (lock) {
EasyRetryLog.LOCAL.info("[{}] 锁已释放", lockName);
lockProvider.unlock();
EasyRetryLog.LOCAL.info("[{}] 锁已释放", lockName);
} else {
// 未获取到锁直接清除线程中存储的锁信息
LockManager.clear();
@ -122,6 +122,7 @@ public class DistributedLockHandler {
EasyRetryLog.LOCAL.error("lock execute error. lockName:[{}]", lockName, e);
} finally {
if (lock) {
EasyRetryLog.LOCAL.info("[{}] 锁已释放", lockName);
lockProvider.unlock();
} else {
// 未获取到锁直接清除线程中存储的锁信息