Git Product home page Git Product logo

Comments (1)

glmapper avatar glmapper commented on July 26, 2024 1

目前zk在做服务注册发现的时候操作RegistryDataCache是在childEvent中调用的。

在这种情况下,无论是生产者还是消费者,其增删改事件都是在一个watcher线程顺序消费的,因此RegistryDataCache的实现可以不是线程安全的,且不存在线程安全问题。

但是registry那边的实现,是在Controller(tomcat线程池)中跟新cache

/**
 * 获取服务列表
 *
 * @return
 */
@GetMapping("all")
public List<ServiceModel> queryServiceList() {
    doRefreshCache();
    List<ServiceModel> data = new ArrayList<>();
    Map<String, RpcService> rpcServices = registryDataCache.fetchService();
    for (Map.Entry<String, RpcService> rpcServiceEntry : rpcServices.entrySet()) {
        final String serviceName = rpcServiceEntry.getKey();
        ServiceModel model = fetchServiceModel(serviceName);
        data.add(model);
    }

    return data;
}

此时由于RegistryDataCache实现地非线程安全,就可能会出现问题。

解决方案是让RegistryDataCache实现变得线程安全,比如使用ConcurrentHashMap#compute更新缓存map,或者添加轻量级锁比如stampedlock等。

比如:

@Override
public void addProviders(String serviceName, List<RpcProvider> providerList) {

    RpcService rpcService = services.get(serviceName);

    if (rpcService == null) {
        LOGGER.warn("receive provider registry data add, but service name is not exist now,{}",
            serviceName);
        return;
    }

    providers.compute(rpcService, (key, origin) -> { //This might be thread-safe
        if (origin == null) {
            return providerList;
        }
        List<RpcProvider> aggregate = new ArrayList<>(origin);
        aggregate.addAll(providerList);
        return aggregate;
    });
    LOGGER.info("receive provider registry data add, data is {}", providerList);
}

@chpengzh 感谢关注。这里其实确实会有线程安全问题:

  • 对于 doRefreshCache 操作只会在 注册中心是 SOFARegitry 时才会对缓存操作起作用,对于 Zookeeper 则不会。
  • 在 SOFARegitry 作为注册中心的情况,每次进入页面,都会对当前缓存进行清空,然后再拉取注册中心session数据,在多线程场景下,会引发线程安全问题。

关于这个点,还需要评估下是否在使用 SOFARegitry 情况下继续沿用 cache 机制,如果 SOFARegitry 本身数据了很大,对 Dashboard 来说缓存会造成一定的负担。后面会和 SOFARegitry 同学沟通优化方案

from sofa-dashboard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.