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

Refactor/syn to lock #1382

Closed
wants to merge 4 commits into from
Closed
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 @@ -554,13 +554,16 @@ public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) {
transport = uninitializedConnections.get(providerInfo);
if (transport != null) {
// 未初始化则初始化
synchronized (this) {
providerLock.lock();
try {
transport = uninitializedConnections.get(providerInfo);
if (transport != null) {
initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
uninitializedConnections.remove(providerInfo);
}
return getAvailableClientTransport(providerInfo);
} finally {
providerLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* 规则id的配置形式:a,b,!c,d
Expand Down Expand Up @@ -49,18 +51,21 @@ public abstract class BeanIdMatchFilter extends Filter {
private List<String> excludeId;

private volatile boolean formatComplete;
private final Object formatLock = new Object();
protected final Lock lock = new ReentrantLock();

@Override
public boolean needToLoad(FilterInvoker invoker) {
AbstractInterfaceConfig config = invoker.config;
String invokerId = config.getId();
if (!formatComplete) {
synchronized (formatLock) {
lock.lock();
try {
if (!formatComplete) {
formatId(idRule);
formatComplete = true;
}
} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author bystander
Expand All @@ -37,6 +39,11 @@ public class DynamicConfigManagerFactory {
*/
private final static ConcurrentMap<String, DynamicConfigManager> ALL_DYNAMICS = new ConcurrentHashMap<String, DynamicConfigManager>();

/**
* 类锁
*/
private final static Lock classLock = new ReentrantLock();

/**
* slf4j Logger for this class
*/
Expand All @@ -49,12 +56,13 @@ public class DynamicConfigManagerFactory {
* @param alias 别名
* @return DynamicManager 实现
*/
public static synchronized DynamicConfigManager getDynamicManager(String appName, String alias) {
public static DynamicConfigManager getDynamicManager(String appName, String alias) {
if (ALL_DYNAMICS.size() > 3) { // 超过3次 是不是配错了?
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Size of dynamic manager is greater than 3, Please check it!");
}
}
classLock.lock();
try {
// 注意:RegistryConfig重写了equals方法,如果多个RegistryConfig属性一样,则认为是一个对象
DynamicConfigManager registry = ALL_DYNAMICS.get(alias);
Expand All @@ -73,6 +81,8 @@ public static synchronized DynamicConfigManager getDynamicManager(String appName
} catch (Throwable e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "DynamicConfigManager", alias),
e);
} finally {
classLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author <a href="mailto:[email protected]">LiWei.Liangen</a>
Expand All @@ -36,10 +38,12 @@ public class RpcLookoutId {
private final ConcurrentMap<String, Id> serverConfigIds = new ConcurrentHashMap<String, Id>();

private volatile Id consumerConfigId;
private final Object consumerConfigIdLock = new Object();

private volatile Id providerConfigId;
private final Object providerConfigIdLock = new Object();

private static final Lock classLock = new ReentrantLock();
private final Lock consumerConfigIdLock = new ReentrantLock();
private final Lock providerConfigIdLock = new ReentrantLock();

/**
* create consumerId
Expand All @@ -51,12 +55,15 @@ public Id fetchConsumerStatId(Map<String, String> tags) {
String key = tags.toString();
Id lookoutId = consumerIds.get(key);
if (lookoutId == null) {
synchronized (RpcLookoutId.class) {
classLock.lock();
try {
lookoutId = consumerIds.get(key);
if (lookoutId == null) {
lookoutId = Lookout.registry().createId("rpc.consumer.service.stats", tags);
consumerIds.put(key, lookoutId);
}
} finally {
classLock.unlock();
}
}
return lookoutId;
Expand All @@ -71,34 +78,43 @@ public Id fetchProviderStatId(Map<String, String> tags) {
String key = tags.toString();
Id lookoutId = providerIds.get(key);
if (lookoutId == null) {
synchronized (RpcLookoutId.class) {
classLock.lock();
try {
lookoutId = providerIds.get(key);
if (lookoutId == null) {
lookoutId = Lookout.registry().createId("rpc.provider.service.stats", tags);
providerIds.put(key, lookoutId);
}
} finally {
classLock.unlock();
}
}
return lookoutId;
}

public Id fetchConsumerSubId() {
if (consumerConfigId == null) {
synchronized (consumerConfigIdLock) {
consumerConfigIdLock.lock();
try {
if (consumerConfigId == null) {
consumerConfigId = Lookout.registry().createId("rpc.consumer.info.stats");
}
} finally {
consumerConfigIdLock.unlock();
}
}
return consumerConfigId;
}

public Id fetchProviderPubId() {
if (providerConfigId == null) {
synchronized (providerConfigIdLock) {
providerConfigIdLock.lock();
try {
if (providerConfigId == null) {
providerConfigId = Lookout.registry().createId("rpc.provider.info.stats");
}
} finally {
providerConfigIdLock.unlock();
}
}
return providerConfigId;
Expand Down Expand Up @@ -127,12 +143,15 @@ public Id fetchServerThreadPoolQueueSizeId(ServerConfig serverConfig) {
private Id fetchServerConfigId(String key) {
Id lookoutId = serverConfigIds.get(key);
if (lookoutId == null) {
synchronized (RpcLookout.class) {
classLock.lock();
try {
lookoutId = serverConfigIds.get(key);
if (lookoutId == null) {
lookoutId = Lookout.registry().createId(key);
serverConfigIds.put(key, lookoutId);
}
} finally {
classLock.unlock();
}
}
return lookoutId;
Expand Down
Loading