Search in sources :

Example 1 with MigrationClusterInvoker

use of org.apache.dubbo.rpc.cluster.support.migration.MigrationClusterInvoker in project dubbo by alibaba.

the class ZoneAwareClusterInvoker method list.

@Override
protected List<Invoker<T>> list(Invocation invocation) throws RpcException {
    List<Invoker<T>> invokers = super.list(invocation);
    if (null == invokers || invokers.size() < 2) {
        return invokers;
    }
    List<Invoker<T>> interfaceInvokers = new ArrayList<>();
    List<Invoker<T>> serviceInvokers = new ArrayList<>();
    boolean addressChanged = false;
    for (Invoker<T> invoker : invokers) {
        MigrationClusterInvoker migrationClusterInvoker = (MigrationClusterInvoker) invoker;
        if (migrationClusterInvoker.isServiceInvoker()) {
            serviceInvokers.add(invoker);
        } else {
            interfaceInvokers.add(invoker);
        }
        if (migrationClusterInvoker.invokersChanged().compareAndSet(true, false)) {
            addressChanged = true;
        }
    }
    if (serviceInvokers.isEmpty() || interfaceInvokers.isEmpty()) {
        return invokers;
    }
    MigrationRule rule = null;
    for (Invoker<T> invoker : serviceInvokers) {
        MigrationClusterInvoker migrationClusterInvoker = (MigrationClusterInvoker) invoker;
        if (rule == null) {
            rule = migrationClusterInvoker.getMigrationRule();
            continue;
        }
        // inconsistency rule
        if (!rule.equals(migrationClusterInvoker.getMigrationRule())) {
            rule = MigrationRule.queryRule();
            break;
        }
    }
    MigrationStep step = rule.getStep();
    switch(step) {
        case FORCE_INTERFACE:
            clusterRefresh(addressChanged, interfaceInvokers);
            clusterDestroy(addressChanged, serviceInvokers, true);
            if (logger.isDebugEnabled()) {
                logger.debug("step is FORCE_INTERFACE");
            }
            return interfaceInvokers;
        case APPLICATION_FIRST:
            clusterRefresh(addressChanged, serviceInvokers);
            clusterRefresh(addressChanged, interfaceInvokers);
            boolean serviceAvailable = !serviceInvokers.isEmpty();
            if (serviceAvailable) {
                if (shouldMigrate(addressChanged, serviceInvokers, interfaceInvokers)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("step is APPLICATION_FIRST shouldMigrate true get serviceInvokers");
                    }
                    return serviceInvokers;
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("step is APPLICATION_FIRST " + (serviceInvokers.isEmpty() ? "serviceInvokers is empty" : "shouldMigrate false") + " get interfaceInvokers");
            }
            return interfaceInvokers;
        case FORCE_APPLICATION:
            clusterRefresh(addressChanged, serviceInvokers);
            clusterDestroy(addressChanged, interfaceInvokers, true);
            if (logger.isDebugEnabled()) {
                logger.debug("step is FORCE_APPLICATION");
            }
            return serviceInvokers;
    }
    throw new UnsupportedOperationException(rule.getStep().name());
}
Also used : MockClusterInvoker(org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker) Invoker(org.apache.dubbo.rpc.Invoker) MigrationClusterInvoker(org.apache.dubbo.rpc.cluster.support.migration.MigrationClusterInvoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) ClusterInvoker(org.apache.dubbo.rpc.cluster.ClusterInvoker) MigrationClusterInvoker(org.apache.dubbo.rpc.cluster.support.migration.MigrationClusterInvoker) ArrayList(java.util.ArrayList) MigrationStep(org.apache.dubbo.rpc.cluster.support.migration.MigrationStep) MigrationRule(org.apache.dubbo.rpc.cluster.support.migration.MigrationRule)

Aggregations

ArrayList (java.util.ArrayList)1 Invoker (org.apache.dubbo.rpc.Invoker)1 ClusterInvoker (org.apache.dubbo.rpc.cluster.ClusterInvoker)1 AbstractClusterInvoker (org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker)1 MigrationClusterInvoker (org.apache.dubbo.rpc.cluster.support.migration.MigrationClusterInvoker)1 MigrationRule (org.apache.dubbo.rpc.cluster.support.migration.MigrationRule)1 MigrationStep (org.apache.dubbo.rpc.cluster.support.migration.MigrationStep)1 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)1