From aa8c321ffe3805e515f4eed26ece8d4b918077b2 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 27 Jun 2024 13:54:29 +0800 Subject: [PATCH] perf[persist]: reduce the pressure of concurrent writes to the database --- orm/src/main/java/com/zfoo/orm/cache/EntityCache.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java index d3eea459..427c79c7 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -28,6 +28,7 @@ import com.zfoo.protocol.exception.RunException; import com.zfoo.protocol.model.Pair; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.ThreadUtils; +import com.zfoo.scheduler.manager.SchedulerBus; import com.zfoo.scheduler.util.LazyCache; import com.zfoo.scheduler.util.TimeUtils; import org.slf4j.Logger; @@ -36,6 +37,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.stream.Collectors; @@ -262,6 +264,7 @@ public class EntityCache, E extends IEntity> imple } } }); + var count = 0; for (var entry : updateMap.entrySet()) { var threadId = entry.getKey(); var updateList = entry.getValue(); @@ -269,7 +272,8 @@ public class EntityCache, E extends IEntity> imple if (executor == null) { EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList)); } else { - executor.execute(() -> doPersist(updateList)); + // 使用scheduler均匀的分配入库的时间点,减少数据库的并发写入压力 + SchedulerBus.schedule(() -> executor.execute(() -> doPersist(updateList)), count++ * 100L, TimeUnit.MILLISECONDS); } } }