Don't reload config when file wasn't modified

The launcher thread will reload the config in the main loop every second
(watermark sleep) without checking if the config changed. This can take
quite a bit of CPU time as we've seen from different Yappi profiles.

This change uses the last modified timestamp of the config file to
detect if the Nodepool config needs to be reloaded.

The test `test_nodepool_occ_config_reload` was changed to update the
provider config so that a new OpenStack client object is created.

Change-Id: I93400cc156d09ea1add4fc753846df923242c0e6
This commit is contained in:
Simon Westphahl
2023-06-29 13:13:42 +02:00
parent 61b5ca3c8c
commit 1a71b46cb5
2 changed files with 23 additions and 0 deletions

View File

@@ -1029,7 +1029,9 @@ class NodePool(threading.Thread):
watermark_sleep=WATERMARK_SLEEP):
threading.Thread.__init__(self, name='NodePool')
self.securefile = securefile
self._secure_mtime = 0
self.configfile = configfile
self._config_mtime = 0
self.watermark_sleep = watermark_sleep
self.cleanup_interval = 60
self.delete_interval = 5
@@ -1134,6 +1136,16 @@ class NodePool(threading.Thread):
t.provider_name == provider_name]
def updateConfig(self):
secure_mtime = 0
config_mtime = os.stat(self.configfile).st_mtime_ns
if self._config_mtime == config_mtime:
if self.securefile:
secure_mtime = os.stat(self.securefile).st_mtime_ns
if self._secure_mtime == secure_mtime:
return
else:
return
config = self.loadConfig()
self.reconfigureZooKeeper(config)
provider_manager.ProviderManager.reconfigure(self.config, config,
@@ -1141,7 +1153,10 @@ class NodePool(threading.Thread):
for provider_name in list(config.providers.keys()):
if provider_name not in config.provider_managers:
del config.providers[provider_name]
self.setConfig(config)
self._config_mtime = config_mtime
self._secure_mtime = secure_mtime
def removeCompletedRequests(self):
'''

View File

@@ -85,6 +85,14 @@ class TestShadeIntegration(tests.IntegrationTestCase):
with open(self.clouds_path, 'w') as h:
yaml.safe_dump(occ_config, h)
# Change the provider to force a new OpenStack client object
# to be created.
with open(configfile) as f:
config = yaml.safe_load(f)
config['providers'][0]['rate'] = 0.0002
with open(configfile, 'w') as f:
yaml.safe_dump(config, f)
pool.updateConfig()
pm = pool.config.provider_managers['real-provider']
self.assertEqual(pm.adapter._client.auth, auth_data)