DaanHoogland commented on a change in pull request #3575: [WIP DO NOT MERGE] 
Health check feature for virtual router
URL: https://github.com/apache/cloudstack/pull/3575#discussion_r358170833
 
 

 ##########
 File path: 
server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
 ##########
 @@ -1186,6 +1227,431 @@ protected void pushToUpdateQueue(final List<NetworkVO> 
networks) throws Interrup
         }
     }
 
+    protected class FetchRouterHealthChecksResultTask extends 
ManagedContextRunnable {
+        public FetchRouterHealthChecksResultTask() {
+        }
+
+        @Override
+        protected void runInContext() {
+            try {
+                final List<DomainRouterVO> routers = 
_routerDao.listByStateAndManagementServer(VirtualMachine.State.Running, 
mgmtSrvrId);
+                s_logger.debug("Found " + routers.size() + " running routers. 
");
+
+                for (final DomainRouterVO router : routers) {
+                    GetRouterMonitorResultsAnswer answer = 
fetchAndUpdateRouterHealthChecks(router, false);
+                    String checkFailsToRestartVr = 
RouterHealthChecksFailuresToRestartVr.valueIn(router.getDataCenterId());
+                    if (answer == null) {
+                        s_logger.warn("Unable to fetch monitor results for 
router " + router);
+                        updateRouterConnectivityHealthCheck(router.getId(), 
false, "Communication failed");
+                    } else if (!answer.getResult()) {
+                        s_logger.warn("Failed to fetch monitor results from 
router " + router + " with details: " + answer.getDetails());
+                        updateRouterConnectivityHealthCheck(router.getId(), 
false, "Failed to fetch results with details: " + answer.getDetails());
+                    } else {
+                        updateRouterConnectivityHealthCheck(router.getId(), 
true, "Successfully fetched data");
+                        parseAndMergeDbHealthChecks(router.getId(), 
answer.getMonitoringResults());
+
+                        // Check failing tests and restart if needed
+                        if (answer.getFailingChecks().size() > 0 && 
StringUtils.isNotBlank(checkFailsToRestartVr)) {
+                            s_logger.info("Checking failed health checks to 
see if router needs reboot");
+                            for (String failedCheck : 
answer.getFailingChecks()) {
+                                if 
(checkFailsToRestartVr.contains(failedCheck)) {
+                                    s_logger.info("Found failing health check 
" + failedCheck + " so restarting router.");
+                                    rebootRouter(router.getId(), true);
+                                }
+                            }
+                        }
+                    }
+                }
+            } catch (final Exception ex) {
+                s_logger.error("Fail to complete the 
FetchRouterHealthChecksResultTask! ", ex);
+                ex.printStackTrace();
+            }
+        }
+    }
+
+    private Map<String, Map<String, RouterHealthCheckResultVO>> 
getHealthChecksFromDb(long routerId) {
+        List<RouterHealthCheckResultVO> healthChecksList = 
routerHealthCheckResultDao.getHealthCheckResults(routerId);
+        Map<String, Map<String, RouterHealthCheckResultVO>> healthCheckResults 
= new HashMap<>();
+        if (healthChecksList.isEmpty()) {
+            return healthCheckResults;
+        }
+
+        for (RouterHealthCheckResultVO healthCheck : healthChecksList) {
+            if (!healthCheckResults.containsKey(healthCheck.getCheckType())) {
+                healthCheckResults.put(healthCheck.getCheckType(), new 
HashMap<>());
+            }
+            
healthCheckResults.get(healthCheck.getCheckType()).put(healthCheck.getCheckName(),
 healthCheck);
+        }
+
+        return healthCheckResults;
+    }
+
+    private RouterHealthCheckResultVO 
updateRouterConnectivityHealthCheck(final long routerId, boolean connected, 
String message) {
+        boolean newEntry = false;
+        RouterHealthCheckResultVO connectivityVO = 
routerHealthCheckResultDao.getRouterHealthCheckResult(routerId, "connectivity", 
"basic");
+        if (connectivityVO == null) {
+            connectivityVO = new RouterHealthCheckResultVO(routerId, 
"connectivity", "basic");
+            newEntry = true;
+        }
+
+        connectivityVO.setCheckResult(connected);
+        connectivityVO.setLastUpdateTime(new Date());
+        connectivityVO.setCheckDetails(StringUtils.isNotEmpty(message) ? 
message.getBytes(Charset.forName("US-ASCII")) : null);
+
+        if (newEntry) {
+            routerHealthCheckResultDao.persist(connectivityVO);
+        } else {
+            routerHealthCheckResultDao.update(connectivityVO.getId(), 
connectivityVO);
+        }
+
+        return routerHealthCheckResultDao.getRouterHealthCheckResult(routerId, 
"connectivity", "basic");
+    }
+
+    private List<RouterHealthCheckResult> parseAndMergeDbHealthChecks(final 
long routerId, final String monitoringResult) {
+        if (StringUtils.isBlank(monitoringResult)) {
+            s_logger.warn("Attempted parsing empty monitoring results string 
for router " + routerId);
+            return Collections.emptyList();
+        }
+
+        try {
+            s_logger.info("Parsing and updating DB health check data for 
router: " + routerId);
+            s_logger.info("Retrieved data" + monitoringResult);
+            final Map<String, Map<String, RouterHealthCheckResultVO>> 
checksInDb = getHealthChecksFromDb(routerId);
+            final Type t = new TypeToken<Map<String, Map<String, Map<String, 
String>>>>() {}.getType();
+            final Map<String, Map<String, Map<String, String>>> checks = 
GsonHelper.getGson().fromJson(monitoringResult, t);
 
 Review comment:
   We are using an antiquated version of Gson. I did a quick check on 
compatibility with modern versions. 👍 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to