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

[3.0] fix urlInvokerMap concurrent access issue #8701

Merged
merged 15 commits into from
Sep 21, 2021
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 @@ -130,4 +130,6 @@ public interface RegistryConstants {

String REGISTRY_SERVICE_REFERENCE_PATH = "org.apache.dubbo.registry.RegistryService";
String INIT = "INIT";

float DEFAULT_HASHMAP_LOAD_FACTOR = 0.75f;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_HASHMAP_LOAD_FACTOR;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_TYPE_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE;
Expand Down Expand Up @@ -126,7 +128,7 @@ public void buildRouterChain(URL url) {

@Override
public boolean isAvailable() {
if (isDestroyed()) {
if (isDestroyed() || this.forbidden) {
return false;
}
Map<String, Invoker<T>> localUrlInvokerMap = urlInvokerMap;
Expand Down Expand Up @@ -248,12 +250,18 @@ private void refreshInvoker(List<URL> invokerUrls) {
destroyAllInvokers(); // Close all invokers
} else {
this.forbidden = false; // Allow accessing
Map<String, Invoker<T>> oldUrlInvokerMap = this.urlInvokerMap; // local reference
if (CollectionUtils.isEmpty(invokerUrls)) {
return;
}

Map<String, Invoker<T>> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map
// can't use local reference because this.urlInvokerMap might be accessed at isAvailable() by main thread concurrently.
Map<String, Invoker<T>> oldUrlInvokerMap = null;
if (this.urlInvokerMap != null) {
// the initial capacity should be set greater than the maximum number of entries divided by the load factor to avoid resizing.
oldUrlInvokerMap = new LinkedHashMap<>(Math.round(1 + this.urlInvokerMap.size() / DEFAULT_HASHMAP_LOAD_FACTOR));
this.urlInvokerMap.forEach(oldUrlInvokerMap::put);
}
Map<String, Invoker<T>> newUrlInvokerMap = toInvokers(oldUrlInvokerMap, invokerUrls);// Translate url list to Invoker map
logger.info("Refreshed invoker size " + newUrlInvokerMap.size());

if (CollectionUtils.isEmptyMap(newUrlInvokerMap)) {
Expand Down Expand Up @@ -282,11 +290,13 @@ private void refreshInvoker(List<URL> invokerUrls) {

/**
* Turn urls into invokers, and if url has been refer, will not re-reference.
* the items that will be put into newUrlInvokeMap will be removed from oldUrlInvokerMap.
*
* @param oldUrlInvokerMap
* @param urls
* @return invokers
*/
private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
private Map<String, Invoker<T>> toInvokers(Map<String, Invoker<T>> oldUrlInvokerMap, List<URL> urls) {
Map<String, Invoker<T>> newUrlInvokerMap = new HashMap<>();
if (urls == null || urls.isEmpty()) {
return newUrlInvokerMap;
Expand All @@ -311,7 +321,7 @@ private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
instanceAddressURL = overrideWithConfigurator(instanceAddressURL);
}

Invoker<T> invoker = urlInvokerMap == null ? null : urlInvokerMap.get(instanceAddressURL.getAddress());
Invoker<T> invoker = oldUrlInvokerMap == null ? null : oldUrlInvokerMap.get(instanceAddressURL.getAddress());
if (invoker == null || urlChanged(invoker, instanceAddressURL)) { // Not in the cache, refer again
try {
boolean enabled = true;
Expand All @@ -331,7 +341,7 @@ private Map<String, Invoker<T>> toInvokers(List<URL> urls) {
}
} else {
newUrlInvokerMap.put(instanceAddressURL.getAddress(), invoker);
urlInvokerMap.remove(instanceAddressURL.getAddress(), invoker);
oldUrlInvokerMap.remove(instanceAddressURL.getAddress(), invoker);
}
}
return newUrlInvokerMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -69,6 +70,7 @@
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_HASHMAP_LOAD_FACTOR;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
Expand Down Expand Up @@ -209,7 +211,7 @@ private void refreshInvoker(List<URL> invokerUrls) {
destroyAllInvokers(); // Close all invokers
} else {
this.forbidden = false; // Allow to access
Map<URL, Invoker<T>> oldUrlInvokerMap = this.urlInvokerMap; // local reference

if (invokerUrls == Collections.<URL>emptyList()) {
invokerUrls = new ArrayList<>();
}
Expand All @@ -222,7 +224,15 @@ private void refreshInvoker(List<URL> invokerUrls) {
if (invokerUrls.isEmpty()) {
return;
}
Map<URL, Invoker<T>> newUrlInvokerMap = toInvokers(invokerUrls);// Translate url list to Invoker map

// can't use local reference because this.urlInvokerMap might be accessed at isAvailable() by main thread concurrently.
Map<URL, Invoker<T>> oldUrlInvokerMap = null;
if (this.urlInvokerMap != null) {
// the initial capacity should be set greater than the maximum number of entries divided by the load factor to avoid resizing.
oldUrlInvokerMap = new LinkedHashMap<>(Math.round(1 + this.urlInvokerMap.size() / DEFAULT_HASHMAP_LOAD_FACTOR));
this.urlInvokerMap.forEach(oldUrlInvokerMap::put);
}
Map<URL, Invoker<T>> newUrlInvokerMap = toInvokers(oldUrlInvokerMap, invokerUrls);// Translate url list to Invoker map

/**
* If the calculation is wrong, it is not processed.
Expand Down Expand Up @@ -313,11 +323,13 @@ private Optional<List<Router>> toRouters(List<URL> urls) {

/**
* Turn urls into invokers, and if url has been refer, will not re-reference.
* the items that will be put into newUrlInvokeMap will be removed from oldUrlInvokerMap.
*
* @param oldUrlInvokerMap
* @param urls
* @return invokers
*/
private Map<URL, Invoker<T>> toInvokers(List<URL> urls) {
private Map<URL, Invoker<T>> toInvokers(Map<URL, Invoker<T>> oldUrlInvokerMap, List<URL> urls) {
Map<URL, Invoker<T>> newUrlInvokerMap = new ConcurrentHashMap<>();
if (urls == null || urls.isEmpty()) {
return newUrlInvokerMap;
Expand Down Expand Up @@ -351,8 +363,7 @@ private Map<URL, Invoker<T>> toInvokers(List<URL> urls) {
URL url = mergeUrl(providerUrl);

// Cache key is url that does not merge with consumer side parameters, regardless of how the consumer combines parameters, if the server url changes, then refer again
Map<URL, Invoker<T>> localUrlInvokerMap = this.urlInvokerMap; // local reference
Invoker<T> invoker = localUrlInvokerMap == null ? null : localUrlInvokerMap.remove(url);
Invoker<T> invoker = oldUrlInvokerMap == null ? null : oldUrlInvokerMap.remove(url);
if (invoker == null) { // Not in the cache, refer again
try {
boolean enabled = true;
Expand Down Expand Up @@ -567,7 +578,7 @@ public void setRegisteredConsumerUrl(URL url) {

@Override
public boolean isAvailable() {
if (isDestroyed()) {
if (isDestroyed() || this.forbidden) {
return false;
}
Map<URL, Invoker<T>> localUrlInvokerMap = urlInvokerMap;
Expand Down