This is an automated email from the ASF dual-hosted git repository.

maxyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit cd4a4d2cf669b37a0c8db8ab98df5958facde3ca
Author: Xing Guo <[email protected]>
AuthorDate: Mon May 6 13:57:58 2024 +0800

    [7X] Skip ssh if segments are on the same host with coordinator. (#16812)
    
    We can skip ssh connections if segments are on the same host with the
    coordinator. We can save some time when executing commands. E.g., with
    this patch, we don't ssh connection when creating the demo cluster and
    the creation time can be reduced from 18s to 10s on my laptop. Besides,
    we can skip setting up ssh keys for deploying the demo cluster.
---
 gpMgmt/bin/gppylib/commands/base.py                      | 16 +++++++++++-----
 gpMgmt/bin/gppylib/commands/gp.py                        |  6 ++++--
 .../bin/gppylib/operations/detect_unreachable_hosts.py   |  8 +++++++-
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/gpMgmt/bin/gppylib/commands/base.py 
b/gpMgmt/bin/gppylib/commands/base.py
index 464a201445..87290cb11f 100755
--- a/gpMgmt/bin/gppylib/commands/base.py
+++ b/gpMgmt/bin/gppylib/commands/base.py
@@ -22,6 +22,7 @@ from threading import Thread
 
 import os
 import signal
+import socket
 import subprocess
 import sys
 import time
@@ -502,14 +503,19 @@ class RemoteExecutionContext(LocalExecutionContext):
 
         # Escape \ and  " for remote execution
         cmd.cmdStr = cmd.cmdStr.replace('\\','\\\\').replace('"', '\\"')
-        cmd.cmdStr = "ssh -o StrictHostKeyChecking=no -o 
ServerAliveInterval=60 " \
-                     "{targethost} \"{gphome} 
{cmdstr}\"".format(targethost=self.targetHost,
-                                                                 gphome=". 
%s/greenplum_path.sh;" % self.gphome,
-                                                                 
cmdstr=cmd.cmdStr)
+
+        localhost = socket.gethostname()
+        if localhost != self.targetHost:
+            cmd.cmdStr = "ssh -o StrictHostKeyChecking=no -o 
ServerAliveInterval=60 " \
+                         "{targethost} \"{gphome} 
{cmdstr}\"".format(targethost=self.targetHost,
+                                                                     gphome=". 
%s/greenplum_path.sh;" % self.gphome,
+                                                                     
cmdstr=cmd.cmdStr)
+        else:
+            cmd.cmdStr = "bash -c \"{gphome} {cmdstr}\"".format(gphome=". 
%s/greenplum_path.sh;" % self.gphome,
+                                                                
cmdstr=cmd.cmdStr)
         LocalExecutionContext.execute(self, cmd, pickled=pickled, 
start_new_session=start_new_session)
         if (cmd.get_stderr().startswith('ssh_exchange_identification: 
Connection closed by remote host')):
             self.__retry(cmd, 0, pickled)
-        pass
 
     def __retry(self, cmd, count, pickled):
         if count == SSH_MAX_RETRY:
diff --git a/gpMgmt/bin/gppylib/commands/gp.py 
b/gpMgmt/bin/gppylib/commands/gp.py
index 129ee13d50..3ec845203a 100644
--- a/gpMgmt/bin/gppylib/commands/gp.py
+++ b/gpMgmt/bin/gppylib/commands/gp.py
@@ -1651,11 +1651,13 @@ class IfAddrs:
         cmd = ["echo 'START_CMD_OUTPUT'; %s/libexec/ifaddrs" % GPHOME]
         if not include_loopback:
             cmd.append('--no-loopback')
-        if hostname:
+        localhost = socket.gethostname()
+        if hostname and hostname != localhost:
             args = ['ssh', '-n', hostname]
             args.append(' '.join(cmd))
         else:
-            args = cmd
+            args = ['bash', '-c']
+            args.append(' '.join(cmd))
 
         result = subprocess.check_output(args).decode()
         return result.split('START_CMD_OUTPUT\n')[1].splitlines()
diff --git a/gpMgmt/bin/gppylib/operations/detect_unreachable_hosts.py 
b/gpMgmt/bin/gppylib/operations/detect_unreachable_hosts.py
index 7ce465704a..0e2d92da00 100644
--- a/gpMgmt/bin/gppylib/operations/detect_unreachable_hosts.py
+++ b/gpMgmt/bin/gppylib/operations/detect_unreachable_hosts.py
@@ -2,6 +2,7 @@ from gppylib import gplog
 from gppylib.commands.base import Command
 from gppylib.commands import base
 from gppylib.gparray import STATUS_DOWN
+import socket
 
 logger = gplog.get_default_logger()
 
@@ -12,8 +13,13 @@ def get_unreachable_segment_hosts(hosts, num_workers):
 
     pool = base.WorkerPool(numWorkers=num_workers)
     try:
+        localhost = socket.gethostname()
         for host in set(hosts):
-            cmd = Command(name='check %s is up' % host, cmdStr="ssh %s 'echo 
%s'" % (host, host))
+            cmd = None
+            if host != localhost:
+                cmd = Command(name='check %s is up' % host, cmdStr="ssh %s 
'echo %s'" % (host, host))
+            else:
+                cmd = Command(name='check %s is up' % host, cmdStr="echo 
{}".format(host))
             pool.addCommand(cmd)
         pool.join()
     finally:


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

Reply via email to