aloyszhang commented on code in PR #9908:
URL: https://github.com/apache/inlong/pull/9908#discussion_r1545953050


##########
inlong-agent/agent-installer/src/main/java/org/apache/inlong/agent/installer/ModuleManager.java:
##########
@@ -216,9 +218,158 @@ private boolean updateModules(List<ModuleConfig> 
managerModuleList) {
         managerModuleList.forEach((moduleConfig) -> {
             modulesFromManager.put(moduleConfig.getId(), moduleConfig);
         });
+        traverseManagerModulesToLocal(modulesFromManager);
+        traverseLocalModulesToManager(modulesFromManager);
         return true;
     }
 
+    private void traverseManagerModulesToLocal(Map<Integer, ModuleConfig> 
modulesFromManager) {
+        modulesFromManager.values().forEach((managerModule) -> {
+            ModuleConfig localModule = 
currentModules.get(managerModule.getId());
+            if (localModule == null) {
+                LOGGER.info("traverseManagerModulesToLocal module {} {} {} not 
found in local, add it",
+                        managerModule.getId(), managerModule.getName(), 
managerModule.getVersion());
+                addModule(managerModule);
+            } else {
+                if (managerModule.getMd5().equals(localModule.getMd5())) {
+                    LOGGER.info("traverseManagerModulesToLocal module {} {} {} 
md5 no change, do nothing",
+                            localModule.getId(), localModule.getName(), 
localModule.getVersion());
+                } else {
+                    LOGGER.info("traverseManagerModulesToLocal module {} {} {} 
md5 changed, update it",
+                            localModule.getId(), localModule.getName(), 
localModule.getVersion());
+                    updateModule(localModule, managerModule);
+                }
+            }
+        });
+    }
+
+    private void traverseLocalModulesToManager(Map<Integer, ModuleConfig> 
modulesFromManager) {
+        currentModules.values().forEach((localModule) -> {
+            ModuleConfig managerModule = 
modulesFromManager.get(localModule.getId());
+            if (managerModule == null) {
+                LOGGER.info("traverseLocalModulesToManager module {} {} {} not 
found in local, delete it",
+                        localModule.getId(), localModule.getName(), 
localModule.getVersion());
+                deleteModule(localModule);
+            }
+        });
+    }
+
+    private void addModule(ModuleConfig module) {
+        LOGGER.info("add module {} start", module.getName());
+        addAndSaveModuleConfig(module);
+        if (!downloadModule(module)) {
+            LOGGER.error("add module {} but download failed", 
module.getName());
+            return;
+        }
+        saveModuleState(module.getId(), ModuleStateEnum.DOWNLOADED);
+        installModule(module);
+        saveModuleState(module.getId(), ModuleStateEnum.INSTALLED);
+        startModule(module);
+        LOGGER.info("add module {} end", module.getId());
+    }
+
+    private void deleteModule(ModuleConfig module) {
+        LOGGER.info("delete module {} start", module.getId());
+        stopModule(module);
+        uninstallModule(module);
+        deleteAndSaveModuleConfig(module);
+        LOGGER.info("delete module {} end", module.getId());
+    }
+
+    private void updateModule(ModuleConfig localModule, ModuleConfig 
managerModule) {
+        LOGGER.info("update module {} start", localModule.getId());
+        if 
(localModule.getPackageConfig().getMd5().equals(managerModule.getPackageConfig().getMd5()))
 {
+            LOGGER.info("package md5 changed, will reinstall", 
localModule.getId());
+            deleteModule(localModule);
+            addModule(managerModule);
+        } else {
+            LOGGER.info("package md5 no chang, will restart", 
localModule.getId());
+            restartModule(localModule, managerModule);
+        }
+        LOGGER.info("update module {} end", localModule.getId());
+    }
+
+    private void addAndSaveModuleConfig(ModuleConfig module) {
+        module.setState(ModuleStateEnum.NEW);
+        if (currentModules.containsKey(module.getId())) {
+            LOGGER.error("should not happen! module {} found! will force to 
replace it!", module.getId());
+        }
+        currentModules.put(module.getId(), module);
+        saveToLocalFile(confPath);
+    }
+
+    private void deleteAndSaveModuleConfig(ModuleConfig module) {
+        if (!currentModules.containsKey(module.getId())) {
+            LOGGER.error("should not happen! module {} not found!", 
module.getId());
+            return;
+        }
+        currentModules.remove(module.getId());
+        saveToLocalFile(confPath);
+    }
+
+    private boolean saveModuleState(Integer moduleId, ModuleStateEnum state) {
+        ModuleConfig module = currentModules.get(moduleId);
+        if (module == null) {
+            LOGGER.error("should not happen! module {} not found!", moduleId);
+            return false;
+        }
+        LOGGER.info("save module state to {} {}", moduleId, state);
+        module.setState(state);
+        saveToLocalFile(confPath);

Review Comment:
   ```suggestion
           module.setState(state);
           saveToLocalFile(confPath);
           LOGGER.info("save module state to {} {}", moduleId, state);
   ```



-- 
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]

Reply via email to