feat: 2.6.0

1. 修复LockManager未获取到锁未清除问题
2. 优化日志未打印堆栈问题
This commit is contained in:
byteblogs168 2024-01-12 09:36:23 +08:00
parent e2e98e03d9
commit 58302f0f8d
12 changed files with 51 additions and 37 deletions

View File

@ -315,10 +315,10 @@ CREATE TABLE `job_log_message`
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`namespace_id` varchar(64) NOT NULL DEFAULT '764d604ec6fc45f68cd92514c40e9e1a' COMMENT '命名空间id',
`group_name` varchar(64) NOT NULL COMMENT '组名称',
`job_id` bigint(20) NOT NULL COMMENT '任务信息id',
longtext `job_id` bigint(20) NOT NULL COMMENT '任务信息id',
`task_batch_id` bigint(20) NOT NULL COMMENT '任务批次id',
`task_id` bigint(20) NOT NULL COMMENT '调度任务id',
`message` text NOT NULL COMMENT '调度信息',
`message` NOT NULL COMMENT '调度信息',
`log_num` int(11) NOT NULL DEFAULT 1 COMMENT '日志数量',
`real_time` bigint(13) NOT NULL DEFAULT 0 COMMENT '上报时间',
`create_dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

View File

@ -11,6 +11,7 @@ import org.slf4j.spi.LocationAwareLogger;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* <a href="http://www.slf4j.org/">SLF4J</a> log.<br>
@ -126,7 +127,12 @@ public class Slf4jLog extends AbstractLog {
if (this.isLocationAwareLogger) {
locationAwareLog((LocationAwareLogger) this.logger, fqcn, LocationAwareLogger.ERROR_INT, t, format, remote, arguments);
} else {
logger.error(StrUtil.format(format, arguments), t);
if (Objects.nonNull(t)) {
logger.error(format, t);
} else {
logger.error(format, arguments);
}
}
}
}
@ -178,7 +184,7 @@ public class Slf4jLog extends AbstractLog {
map.put(LogFieldConstant.MDC_REMOTE, remote.toString());
MDC.getMDCAdapter().setContextMap(map);
}
logger.log(null, fqcn, level_int, StrUtil.format(msgTemplate, arguments), null, t);
logger.log(null, fqcn, level_int, msgTemplate, arguments, t);
}
/**

View File

@ -92,6 +92,10 @@
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
</dependency>
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-common-log</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -1,10 +1,10 @@
package com.aizuda.easy.retry.server.common.cache;
import com.aizuda.easy.retry.common.core.log.LogUtils;
import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.server.common.Lifecycle;
import com.aizuda.easy.retry.server.common.lock.LockManager;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@ -18,7 +18,6 @@ import java.time.Duration;
* @date 2023-07-20 22:53:21
* @since 2.1.0
*/
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CacheLockRecord implements Lifecycle {
@ -38,6 +37,7 @@ public class CacheLockRecord implements Lifecycle {
public static void remove(String lockName) {
CACHE.invalidate(lockName);
LockManager.clear();
}
public static void clear() {
@ -46,7 +46,7 @@ public class CacheLockRecord implements Lifecycle {
@Override
public void start() {
LogUtils.info(log, "CacheLockRecord start");
EasyRetryLog.LOCAL.info("CacheLockRecord start");
CACHE = CacheBuilder.newBuilder()
// 设置并发级别为cpu核心数
.concurrencyLevel(Runtime.getRuntime().availableProcessors())

View File

@ -30,7 +30,7 @@ public abstract class AbstractLockProvider implements LockProvider {
Assert.isFalse(lockAtLeast.compareTo(lockAtMost) > 0, () -> new EasyRetryServerException("lockAtLeast is longer than lockAtMost for lock. lockName:[{}]", lockName));
LockManager.setLockAtLeast(lockAtLeast);
LockManager.setLockAtLeast(lockAtMost);
LockManager.setLockAtMost(lockAtMost);
boolean tryToCreateLockRecord = !CacheLockRecord.lockRecordRecentlyCreated(lockName);
if (tryToCreateLockRecord) {
@ -42,17 +42,24 @@ public abstract class AbstractLockProvider implements LockProvider {
CacheLockRecord.addLockRecord(lockName);
}
boolean lock = false;
try {
return doLockAfter(lockConfig);
lock = doLockAfter(lockConfig);
} catch (Exception e) {
if (tryToCreateLockRecord) {
CacheLockRecord.remove(lockName);
}
throw e;
} finally {
if (!lock) {
LockManager.clear();
}
}
return lock;
}
protected boolean doLockAfter(LockConfig lockConfig) {
return renewal(lockConfig);
}
@ -62,17 +69,18 @@ public abstract class AbstractLockProvider implements LockProvider {
}
@Override
public boolean unlock() {
public void unlock() {
try {
LockConfig lockConfig = LockManager.getLockConfig();
return doUnlock(lockConfig);
Assert.notNull(lockConfig, () -> new EasyRetryServerException("lockConfig can not be null."));
doUnlock(lockConfig);
} finally {
LockManager.clear();
}
}
protected abstract boolean doUnlock(LockConfig lockConfig);
protected abstract void doUnlock(LockConfig lockConfig);
protected abstract boolean createLock(final LockConfig lockConfig);

View File

@ -12,8 +12,8 @@ import com.aizuda.easy.retry.server.common.lock.persistence.LockStorageFactory;
public class DisposableLockProvider extends AbstractLockProvider {
@Override
protected boolean doUnlock(LockConfig lockConfig) {
return doUnlockWithDelete(lockConfig);
protected void doUnlock(LockConfig lockConfig) {
doUnlockWithDelete(lockConfig);
}
protected boolean doUnlockWithDelete(LockConfig lockConfig) {

View File

@ -21,7 +21,6 @@ public final class LockBuilder {
return new LockBuilder();
}
public LockBuilder withResident(String lockName) {
this.lockName = lockName;
resident = Boolean.TRUE;

View File

@ -1,8 +1,6 @@
package com.aizuda.easy.retry.server.common.lock;
import com.aizuda.easy.retry.server.common.dto.LockConfig;
import java.time.Duration;
/**
@ -16,6 +14,6 @@ public interface LockProvider {
boolean lock(Duration lockAtMost);
boolean unlock();
void unlock();
}

View File

@ -13,13 +13,13 @@ public class ResidentLockProvider extends AbstractLockProvider {
@Override
protected boolean doUnlock(LockConfig lockConfig) {
return doUnlockWithUpdate(lockConfig);
protected void doUnlock(LockConfig lockConfig) {
doUnlockWithUpdate(lockConfig);
}
protected boolean doUnlockWithUpdate(LockConfig lockConfig) {
protected void doUnlockWithUpdate(LockConfig lockConfig) {
LockStorage lockStorage = LockStorageFactory.getLockStorage();
return lockStorage.releaseLockWithUpdate(lockConfig.getLockName(), lockConfig.getLockAtLeast());
lockStorage.releaseLockWithUpdate(lockConfig.getLockName(), lockConfig.getLockAtLeast());
}

View File

@ -1,9 +1,8 @@
package com.aizuda.easy.retry.server.common.lock.persistence;
import com.aizuda.easy.retry.common.core.log.LogUtils;
import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.server.common.Lifecycle;
import com.aizuda.easy.retry.server.common.cache.CacheLockRecord;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.dto.LockConfig;
import com.aizuda.easy.retry.server.common.register.ServerRegister;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
@ -11,7 +10,8 @@ import com.aizuda.easy.retry.template.datasource.persistence.mapper.DistributedL
import com.aizuda.easy.retry.template.datasource.persistence.po.DistributedLock;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
@ -32,19 +32,18 @@ import java.util.List;
* @since 2.1.0
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@RequiredArgsConstructor
@Slf4j
public class JdbcLockProvider implements LockStorage, Lifecycle {
private final DistributedLockMapper distributedLockMapper;
private final SystemProperties systemProperties;
protected static final List<String> ALLOW_DB = Arrays.asList(DbTypeEnum.MYSQL.getDb(),
DbTypeEnum.MARIADB.getDb(),
DbTypeEnum.POSTGRES.getDb());
@Override
public boolean supports(final String storageMedium) {
return ALLOW_DB.contains(systemProperties.getDbType().getDb());
return ALLOW_DB.contains(storageMedium);
}
@Override
@ -62,7 +61,7 @@ public class JdbcLockProvider implements LockStorage, Lifecycle {
} catch (DuplicateKeyException | ConcurrencyFailureException | TransactionSystemException e) {
return false;
} catch (DataIntegrityViolationException | BadSqlGrammarException | UncategorizedSQLException e) {
LogUtils.error(log, "Unexpected exception. lockName:[{}]", lockConfig.getLockName(), e);
EasyRetryLog.LOCAL.error("Unexpected exception. lockName:[{}]", lockConfig.getLockName(), e);
return false;
}
}
@ -94,7 +93,7 @@ public class JdbcLockProvider implements LockStorage, Lifecycle {
return distributedLockMapper.delete(new LambdaUpdateWrapper<DistributedLock>()
.eq(DistributedLock::getName, lockName)) > 0;
} catch (Exception e) {
LogUtils.error(log, "unlock error. retrying attempt [{}] ", i, e);
EasyRetryLog.LOCAL.error("unlock error. retrying attempt [{}] ", i, e);
}
}
@ -114,7 +113,7 @@ public class JdbcLockProvider implements LockStorage, Lifecycle {
return distributedLockMapper.update(distributedLock, new LambdaUpdateWrapper<DistributedLock>()
.eq(DistributedLock::getName, lockName)) > 0;
} catch (Exception e) {
LogUtils.error(log, "unlock error. retrying attempt [{}] ", i, e);
EasyRetryLog.LOCAL.error("unlock error. retrying attempt [{}] ", i, e);
}
}

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.server.common.schedule;
import cn.hutool.core.lang.Assert;
import com.aizuda.easy.retry.common.core.log.LogUtils;
import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.server.common.Schedule;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.exception.EasyRetryServerException;
@ -26,10 +27,6 @@ public abstract class AbstractSchedule implements Schedule {
@Autowired
@Qualifier("scheduledExecutorService")
protected TaskScheduler taskScheduler;
@Autowired
private List<LockProvider> lockProviders;
@Autowired
private SystemProperties systemProperties;
@Override
public void execute() {

View File

@ -7,6 +7,7 @@ import com.aizuda.easy.retry.common.core.enums.JobOperationReasonEnum;
import com.aizuda.easy.retry.common.core.enums.JobTaskBatchStatusEnum;
import com.aizuda.easy.retry.common.core.enums.JobTaskStatusEnum;
import com.aizuda.easy.retry.common.core.enums.WorkflowNodeTypeEnum;
import com.aizuda.easy.retry.common.core.log.LogUtils;
import com.aizuda.easy.retry.common.core.util.JsonUtil;
import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.server.common.akka.ActorGenerator;
@ -91,7 +92,9 @@ public class CallbackWorkflowExecutor extends AbstractWorkflowExecutor {
result = exchange.getBody();
log.info("回调结果. webHook:[{}],参数: [{}]", decisionConfig.getWebhook(), result);
} catch (Exception e) {
log.error("回调异常. webHook:[{}],参数: [{}]", decisionConfig.getWebhook(), context.getTaskResult(), e);
// log.error("回调异常. webHook:[{}],参数: [{}]", decisionConfig.getWebhook(), context.getTaskResult(), e);
EasyRetryLog.LOCAL.error("回调异常. webHook:[{}],参数: [{}]", decisionConfig.getWebhook(), context.getTaskResult(), e);
taskBatchStatus = JobTaskBatchStatusEnum.FAIL.getStatus();
operationReason = JobOperationReasonEnum.WORKFLOW_CALLBACK_NODE_EXECUTOR_ERROR.getReason();
jobTaskStatus = JobTaskStatusEnum.FAIL.getStatus();