Copilot commented on code in PR #15639:
URL: https://github.com/apache/dubbo/pull/15639#discussion_r2299526214


##########
dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractServiceNameMapping.java:
##########
@@ -70,6 +89,168 @@ public AbstractServiceNameMapping(ApplicationModel 
applicationModel) {
                         .getBeanFactory()
                         .getBean(FrameworkExecutorRepository.class)
                         .getCacheRefreshingScheduledExecutor());
+        metadataReportInstance = 
applicationModel.getBeanFactory().getBean(MetadataReportInstance.class);
+    }
+
+    // only for ut
+    public void setMetadataReportInstance(MetadataReportInstance 
metadataReportInstance) {
+        this.metadataReportInstance = metadataReportInstance;
+    }
+
+    @Override
+    public void mapping(URL url) {
+        if (CollectionUtils.isEmpty(
+                
applicationModel.getApplicationConfigManager().getMetadataConfigs())) {
+            logger.warn(
+                    COMMON_PROPERTY_TYPE_MISMATCH,
+                    "",
+                    "",
+                    "[METADATA_REGISTER] [SERVICE_NAME_MAPPING] No valid 
metadata config center found for mapping report.");
+            return;
+        }
+        String serviceInterface = url.getServiceInterface();
+        if (IGNORED_SERVICE_INTERFACES.contains(serviceInterface)) {
+            return;
+        }
+
+        String appName = applicationModel.getApplicationName();
+        for (Map.Entry<String, MetadataReport> entry :
+                metadataReportInstance.getMetadataReports(true).entrySet()) {
+            MetadataReport metadataReport = entry.getValue();
+            if (metadataReport.registerServiceAppMapping(serviceInterface, 
appName, url)) {
+                // MetadataReport support directly register service-app mapping
+                continue;
+            }
+
+            boolean succeeded = false;
+            try {
+                succeeded = doMapping(metadataReport, url);
+            } catch (Exception e) {
+                logger.warn(
+                        CONFIG_SERVER_DISCONNECTED,
+                        e.getMessage(),
+                        "service: " + serviceInterface + ", metadata-center 
url: " + metadataReport.getUrl(),
+                        "[METADATA_REGISTER] [SERVICE_NAME_MAPPING] Failed 
registering mapping to remote."
+                                + metadataReport,
+                        e);
+            }
+            if (!succeeded) {
+                getServiceNameMappingReportRetry().addTask(metadataReport, 
url);
+            }
+        }
+        getServiceNameMappingReportRetry().start();
+    }
+
+    protected boolean doMapping(MetadataReport metadataReport, URL url) {
+        if (!metadataReport.isAvailable()) {
+            throw new IllegalStateException("metadata reporter is not 
available");
+        }
+        String appName = applicationModel.getApplicationName(), 
newConfigContent = appName;
+        String serviceInterface = url.getServiceInterface();
+
+        ConfigItem configItem = metadataReport.getConfigItem(serviceInterface, 
DEFAULT_MAPPING_GROUP);
+        String oldConfigContent = configItem.getContent();
+        boolean exist = false;
+        if (StringUtils.isNotEmpty(oldConfigContent)) {
+            String[] oldAppNames = oldConfigContent.split(COMMA_SEPARATOR);
+            for (String oldAppName : oldAppNames) {
+                if (StringUtils.trim(oldAppName).equals(appName)) {
+                    exist = true;
+                    break;
+                }
+            }
+            if (!exist) {
+                newConfigContent = oldConfigContent + COMMA_SEPARATOR + 
appName;
+            }
+        }
+        if (exist) {
+            return true;
+        }
+        return metadataReport.registerServiceAppMapping(
+                serviceInterface, DEFAULT_MAPPING_GROUP, newConfigContent, 
configItem.getTicket());
+    }
+
+    private static final Object initializeRetry = new Object();
+    private static final NamedThreadFactory namedThreadFactory =
+            new NamedThreadFactory("DubboServiceNameMappingReportRetry", true);
+    private volatile ServiceNameMappingReportRetry 
serviceNameMappingReportRetry;
+
+    protected ServiceNameMappingReportRetry getServiceNameMappingReportRetry() 
{
+        if (serviceNameMappingReportRetry == null) {
+            synchronized (initializeRetry) {
+                if (serviceNameMappingReportRetry == null) {
+                    serviceNameMappingReportRetry = new 
ServiceNameMappingReportRetry(namedThreadFactory);
+                }
+            }
+        }
+        return serviceNameMappingReportRetry;
+    }
+
+    class ServiceNameMappingReportRetry {
+        private final ScheduledExecutorService executor;
+        private volatile ScheduledFuture<?> future;
+        private final Map<MetadataReport, Set<URL>> taskQueue = new 
ConcurrentHashMap<>();
+        private final Object startLock = new Object();
+
+        private ServiceNameMappingReportRetry(NamedThreadFactory 
namedThreadFactory) {
+            this.executor = Executors.newScheduledThreadPool(0, 
namedThreadFactory);

Review Comment:
   Creating a ScheduledThreadPool with 0 core threads will prevent any tasks 
from being executed. This should be at least 1 to ensure the retry mechanism 
works properly.
   ```suggestion
               this.executor = Executors.newScheduledThreadPool(1, 
namedThreadFactory);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to