[8.17] [UA] Remove noisey warn when deleting from `.tasks` (#204720) (#204758)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[UA] Remove noisey warn when deleting from `.tasks`
(#204720)](https://github.com/elastic/kibana/pull/204720)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Jean-Louis
Leysens","email":"jeanlouis.leysens@elastic.co"},"sourceCommit":{"committedDate":"2024-12-18T13:14:57Z","message":"[UA]
Remove noisey warn when deleting from `.tasks` (#204720)\n\n##
Summary\r\n\r\nReduce the number of `warn` logs reindexing will create
due to failed\r\nattempts to delete from `.tasks`. In this PR we only
log for responses\r\nthat do not have status code 403 or
401.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"ff3a600d5eca300c769f67ce144364b5de3a2409","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","release_note:skip","Feature:Upgrade
Assistant","v9.0.0","backport:version","v8.18.0","v8.17.1"],"title":"[UA]
Remove noisey warn when deleting from
`.tasks`","number":204720,"url":"https://github.com/elastic/kibana/pull/204720","mergeCommit":{"message":"[UA]
Remove noisey warn when deleting from `.tasks` (#204720)\n\n##
Summary\r\n\r\nReduce the number of `warn` logs reindexing will create
due to failed\r\nattempts to delete from `.tasks`. In this PR we only
log for responses\r\nthat do not have status code 403 or
401.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"ff3a600d5eca300c769f67ce144364b5de3a2409"}},"sourceBranch":"main","suggestedTargetBranches":["8.x","8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204720","number":204720,"mergeCommit":{"message":"[UA]
Remove noisey warn when deleting from `.tasks` (#204720)\n\n##
Summary\r\n\r\nReduce the number of `warn` logs reindexing will create
due to failed\r\nattempts to delete from `.tasks`. In this PR we only
log for responses\r\nthat do not have status code 403 or
401.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"ff3a600d5eca300c769f67ce144364b5de3a2409"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Jean-Louis Leysens <jeanlouis.leysens@elastic.co>
This commit is contained in:
Kibana Machine 2024-12-19 02:01:19 +11:00 committed by GitHub
parent 8db1b876ec
commit 289d88ee14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 17 deletions

View file

@ -26,7 +26,6 @@ import { esIndicesStateCheck } from '../es_indices_state_check';
import { versionService } from '../version';
import { ReindexService, reindexServiceFactory } from './reindex_service';
import { error } from './error';
const asApiResponse = <T>(body: T): TransportResult<T> =>
({
@ -638,12 +637,7 @@ describe('reindexService', () => {
index: '.tasks',
id: 'xyz',
});
expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith(
error.reindexTaskCannotBeDeleted(
`Could not delete reindexing task xyz, got response "!?"`
)
);
expect(log.warn).toHaveBeenCalledTimes(0); // Do not log anything in this case
});
it('does not throw if task doc deletion throws', async () => {
@ -672,6 +666,32 @@ describe('reindexService', () => {
expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith(new Error('FAILED!'));
});
it.each([401, 403])(
'does not throw if task doc deletion throws AND does not log due to missing access permission: %d',
async (statusCode) => {
clusterClient.asCurrentUser.tasks.get.mockResponseOnce({
completed: true,
// @ts-expect-error not full interface
task: { status: { created: 100, total: 100 } },
});
clusterClient.asCurrentUser.count.mockResponseOnce(
// @ts-expect-error not full interface
{
count: 100,
}
);
const e = new Error();
Object.defineProperty(e, 'statusCode', { value: statusCode });
clusterClient.asCurrentUser.delete.mockRejectedValue(e);
const updatedOp = await service.processNextStep(reindexOp);
expect(updatedOp.attributes.lastCompletedStep).toEqual(ReindexStep.reindexCompleted);
expect(updatedOp.attributes.reindexTaskPercComplete).toEqual(1);
expect(log.warn).toHaveBeenCalledTimes(0);
}
);
});
describe('reindex task is cancelled', () => {

View file

@ -309,20 +309,17 @@ export const reindexServiceFactory = (
}
try {
// Delete the task from ES .tasks index
const deleteTaskResp = await esClient.delete({
// Best effort, delete the task from ES .tasks index...
await esClient.delete({
index: '.tasks',
id: taskId,
});
if (deleteTaskResp.result !== 'deleted') {
log.warn(
error.reindexTaskCannotBeDeleted(
`Could not delete reindexing task ${taskId}, got response "${deleteTaskResp.result}"`
)
);
}
} catch (e) {
log.warn(e);
// We explicitly ignore authz related error codes bc we expect this to be
// very common when deleting from .tasks
if (e?.statusCode !== 401 && e?.statusCode !== 403) {
log.warn(e);
}
}
return reindexOp;