[ISSUE #68]Use multithreading for topic data collection in collectTask (#69)

* [ISSUE #68]Use multithreading for topic data collection in collectTask

* modify ut

* Optimize exception log printing

Co-authored-by: zhangjidi <zhangjidi@cmss.chinamobile.com>
This commit is contained in:
zhangjidi2016
2022-02-11 09:27:53 +08:00
committed by GitHub
parent d2da9ca41f
commit 653332d488
8 changed files with 271 additions and 94 deletions

View File

@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.dashboard.config;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert;
import org.junit.Test;
public class CollectExecutorConfigTest {
private final static int COUNT = 100;
@Test
public void testCollectExecutor() throws Exception {
AtomicInteger num = new AtomicInteger(0);
CollectExecutorConfig config = new CollectExecutorConfig();
config.setCoreSize(10);
config.setMaxSize(10);
config.setQueueSize(500);
config.setKeepAliveTime(3000);
ExecutorService collectExecutor = config.collectExecutor(config);
Assert.assertNotNull(collectExecutor);
CountDownLatch countDownLatch = new CountDownLatch(COUNT);
for (int i = 0; i < COUNT; i++) {
collectExecutor.submit(() -> {
num.getAndIncrement();
countDownLatch.countDown();
});
}
countDownLatch.await();
System.out.println(collectExecutor.isTerminated());
Assert.assertEquals(COUNT, num.get());
}
}

View File

@@ -30,6 +30,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.protocol.body.BrokerStatsData;
import org.apache.rocketmq.common.protocol.body.ClusterInfo;
@@ -38,6 +40,7 @@ import org.apache.rocketmq.common.protocol.body.KVTable;
import org.apache.rocketmq.common.protocol.body.TopicList;
import org.apache.rocketmq.common.protocol.route.TopicRouteData;
import org.apache.rocketmq.dashboard.BaseTest;
import org.apache.rocketmq.dashboard.config.CollectExecutorConfig;
import org.apache.rocketmq.dashboard.config.RMQConfigure;
import org.apache.rocketmq.dashboard.service.impl.DashboardCollectServiceImpl;
import org.apache.rocketmq.dashboard.util.JsonUtil;
@@ -68,6 +71,9 @@ public class DashboardCollectTaskTest extends BaseTest {
@Mock
private RMQConfigure rmqConfigure;
@Mock
private ExecutorService collectExecutor;
private int taskExecuteNum = 10;
private File brokerFile;
@@ -96,6 +102,7 @@ public class DashboardCollectTaskTest extends BaseTest {
{
TopicList topicList = new TopicList();
Set<String> topicSet = new HashSet<>();
topicSet.add("rmq_sys_xxx");
topicSet.add("topic_test");
topicSet.add("%RETRY%group_test");
topicSet.add("%DLQ%group_test");
@@ -121,19 +128,35 @@ public class DashboardCollectTaskTest extends BaseTest {
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "fetchAllTopicList exception");
}
dashboardCollectTask.collectTopic();
// multiple topic collection
CollectExecutorConfig config = new CollectExecutorConfig();
config.setCoreSize(10);
config.setMaxSize(10);
config.setQueueSize(500);
config.setKeepAliveTime(3000);
ExecutorService collectExecutor = config.collectExecutor(config);
for (int i = 0; i < taskExecuteNum; i++) {
dashboardCollectTask.collectTopic();
CollectTaskRunnble collectTask = new CollectTaskRunnble("topic_test" + i, mqAdminExt, dashboardCollectService);
collectExecutor.submit(collectTask);
}
collectExecutor.shutdown();
boolean loop = true;
do {
// Wait for all collectTasks to complete
loop = !collectExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
}
while (loop);
LoadingCache<String, List<String>> map = dashboardCollectService.getTopicMap();
Assert.assertEquals(map.size(), 1);
Assert.assertEquals(map.get("topic_test").size(), taskExecuteNum);
Assert.assertEquals(map.size(), taskExecuteNum);
dashboardCollectTask.saveData();
Assert.assertEquals(topicFile.exists(), true);
Map<String, List<String>> topicData =
JsonUtil.string2Obj(MixAll.file2String(topicFile),
new TypeReference<Map<String, List<String>>>() {
});
Assert.assertEquals(topicData.get("topic_test").size(), taskExecuteNum);
Assert.assertEquals(topicData.size(), taskExecuteNum);
}
@Test
@@ -187,8 +210,8 @@ public class DashboardCollectTaskTest extends BaseTest {
private void mockBrokerFileExistBeforeSaveData() throws Exception {
Map<String, List<String>> map = new HashMap<>();
map.put("broker-a" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
map.put("broker-b" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
map.put("broker-a" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
map.put("broker-b" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
MixAll.string2File(JsonUtil.obj2String(map), brokerFile.getAbsolutePath());
}
}