Skip to content

Commit

Permalink
Service offline speedup (#12869)
Browse files Browse the repository at this point in the history
* Service offline speedup

* Service offline speedup
  • Loading branch information
AlbumenJ authored Aug 9, 2023
1 parent 78a4dba commit 1dd248a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.qos.api.BaseCommand;
import org.apache.dubbo.qos.api.CommandContext;
Expand All @@ -29,14 +30,20 @@
import org.apache.dubbo.rpc.model.ServiceMetadata;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

public class BaseOffline implements BaseCommand {
private static final Logger logger = LoggerFactory.getLogger(BaseOffline.class);
public FrameworkServiceRepository serviceRepository;
private ExecutorService executorService;

public BaseOffline(FrameworkModel frameworkModel) {
this.serviceRepository = frameworkModel.getServiceRepository();
this.executorService = frameworkModel.getBeanFactory().getBean(FrameworkExecutorRepository.class).getSharedExecutor();
}

@Override
Expand All @@ -63,18 +70,28 @@ protected boolean doExecute(String servicePattern) {
public boolean offline(String servicePattern) {
boolean hasService = false;

Collection<ProviderModel> providerModelList = serviceRepository.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
ServiceMetadata metadata = providerModel.getServiceMetadata();
if (metadata.getServiceKey().matches(servicePattern) || metadata.getDisplayServiceKey().matches(servicePattern)) {
hasService = true;
List<ProviderModel.RegisterStatedURL> statedUrls = providerModel.getStatedUrl();
for (ProviderModel.RegisterStatedURL statedURL : statedUrls) {
if (statedURL.isRegistered()) {
doUnexport(statedURL);
try {
List<CompletableFuture<Void>> futures = new LinkedList<>();
Collection<ProviderModel> providerModelList = serviceRepository.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
ServiceMetadata metadata = providerModel.getServiceMetadata();
if (metadata.getServiceKey().matches(servicePattern) || metadata.getDisplayServiceKey().matches(servicePattern)) {
hasService = true;
List<ProviderModel.RegisterStatedURL> statedUrls = providerModel.getStatedUrl();
for (ProviderModel.RegisterStatedURL statedURL : statedUrls) {
if (statedURL.isRegistered()) {
futures.add(CompletableFuture.runAsync(() -> {
doUnexport(statedURL);
}, executorService));
}
}
}
}
for (CompletableFuture<Void> future : futures) {
future.get();
}
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}

return hasService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public GracefulShutdown(FrameworkModel frameworkModel) {

@Override
public String execute(CommandContext commandContext, String[] args) {
offline.execute(commandContext, new String[0]);
for (org.apache.dubbo.rpc.GracefulShutdown gracefulShutdown :
org.apache.dubbo.rpc.GracefulShutdown.getGracefulShutdowns(frameworkModel)) {
gracefulShutdown.readonly();
}
offline.execute(commandContext, new String[0]);
return "OK";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,22 @@ public void register() {
public synchronized void unregister() {
if (registered.compareAndSet(true, false)) {
Registry registry = RegistryProtocol.this.getRegistry(getRegistryUrl(originInvoker));
try {
registry.unregister(registerUrl);
} catch (Throwable t) {
logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);

ProviderModel providerModel = frameworkModel.getServiceRepository()
.lookupExportedService(getRegisterUrl().getServiceKey());

List<ProviderModel.RegisterStatedURL> statedUrls = providerModel.getStatedUrl();
if (statedUrls.stream()
.filter(u -> u.getRegistryUrl().equals(getRegisterUrl())
&& u.getProviderUrl().getProtocol().equals(getRegisterUrl().getProtocol()))
.anyMatch(ProviderModel.RegisterStatedURL::isRegistered)) {
try {
registry.unregister(registerUrl);
} catch (Throwable t) {
logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
}
}

try {
if (subscribeUrl != null) {
Map<URL, Set<NotifyListener>> overrideListeners = getProviderConfigurationListener(subscribeUrl).getOverrideListeners();
Expand Down

0 comments on commit 1dd248a

Please sign in to comment.