File calculate-fqdns-in-parallel-to-avoid-blockings-bsc-1.patch of Package salt

From d10369e0d9c686c61c0cb790df6c1ab99ece32f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
 <psuarezhernandez@suse.com>
Date: Fri, 12 Apr 2019 16:47:03 +0100
Subject: [PATCH] Calculate FQDNs in parallel to avoid blockings
 (bsc#1129079)

---
 salt/grains/core.py | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/salt/grains/core.py b/salt/grains/core.py
index 0efa51f605..df17640873 100644
--- a/salt/grains/core.py
+++ b/salt/grains/core.py
@@ -20,9 +20,12 @@ import re
 import platform
 import logging
 import locale
+import time
 import datetime
 import salt.exceptions
 
+from multiprocessing.dummy import Pool as ThreadPool
+
 __proxyenabled__ = ['*']
 __FQDN__ = None
 
@@ -1684,7 +1687,6 @@ def fqdns():
     # Provides:
     # fqdns
 
-    grains = {}
     fqdns = set()
 
     addresses = salt.utils.network.ip_addrs(include_loopback=False,
@@ -1692,9 +1694,10 @@ def fqdns():
     addresses.extend(salt.utils.network.ip_addrs6(include_loopback=False,
                                                   interface_data=_INTERFACES))
     err_message = 'Exception during resolving address: %s'
-    for ip in addresses:
+
+    def _lookup_fqdn(ip):
         try:
-            fqdns.add(socket.getfqdn(socket.gethostbyaddr(ip)[0]))
+            return [socket.getfqdn(socket.gethostbyaddr(ip)[0])]
         except socket.herror as err:
             if err.errno == 0:
                 # No FQDN for this IP address, so we don't need to know this all the time.
@@ -1704,8 +1707,21 @@ def fqdns():
         except (socket.error, socket.gaierror, socket.timeout) as err:
             log.error(err_message, err)
 
-    grains['fqdns'] = list(fqdns)
-    return grains
+    start = time.time()
+
+    # Create a ThreadPool to process the underlying calls to 'socket.gethostbyaddr' in parallel.
+    # This avoid blocking the execution when the "fqdn" is not defined for certains IP addresses, which was causing
+    # that "socket.timeout" was reached multiple times secuencially, blocking execution for several seconds.
+    pool = ThreadPool(8)
+    results = pool.map(_lookup_fqdn, addresses)
+    pool.close()
+    pool.join()
+
+    [fqdns.update(item) for item in results if item]
+    elapsed = time.time() - start
+    log.debug('Elapsed time getting FQDNs: {0} seconds'.format(elapsed))
+
+    return {"fqdns": sorted(list(fqdns))}
 
 
 def ip_fqdn():
-- 
2.17.1


openSUSE Build Service is sponsored by