Skip to content

Commit

Permalink
[Reporting] Support 'auto' value for csv scroll duration config (elas…
Browse files Browse the repository at this point in the history
…tic#175005)

## Summary

Closes elastic#174988


### Checklist

Delete any items that are not applicable to this PR.
<!--
- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials -->
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
<!--
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
-->
  • Loading branch information
eokoneyo authored and CoenWarmer committed Feb 15, 2024
1 parent b3b00a2 commit 56d75ec
Show file tree
Hide file tree
Showing 17 changed files with 702 additions and 78 deletions.
4 changes: 2 additions & 2 deletions docs/settings/reporting-settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ You may need to lower this setting if the default number of documents creates a
============

`xpack.reporting.csv.scroll.duration`::
Amount of {time-units}[time] allowed before {kib} cleans the scroll context during a CSV export. Defaults to `30s`.
Amount of {time-units}[time] allowed before {kib} cleans the scroll context during a CSV export. Valid option is either `auto` or {time-units}[time], Defaults to `30s`.
[NOTE]
============
If search latency in {es} is sufficiently high, such as if you are using {ccs}, you may need to increase the setting.
If search latency in {es} is sufficiently high, such as if you are using {ccs}, you may either need to increase the time setting or set this config value to `auto`. When the config value is set to `auto` the scroll context will be preserved for as long as is possible, before the report task is terminated due to the limits of `xpack.reporting.queue.timeout`.
============

`xpack.reporting.csv.scroll.strategy`::
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

240 changes: 226 additions & 14 deletions packages/kbn-generate-csv/src/generate_csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { identity, range } from 'lodash';
import * as Rx from 'rxjs';
import type { Writable } from 'stream';
import { add, type Duration } from 'date-fns';

import { errors as esErrors, estypes } from '@elastic/elasticsearch';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
Expand All @@ -21,6 +22,8 @@ import {
} from '@kbn/core/server/mocks';
import { ISearchClient, ISearchStartSearchSource } from '@kbn/data-plugin/common';
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks';
import type { IScopedSearchClient } from '@kbn/data-plugin/server';
import type { IKibanaSearchResponse } from '@kbn/data-plugin/common';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { FieldFormatsRegistry } from '@kbn/field-formats-plugin/common';
import { CancellationToken } from '@kbn/reporting-common';
Expand Down Expand Up @@ -360,7 +363,11 @@ describe('CsvGenerator', () => {
expect(mockDataClient.search).toHaveBeenCalledTimes(10);
expect(mockDataClient.search).toBeCalledWith(
{ params: { body: {}, ignore_throttled: undefined, max_concurrent_shard_requests: 5 } },
{ strategy: 'es', transport: { maxRetries: 0, requestTimeout: '30s' } }
{
abortSignal: expect.any(AbortSignal),
strategy: 'es',
transport: { maxRetries: 0, requestTimeout: '30s' },
}
);

expect(mockEsClient.asCurrentUser.openPointInTime).toHaveBeenCalledTimes(1);
Expand All @@ -370,7 +377,12 @@ describe('CsvGenerator', () => {
index: 'logstash-*',
keep_alive: '30s',
},
{ maxConcurrentShardRequests: 5, maxRetries: 0, requestTimeout: '30s' }
{
maxConcurrentShardRequests: 5,
maxRetries: 0,
requestTimeout: '30s',
signal: expect.any(AbortSignal),
}
);

expect(mockEsClient.asCurrentUser.closePointInTime).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -548,6 +560,203 @@ describe('CsvGenerator', () => {
});
});

describe('export behavior when scroll duration config is auto', () => {
const getTaskInstanceFields = (intervalFromNow: Duration) => {
const now = new Date(Date.now());
return { startedAt: now, retryAt: add(now, intervalFromNow) };
};

let mockConfigWithAutoScrollDuration: ReportingConfigType['csv'];
let mockDataClientSearchFn: jest.MockedFunction<IScopedSearchClient['search']>;

beforeEach(() => {
mockConfigWithAutoScrollDuration = {
...mockConfig,
scroll: {
...mockConfig.scroll,
duration: 'auto',
},
};

mockDataClientSearchFn = jest.fn();

jest.useFakeTimers();
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();

mockDataClientSearchFn.mockRestore();
});

it('csv gets generated if search resolves without errors before the computed timeout value passed to the search data client elapses', async () => {
const timeFromNowInMs = 4 * 60 * 1000;

const taskInstanceFields = getTaskInstanceFields({
seconds: timeFromNowInMs / 1000,
});

mockDataClientSearchFn.mockImplementation((_, options) => {
const getSearchResult = () => {
const queuedAt = Date.now();

return new Promise<IKibanaSearchResponse<ReturnType<typeof getMockRawResponse>>>(
(resolve, reject) => {
setTimeout(() => {
if (
new Date(Date.now()).getTime() - new Date(queuedAt).getTime() >
Number((options?.transport?.requestTimeout! as string).replace(/ms/, ''))
) {
reject(
new esErrors.ResponseError({ statusCode: 408, meta: {} as any, warnings: [] })
);
} else {
resolve({
rawResponse: getMockRawResponse(
[
{
fields: { a: ['a1'], b: ['b1'] },
} as unknown as estypes.SearchHit,
],
3
),
});
}
}, timeFromNowInMs / 4);
}
);
};

return Rx.defer(getSearchResult);
});

const generateCsvPromise = new CsvGenerator(
createMockJob({ searchSource: {}, columns: ['a', 'b'] }),
mockConfigWithAutoScrollDuration,
taskInstanceFields,
{
es: mockEsClient,
data: {
...mockDataClient,
search: mockDataClientSearchFn,
},
uiSettings: uiSettingsClient,
},
{
searchSourceStart: mockSearchSourceService,
fieldFormatsRegistry: mockFieldFormatsRegistry,
},
new CancellationToken(),
mockLogger,
stream
).generateData();

await jest.advanceTimersByTimeAsync(timeFromNowInMs);

expect(await generateCsvPromise).toEqual(
expect.objectContaining({
warnings: [],
})
);

expect(mockDataClientSearchFn).toBeCalledWith(
{ params: { body: {}, ignore_throttled: undefined, max_concurrent_shard_requests: 5 } },
{
abortSignal: expect.any(AbortSignal),
strategy: 'es',
transport: { maxRetries: 0, requestTimeout: `${timeFromNowInMs}ms` },
}
);

expect(content).toMatchSnapshot();
});

it('csv generation errors if search request does not resolve before the computed timeout value passed to the search data client elapses', async () => {
const timeFromNowInMs = 4 * 60 * 1000;

const taskInstanceFields = getTaskInstanceFields({
seconds: timeFromNowInMs / 1000,
});

const requestDuration = timeFromNowInMs + 1000;

mockDataClientSearchFn.mockImplementation((_, options) => {
const getSearchResult = () => {
const queuedAt = Date.now();

return new Promise<IKibanaSearchResponse<ReturnType<typeof getMockRawResponse>>>(
(resolve, reject) => {
setTimeout(() => {
if (
new Date(Date.now()).getTime() - new Date(queuedAt).getTime() >
Number((options?.transport?.requestTimeout! as string).replace(/ms/, ''))
) {
reject(
new esErrors.ResponseError({ statusCode: 408, meta: {} as any, warnings: [] })
);
} else {
resolve({
rawResponse: getMockRawResponse(
[
{
fields: { a: ['a1'], b: ['b1'] },
} as unknown as estypes.SearchHit,
],
3
),
});
}
}, requestDuration);
}
);
};

return Rx.defer(getSearchResult);
});

const generateCsvPromise = new CsvGenerator(
createMockJob({ searchSource: {}, columns: ['a', 'b'] }),
mockConfigWithAutoScrollDuration,
taskInstanceFields,
{
es: mockEsClient,
data: {
...mockDataClient,
search: mockDataClientSearchFn,
},
uiSettings: uiSettingsClient,
},
{
searchSourceStart: mockSearchSourceService,
fieldFormatsRegistry: mockFieldFormatsRegistry,
},
new CancellationToken(),
mockLogger,
stream
).generateData();

await jest.advanceTimersByTimeAsync(requestDuration);

expect(await generateCsvPromise).toEqual(
expect.objectContaining({
warnings: expect.arrayContaining([
expect.stringContaining('Received a 408 response from Elasticsearch'),
]),
})
);

expect(mockDataClientSearchFn).toBeCalledWith(
{ params: { body: {}, ignore_throttled: undefined, max_concurrent_shard_requests: 5 } },
{
abortSignal: expect.any(AbortSignal),
strategy: 'es',
transport: { maxRetries: 0, requestTimeout: `${timeFromNowInMs}ms` },
}
);
});
});

describe('Scroll strategy', () => {
const mockJobUsingScrollPaging = createMockJob({
columns: ['date', 'ip', 'message'],
Expand Down Expand Up @@ -654,7 +863,11 @@ describe('CsvGenerator', () => {
max_concurrent_shard_requests: 5,
}),
},
{ strategy: 'es', transport: { maxRetries: 0, requestTimeout: '30s' } }
{
abortSignal: expect.any(AbortSignal),
strategy: 'es',
transport: { maxRetries: 0, requestTimeout: '30s' },
}
);

expect(mockEsClient.asCurrentUser.openPointInTime).not.toHaveBeenCalled();
Expand Down Expand Up @@ -1200,17 +1413,12 @@ describe('CsvGenerator', () => {
index: 'logstash-*',
keep_alive: '30s',
},
{ maxConcurrentShardRequests: 5, maxRetries: 0, requestTimeout: '30s' }
);

expect(mockEsClient.asCurrentUser.openPointInTime).toHaveBeenCalledWith(
{
ignore_unavailable: true,
ignore_throttled: false,
index: 'logstash-*',
keep_alive: '30s',
},
{ maxConcurrentShardRequests: 5, maxRetries: 0, requestTimeout: '30s' }
maxConcurrentShardRequests: 5,
maxRetries: 0,
requestTimeout: '30s',
signal: expect.any(AbortSignal),
}
);

expect(mockDataClient.search).toBeCalledWith(
Expand All @@ -1220,7 +1428,11 @@ describe('CsvGenerator', () => {
max_concurrent_shard_requests: 5,
},
},
{ strategy: 'es', transport: { maxRetries: 0, requestTimeout: '30s' } }
{
abortSignal: expect.any(AbortSignal),
strategy: 'es',
transport: { maxRetries: 0, requestTimeout: '30s' },
}
);
});

Expand Down
Loading

0 comments on commit 56d75ec

Please sign in to comment.