-
Notifications
You must be signed in to change notification settings - Fork 26.6k
[GSoC 2025] Add discovery-timeline QoS command to display real-time service discovery metadata #15590
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
Open
ankitshokeen
wants to merge
14
commits into
apache:3.3
Choose a base branch
from
ankitshokeen:feature/discovery-timeline
base: 3.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[GSoC 2025] Add discovery-timeline QoS command to display real-time service discovery metadata #15590
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
855646f
[Enhancement] Add discovery-timeline QoS command for service discover…
ankitshokeen 67ddb33
Merge branch '3.3' into feature/discovery-timeline
ankitshokeen f994f18
fix/formatting
ankitshokeen b446f9d
Merge branch '3.3' into feature/discovery-timeline
ankitshokeen b9bfc4c
Delete redundant configuration
zrlw 1f4a4eb
Update pom.xml
zrlw 9743d31
fix(qos): add ASF license header and use proper logger codes in Disco…
ankitshokeen 2c06cac
Merge branch '3.3' into feature/discovery-timeline
ankitshokeen 6a99568
revert
ankitshokeen 6839f23
migrate to logger
ankitshokeen b9b4995
fix formatting issues
ankitshokeen c4a89a1
Update org.apache.dubbo.qos.api.BaseCommand
zrlw 04b7a9a
Update pom.xml
zrlw 3bd9d97
Merge branch '3.3' into feature/discovery-timeline
ankitshokeen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,4 +32,3 @@ dubbo: | |
| metadata-report: | ||
| address: zookeeper://127.0.0.1:2181 | ||
|
|
||
|
|
||
349 changes: 349 additions & 0 deletions
349
...n/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/DiscoveryTimelineCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,349 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.dubbo.qos.command.impl; | ||
|
|
||
| import org.apache.dubbo.common.logger.Logger; | ||
| import org.apache.dubbo.common.logger.LoggerFactory; | ||
| import org.apache.dubbo.common.utils.StringUtils; | ||
| import org.apache.dubbo.qos.api.BaseCommand; | ||
| import org.apache.dubbo.qos.api.Cmd; | ||
| import org.apache.dubbo.qos.api.CommandContext; | ||
| import org.apache.dubbo.registry.client.ServiceDiscovery; | ||
| import org.apache.dubbo.registry.client.ServiceInstance; | ||
| import org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent; | ||
| import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener; | ||
| import org.apache.dubbo.registry.support.RegistryManager; | ||
| import org.apache.dubbo.rpc.model.ApplicationModel; | ||
| import org.apache.dubbo.rpc.model.FrameworkModel; | ||
| import org.apache.dubbo.rpc.model.FrameworkServiceRepository; | ||
| import org.apache.dubbo.rpc.model.ProviderModel; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Cmd( | ||
| name = "discovery-timeline", | ||
| summary = "Show service discovery timeline", | ||
| example = { | ||
| "discovery-timeline", | ||
| "discovery-timeline service=com.example.Service", | ||
| "discovery-timeline registry=zookeeper://localhost:2181", | ||
| "discovery-timeline page=2", | ||
| "discovery-timeline limit=5" | ||
| }) | ||
| public class DiscoveryTimelineCommand implements BaseCommand { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(DiscoveryTimelineCommand.class); | ||
| private final FrameworkModel frameworkModel; | ||
| private static final int DEFAULT_LIMIT = 10; | ||
| private static final Map<String, Long> globalProviderRegistrationTimes = new HashMap<>(); | ||
| private static final String TIMESTAMP_KEY = "timestamp"; | ||
|
|
||
| public DiscoveryTimelineCommand(FrameworkModel frameworkModel) { | ||
| this.frameworkModel = frameworkModel; | ||
| } | ||
|
|
||
| @Override | ||
| public String execute(CommandContext commandContext, String[] args) { | ||
| logger.debug("DiscoveryTimelineCommand started"); | ||
|
|
||
| try { | ||
| ApplicationModel applicationModel = frameworkModel.defaultApplication(); | ||
| if (applicationModel == null) { | ||
| throw new IllegalStateException("No ApplicationModel available"); | ||
| } | ||
|
|
||
| RegistryManager registryManager = applicationModel.getBeanFactory().getBean(RegistryManager.class); | ||
| if (registryManager == null) { | ||
| logger.warn("RegistryManager not available"); | ||
| return "Error: RegistryManager not available. Check configuration."; | ||
| } | ||
|
|
||
| List<ServiceDiscovery> serviceDiscoveries = registryManager.getServiceDiscoveries(); | ||
| if (serviceDiscoveries == null || serviceDiscoveries.isEmpty()) { | ||
| logger.warn("No ServiceDiscovery found"); | ||
| return "Error: No ServiceDiscovery instances found."; | ||
| } | ||
|
|
||
| String filterServiceName = null; | ||
| String filterRegistry = null; | ||
| int page = 1; | ||
| int limit = DEFAULT_LIMIT; | ||
| if (args != null && args.length > 0) { | ||
| for (String arg : args) { | ||
| if (arg == null) continue; | ||
| if (arg.startsWith("service=")) { | ||
| filterServiceName = arg.substring("service=".length()); | ||
| } else if (arg.startsWith("registry=")) { | ||
| filterRegistry = arg.substring("registry=".length()); | ||
| } else if (arg.startsWith("page=")) { | ||
| try { | ||
| page = Math.max(1, Integer.parseInt(arg.substring("page=".length()))); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn("Invalid page number: {}", arg, e); | ||
| } | ||
| } else if (arg.startsWith("limit=")) { | ||
| try { | ||
| limit = Math.max(1, Integer.parseInt(arg.substring("limit=".length()))); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn("Invalid limit number: {}", arg, e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final String finalFilterServiceName = filterServiceName; | ||
|
|
||
| Map<ServiceDiscovery, CustomServiceInstancesChangedListener> listeners = new HashMap<>(); | ||
| Map<String, Long> providerRegistrationTimes = new HashMap<>(globalProviderRegistrationTimes); | ||
| Map<ServiceDiscovery, Set<String>> registryServices = new HashMap<>(); | ||
|
|
||
| FrameworkServiceRepository serviceRepository = frameworkModel.getServiceRepository(); | ||
| Set<ProviderModel> uniqueProviderModels = new HashSet<>(serviceRepository.allProviderModels()); | ||
| List<ProviderModel> providerModels = uniqueProviderModels.stream() | ||
| .filter(model -> { | ||
| String serviceName = model.getServiceKey(); | ||
| return serviceName != null | ||
| && (finalFilterServiceName == null || serviceName.contains(finalFilterServiceName)); | ||
| }) | ||
| .sorted((m1, m2) -> { | ||
| String key1 = m1.getServiceKey(); | ||
| String key2 = m2.getServiceKey(); | ||
| String numStr1 = key1.replaceAll("[^0-9]", ""); | ||
| String numStr2 = key2.replaceAll("[^0-9]", ""); | ||
| if (StringUtils.isEmpty(numStr1) || StringUtils.isEmpty(numStr2)) { | ||
| return key1.compareTo(key2); | ||
| } | ||
| try { | ||
| int num1 = Integer.parseInt(numStr1); | ||
| int num2 = Integer.parseInt(numStr2); | ||
| return Integer.compare(num1, num2); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn("Failed to parse numbers for sorting: {} vs {}", numStr1, numStr2, e); | ||
| return key1.compareTo(key2); | ||
| } | ||
| }) | ||
| .collect(Collectors.toList()); | ||
| logger.debug( | ||
| "Filtered and sorted provider models: {}", | ||
| providerModels.stream().map(ProviderModel::getServiceKey).collect(Collectors.toList())); | ||
|
|
||
| boolean hasServices = false; | ||
| for (ServiceDiscovery serviceDiscovery : serviceDiscoveries) { | ||
| if (filterRegistry != null | ||
| && (serviceDiscovery.getUrl() == null | ||
| || !serviceDiscovery.getUrl().getAddress().contains(filterRegistry))) { | ||
| continue; | ||
| } | ||
|
|
||
| Set<String> serviceNames = new HashSet<>(); | ||
| ServiceInstance instance = serviceDiscovery.getLocalInstance(); | ||
| if (instance == null) { | ||
| logger.warn("No local instance found for registry: {}", serviceDiscovery.getUrl()); | ||
| continue; | ||
| } | ||
|
|
||
| Map<String, String> metadata = instance.getMetadata(); | ||
| if (metadata == null || metadata.isEmpty()) { | ||
| logger.warn("No metadata found for instance in registry: {}", serviceDiscovery.getUrl()); | ||
| metadata = new HashMap<>(); | ||
| } | ||
|
|
||
| for (ProviderModel providerModel : providerModels) { | ||
| String serviceName = providerModel.getServiceKey(); | ||
| if (serviceName != null) { | ||
| serviceNames.add(serviceName); | ||
| if (!providerRegistrationTimes.containsKey(serviceName)) { | ||
| String timestampStr = | ||
| metadata.getOrDefault(TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); | ||
| try { | ||
| providerRegistrationTimes.put(serviceName, Long.parseLong(timestampStr)); | ||
| logger.debug("Set timestamp for {}: {}", serviceName, timestampStr); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn( | ||
| "Invalid timestamp format for service: {}, timestamp: {}", | ||
| serviceName, | ||
| timestampStr, | ||
| e); | ||
| providerRegistrationTimes.put(serviceName, System.currentTimeMillis()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| registryServices.put(serviceDiscovery, serviceNames); | ||
| CustomServiceInstancesChangedListener listener = new CustomServiceInstancesChangedListener( | ||
| serviceNames, serviceDiscovery, providerRegistrationTimes); | ||
| serviceDiscovery.addServiceInstancesChangedListener(listener); | ||
| listeners.put(serviceDiscovery, listener); | ||
| hasServices = true; | ||
| logger.debug("Registered services for discovery {}: {}", serviceDiscovery.getUrl(), serviceNames); | ||
| } | ||
|
|
||
| for (Map.Entry<String, Long> entry : providerRegistrationTimes.entrySet()) { | ||
| globalProviderRegistrationTimes.putIfAbsent(entry.getKey(), entry.getValue()); | ||
| } | ||
|
|
||
| if (!hasServices) { | ||
| return "Error: No services discovered."; | ||
| } | ||
|
|
||
| StringBuilder timeline = new StringBuilder("Discovery Timeline\n"); | ||
| timeline.append("------------------------------------------------------------\n"); | ||
| timeline.append(String.format("%-30s|%-30s%n", "Registry", "Last Refresh")); | ||
| timeline.append("------------------------------------------------------------\n"); | ||
|
|
||
| for (ServiceDiscovery sd : serviceDiscoveries) { | ||
| if (filterRegistry != null | ||
| && (sd.getUrl() == null || !sd.getUrl().getAddress().contains(filterRegistry))) { | ||
| continue; | ||
| } | ||
| String refreshTimeStr = "Unknown"; | ||
| ServiceInstance instance = sd.getLocalInstance(); | ||
| if (instance != null | ||
| && instance.getMetadata() != null | ||
| && instance.getMetadata().containsKey(TIMESTAMP_KEY)) { | ||
| String timestampStr = instance.getMetadata() | ||
| .getOrDefault(TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); | ||
| try { | ||
| long registryTimestamp = Long.parseLong(timestampStr); | ||
| refreshTimeStr = new Date(registryTimestamp).toString(); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn( | ||
| "Invalid timestamp format for registry: {}, timestamp: {}", | ||
| sd.getUrl().getAddress(), | ||
| timestampStr, | ||
| e); | ||
| refreshTimeStr = new Date(System.currentTimeMillis()).toString(); | ||
| } | ||
| } | ||
| timeline.append(String.format("%-30s|%-30s%n", sd.getUrl().getAddress(), refreshTimeStr)); | ||
| } | ||
|
|
||
| timeline.append("------------------------------------------------------------\n"); | ||
| timeline.append("Provider Services\n"); | ||
|
|
||
| // Apply pagination | ||
| int providerStart = (page - 1) * limit; | ||
| int providerEnd = Math.min(providerStart + limit, providerModels.size()); | ||
| logger.debug( | ||
| "Pagination: page={}, limit={}, start={}, end={}, total providers={}", | ||
| page, | ||
| limit, | ||
| providerStart, | ||
| providerEnd, | ||
| providerModels.size()); | ||
| List<ProviderModel> paginatedProviders = providerModels.subList( | ||
| Math.min(providerStart, providerModels.size()), Math.min(providerEnd, providerModels.size())); | ||
| logger.debug( | ||
| "Paginated providers: {}", | ||
| paginatedProviders.stream() | ||
| .map(ProviderModel::getServiceKey) | ||
| .collect(Collectors.toList())); | ||
|
|
||
| for (ProviderModel providerModel : paginatedProviders) { | ||
| String serviceName = providerModel.getServiceKey(); | ||
| Long lastRegistrationTime = providerRegistrationTimes.get(serviceName); | ||
| String refreshTimeStr = | ||
| lastRegistrationTime != null ? new Date(lastRegistrationTime).toString() : "Unknown"; | ||
| timeline.append(String.format("%-30s|%-30s%n", "Discovered: " + serviceName, refreshTimeStr)); | ||
| } | ||
|
|
||
| timeline.append("------------------------------------------------------------\n"); | ||
| String result = timeline.toString(); | ||
| logger.debug("Final output:\n{}", result); | ||
| return result; | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Failed to generate discovery timeline", e); | ||
| return "Error: " + e.getMessage(); | ||
| } | ||
| } | ||
|
|
||
| private static class CustomServiceInstancesChangedListener extends ServiceInstancesChangedListener { | ||
| private final Set<String> serviceNames; | ||
| private final Map<String, Long> serviceRegistrationTimes; | ||
| private Set<String> previousInstances = new HashSet<>(); | ||
|
|
||
| public CustomServiceInstancesChangedListener( | ||
| Set<String> serviceNames, | ||
| ServiceDiscovery serviceDiscovery, | ||
| Map<String, Long> serviceRegistrationTimes) { | ||
| super(serviceNames, serviceDiscovery); | ||
| this.serviceNames = serviceNames; | ||
| this.serviceRegistrationTimes = serviceRegistrationTimes; | ||
| initializeRegistrationTimes(); | ||
| } | ||
|
|
||
| private void initializeRegistrationTimes() { | ||
| long currentTime = System.currentTimeMillis(); | ||
| for (String serviceName : serviceNames) { | ||
| if (!serviceRegistrationTimes.containsKey(serviceName)) { | ||
| serviceRegistrationTimes.put(serviceName, currentTime); | ||
| logger.debug( | ||
| "Initialized registration time for {}: {}", serviceName, new Date(currentTime).toString()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onEvent(ServiceInstancesChangedEvent event) { | ||
| super.onEvent(event); | ||
| String serviceName = event.getServiceName(); | ||
| Set<String> currentInstances = event.getServiceInstances().stream() | ||
| .map(ServiceInstance::getServiceName) | ||
| .filter(name -> name != null) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| if (previousInstances.isEmpty() || !previousInstances.equals(currentInstances)) { | ||
| serviceNames.add(serviceName); | ||
| for (ServiceInstance instance : event.getServiceInstances()) { | ||
| String timestampStr = instance.getMetadata() != null | ||
| ? instance.getMetadata() | ||
| .getOrDefault(TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())) | ||
| : String.valueOf(System.currentTimeMillis()); | ||
| if (!serviceRegistrationTimes.containsKey(serviceName)) { | ||
| try { | ||
| serviceRegistrationTimes.put(serviceName, Long.parseLong(timestampStr)); | ||
| logger.debug( | ||
| "Updated timestamp for {}: {}", | ||
| serviceName, | ||
| new Date(Long.parseLong(timestampStr)).toString()); | ||
| } catch (NumberFormatException e) { | ||
| logger.warn( | ||
| "Invalid timestamp format for service: {}, timestamp: {}", | ||
| serviceName, | ||
| timestampStr, | ||
| e); | ||
| serviceRegistrationTimes.put(serviceName, System.currentTimeMillis()); | ||
| } | ||
| } | ||
| } | ||
| previousInstances = new HashSet<>(currentInstances); | ||
| } | ||
| logger.debug( | ||
| "Event received for service: {}, current instances: {}, registration times: {}", | ||
| serviceName, | ||
| currentInstances, | ||
| serviceRegistrationTimes); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All warning or error log info should logged by ErrorTypeAwareLogger. e.g.,
logger.warn(INTERNAL_ERROR, "some cause info", "", e.getMessage(), e);logger.warn(INTERNAL_ERROR, "", "", "warning messages");INTERNAL_ERRORshould be selected from LoggerCodeConstants.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated by using ErrorTypeAwareLogger