Skip to content

Regroup all session creation in util get_localAPI_session or APISession class. - #129

Open
Ythogtha wants to merge 8 commits into
3.2.12-8.3-develfrom
agf-xapi_session_refactoring
Open

Regroup all session creation in util get_localAPI_session or APISession class.#129
Ythogtha wants to merge 8 commits into
3.2.12-8.3-develfrom
agf-xapi_session_refactoring

Conversation

@Ythogtha

Copy link
Copy Markdown

New APISession class to handle lifecycle of the session, with context, or atexit handlers.
Use various originator for xapi session creation, to differentiate where they are being created.

APISession uses get_localAPI_session() for session creation, so there is only one place for session creation, using xenapi.login_with_password.
get_localAPI_session should be avoided and only remain in lcache.py.

The session originator is shown on all logs messages pertaining to sessions, in SMlog, in the format APISession [{originator}] {message}.
All login and logout messages starts with login or logout hence it is possible to count them to check if there is session leakage :

$ echo "login: $(grep -e 'APISession \[.*\] login' SMlog* | wc -l), logout: $(grep -e 'APISession \[.*\] logout' SMlog* | wc -l)"
login: 8024, logout: 8024

@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 12 times, most recently from 0f82c30 to be646e3 Compare April 2, 2026 05:09
@Ythogtha
Ythogtha requested review from Nambrok and Wescoeur April 2, 2026 05:10
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 3 times, most recently from e1507b2 to c0658d5 Compare April 2, 2026 13:45
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from eae5e31 to cee2b15 Compare April 21, 2026 09:35
@Wescoeur
Wescoeur requested a review from a team April 29, 2026 13:40
@Wescoeur
Wescoeur force-pushed the 3.2.12-8.3 branch 4 times, most recently from bc30ef6 to 1dfb701 Compare May 19, 2026 17:17
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from cee2b15 to 4a333e5 Compare May 20, 2026 16:16
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py
Comment thread drivers/util.py Outdated
Comment thread drivers/lcache.py Outdated
@Ythogtha
Ythogtha requested a review from Millefeuille42 May 24, 2026 16:12

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All good for me except the remaining import util

Comment thread drivers/lcache.py Outdated
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 2 times, most recently from bd3737d to 119cae3 Compare May 26, 2026 09:46
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/coalesce-leaf Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/lcache.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 5 times, most recently from d23fa61 to 8173b33 Compare June 23, 2026 15:22
@Wescoeur
Wescoeur force-pushed the 3.2.12-8.3 branch 5 times, most recently from 252789d to d8ca026 Compare June 25, 2026 22:04
@Ythogtha
Ythogtha changed the base branch from 3.2.12-8.3 to 3.2.12-8.3-devel June 26, 2026 14:41
@Nambrok
Nambrok force-pushed the 3.2.12-8.3-devel branch from e712aa5 to fce2d13 Compare June 29, 2026 07:36
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from 8173b33 to 3c34049 Compare June 29, 2026 14:10
@AnthoineB
AnthoineB requested a review from a team July 2, 2026 13:04
@Nambrok
Nambrok force-pushed the 3.2.12-8.3-devel branch from 6607beb to c4dce9e Compare July 2, 2026 14:25
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from 3c34049 to 95a0211 Compare July 7, 2026 10:00
Ythogtha added 6 commits July 24, 2026 10:59
…on class.

New APISession class to handle lifecycle of the session, with context, or atexit handlers.
Use various originator for xapi session creation, to differentiate where they are being created.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Do not SMlog information when __del__ is called and the session has already been closed.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
… are set.

SMlogs are set to LOG_DEBUG, except on failure to create a session.
Fixed some less readable code:
- lcache.py: import util on top instead of inside a method
- util.py; removed double tempfile import

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
…out calls, privatise some APISession methods.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Also fix some strings.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
…in cleanup.py

Due to the rebase, some old-style session management appeared, this commit makes them use the new APISession.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Comment thread drivers/linstorcowutil.py Outdated
Comment thread drivers/mpathcount.py Outdated
Comment thread drivers/mpathcount.py
Comment thread drivers/blktap2.py Outdated
Comment on lines 885 to 888
api_session = util.timeout(5, util.APISession, "blktap-abort_linstor_gc")
session = api_session.session
try:
srs = util.get_linstor_srs_uuid(session)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
api_session = util.timeout(5, util.APISession, "blktap-abort_linstor_gc")
session = api_session.session
try:
srs = util.get_linstor_srs_uuid(session)
api_session = util.timeout(5, util.APISession, "SM-blktap2-abort-linstor-gc")
try:
srs = util.get_linstor_srs_uuid(api_session.session)

Just to remove a temporary variable (and change session name to match other names and SM-blktap2 prefix). (:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"SM-blktap2-abort-linstor-gc" is not applied in new commits.

Comment thread drivers/resetvdis.py Outdated
Comment thread drivers/cleanup.py Outdated
if self.session is None:
self.session = self.getSession()
self.sessionPrivate = True
self._api_session = util.APISession("SM-cleanup-XAPI")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should standardize the names here. An "SMGC" prefix could match the existing logs. Or maybe "SM-GC" or just "GC". What do you think? @Ythogtha @Millefeuille42

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm goign for SM-GC, and looking at all APISession being opened as SM-{something}.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not fond of SM-GC even if more coherent with the rest, as GC related logs are identified as "SMGC" already.

Aug  4 07:29:34 r640-s1 SMGC: [2091943][MainThread] GC process exiting, no work left

If looking for anything GC related in the logs I know to check for "SMGC".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ping @Nambrok for discussion.

Comment thread drivers/cleanup.py Outdated
# Everything was coalescable, remove the message
xapi_session.xenapi.SR.remove_from_sm_config(self.xapi.srRef, VDI.DB_GC_NO_SPACE)
xapi_session.xenapi.message.destroy(msg_id)
with util.APISession("GC-check_no_space") as xapi_session:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
with util.APISession("GC-check_no_space") as xapi_session:
with util.APISession("XXX-check-no-space") as xapi_session:

Suggested in a previous comment. 👀
But XXX because we must discuss about the prefix to use.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

check-no-space with "-" is not applied in new commits.

Comment thread drivers/cleanup.py Outdated
))
finally:
session.xenapi.session.logout()
with util.timeout(5), util.APISession("GC-coalescing") as session:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same, prefix to discuss.

Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Reworked some APISession originator for better consistency.
Removed unuseful `session` intermediate variables.
Reworked restevdis to avoid a huge indentation caused by APISession, by using a function.
Adds a try/finally for session.logout() in util.py#sr_get_capability().

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from fb254f1 to c4d891b Compare July 30, 2026 13:10
Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
@Ythogtha
Ythogtha requested a review from Wescoeur July 30, 2026 15:37
@Wescoeur
Wescoeur requested a review from Millefeuille42 August 3, 2026 14:31

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK for me if and when all pending comments are resolved.

Comment thread drivers/linstorcowutil.py
def _init_executor_thread(self):
session = util.get_localAPI_session()
load = self.Load(session)
api_session = util.ApiSession("SM-linstorvhdutil")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd suggest to see if we can get automatically the file+function name in the session name, this will reduce maintenance in case of changes (see, for example it should be SM-linstorcowutil now) and also it would allow to not specify everytime the name of the sesion.

This is a suggestion but ain't blocking for me, could even be addressed in a separate PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer having an explicit session name; logs indicating a session aren't necessarily debug logs. They serve as a label that is human-readable and can be independent of the implementation. If we want to be more precise here, it would be SM-linstor-multi-cow-util here.

Comment thread drivers/util.py
Comment on lines +776 to +778
class ApiSession(contextlib.AbstractContextManager):
session=None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thinking about it, could it be interesting to define/override __call__ to login and return session? This would avoid the long and redundant api_session.session and reduce it to api_session().

Not sure if it's very clear and explicit when reading the code thought.

This is a non blocking comment, only putting it here to start a discussion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess we can have a card about that.

<!-- Agent database query errors 150+ -->
<code>
<name>APISession</name>
<name>ApiSession</name>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Nambrok For additional comment.

  1. I don't think we should change this field here (it would create a style inconsistent with the others below).
  2. IMHO We should specify this error code in the ApiSession class in the event of a connection failure to the XAPI.

Comment thread drivers/cleanup.py Outdated
# Everything was coalescable, remove the message
xapi_session.xenapi.SR.remove_from_sm_config(self.xapi.srRef, VDI.DB_GC_NO_SPACE)
xapi_session.xenapi.message.destroy(msg_id)
with util.APISession("GC-check_no_space") as xapi_session:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

check-no-space with "-" is not applied in new commits.

Comment thread drivers/blktap2.py Outdated
Comment on lines 885 to 888
api_session = util.timeout(5, util.APISession, "blktap-abort_linstor_gc")
session = api_session.session
try:
srs = util.get_linstor_srs_uuid(session)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"SM-blktap2-abort-linstor-gc" is not applied in new commits.

Comment thread drivers/util.py
Comment on lines +776 to +778
class ApiSession(contextlib.AbstractContextManager):
session=None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess we can have a card about that.

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