Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement session expiry cleanup for persisted sessions #739

Merged
merged 14 commits into from
Jun 3, 2023

Conversation

andsel
Copy link
Collaborator

@andsel andsel commented Mar 20, 2023

Release notes

Implement the logic to expire the not clean sessions.

What does it do?

Track all expiration time after a session is closed or disconnects. Uses a delay queue to track the not clean session that expires and using a scheduled task, every second, check for the expired sessions and removes from the both SessionRegistry and also from the persistent store (SessionRepositoy).

Why it's important for the user?

This feature let the user to auto-purge the state of the not clean session after a certain amount of time configured at broker level in moquette.conf file through the persistent_client_expiration property which could have the format:
<number><unit> such as 1d for 1 day. Admitted values to unit are s m h d w M y which respectively means seconds, minutes, hours, days, weeks, months and years. If not specified this global expiry defaults to INFINITE_EXPIRY which is ~80 years.

@andsel andsel force-pushed the feature/session_expiry_cleanup branch 2 times, most recently from a2851ee to 85dd80b Compare March 26, 2023 16:08
@andsel andsel changed the title [WIP] Session expiry cleanup Implement session expiry cleanup for persisted sessions Mar 26, 2023
@andsel andsel added the v0.17.0 label Mar 26, 2023
@andsel andsel marked this pull request as ready for review March 29, 2023 10:59
@andsel andsel force-pushed the feature/session_expiry_cleanup branch from 7d40949 to cf36fd7 Compare March 29, 2023 11:02
@andsel
Copy link
Collaborator Author

andsel commented Mar 29, 2023

Hi @hylkevds may I ask you if you would like to be my buddy in this effort and provide a review to this functionality, please 🙏 ?

Copy link
Collaborator

@hylkevds hylkevds left a comment

Choose a reason for hiding this comment

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

Looks good, just one concern about concurrency and one about re-activated sessions not being removed from the expiration queue.

}
}

private void trackForRemovalOnExpiration(ISessionsRepository.SessionData session) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we also need the opposite, unTrackForRemovalOnExpiration, for when a session is re-opened.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @hylkevds, you are right. It has been fixed with ae786d0, so please could you give it another review?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to implement it :)

@andsel andsel force-pushed the feature/session_expiry_cleanup branch from cf36fd7 to 203edd4 Compare April 14, 2023 15:32
@andsel andsel force-pushed the feature/session_expiry_cleanup branch from c1acc2b to ad2bb43 Compare May 19, 2023 14:39
@andsel andsel requested a review from hylkevds May 20, 2023 13:25
}
}

private void trackForRemovalOnExpiration(ISessionsRepository.SessionData session) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to implement it :)

}

private void untrackFromRemovalOnExpiration(ISessionsRepository.SessionData session) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs implementation...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤦

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed by these 2 commits:

  • 250752b fixes test condition to verify effectively as failure when there is no implementation of the method untrackFromRemovalOnExpiration
  • d2a5d3f implements the untracking method

String propertyValue = getProperty(propertyName);
final char timeSpecifier = propertyValue.charAt(propertyValue.length() - 1);
final TemporalUnit periodType;
switch (timeSpecifier) {
Copy link
Collaborator

@hylkevds hylkevds May 28, 2023

Choose a reason for hiding this comment

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

Please also add seconds and minutes. In high-volume cases we don't want sessions to stay alive for more than a minute or so...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed with e29ad31

@hylkevds
Copy link
Collaborator

While working on the subscription tree this weekend I noticed that subscriptions are not cleaned up when a session is cleaned up.
I noticed because the interceptor didn't get notified of any unsubscribe events when a (clean) session terminated due to a disconnect.

This may need its own issue, WDYT?

@andsel
Copy link
Collaborator Author

andsel commented Jun 2, 2023

@hylkevds I think this could be a bug but:

interceptor didn't get notified of any unsubscribe events

the interceptor is notified only when an UNSUBSCRIBE message is received. When a clean session is wiped due to a disconnection it doesn't mean that the unsubscribe interceptor is notified, or do you thing it should?

@hylkevds
Copy link
Collaborator

hylkevds commented Jun 2, 2023

I think it should, yes.
If an interceptor wants to know about subscribe and unsubscribe events it's most likely to keep track of which topics have subscribers so it know which topics to send data to. For that use-case it is important to get all unsubscribe events, regardless of why one happened.

In our specific use-case there is no simple one-to-one mapping between events and the topics that may be interested in those events. It's an (in some cases extreme) many-to-many mapping. One event may have to be sent to potentially thousands of topics. That's why we need to keep track of exactly which topics actually have subscribers, so we can avoid generating messages for topics that have no subscriptions.

Of course the subscription not being cleaned up and the interceptor not being notified are two distinct, but related, issues.

@andsel
Copy link
Collaborator Author

andsel commented Jun 3, 2023

@hylkevds please open an issue describing for that, describing the behavior. It will addressed in a separate PR.

@andsel andsel requested a review from hylkevds June 3, 2023 10:56
@andsel
Copy link
Collaborator Author

andsel commented Jun 3, 2023

Hi @hylkevds I've addressed all your latest concerns. Please could you review it?! :-)

hylkevds
hylkevds previously approved these changes Jun 3, 2023
Copy link
Collaborator

@hylkevds hylkevds left a comment

Choose a reason for hiding this comment

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

Looks good!

andsel added 11 commits June 3, 2023 21:27
- made SessionData opaque of time using a Clock so that time can be easily managed in unit tests
- spread the use of this Clock over interested classes and introduced test utilty class ForwardableClock
- introduced delete method of session into Session's repository and used from the cleanup expired method.
@andsel andsel self-assigned this Jun 3, 2023
@andsel andsel merged commit b8bbc1e into moquette-io:main Jun 3, 2023
4 checks passed
andsel added a commit that referenced this pull request Jun 21, 2023
Leverage the existing global expiry harness introduced with #739, reading the expiration time from the CONNECT's MQTT property SESSION_EXPIRY_INTERVAL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants