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

Add delay export test case #3447

Merged
merged 3 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -27,7 +27,6 @@
import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker;
Expand Down Expand Up @@ -56,8 +55,6 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static org.apache.dubbo.common.Constants.LOCALHOST_VALUE;
Expand Down Expand Up @@ -102,11 +99,6 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
*/
private static final Map<String, Integer> RANDOM_PORT_MAP = new HashMap<String, Integer>();

/**
* A delayed exposure service timer
*/
private static final ScheduledExecutorService delayExportExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("DubboServiceDelayExporter", true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this delayExportExecutor is a global delay exposed component. I don't think we can delete this field.

Since ServiceConfig exists, we can export the service at runtime, so we can't determine if delayExportExecutor will still be used. In addition, maintaining an object in memory does not have a big impact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each ServerConfig instance holds its own delayExportExecutor and delayExportExecutor only used for delay exporting,I feel it is unnecessary to use it.

agree with @carryxyh , all ServiceConfig holds the same delayExportExecutor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault, I didn't notice that it was static.


/**
* The urls of the services exported
*/
Expand Down Expand Up @@ -341,7 +333,15 @@ public synchronized void export() {
}

if (delay != null && delay > 0) {
delayExportExecutor.schedule(this::doExport, delay, TimeUnit.MILLISECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we call delayExportExecutor.shutdown() method after the delay task add work queue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,This is another way to fix this .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so.
If I want to export the service at runtime(use ServiceConfig), turning off delayExportExecutor will cause problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so.
If I want to export the service at runtime(use ServiceConfig), turning off delayExportExecutor will cause problems.

you are right

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so.
If I want to export the service at runtime(use ServiceConfig), turning off delayExportExecutor will cause problems.

yes.

new NamedThreadFactory("DubboServiceDelayExporter", true).newThread(() -> {
try {
logger.info("Dubbo service " + interfaceClass.getName() + " will delay export for " + delay + " ms");
TimeUnit.MILLISECONDS.sleep(delay);
doExport();
} catch (InterruptedException e) {
logger.error("Expected error occured when export " + interfaceClass.getName(), e);
}
}).start();
} else {
doExport();
}
Expand Down Expand Up @@ -779,7 +779,7 @@ private void createProviderIfAbsent() {
if (provider != null) {
return;
}
setProvider (
setProvider(
ConfigManager.getInstance()
.getDefaultProvider()
.orElseGet(() -> {
Expand Down Expand Up @@ -810,15 +810,15 @@ private void convertProtocolIdsToProtocols() {

if (StringUtils.isEmpty(protocolIds)) {
if (CollectionUtils.isEmpty(protocols)) {
setProtocols(
ConfigManager.getInstance().getDefaultProtocols()
.filter(CollectionUtils::isNotEmpty)
.orElseGet(() -> {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.refresh();
return Arrays.asList(protocolConfig);
})
);
setProtocols(
ConfigManager.getInstance().getDefaultProtocols()
.filter(CollectionUtils::isNotEmpty)
.orElseGet(() -> {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.refresh();
return Arrays.asList(protocolConfig);
})
);
}
} else {
String[] arr = Constants.COMMA_SPLIT_PATTERN.split(protocolIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.service.GenericService;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -40,6 +39,7 @@
import org.mockito.Mockito;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_BEAN;
import static org.apache.dubbo.common.Constants.GENERIC_SERIALIZATION_DEFAULT;
Expand All @@ -60,7 +60,7 @@ public class ServiceConfigTest {
private Exporter exporter = Mockito.mock(Exporter.class);
private ServiceConfig<DemoServiceImpl> service = new ServiceConfig<DemoServiceImpl>();
private ServiceConfig<DemoServiceImpl> service2 = new ServiceConfig<DemoServiceImpl>();

private ServiceConfig<DemoServiceImpl> delayService = new ServiceConfig<DemoServiceImpl>();

@BeforeEach
public void setUp() throws Exception {
Expand Down Expand Up @@ -104,6 +104,14 @@ public void setUp() throws Exception {
service2.setMethods(Collections.singletonList(method));
service2.setProxy("testproxyfactory");

delayService.setProvider(provider);
delayService.setApplication(app);
delayService.setRegistry(registry);
delayService.setInterface(DemoService.class);
delayService.setRef(new DemoServiceImpl());
delayService.setMethods(Collections.singletonList(method));
delayService.setDelay(100);

ConfigManager.getInstance().clear();
}

Expand Down Expand Up @@ -143,6 +151,15 @@ public void testProxy() throws Exception {
assertEquals(2, TestProxyFactory.count); // local injvm and registry protocol, so expected is 2
}


@Test
public void testDelayExport() throws Exception {
delayService.export();
//add 300ms to ensure that the delayService has been exported
LiZhenNet marked this conversation as resolved.
Show resolved Hide resolved
TimeUnit.MILLISECONDS.sleep(delayService.getDelay() + 300);
assertThat(delayService.getExportedUrls(), hasSize(1));
}

@Test
@Disabled("cannot pass in travis")
public void testUnexport() throws Exception {
Expand Down