Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add createConsumers() #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,38 @@ protected void baseStartConsume(BaseMessageProcessor processor, int concurrency,
}

private void startConsume(BaseMessageProcessor processor, int concurrency, Map<String, Integer> extraConcurrency, List<Node> servers) {
int totalThreads = concurrency > 0 ? Math.max(concurrency, servers.size()) : 0;
int serverCnt = servers.size();
int totalThreads = concurrency > 0 ? Math.max(concurrency, serverCnt) : 0;
for (Integer topicConcurrency : extraConcurrency.values()) {
totalThreads += topicConcurrency > 0 ? Math.max(topicConcurrency, servers.size()) : 0;
totalThreads += topicConcurrency > 0 ? Math.max(topicConcurrency, serverCnt) : 0;
}
if (totalThreads == 0) {
throw new RuntimeException("concurrency is too small, at least one for each server.");
}
executorService = Executors.newFixedThreadPool(totalThreads, new ThreadFactoryBuilder().setNameFormat("MessageProcess-%d").build());
createConsumers(processor, concurrency, servers, null);
for (Map.Entry<String, Integer> entry : extraConcurrency.entrySet()) {
int c = entry.getValue();
String topic = entry.getKey();
createConsumers(processor, c, servers, topic);
}
}

Collections.shuffle(servers);

protected void createConsumers(BaseMessageProcessor processor, int concurrency, List<Node> servers, String topic) {
if (concurrency <= 0) {
return;
}
int serverCnt = servers.size();
if (concurrency > 0) {
if (concurrency < serverCnt) {
LOGGER.warn("concurrency({})<server number({}), use {} as concurrency", concurrency, serverCnt, serverCnt);
concurrency = serverCnt;
}
for (int i = 0; i < serverCnt; i++) {
int threadNumber = concurrency / serverCnt;
threadNumber += i < concurrency % serverCnt ? 1 : 0;
if (threadNumber == 0) {
LOGGER.warn("no thread for server:{}", servers.get(i));
} else {
createConsumer(processor, threadNumber, servers.get(i), null);
}
}
if (concurrency < serverCnt) {
LOGGER.warn("concurrency({})<server number({}), use {} as concurrency", concurrency, serverCnt, serverCnt);
concurrency = serverCnt;
}

for (Map.Entry<String, Integer> entry : extraConcurrency.entrySet()) {
int c = entry.getValue();
if (c == 0) continue;
if (c < serverCnt) {
LOGGER.warn("concurrency({})<server number({}), use {} as concurrency", c, serverCnt, serverCnt);
c = serverCnt;
}
Collections.shuffle(servers);
for (int i = 0; i < serverCnt; i++) {
int threadNumber = c / serverCnt;
threadNumber += i < c % serverCnt ? 1 : 0;
createConsumer(processor, threadNumber, servers.get(i), entry.getKey());
}
Collections.shuffle(servers);
int threadNumber = concurrency / serverCnt;
int threadExtraNumber = concurrency % serverCnt;
for (int i = 0; i < serverCnt; i++) {
int extra = i < threadExtraNumber ? 1 : 0;
createConsumer(processor, threadNumber + extra, servers.get(i), topic);
}
}

Expand Down