-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Search Sessions] Cancel the previous session #99658
Changes from all commits
12b95ab
6e870dd
436dd51
984eba2
f5fdd88
768d7ed
77624ff
e5904ac
4f32b09
f9f1ebb
44023c4
54bb6cd
9ffbb03
6de0b62
a343e13
6d96554
49fe28a
b3e093a
f911163
a988edd
25445c6
e1db913
74c145e
6e6fcb2
ee5ecc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,10 @@ export class SessionService { | |
*/ | ||
public start() { | ||
if (!this.currentApp) throw new Error('this.currentApp is missing'); | ||
|
||
// cancel previous session if needed | ||
this.cleanupPreviousSession(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I understand the full lifecycle so please correct me - this function calls "cancel" under the hood so is it possible to have a race-condition between creating a new search and the time it takes to cancel the current one so that we cancel the new search? FWIW, I was not able to reproduce this, purely based on my current understanding. |
||
|
||
this.state.transitions.start({ appName: this.currentApp }); | ||
return this.getSessionId()!; | ||
} | ||
|
@@ -241,24 +245,34 @@ export class SessionService { | |
); | ||
return; | ||
} | ||
|
||
this.cleanupPreviousSession(); | ||
this.state.transitions.clear(); | ||
this.searchSessionInfoProvider = undefined; | ||
this.searchSessionIndicatorUiConfig = undefined; | ||
} | ||
|
||
private async cleanupPreviousSession() { | ||
const { pendingSearches, sessionId, isRestore, isStored } = this.state.get(); | ||
if (sessionId && !isRestore && !isStored) { | ||
pendingSearches.forEach((s) => { | ||
s.abort(); | ||
}); | ||
await this.sessionsClient.cancel(sessionId).catch(() => {}); | ||
} | ||
} | ||
|
||
/** | ||
* Request a cancellation of on-going search requests within current session | ||
*/ | ||
public async cancel(): Promise<void> { | ||
const isStoredSession = this.state.get().isStored; | ||
this.state.get().pendingSearches.forEach((s) => { | ||
const { pendingSearches, sessionId } = this.state.get(); | ||
if (!sessionId) return; | ||
|
||
pendingSearches.forEach((s) => { | ||
s.abort(); | ||
}); | ||
await this.sessionsClient.cancel(sessionId).catch(() => {}); | ||
this.state.transitions.cancel(); | ||
if (isStoredSession) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced deletion with cancellation. |
||
await this.sessionsClient.delete(this.state.get().sessionId!); | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,10 @@ export class SessionsClient { | |
}); | ||
} | ||
|
||
public cancel(sessionId: string): Promise<void> { | ||
return this.http!.post(`/internal/session/${encodeURIComponent(sessionId)}/cancel`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super minor nit: Can we either standardize on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me, it would make more sense to swallow the 404 errors here in the client rather in the API itself, and only log the errors on the server if it's not a 404. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't intend do to this first, but you can't eliminate the 404 from the console, and it looks bad IMO What do you think? |
||
} | ||
|
||
public delete(sessionId: string): Promise<void> { | ||
return this.http!.delete(`/internal/session/${encodeURIComponent(sessionId)}`); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,8 +49,8 @@ export default function ({ getService }: FtrProviderContext) { | |
await supertest.get(`/internal/session/${sessionId}`).set('kbn-xsrf', 'foo').expect(200); | ||
}); | ||
|
||
it('should fail to delete an unknown session', async () => { | ||
await supertest.delete(`/internal/session/123`).set('kbn-xsrf', 'foo').expect(404); | ||
it('should NOT fail when deleting an unknown session', async () => { | ||
await supertest.delete(`/internal/session/123`).set('kbn-xsrf', 'foo').expect(200); | ||
}); | ||
|
||
it('should create and delete a session', async () => { | ||
|
@@ -487,7 +487,9 @@ export default function ({ getService }: FtrProviderContext) { | |
.delete(`/internal/session/${sessionId}`) | ||
.set('kbn-xsrf', 'foo') | ||
.auth('other_user', 'password') | ||
.expect(404); | ||
.expect(200); | ||
|
||
await supertest.get(`/internal/session/${sessionId}`).set('kbn-xsrf', 'foo').expect(200); | ||
}); | ||
|
||
it(`should prevent users from cancelling other users' sessions`, async () => { | ||
|
@@ -508,7 +510,15 @@ export default function ({ getService }: FtrProviderContext) { | |
.post(`/internal/session/${sessionId}/cancel`) | ||
.set('kbn-xsrf', 'foo') | ||
.auth('other_user', 'password') | ||
.expect(404); | ||
.expect(200); | ||
|
||
const resp = await supertest | ||
.get(`/internal/session/${sessionId}`) | ||
.set('kbn-xsrf', 'foo') | ||
.expect(200); | ||
|
||
const { status } = resp.body.attributes; | ||
expect(status).not.to.equal(SearchSessionStatus.CANCELLED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the expected status then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either IN_PROGRESS or COMPLETED :-) |
||
}); | ||
|
||
it(`should prevent users from extending other users' sessions`, async () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to make sure it's called with the
id
returned fromstart()
?