DaanHoogland commented on a change in pull request #2309: CLOUDSTACK-10132: 
Multiple Management Servers Support for agents
URL: https://github.com/apache/cloudstack/pull/2309#discussion_r150809054
 
 

 ##########
 File path: agent/src/com/cloud/agent/Agent.java
 ##########
 @@ -699,16 +719,97 @@ public void processResponse(final Response response, 
final Link link) {
         }
     }
 
-    public void processReadyCommand(final Command cmd) {
+    /**
+     * Checks if received list is different to actual management server list 
(in order and size)
+     * @param actual actual list
+     * @param received received list
+     * @return true if list is updated, false if not
+     */
+    private boolean isReceivedListUpdated(List<String> actual, List<String> 
received) {
+        if (actual.size() != received.size()) {
+            return true;
+        }
+        for (int i = 0; i < received.size(); i++) {
+            if (!received.get(i).equals(actual.get(i))) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Persist newly received management servers list and reset host counter
+     * @param msList management servers list
+     */
+    private void persistNewManagementServersList(List<String> msList) {
+        final String newHosts = StringUtils.toCSVList(msList);
+        _shell.setHosts(newHosts);
+        _shell.setPersistentProperty(null, "host", newHosts);
+        _shell.resetHostCounter();
+        s_logger.info("Saved new management servers list: " + msList);
+    }
 
+    /**
+     * Try reconnecting to preferred management server after new list is 
received.
+     * If connection is not performed, host counter is reset
+     */
+    private void attemptReconnectionToPrimaryHost(String preferred) {
+        boolean connection = false;
+        try {
+            if (_link != null) {
+                boolean isHostUp = true;
+                try (final Socket socket = new Socket()) {
+                    socket.connect(new InetSocketAddress(preferred, 
_shell.getPort()), 5000);
+                } catch (final IOException e) {
+                    isHostUp = false;
+                    if (s_logger.isDebugEnabled()) {
+                        s_logger.debug("Preferred host: " + preferred + " is 
not reachable");
+                    }
+                }
+                if (isHostUp && _link != null) {
+                    long initialTime = System.currentTimeMillis();
+                    while (System.currentTimeMillis() - initialTime < 
_shell.getFailbackPollingWait() && _inProgress.get() > 0) {
+                        s_logger.debug("Waiting for active commands to be 
completed");
+                        Thread.sleep(5000l);
+                    }
+                    if (_inProgress.get() == 0) {
+                        if (s_logger.isDebugEnabled()) {
+                            s_logger.debug("Preferred host: " +  preferred + " 
is found to be reachable, trying to reconnect");
+                        }
+                        reconnect(_link);
+                        connection = true;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            s_logger.warn("Couldn't reconnect to preferred host: " + preferred 
+ " received on management servers list");
+        } finally {
+            if (!connection) {
+                _shell.resetHostCounter();
+            }
+        }
+    }
+
+    public void processReadyCommand(final Command cmd) {
         final ReadyCommand ready = (ReadyCommand)cmd;
 
-        s_logger.info("Proccess agent ready command, agent id = " + 
ready.getHostId());
+        s_logger.info("Processing agent ready command, agent id = " + 
ready.getHostId());
         if (ready.getHostId() != null) {
             setId(ready.getHostId());
         }
-        s_logger.info("Ready command is processed: agent id = " + getId());
 
+        final List<String> msList = ready.getMsList();
+        if (msList != null && msList.size() > 0) {
+            final List<String> hosts = Arrays.asList(_shell.getHosts());
+            s_logger.info("Received management servers list: " + msList + ", 
current list: " + hosts);
+            if (isReceivedListUpdated(hosts, msList)) {
 
 Review comment:
   That seems to be a design issue then. When the order has changed the 
(ordered) list should be updated. It is debatable if the connection should be 
reset, though.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to