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

[ISSUE #7]not send nearby if local mq less than threshold #11

Merged
merged 1 commit into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
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 @@ -58,6 +58,8 @@ public class DeFiBusClientConfig {
private long pullTimeDelayMillsWhenFlowControl = 50;
private long pullTimeDelayMillsWhenSuspend = 500;

private int minMqNumWhenSendLocal = 1;

public String getProducerGroup() {
return producerGroup;
}
Expand Down Expand Up @@ -270,6 +272,14 @@ public void setPullTimeDelayMillsWhenSuspend(long pullTimeDelayMillsWhenSuspend)
this.pullTimeDelayMillsWhenSuspend = pullTimeDelayMillsWhenSuspend;
}

public int getMinMqNumWhenSendLocal() {
return minMqNumWhenSendLocal;
}

public void setMinMqNumWhenSendLocal(int minMqNumWhenSendLocal) {
this.minMqNumWhenSendLocal = minMqNumWhenSendLocal;
}

@Override public String toString() {
return "DeFiBusClientConfig{" +
"producerGroup='" + producerGroup + '\'' +
Expand Down Expand Up @@ -299,6 +309,7 @@ public void setPullTimeDelayMillsWhenSuspend(long pullTimeDelayMillsWhenSuspend)
", pullTimeDelayMillsWhenExcept=" + pullTimeDelayMillsWhenExcept +
", pullTimeDelayMillsWhenFlowControl=" + pullTimeDelayMillsWhenFlowControl +
", pullTimeDelayMillsWhenSuspend=" + pullTimeDelayMillsWhenSuspend +
", minMqNumWhenSendLocal=" + minMqNumWhenSendLocal +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public class DeFiBusProducerImpl {
public DeFiBusProducerImpl(DeFiBusProducer deFiBusProducer, DeFiBusClientConfig deFiBusClientConfig,
DeFiBusClientInstance deFiBusClientInstance) {
this.deFiBusProducer = deFiBusProducer;
this.messageQueueSelector = new HealthyMessageQueueSelector(new MessageQueueHealthManager(deFiBusClientConfig.getQueueIsolateTimeMillis()));
this.messageQueueSelector = new HealthyMessageQueueSelector(new MessageQueueHealthManager(deFiBusClientConfig.getQueueIsolateTimeMillis()),
deFiBusClientConfig.getMinMqNumWhenSendLocal());

executorService = deFiBusClientInstance.getExecutorService();
scheduledExecutorService = deFiBusClientInstance.getScheduledExecutorService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public class HealthyMessageQueueSelector implements MessageQueueSelector {
private final AtomicInteger sendWhichLocalQueue = new AtomicInteger(0);
private final AtomicInteger sendWhichRemoteQueue = new AtomicInteger(0);
private final MessageQueueHealthManager messageQueueHealthManager;
private int minMqCountWhenSendLocal = 1;
private Map<String, Boolean> sendNearbyMapping = new HashMap<>();
private Set<String> localBrokers = new HashSet<String>();

public HealthyMessageQueueSelector(MessageQueueHealthManager messageQueueHealthManager) {
public HealthyMessageQueueSelector(MessageQueueHealthManager messageQueueHealthManager, int minMqCountWhenSendLocal) {
this.messageQueueHealthManager = messageQueueHealthManager;
this.minMqCountWhenSendLocal = minMqCountWhenSendLocal;
}

@Override
Expand All @@ -61,7 +63,14 @@ public MessageQueue select(List<MessageQueue> mqs, Message msg, final Object sel
if (pub2local) {
List<MessageQueue> localMQs = new ArrayList<>();
List<MessageQueue> remoteMqs = new ArrayList<>();
separateLocalAndRemoteMQs(mqs, localBrokers, localMQs, remoteMqs);
HashMap<String, Integer> localBrokerMQCount = separateLocalAndRemoteMQs(mqs, localBrokers, localMQs, remoteMqs);

for (String brokerName : localBrokerMQCount.keySet()) {
//if MQ num less than threshold, send msg to all broker
if (localBrokerMQCount.get(brokerName) <= minMqCountWhenSendLocal) {
localMQs.addAll(remoteMqs);
}
}

//try select a mq from local idc first
MessageQueue candidate = selectMessageQueue(localMQs, sendWhichLocalQueue, lastOne, msg);
Expand Down Expand Up @@ -154,20 +163,26 @@ private List<MessageQueue> filterMqsByBrokerName(final List<MessageQueue> mqs, S
return result;
}

private void separateLocalAndRemoteMQs(List<MessageQueue> mqs, Set<String> localBrokers,
private HashMap<String, Integer> separateLocalAndRemoteMQs(List<MessageQueue> mqs, Set<String> localBrokers,
List<MessageQueue> localMQs, List<MessageQueue> remoteMQs) {
if (localMQs == null)
localMQs = new ArrayList<>();
if (remoteMQs == null)
remoteMQs = new ArrayList<>();

HashMap<String, Integer> brokerMQCount = new HashMap<>();
for (MessageQueue mq : mqs) {
if (localBrokers.contains(mq.getBrokerName())) {
localMQs.add(mq);
Integer count = brokerMQCount.get(mq.getBrokerName());
if (count == null) {
count = 0;
}
brokerMQCount.put(mq.getBrokerName(), count+1);
} else {
remoteMQs.add(mq);
}
}
return brokerMQCount;
}

public MessageQueueHealthManager getMessageQueueHealthManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testLocalValidQueue() {
// PowerMockito.when(producerImplMock.getLocalBrokers()).thenReturn(locBrokers);

MessageQueueHealthManager manager = new MessageQueueHealthManager(60 * 1000);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager, 1);
selector.setLocalBrokers(locBrokers);

List<MessageQueue> mqs = new ArrayList<>();
Expand All @@ -65,7 +65,7 @@ public void testErrorQueue() {
locBrokers.add("localIDC");

MessageQueueHealthManager manager = new MessageQueueHealthManager(60 * 1000);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager, 1);
selector.setLocalBrokers(locBrokers);

List<MessageQueue> mqs = new ArrayList<>();
Expand All @@ -92,7 +92,7 @@ public void testOtherValidQueue() {
locBrokers.add("localIDC");

MessageQueueHealthManager manager = new MessageQueueHealthManager(60 * 1000);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager, 1);
selector.setLocalBrokers(locBrokers);

List<MessageQueue> mqs = new ArrayList<>();
Expand Down Expand Up @@ -122,7 +122,7 @@ public void testBizTopic() {
localBrokers.add(localBrokerName);

MessageQueueHealthManager manager = new MessageQueueHealthManager(60 * 1000);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager, 1);
selector.setLocalBrokers(localBrokers);

//construct mq data
Expand Down Expand Up @@ -193,7 +193,7 @@ public void testRetryBizTopic() {
}

MessageQueueHealthManager manager = new MessageQueueHealthManager(60 * 1000);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager);
HealthyMessageQueueSelector selector = new HealthyMessageQueueSelector(manager, 1);
selector.setLocalBrokers(localBrokers);

//construct mq data
Expand Down