SofaRegistrySubscribeCallback implements SubscriberDataObserver, ConfigDataObserver { List<ProviderInfo> mergeProviderInfo(List<String> userDatas, List<String> configDatas) { List<ProviderInfo> providers = SofaRegistryHelper.parseProviderInfos(userDatas); if (CommonUtils.isNotEmpty(providers) && CommonUtils.isNotEmpty(configDatas)) { List<ProviderInfo> override = SofaRegistryHelper.parseProviderInfos(configDatas); Iterator<ProviderInfo> iterator = providers.iterator(); while (iterator.hasNext()) { ProviderInfo origin = iterator.next(); for (ProviderInfo over : override) { if (PROTOCOL_TYPE_OVERRIDE.equals(over.getProtocolType()) && StringUtils.equals(origin.getHost(), over.getHost()) && origin.getPort() == over.getPort()) { if (over.getWeight() != origin.getWeight()) { origin.setWeight(over.getWeight()); } if (CommonUtils.isTrue(over.getAttr(ProviderInfoAttrs.ATTR_DISABLED))) { if (LOGGER.isInfoEnabled()) { LOGGER.info("Provider is disabled by override. {}", origin.toUrl()); } iterator.remove(); } } } } } return providers; } SofaRegistrySubscribeCallback(); @Override void handleData(String dataId, UserData userData); int getListenerNum(); @Override void handleData(String dataId, ConfigData configData); static final String CONFIG_SEPARATOR; static final String DEFAULT_ZONE; }
@Test public void mergeProviderInfo() throws Exception { SofaRegistrySubscribeCallback callback = new SofaRegistrySubscribeCallback(); List<ProviderInfo> result = callback.mergeProviderInfo(null, null); Assert.assertTrue(CommonUtils.isEmpty(result)); List<String> listData = new ArrayList<String>(); result = callback.mergeProviderInfo(listData, null); Assert.assertTrue(CommonUtils.isEmpty(result)); List<String> attrData = new ArrayList<String>(); result = callback.mergeProviderInfo(null, attrData); Assert.assertTrue(CommonUtils.isEmpty(result)); result = callback.mergeProviderInfo(listData, attrData); Assert.assertTrue(CommonUtils.isEmpty(result)); attrData.add("override: result = callback.mergeProviderInfo(listData, attrData); Assert.assertTrue(CommonUtils.isEmpty(result)); attrData.clear(); listData.add("127.0.0.1:22000?weight=100"); listData.add("127.0.0.1:22001?weight=100"); result = callback.mergeProviderInfo(listData, attrData); Assert.assertTrue(result.size() == 2); for (ProviderInfo providerInfo : result) { if (providerInfo.getPort() == 22000) { Assert.assertTrue(providerInfo.getWeight() == 100); } else if (providerInfo.getPort() == 22001) { Assert.assertTrue(providerInfo.getWeight() == 100); } } attrData.add("override: attrData.add("override: result = callback.mergeProviderInfo(listData, attrData); Assert.assertTrue(result.size() == 2); for (ProviderInfo providerInfo : result) { if (providerInfo.getPort() == 22000) { Assert.assertTrue(providerInfo.getWeight() == 100); } else if (providerInfo.getPort() == 22001) { Assert.assertTrue(providerInfo.getWeight() == 100); } } attrData.add("override: attrData.add("override: result = callback.mergeProviderInfo(listData, attrData); Assert.assertTrue(result.size() == 1); for (ProviderInfo providerInfo : result) { if (providerInfo.getPort() == 22000) { Assert.assertTrue(providerInfo.getWeight() == 200); } } }