use of org.apache.dubbo.rpc.cluster.support.migration.MigrationRule 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());
}
use of org.apache.dubbo.rpc.cluster.support.migration.MigrationRule in project dubbo by alibaba.
the class MigrationRuleHandler method doMigrate.
public void doMigrate(String rawRule) {
MigrationRule rule = MigrationRule.parse(rawRule);
if (null != currentStep && currentStep.equals(rule.getStep())) {
if (logger.isInfoEnabled()) {
logger.info("Migration step is not change. rule.getStep is " + currentStep.name());
}
return;
} else {
currentStep = rule.getStep();
}
migrationInvoker.setMigrationRule(rule);
if (migrationInvoker.isMigrationMultiRegistry()) {
if (migrationInvoker.isServiceInvoker()) {
migrationInvoker.refreshServiceDiscoveryInvoker();
} else {
migrationInvoker.refreshInterfaceInvoker();
}
} else {
switch(rule.getStep()) {
case APPLICATION_FIRST:
migrationInvoker.migrateToServiceDiscoveryInvoker(false);
break;
case FORCE_APPLICATION:
migrationInvoker.migrateToServiceDiscoveryInvoker(true);
break;
case FORCE_INTERFACE:
default:
migrationInvoker.fallbackToInterfaceInvoker();
}
}
}
Aggregations