enhancement #I49JP1 DataBus中SlotSize的大小不支持动态扩展,无法应对高并发下的流量突增

This commit is contained in:
bryan31
2021-09-20 22:22:25 +08:00
parent 2523e5ef3b
commit a052ad50ff
4 changed files with 15 additions and 5 deletions

View File

@@ -137,6 +137,9 @@ public class FlowExecutor {
throw new FlowExecutorNotInitException(errorMsg);
}
}
//初始化DataBus
DataBus.init();
}
/**

View File

@@ -32,14 +32,18 @@ public class DataBus {
//这里为什么采用ConcurrentHashMap作为slot存放的容器
//因为ConcurrentHashMap的随机取值复杂度也和数组一样为O(1),并且没有并发问题,还有自动扩容的功能
//用数组的话扩容涉及copy线程安全问题还要自己处理
private static final ConcurrentHashMap<Integer, Slot> SLOTS;
private static ConcurrentHashMap<Integer, Slot> SLOTS;
private static final ConcurrentLinkedQueue<Integer> QUEUE;
private static ConcurrentLinkedQueue<Integer> QUEUE;
//当前slot的下标index的最大值
private static Integer currentIndexMaxValue;
static {
//这里原先版本中是static块现在改成init静态方法由FlowExecutor中的init去调用
//这样的改动对项目来说没有什么实际意义,但是在单元测试中,却有意义。
//因为单元测试中所有的一起跑jvm是不退出的所以如果是static块的话跑多个testsuite只会执行一次。
//而由FlowExecutor中的init去调用是会被执行多次的。保证了每个单元测试都能初始化一遍
public static void init() {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
currentIndexMaxValue = liteflowConfig.getSlotSize();

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.springboot;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.entity.data.DataBus;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

View File

@@ -55,7 +55,9 @@ public class ResizeSlotSpringbootTest extends BaseTest {
Field field = ReflectUtil.getField(DataBus.class, "QUEUE");
ConcurrentLinkedQueue<Integer> queue = (ConcurrentLinkedQueue<Integer>) ReflectUtil.getStaticFieldValue(field);
//因为初始slotSize是4按照0.75的扩容比要满足100个线程应该扩容6次6次之后应该是扩容到114
Assert.assertEquals(queue.size(),114);
//因为初始slotSize是4按照0.75的扩容比要满足100个线程应该扩容5~6次5次=656次=114
//为什么不是直接114呢
//因为在单测中根据机器的性能在多线程情况下有些机器跑的慢一点也就是说65个就足够了。有些机器跑的快一点是能真正扩容到114个的
Assert.assertTrue(queue.size() ==65 || queue.size() == 114);
}
}