Search in sources :

Example 1 with ProviderGroup

use of com.alipay.sofa.rpc.client.ProviderGroup in project sofa-rpc by sofastack.

the class FaultToleranceSubscriber method onEvent.

@Override
public void onEvent(Event originEvent) {
    Class eventClass = originEvent.getClass();
    if (eventClass == ClientSyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 同步结果
        ClientSyncReceiveEvent event = (ClientSyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ClientAsyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 异步结果
        ClientAsyncReceiveEvent event = (ClientAsyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ProviderInfoRemoveEvent.class) {
        ProviderInfoRemoveEvent event = (ProviderInfoRemoveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderGroup providerGroup = event.getProviderGroup();
        if (!ProviderHelper.isEmpty(providerGroup)) {
            for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
                InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
            }
        }
    } else if (eventClass == ProviderInfoUpdateEvent.class) {
        ProviderInfoUpdateEvent event = (ProviderInfoUpdateEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroup(event.getOldProviderGroup(), event.getNewProviderGroup(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    } else if (eventClass == ProviderInfoUpdateAllEvent.class) {
        ProviderInfoUpdateAllEvent event = (ProviderInfoUpdateAllEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroups(event.getOldProviderGroups(), event.getNewProviderGroups(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    }
}
Also used : InvocationStat(com.alipay.sofa.rpc.client.aft.InvocationStat) ArrayList(java.util.ArrayList) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with ProviderGroup

use of com.alipay.sofa.rpc.client.ProviderGroup in project sofa-rpc by sofastack.

the class FaultBaseTest method getProviderInfoByHost.

static ProviderInfo getProviderInfoByHost(ConsumerConfig consumerConfig, String host) {
    ConsumerBootstrap consumerBootStrap = consumerConfig.getConsumerBootstrap();
    AddressHolder addressHolder = consumerBootStrap.getCluster().getAddressHolder();
    List<ProviderGroup> providerGroups = addressHolder.getProviderGroups();
    for (ProviderGroup providerGroup : providerGroups) {
        for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
            if (providerInfo.getHost().equals(host)) {
                return providerInfo;
            }
        }
    }
    return null;
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 3 with ProviderGroup

use of com.alipay.sofa.rpc.client.ProviderGroup in project sofa-rpc by sofastack.

the class FaultToleranceSubscriberTest method onProviderEvent.

@Test
public void onProviderEvent() {
    FaultToleranceConfig config = new FaultToleranceConfig();
    config.setRegulationEffective(true);
    FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
    ProviderInfo providerInfo1 = ProviderHelper.toProviderInfo("127.0.0.1");
    ProviderInfo providerInfo2 = ProviderHelper.toProviderInfo("127.0.0.2");
    ProviderInfo providerInfo3 = ProviderHelper.toProviderInfo("127.0.0.3");
    ProviderInfo providerInfo4 = ProviderHelper.toProviderInfo("127.0.0.4");
    ProviderInfo providerInfo5 = ProviderHelper.toProviderInfo("127.0.0.5");
    FaultToleranceSubscriber subscriber = new FaultToleranceSubscriber();
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo1, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo2, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo3, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo4, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 4);
    subscriber.onEvent(new ProviderInfoRemoveEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.3"), ProviderHelper.toProviderInfo("127.0.0.4"))), new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 2);
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo5, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateAllEvent(consumerConfig, Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))), Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1"), ProviderHelper.toProviderInfo("127.0.0.4"))))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 1);
}
Also used : ProviderInfoRemoveEvent(com.alipay.sofa.rpc.event.ProviderInfoRemoveEvent) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderInfoUpdateEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateEvent) ProviderInfoUpdateAllEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateAllEvent) FaultToleranceSubscriber(com.alipay.sofa.rpc.event.FaultToleranceSubscriber) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) Test(org.junit.Test)

Example 4 with ProviderGroup

use of com.alipay.sofa.rpc.client.ProviderGroup in project sofa-rpc by sofastack.

the class ZookeeperRegistryTest method testProviderObserver.

/**
 * 测试Zookeeper Provider Observer
 *
 * @throws Exception
 */
@Test
public void testProviderObserver() throws Exception {
    int timeoutPerSub = 2000;
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    // 注册
    registry.register(provider);
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    // 订阅
    CountDownLatch latch = new CountDownLatch(1);
    MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
    providerInfoListener.setCountDownLatch(latch);
    consumer.setProviderInfoListener(providerInfoListener);
    List<ProviderGroup> all = registry.subscribe(consumer);
    providerInfoListener.updateAllProviders(all);
    Map<String, ProviderInfo> ps = providerInfoListener.getData();
    Assert.assertEquals("after register: 1", 1, ps.size());
    // 订阅 错误的uniqueId
    ConsumerConfig<?> consumerNoUniqueId = new ConsumerConfig();
    consumerNoUniqueId.setInterfaceId(TEST_SERVICE_NAME).setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    latch = new CountDownLatch(1);
    MockProviderInfoListener providerInfoListener3 = new MockProviderInfoListener();
    providerInfoListener3.setCountDownLatch(latch);
    consumerNoUniqueId.setProviderInfoListener(providerInfoListener3);
    all = registry.subscribe(consumerNoUniqueId);
    providerInfoListener3.updateAllProviders(all);
    Map<String, ProviderInfo> ps3 = providerInfoListener3.getData();
    Assert.assertEquals("wrong uniqueId: 0", 0, ps3.size());
    // 反注册
    latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    registry.unRegister(provider);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Assert.assertEquals("after unregister: 0", 0, ps.size());
    // 一次发2个端口的再次注册
    latch = new CountDownLatch(2);
    providerInfoListener.setCountDownLatch(latch);
    provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
    registry.register(provider);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertEquals("after register two servers: 2", 2, ps.size());
    // 重复订阅
    ConsumerConfig<?> consumer2 = new ConsumerConfig();
    consumer2.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    CountDownLatch latch2 = new CountDownLatch(1);
    MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    providerInfoListener2.updateAllProviders(registry.subscribe(consumer2));
    latch2.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Map<String, ProviderInfo> ps2 = providerInfoListener2.getData();
    Assert.assertEquals("after register duplicate: 2", 2, ps2.size());
    // 取消订阅者1
    registry.unSubscribe(consumer);
    // 批量反注册,判断订阅者2的数据
    latch = new CountDownLatch(2);
    providerInfoListener2.setCountDownLatch(latch);
    List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
    providerConfigList.add(provider);
    registry.batchUnRegister(providerConfigList);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertEquals("after unregister: 0", 0, ps2.size());
    // 批量取消订阅
    List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
    consumerConfigList.add(consumer2);
    registry.batchUnSubscribe(consumerConfigList);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) BaseZkTest(com.alipay.sofa.rpc.registry.zk.base.BaseZkTest) Test(org.junit.Test)

Example 5 with ProviderGroup

use of com.alipay.sofa.rpc.client.ProviderGroup in project sofa-rpc by sofastack.

the class ZookeeperRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isSubscribe()) {
        // 注册中心不订阅
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return null;
    }
    // 订阅如果有必要
    subscribeConsumerUrls(config);
    if (config.isSubscribe()) {
        List<ProviderInfo> matchProviders;
        // 订阅配置
        if (!INTERFACE_CONFIG_CACHE.containsKey(buildConfigPath(rootPath, config))) {
            // 订阅接口级配置
            subscribeConfig(config, config.getConfigListener());
        }
        if (!INTERFACE_OVERRIDE_CACHE.containsKey(buildOverridePath(rootPath, config))) {
            // 订阅IP级配置
            subscribeOverride(config, config.getConfigListener());
        }
        // 订阅Providers节点
        try {
            if (providerObserver == null) {
                // 初始化
                providerObserver = new ZookeeperProviderObserver();
            }
            final String providerPath = buildProviderPath(rootPath, config);
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_SUB, providerPath));
            }
            PathChildrenCache pathChildrenCache = INTERFACE_PROVIDER_CACHE.get(config);
            if (pathChildrenCache == null) {
                // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
                ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
                providerObserver.addProviderListener(config, providerInfoListener);
                // TODO 换成监听父节点变化(只是监听变化了,而不通知变化了什么,然后客户端自己来拉数据的)
                pathChildrenCache = new PathChildrenCache(zkClient, providerPath, true);
                final PathChildrenCache finalPathChildrenCache = pathChildrenCache;
                pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

                    @Override
                    public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                        if (LOGGER.isDebugEnabled(config.getAppName())) {
                            LOGGER.debugWithApp(config.getAppName(), "Receive zookeeper event: " + "type=[" + event.getType() + "]");
                        }
                        switch(event.getType()) {
                            case // 加了一个provider
                            CHILD_ADDED:
                                providerObserver.addProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            case // 删了一个provider
                            CHILD_REMOVED:
                                providerObserver.removeProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            case // 更新一个Provider
                            CHILD_UPDATED:
                                providerObserver.updateProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            default:
                                break;
                        }
                    }
                });
                pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
                INTERFACE_PROVIDER_CACHE.put(config, pathChildrenCache);
            }
            List<ProviderInfo> providerInfos = ZookeeperRegistryHelper.convertUrlsToProviders(providerPath, pathChildrenCache.getCurrentData());
            matchProviders = ZookeeperRegistryHelper.matchProviderInfos(config, providerInfos);
        } catch (Exception e) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
        }
        if (EventBus.isEnable(ConsumerSubEvent.class)) {
            ConsumerSubEvent event = new ConsumerSubEvent(config);
            EventBus.post(event);
        }
        return Collections.singletonList(new ProviderGroup().addAll(matchProviders));
    }
    return null;
}
Also used : PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Aggregations

ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)46 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)24 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)16 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)10 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)6 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)6 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)6 HashMap (java.util.HashMap)6 List (java.util.List)5 Map (java.util.Map)4 File (java.io.File)3 AllConnectConnectionHolder (com.alipay.sofa.rpc.client.AllConnectConnectionHolder)2 ClientProxyInvoker (com.alipay.sofa.rpc.client.ClientProxyInvoker)2