Skip to content
Open
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 @@ -36,12 +36,17 @@
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule;
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Predicate;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.BACKUP_TAG_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.BACKUP_TAG_RECENT_ROUTER;
import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_TAG_ROUTE_EMPTY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_TAG_ROUTE_INVALID;
Expand Down Expand Up @@ -215,11 +220,27 @@ private <T> BitList<Invoker<T>> filterUsingStaticTag(BitList<Invoker<T>> invoker
: invocation.getAttachment(TAG_KEY);
// Tag request
if (!StringUtils.isEmpty(tag)) {
// Start by looking at the dubbo.tags. Start by looking at the dubbo tag.
result = filterInvoker(
invokers,
invoker ->
ANY_VALUE.equals(tag) || tag.equals(invoker.getUrl().getParameter(TAG_KEY)));
if (CollectionUtils.isEmpty(result) && !isForceUseTag(invocation)) {
// If it is empty, then use the backup tag
if (CollectionUtils.isEmpty(result)) {
result = filterInvoker(
invokers,
invoker -> StringUtils.isContains(invoker.getUrl().getParameter(BACKUP_TAG_KEY), tag));
}
// Obtain the forceUseTag identifier.
boolean forceUseTag = isForceUseTag(invocation);
// If forced labeling is not enabled, and the nearest route is set to the backup label,
// This function is similar to dubbo.force.tag. It's used for recent searches.
if (CollectionUtils.isEmpty(result) && isRecentUseBackupTag(invocation) && !forceUseTag) {
result = filterRecentBackupTag(invokers, tag);
}

// Not mandatory, chosen randomly
if (CollectionUtils.isEmpty(result) && !forceUseTag) {
result = filterInvoker(
invokers,
invoker -> StringUtils.isEmpty(invoker.getUrl().getParameter(TAG_KEY)));
Expand All @@ -231,6 +252,33 @@ private <T> BitList<Invoker<T>> filterUsingStaticTag(BitList<Invoker<T>> invoker
return result;
}

private static <T> BitList<Invoker<T>> filterRecentBackupTag(BitList<Invoker<T>> invokers, String tag) {
BitList<Invoker<T>> result = null;
TreeMap<String, ArrayList<Invoker<T>>> sort = new TreeMap<>();
invokers.forEach(invoker -> {
String backupTags = invoker.getUrl().getParameter(BACKUP_TAG_KEY);
if (StringUtils.isBlank(backupTags)) {
return;
}
String[] backupTagArr = backupTags.split(",");
for (String backupTag : backupTagArr) {
if (StringUtils.isBlank(backupTag)) {
continue;
}
ArrayList<Invoker<T>> bitList = sort.computeIfAbsent(backupTag, k -> new ArrayList<>());
bitList.add(invoker);
}
});

Entry<String, ArrayList<Invoker<T>>> ceilingEntries = sort.ceilingEntry(tag);

// Reassigned local variable
if (ceilingEntries != null && CollectionUtils.isNotEmpty(ceilingEntries.getValue())) {
result = new BitList<>(ceilingEntries.getValue());
}
return result;
}

@Override
public boolean isRuntime() {
return tagRouterRule != null && tagRouterRule.isRuntime();
Expand All @@ -247,6 +295,11 @@ private boolean isForceUseTag(Invocation invocation) {
invocation.getAttachment(FORCE_USE_TAG, this.getUrl().getParameter(FORCE_USE_TAG, "false")));
}

private boolean isRecentUseBackupTag(Invocation invocation) {
return Boolean.parseBoolean(invocation.getAttachment(
BACKUP_TAG_RECENT_ROUTER, this.getUrl().getParameter(BACKUP_TAG_RECENT_ROUTER, "false")));
}

private <T> BitList<Invoker<T>> filterInvoker(BitList<Invoker<T>> invokers, Predicate<Invoker<T>> predicate) {
if (invokers.stream().allMatch(predicate)) {
return invokers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.apache.dubbo.common.constants.CommonConstants.ALIVE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.BACKUP_TAG_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CORE_THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
Expand Down Expand Up @@ -94,6 +95,9 @@ public URL mergeUrl(URL remoteUrl, Map<String, String> localParametersMap) {
copyOfLocalMap.remove(METHODS_KEY);
copyOfLocalMap.remove(TIMESTAMP_KEY);
copyOfLocalMap.remove(TAG_KEY);
// backup tag is a provider-side attribute, keep the provider's value
// and do not let consumer reference config override it.
copyOfLocalMap.remove(BACKUP_TAG_KEY);

map.putAll(copyOfLocalMap);

Expand Down
Loading
Loading