Skip to content

Commit

Permalink
disable telnet by default and fix ut, reset resources (#8239)
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Jul 9, 2021
1 parent c5a4597 commit 11e15f2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.cluster.Directory;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void setUp() throws Exception {
directory = mock(Directory.class);
firstInvoker = mock(Invoker.class);
secondInvoker = mock(Invoker.class);
invocation = mock(Invocation.class);
invocation = mock(RpcInvocation.class);

}

Expand Down
Expand Up @@ -197,6 +197,7 @@ public void setDynamicConfiguration(DynamicConfiguration dynamicConfiguration) {
public void destroy() throws IllegalStateException {
clearExternalConfigs();
clearAppExternalConfigs();
clearDynamicConfiguration();
}

public PropertiesConfiguration getPropertiesConfiguration() {
Expand Down Expand Up @@ -230,4 +231,8 @@ public void clearAppExternalConfigs() {
this.appExternalConfiguration.clear();
this.appExternalConfigurationMap.clear();
}

public void clearDynamicConfiguration() {
this.dynamicConfiguration = null;
}
}
Expand Up @@ -206,6 +206,8 @@ public static void destroyAll() {
}
}
});
EXTENSION_INSTANCES.clear();
EXTENSION_LOADERS.clear();
}

private static ClassLoader findClassLoader() {
Expand Down
Expand Up @@ -33,7 +33,6 @@
/**
* The shutdown hook thread to do the clean up stuff.
* This is a singleton in order to ensure there is only one shutdown hook registered.
* Because {@link ApplicationShutdownHooks} use {@link java.util.IdentityHashMap}
* to store the shutdown hooks.
*/
public class DubboShutdownHook extends Thread {
Expand All @@ -52,7 +51,7 @@ public class DubboShutdownHook extends Thread {
/**
* Has it already been destroyed or not?
*/
private static final AtomicBoolean destroyed = new AtomicBoolean(false);
private final AtomicBoolean destroyed = new AtomicBoolean(false);

private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension();

Expand Down Expand Up @@ -90,6 +89,7 @@ private void callback() {
*/
public void register() {
if (registered.compareAndSet(false, true)) {
destroyed.set(false);
DubboShutdownHook dubboShutdownHook = getDubboShutdownHook();
Runtime.getRuntime().addShutdownHook(dubboShutdownHook);
dispatch(new DubboShutdownHookRegisteredEvent(dubboShutdownHook));
Expand All @@ -113,6 +113,10 @@ public void unregister() {
public void doDestroy() {
// dispatch the DubboDestroyedEvent @since 2.7.5
dispatch(new DubboServiceDestroyedEvent(this));
if (destroyed.compareAndSet(false, true)) {
AbstractRegistryFactory.destroyAll();
destroyProtocols();
}
}

private void dispatch(Event event) {
Expand All @@ -124,10 +128,7 @@ public boolean getRegistered() {
}

public static void destroyAll() {
if (destroyed.compareAndSet(false, true)) {
AbstractRegistryFactory.destroyAll();
destroyProtocols();
}
getDubboShutdownHook().doDestroy();
}

/**
Expand All @@ -146,4 +147,10 @@ public static void destroyProtocols() {
}
}
}

public static void reset() {
getDubboShutdownHook().destroyed.set(false);
getDubboShutdownHook().unregister();
}

}
Expand Up @@ -876,6 +876,7 @@ private void initMetadataService() {
*/
public DubboBootstrap start() {
if (started.compareAndSet(false, true)) {
destroyed.set(false);
ready.set(false);
initialize();
if (logger.isInfoEnabled()) {
Expand Down Expand Up @@ -1258,16 +1259,15 @@ private ServiceInstance createServiceInstance(String serviceName, String host, i
public void destroy() {
if (destroyLock.tryLock()) {
try {
if (started.compareAndSet(true, false)
&& destroyed.compareAndSet(false, true)) {

unregisterServiceInstance();
unexportMetadataService();
unexportServices();
unreferServices();
if (destroyed.compareAndSet(false, true)) {
if (started.compareAndSet(true, false)) {
unregisterServiceInstance();
unexportMetadataService();
unexportServices();
unreferServices();
}

destroyRegistries();

destroyServiceDiscoveries();
destroyExecutorRepository();
clear();
Expand All @@ -1279,6 +1279,7 @@ public void destroy() {

DubboShutdownHook.destroyAll();
} finally {
initialized.set(false);
destroyLock.unlock();
}
}
Expand Down Expand Up @@ -1428,6 +1429,7 @@ public static void reset() {
*/
@Deprecated
public static void reset(boolean destroy) {
DubboShutdownHook.reset();
if (destroy) {
if (instance != null) {
instance.destroy();
Expand Down
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.utils.service.FooService;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -33,6 +34,7 @@ public class ReferenceConfigCacheTest {
public void setUp() throws Exception {
MockReferenceConfig.setCounter(0);
ReferenceConfigCache.CACHE_HOLDER.clear();
DubboBootstrap.reset();
}

@Test
Expand Down
Expand Up @@ -67,7 +67,7 @@ public String telnet(Channel channel, String message) throws RemotingException {
} else {
buf.append("Command: ");
buf.append(command);
buf.append(" disabled");
buf.append(" disabled for security reasons, please enable support by listing the commands through 'telnet'");
}
} else {
buf.append("Unsupported command: ");
Expand All @@ -86,7 +86,7 @@ public String telnet(Channel channel, String message) throws RemotingException {
private boolean commandEnabled(URL url, String command) {
String supportCommands = url.getParameter(TELNET);
if (StringUtils.isEmpty(supportCommands)) {
return true;
return false;
}
String[] commands = COMMA_SPLIT_PATTERN.split(supportCommands);
for (String c : commands) {
Expand Down

0 comments on commit 11e15f2

Please sign in to comment.