Skip to content

Conversation

Millefeuille42
Copy link

No description provided.

@Millefeuille42 Millefeuille42 self-assigned this Mar 5, 2025
@Wescoeur Wescoeur changed the base branch from 3.2.3-8.3 to 3.2.12-8.3 March 20, 2025 14:33
@Millefeuille42 Millefeuille42 force-pushed the mlr-533-cache-controller-uri-in-a-file branch 2 times, most recently from 958caec to 42b42bc Compare March 20, 2025 14:53
Comment on lines 238 to 244
address = uri.removeprefix("linstor://")
session = util.timeout_call(10, util.get_localAPI_session)
for host_ref, host_record in session.xenapi.host.get_all():
if host_record.get('address', '') != address:
continue
return util.strtobool(
session.xenapi.host.call_plugin(host_ref, PLUGIN, PLUGIN_CMD, {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that's necessary here. We can instead do a refactoring of the codebase:

  • We will assume that the cache is valid 99% of the time.
  • We can directly attempt to create a LINSTOR instance from the URI without checking anything using a plugin. This is the initial idea.
  • I think we can implement a new static function on LinstorVolumeManager that is used to create an instance of the class using the cached value directly without any checks. If it fails, we rebuild the local cache and try again. This allows us to improve several smapi functions.
  • There are a few edge cases left with this idea:
    • In some places we use the URI to create the journaler (linstor.KV) and to create a linstor object. We could try to use the cached URI without checks again and add a try/catch directly on these specific cases. In case of connection failure, we explicitly request the cache update.
    • I see one last edge case concerning the creation of a linstor instance using get_ips_from_xha_config_file, we could directly use the function to get the cached URI. In the worst can we can again fallback on the xha config file.

Copy link
Author

@Millefeuille42 Millefeuille42 Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge cases are almost done. I pushed modification of the added functions 042014f and created the static method you suggested 0364eed. Could you please take a look at the current state of those functions to validate them before I start the refactor ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good from my POV. Feel free to continue when you have time. :)

@Millefeuille42 Millefeuille42 force-pushed the mlr-533-cache-controller-uri-in-a-file branch from 42b42bc to 065e30c Compare April 28, 2025 09:31
@Millefeuille42 Millefeuille42 requested a review from Wescoeur May 16, 2025 14:40
@Millefeuille42 Millefeuille42 force-pushed the mlr-533-cache-controller-uri-in-a-file branch from c315328 to 9760980 Compare June 17, 2025 09:30
@Millefeuille42 Millefeuille42 force-pushed the mlr-533-cache-controller-uri-in-a-file branch from 4b663cb to 363372d Compare June 26, 2025 09:39
Comment on lines 228 to 235
def get_cached_controller_uri(ctx=None):
try:
with ctx if ctx else shared_reader(CONTROLLER_CACHE_PATH) as f:
return f.read().strip()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to read controller URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH,e))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_cached_controller_uri(ctx=None):
try:
with ctx if ctx else shared_reader(CONTROLLER_CACHE_PATH) as f:
return f.read().strip()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to read controller URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH,e))
def _read_controller_uri_from_file(f):
try:
return f.read().strip()
except Exception as e:
util.SMlog('Unable to read controller URI cache file at `{}`: {}'.format(CONTROLLER_CACHE_PATH, e))
def read_controller_uri_cache():
try:
with shared_reader(CONTROLLER_CACHE_PATH) as f:
return _read_controller_uri_from_file(f)
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to read controller URI cache file at `{}`: {}'.format(CONTROLLER_CACHE_PATH, e))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 4f84f23

Comment on lines 238 to 246
def delete_controller_uri_cache(ctx=None):
try:
with ctx if ctx else excl_writer(CONTROLLER_CACHE_PATH) as f:
f.seek(0)
f.truncate()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to delete uri cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def delete_controller_uri_cache(ctx=None):
try:
with ctx if ctx else excl_writer(CONTROLLER_CACHE_PATH) as f:
f.seek(0)
f.truncate()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to delete uri cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e))
def delete_controller_uri_cache():
try:
with excl_writer(CONTROLLER_CACHE_PATH) as f:
f.seek(0)
f.truncate()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to delete URI cache file at `{}`: {}'.format(CONTROLLER_CACHE_PATH, e))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 4f84f23

Comment on lines 249 to 279
def write_controller_uri_cache(uri, ctx=None):
try:
if not os.path.exists(CONTROLLER_CACHE_DIRECTORY):
os.makedirs(CONTROLLER_CACHE_DIRECTORY)
os.chmod(CONTROLLER_CACHE_DIRECTORY, 0o700)
with ctx if ctx else excl_writer(CONTROLLER_CACHE_PATH) as f:
f.seek(0)
f.write(uri)
f.truncate()
except FileNotFoundError:
pass
except Exception as e:
util.SMlog('Unable to write URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e))


def build_controller_uri_cache():
with excl_writer(CONTROLLER_CACHE_PATH) as f:
uri = get_cached_controller_uri(contextlib.nullcontext(f))
if uri:
return uri
uri = _get_controller_uri()
if not uri:
for retries in range(9):
time.sleep(1)
uri = _get_controller_uri()
if uri:
break

retries += 1
if retries >= 10:
break
time.sleep(1)
if uri:
write_controller_uri_cache(uri, contextlib.nullcontext(f))
return uri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot add suggestion due to github limitation but:

def build_controller_uri_cache():
    uri = ''
    try:
        with excl_writer(CONTROLLER_CACHE_PATH) as f:
            uri = _read_controller_uri_from_file(f)
            if uri:
                return uri
            uri = _get_controller_uri()
            if not uri:
                for retries in range(9):
                    time.sleep(1)
                    uri = _get_controller_uri()
                    if uri:
                        break
            if uri:
                f.seek(0)
                f.write(uri)
                f.truncate()
    except FileNotFoundError:
        if os.path.exists(CONTROLLER_CACHE_DIRECTORY):
            raise
        os.makedirs(CONTROLLER_CACHE_DIRECTORY)
        os.chmod(CONTROLLER_CACHE_DIRECTORY, 0o700)
        return build_controller_uri_cache()
    except Exception as e:
        util.SMlog('Unable to write URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e))

    return uri

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 1f6ffbb

Comment on lines 282 to 321
def get_controller_uri():
uri = get_cached_controller_uri()
if not uri:
uri = build_controller_uri_cache()
return uri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_controller_uri():
uri = get_cached_controller_uri()
if not uri:
uri = build_controller_uri_cache()
return uri
def get_controller_uri():
uri = read_controller_uri_cache()
if not uri:
uri = build_controller_uri_cache()
return uri

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 4f84f23

:param function logger: Function to log messages.
:param int attempt_count: Number of attempts to join the controller.
"""
uri = get_cached_controller_uri()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uri = get_cached_controller_uri()
uri = read_controller_uri_cache()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 4f84f23

@Millefeuille42 Millefeuille42 requested a review from Wescoeur June 30, 2025 08:18
@Wescoeur Wescoeur force-pushed the 3.2.12-8.3 branch 3 times, most recently from e7801da to ca5b52d Compare August 28, 2025 15:32
Co-authored-by: Ronan Abhamon <[email protected]>
Co-authored-by: Damien Thenot <[email protected]>
Signed-off-by: Mathieu Labourier <[email protected]>
@Millefeuille42 Millefeuille42 force-pushed the mlr-533-cache-controller-uri-in-a-file branch from 6cdeb37 to 3579d92 Compare September 4, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants