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

Added scheduled session cleanup #10802

Merged
merged 9 commits into from
May 17, 2024

Conversation

dashevchenko
Copy link
Contributor

@dashevchenko dashevchenko commented May 16, 2024

Pull Request description

Added scheduled session cleanup for cases when session was closed before last session command processed

General checklist

  • You have reviewed the guidelines document.
  • Labels that classify your pull request have been added.
  • The milestone is specified and corresponds to fix version.
  • Description references specific issue.
  • Description contains human-readable scope of changes.
  • Description contains brief notes about what needs to be added to the documentation.
  • No merge conflicts, commented blocks of code, code formatting issues.
  • Changes are backward compatible or upgrade script is provided.
  • Similar PR is opened for PE version to simplify merge. Crosslinks between PRs added. Required for internal contributors only.

Front-End feature checklist

  • Screenshots with affected component(s) are added. The best option is to provide 2 screens: before and after changes;
  • If you change the widget or other API, ensure it is backward-compatible or upgrade script is present.
  • Ensure new API is documented here

Back-End feature checklist

  • Added corresponding unit and/or integration test(s). Provide written explanation in the PR description if you have failed to add tests.
  • If new dependency was added: the dependency tree is checked for conflicts.
  • If new service was added: the service is marked with corresponding @TbCoreComponent, @TbRuleEngineComponent, @TbTransportComponent, etc.
  • If new REST API was added: the RestClient.java was updated, issue for Python REST client is created.
  • If new yml property was added: make sure a description is added (above or near the property).

@dashevchenko dashevchenko added the Core Changes to Core label May 16, 2024
@dashevchenko dashevchenko added this to the 3.6.4 milestone May 16, 2024
@@ -539,6 +539,11 @@ public void close(WebSocketSessionRef sessionRef, CloseStatus reason) throws IOE
}
}

@Override
public boolean isOpen(String sessionId) {
return externalSessionMap.containsKey(sessionId);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we should additionally check the actual native WS session:
sessionMd.session.isOpen().

@ashvayka Do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure. If the session is removed from the map, it is no longer used.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just in case :)

wsSessionsMap.values().forEach(md -> {
String sessionId = md.getSessionRef().getSessionId();
if (!msgEndpoint.isOpen(sessionId)) {
log.info("Cleaning up stale session [sessionId: {}, tenantId: {}, userId: {}] ", sessionId,
Copy link
Contributor

Choose a reason for hiding this comment

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

Use standard way of logging: "[tenantId][userId][sessionId] Cleaning up stale session"

md.getSessionRef().getSecurityCtx().getTenantId(), md.getSessionRef().getSecurityCtx().getId());
wsSessionsMap.remove(sessionId);
oldSubService.cancelAllSessionSubscriptions(sessionId);
entityDataSubService.cancelAllSessionSubscriptions(sessionId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need to also call processSessionClose(sessionRef)?

@@ -107,6 +113,8 @@ public void initExecutor() {
subscriptionUpdateExecutor = ThingsBoardExecutors.newWorkStealingPool(20, getClass());
tsCallBackExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("ts-sub-callback"));
serviceId = serviceInfoProvider.getServiceId();
staleSessionCleanupExecutor = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("stale-session-cleanup"));
staleSessionCleanupExecutor.scheduleWithFixedDelay(this::cleanupStaleSessions, 0, 60000, TimeUnit.MILLISECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Initial delay - also 60 seconds

if (sessionSubscriptions.isEmpty()) {
subscriptionsBySessionId.remove(sessionId);
ModifySubscriptionResult modifySubscriptionResult = null;
subsLock.lock();
Copy link
Member

Choose a reason for hiding this comment

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

move up

}

private void pushSubscriptionEvent(ModifySubscriptionResult modifySubResult) {
TbEntitySubEvent event = modifySubResult.getEvent();
Copy link
Member

Choose a reason for hiding this comment

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

try catch with logging (warn) for both pushSubscriptionEvent and modifySubscription

private TbSubscription<?> missedUpdatesCandidate;
private TbEntitySubEvent event;

public boolean isEmpty() {
Copy link
Member

Choose a reason for hiding this comment

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

hasEvent

modifySubscription(subscription.getTenantId(), subscription.getEntityId(), subscription, false);
} catch (Exception e) {
log.warn("[{}][{}] Failed to remove subscription {} due to ", subscription.getTenantId(), subscription.getEntityId(), subscription, e);
List<ModifySubscriptionResult> modifySubscriptionResults = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

-> SubscriptionModificationResult, for variable name might just use 'result(s)'

@ashvayka ashvayka merged commit 2a7efde into thingsboard:hotfix/3.6.4 May 17, 2024
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Changes to Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants