Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip mapping retry if metadata config is invalid #11323

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ protected void mapServiceName(URL url, ServiceNameMapping serviceNameMapping, Sc
} catch (Exception e) {
logger.error(CONFIG_SERVER_DISCONNECTED, "configuration server disconnected", "", "Failed register interface application mapping for service " + url.getServiceKey(), e);
}
if (!succeeded) {
if (!succeeded && serviceNameMapping.hasValidMetadataCenter()) {
scheduleToMapping(scheduledExecutor, serviceNameMapping, url);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@

package org.apache.dubbo.config;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.api.DemoService;
Expand All @@ -44,13 +36,21 @@
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.service.GenericService;

import com.google.common.collect.Lists;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
Expand Down Expand Up @@ -80,7 +80,10 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.withSettings;

class ServiceConfigTest {
Expand Down Expand Up @@ -568,6 +571,11 @@ public boolean map(URL url) {
return count.get() > 10;
}

@Override
public boolean hasValidMetadataCenter() {
return true;
}

@Override
public void initInterfaceAppMapping(URL subscribedURL) {

Expand Down Expand Up @@ -626,4 +634,83 @@ public Set<String> removeCachedMapping(String serviceKey) {
await().until(() -> count.get() > 10);
scheduledExecutorService.shutdown();
}

@Test
void testMappingNoRetry() {
FrameworkModel frameworkModel = new FrameworkModel();
ApplicationModel applicationModel = frameworkModel.newApplication();
ServiceConfig<DemoService> serviceConfig = new ServiceConfig<>(applicationModel.newModule());
serviceConfig.exported();
ScheduledExecutorService scheduledExecutorService = Mockito.spy(Executors.newScheduledThreadPool(1));
AtomicInteger count = new AtomicInteger(0);
ServiceNameMapping serviceNameMapping = new ServiceNameMapping() {
@Override
public boolean map(URL url) {
return false;
}

@Override
public boolean hasValidMetadataCenter() {
return false;
}

@Override
public void initInterfaceAppMapping(URL subscribedURL) {

}

@Override
public Set<String> getAndListen(URL registryURL, URL subscribedURL, MappingListener listener) {
return null;
}

@Override
public MappingListener stopListen(URL subscribeURL, MappingListener listener) {
return null;
}

@Override
public void putCachedMapping(String serviceKey, Set<String> apps) {

}

@Override
public Set<String> getCachedMapping(String mappingKey) {
return null;
}

@Override
public Set<String> getCachedMapping(URL consumerURL) {
return null;
}

@Override
public Set<String> getRemoteMapping(URL consumerURL) {
return null;
}

@Override
public Map<String, Set<String>> getCachedMapping() {
return null;
}

@Override
public Set<String> removeCachedMapping(String serviceKey) {
return null;
}

@Override
public void $destroy() {

}
};
ApplicationConfig applicationConfig = new ApplicationConfig("app");
applicationConfig.setMappingRetryInterval(10);
serviceConfig.setApplication(applicationConfig);
serviceConfig.mapServiceName(URL.valueOf(""), serviceNameMapping, scheduledExecutorService);

verify(scheduledExecutorService, times(0)).schedule((Runnable) any(), anyLong(), any());

scheduledExecutorService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
package org.apache.dubbo.metadata;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand All @@ -24,11 +29,6 @@
import org.apache.dubbo.rpc.model.ScopeModelUtil;
import org.apache.dubbo.rpc.service.Destroyable;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import static java.util.Collections.emptySet;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR;
import static org.apache.dubbo.common.extension.ExtensionScope.APPLICATION;
Expand All @@ -48,6 +48,8 @@ public interface ServiceNameMapping extends Destroyable {
*/
boolean map(URL url);

boolean hasValidMetadataCenter();

/**
* Get the default extension of {@link ServiceNameMapping}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
*/
package org.apache.dubbo.metadata;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.model.ApplicationModel;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDED_BY;
import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY;

Expand Down Expand Up @@ -126,6 +125,11 @@ protected void removeListener(URL url, MappingListener mappingListener) {
public boolean map(URL url) {
return false;
}

@Override
public boolean hasValidMetadataCenter() {
return false;
}
}

private class MockServiceNameMapping2 extends AbstractServiceNameMapping {
Expand Down Expand Up @@ -158,6 +162,11 @@ protected void removeListener(URL url, MappingListener mappingListener) {
public boolean map(URL url) {
return false;
}

@Override
public boolean hasValidMetadataCenter() {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public MetadataServiceNameMapping(ApplicationModel applicationModel) {
casRetryWaitTime = ConfigurationUtils.getGlobalConfiguration(applicationModel).getInt(CAS_RETRY_WAIT_TIME_KEY, DEFAULT_CAS_RETRY_WAIT_TIME);
}

@Override
public boolean hasValidMetadataCenter() {
return !CollectionUtils.isEmpty(applicationModel.getApplicationConfigManager().getMetadataConfigs());
}

/**
* Simply register to all metadata center
*/
Expand Down