All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.dubbo.rpc.cluster.router.xds.EdsEndpointManager Maven / Gradle / Ivy

There is a newer version: 3.3.0-beta.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.rpc.cluster.router.xds;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.registry.xds.util.protocol.message.Endpoint;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class EdsEndpointManager {

    private static final ConcurrentHashMap> ENDPOINT_LISTENERS = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap> ENDPOINT_DATA_CACHE = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap>> EDS_LISTENERS = new ConcurrentHashMap<>();


    public EdsEndpointManager() {
    }

    public synchronized void subscribeEds(String cluster, EdsEndpointListener listener) {

        Set listeners = ENDPOINT_LISTENERS.computeIfAbsent(cluster, key ->
            new ConcurrentHashSet<>()
        );
        if (CollectionUtils.isEmpty(listeners)) {
            doSubscribeEds(cluster);
        }
        listeners.add(listener);

        if (ENDPOINT_DATA_CACHE.containsKey(cluster)) {
            listener.onEndPointChange(cluster, ENDPOINT_DATA_CACHE.get(cluster));
        }
    }

    private void doSubscribeEds(String cluster) {
        EDS_LISTENERS.computeIfAbsent(cluster, key -> endpoints -> {
            notifyEndpointChange(cluster, endpoints);
        });
        Consumer> consumer = EDS_LISTENERS.get(cluster);

        //todo control plane subscribe eds
    }

    public synchronized void unSubscribeEds(String cluster, EdsEndpointListener listener) {
        Set listeners = ENDPOINT_LISTENERS.get(cluster);
        if (CollectionUtils.isEmpty(listeners)) {
            return;
        }
        listeners.remove(listener);
        if (listeners.isEmpty()) {
            ENDPOINT_LISTENERS.remove(cluster);
            doUnsubscribeEds(cluster);
        }
    }

    private void doUnsubscribeEds(String cluster) {
        Consumer> consumer = EDS_LISTENERS.remove(cluster);

        if (consumer != null) {

            //todo control plane unsubscribe eds
        }
        ENDPOINT_DATA_CACHE.remove(cluster);
    }


    public void notifyEndpointChange(String cluster, Set endpoints) {

        ENDPOINT_DATA_CACHE.put(cluster, endpoints);

        Set listeners = ENDPOINT_LISTENERS.get(cluster);
        if (CollectionUtils.isEmpty(listeners)) {
            return;
        }
        for (EdsEndpointListener listener : listeners) {
            listener.onEndPointChange(cluster, endpoints);
        }
    }

    // for test
    static ConcurrentHashMap> getEndpointListeners() {
        return ENDPOINT_LISTENERS;
    }

    // for test
    static ConcurrentHashMap> getEndpointDataCache() {
        return ENDPOINT_DATA_CACHE;
    }

    // for test
    static ConcurrentHashMap>> getEdsListeners() {
        return EDS_LISTENERS;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy