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

[Reporting/CSV] Do not fail the job if scroll ID can not be cleared #76014

Merged
merged 3 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
import expect from '@kbn/expect';
import sinon from 'sinon';
import { CancellationToken } from '../../../../common';
import { LevelLogger } from '../../../lib';
import { createMockLevelLogger } from '../../../test_helpers/create_mock_levellogger';
import { ScrollConfig } from '../../../types';
import { createHitIterator } from './hit_iterator';

const mockLogger = {
error: new Function(),
debug: new Function(),
warning: new Function(),
} as LevelLogger;
const mockLogger = createMockLevelLogger();
const debugLogStub = sinon.stub(mockLogger, 'debug');
const warnLogStub = sinon.stub(mockLogger, 'warning');
const warnLogStub = sinon.stub(mockLogger, 'warn');
const errorLogStub = sinon.stub(mockLogger, 'error');
const mockCallEndpoint = sinon.stub();
const mockSearchRequest = {};
Expand Down Expand Up @@ -134,4 +130,30 @@ describe('hitIterator', function () {
expect(errorLogStub.callCount).to.be(1);
expect(errorThrown).to.be(true);
});

it('handles scroll id could not be cleared', async () => {
// Setup
mockCallEndpoint.withArgs('clearScroll').rejects({ status: 404 });

// Begin
const hitIterator = createHitIterator(mockLogger);
const iterator = hitIterator(
mockConfig,
mockCallEndpoint,
mockSearchRequest,
realCancellationToken
);

while (true) {
const { done: iterationDone, value: hit } = await iterator.next();
if (iterationDone) {
break;
}
expect(hit).to.be('you found me');
}

expect(mockCallEndpoint.callCount).to.be(13);
expect(warnLogStub.callCount).to.be(1);
expect(errorLogStub.callCount).to.be(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ export function createHitIterator(logger: LevelLogger) {
);
}

function clearScroll(scrollId: string | undefined) {
async function clearScroll(scrollId: string | undefined) {
logger.debug('executing clearScroll request');
return callEndpoint('clearScroll', {
scrollId: [scrollId],
});
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap these in a try/catch, or do:

return callEndpoint('clearScroll', { scrollId: [scrollId] }).catch((err) => {
        // Do not throw the error, as the job can still be completed successfully
        logger.warn('Scroll context can not be cleared!');
        logger.error(err);
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a matter of code/preference. The .catch is a bit more concise whereas the try/catch is more idiomatic async/await js

Copy link
Member Author

Choose a reason for hiding this comment

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

It definitely could go either way, in which case I like to defer to the existing async style in other code in the file. In this case, async/await follows the code style in this file

await callEndpoint('clearScroll', {
scrollId: [scrollId],
});
} catch (err) {
// Do not throw the error, as the job can still be completed successfully
logger.warn('Scroll context can not be cleared!');
logger.error(err);
}
}

try {
Expand All @@ -86,7 +92,7 @@ export function createHitIterator(logger: LevelLogger) {
({ scrollId, hits } = await scroll(scrollId));

if (cancellationToken.isCancelled()) {
logger.warning(
logger.warn(
'Any remaining scrolling searches have been cancelled by the cancellation token.'
);
}
Expand Down
Loading