From f23ad83b6da4d2dabcf1f8e3ef60bb172a4b634d Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Sat, 6 May 2023 17:17:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.2.0=201.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/sql/easy_retry.sql | 5 +- .../generator/id/SegmentIdGenerator.java | 6 +- .../generator/id/SegmentIdGeneratorTest.java | 60 +++++++++++++++++++ frontend/src/views/config/GroupList.vue | 4 ++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 easy-retry-server/src/test/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGeneratorTest.java diff --git a/doc/sql/easy_retry.sql b/doc/sql/easy_retry.sql index c372a61f..6f04039e 100644 --- a/doc/sql/easy_retry.sql +++ b/doc/sql/easy_retry.sql @@ -71,6 +71,7 @@ CREATE TABLE `retry_task_0` KEY `idx_group_name_scene_name` (`group_name`, `scene_name`), KEY `idx_retry_status` (`retry_status`), KEY `idx_idempotent_id` (`idempotent_id`), + KEY `idx_biz_no` (`biz_no`), UNIQUE KEY `uk_name_unique_id` (`group_name`, `unique_id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='重试表' ; @@ -93,7 +94,9 @@ CREATE TABLE `retry_task_log` PRIMARY KEY (`id`), KEY `idx_group_name_scene_name` (`group_name`, `scene_name`), KEY `idx_retry_status` (`retry_status`), - KEY `idx_idempotent_id` (`idempotent_id`) + KEY `idx_idempotent_id` (`idempotent_id`), + KEY `idx_unique_id` (`unique_id`), + KEY `idx_biz_no` (`biz_no`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='重试日志表' ; diff --git a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGenerator.java b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGenerator.java index 921996cf..12454992 100644 --- a/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGenerator.java +++ b/easy-retry-server/src/main/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGenerator.java @@ -20,11 +20,9 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -173,7 +171,7 @@ public class SegmentIdGenerator implements IdGenerator, Lifecycle { if (!buffer.isInitOk()) { try { updateSegmentFromDb(key, buffer.getCurrent()); - LogUtils.info(log, "Init buffer. Update leafkey {} {} from db", key, buffer.getCurrent()); + LogUtils.info(log, "Init buffer. Update key {} {} from db", key, buffer.getCurrent()); buffer.setInitOk(true); } catch (Exception e) { LogUtils.warn(log, "Init buffer {} exception", buffer.getCurrent(), e); @@ -293,7 +291,7 @@ public class SegmentIdGenerator implements IdGenerator, Lifecycle { roll += 1; if(roll > 10000) { try { - TimeUnit.MILLISECONDS.sleep(2); + TimeUnit.MILLISECONDS.sleep(20); break; } catch (InterruptedException e) { LogUtils.warn(log,"Thread {} Interrupted",Thread.currentThread().getName()); diff --git a/easy-retry-server/src/test/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGeneratorTest.java b/easy-retry-server/src/test/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGeneratorTest.java new file mode 100644 index 00000000..eb42343a --- /dev/null +++ b/easy-retry-server/src/test/java/com/aizuda/easy/retry/server/support/generator/id/SegmentIdGeneratorTest.java @@ -0,0 +1,60 @@ +package com.aizuda.easy.retry.server.support.generator.id; + +import com.aizuda.easy.retry.common.core.log.LogUtils; +import com.aizuda.easy.retry.server.exception.EasyRetryServerException; +import com.aizuda.easy.retry.server.support.generator.IdGenerator; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +/** + * 测试多线程情况下号段模式的运行情况 + * + * @author: shuguang.zhang + * @date : 2023-05-06 09:16 + * @since 1.2.0 + */ +@SpringBootTest +@Slf4j +public class SegmentIdGeneratorTest { + + @Autowired + @Qualifier("segmentIdGenerator") + private IdGenerator idGenerator; + + @Test + public void idGeneratorTest() throws InterruptedException { + + // step: 100, cpu: 4 memory: 16G, 线程1-150之间出现异常id:-3的情况较少。 + // 如果有特殊需求可以提高step, 可通过系统配置: SystemProperties#step属性进行配置 + Set idSet = new HashSet<>(); + int size = 500; + CountDownLatch count = new CountDownLatch(size); + for (int i = 0; i < size; i++) { + new Thread(new Runnable() { + @Override + public void run() { + count.countDown(); + + String id = idGenerator.idGenerator("example_group"); + LogUtils.info(log, "id:[{}]", id); + if (Long.parseLong(id) < 0) { + throw new EasyRetryServerException("exception id"); + } else if (idSet.contains(id)) { + throw new EasyRetryServerException("duplicate id [{}]", id); + } else { + idSet.add(id); + } + } + }).start(); + } + + count.await(); + } +} diff --git a/frontend/src/views/config/GroupList.vue b/frontend/src/views/config/GroupList.vue index e14fe78f..1f64eccd 100644 --- a/frontend/src/views/config/GroupList.vue +++ b/frontend/src/views/config/GroupList.vue @@ -102,6 +102,10 @@ export default { title: '分区', dataIndex: 'groupPartition', needTotal: true + }, + { + title: 'ID生成模式', + dataIndex: 'idGeneratorModeName' }, { title: '更新时间',