Skip to content

Commit

Permalink
[Fleet] Add concurrency control to Fleet data stream API handler (ela…
Browse files Browse the repository at this point in the history
…stic#174087)

## Summary

Closes elastic#166478

Wrap data stream query logic in a `pMap` call with `concurrency: 50` to
prevent overwhelming Elasticsearch in environments with several thousand
data streams.
  • Loading branch information
kpollich authored Jan 2, 2024
1 parent f799bf6 commit 1dafbab
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions x-pack/plugins/fleet/server/routes/data_streams/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { keyBy, keys, merge } from 'lodash';
import type { RequestHandler } from '@kbn/core/server';
import pMap from 'p-map';

import type { DataStream } from '../../types';
import { KibanaSavedObjectType } from '../../../common/types';
Expand Down Expand Up @@ -120,7 +121,7 @@ export const getListHandler: RequestHandler = async (context, request, response)
);

// Query additional information for each data stream
const dataStreamPromises = dataStreamNames.map(async (dataStreamName) => {
const queryDataStreamInfo = async (dataStreamName: string) => {
const dataStream = dataStreams[dataStreamName];

const dataStreamResponse: DataStream = {
Expand Down Expand Up @@ -197,13 +198,18 @@ export const getListHandler: RequestHandler = async (context, request, response)
}

return dataStreamResponse;
});
};

// Return final data streams objects sorted by last activity, descending
// After filtering out data streams that are missing dataset/namespace/type/package fields
body.data_streams = (await Promise.all(dataStreamPromises))
body.data_streams = (
await pMap(dataStreamNames, (dataStreamName) => queryDataStreamInfo(dataStreamName), {
concurrency: 50,
})
)
.filter(({ dataset, namespace, type }) => dataset && namespace && type)
.sort((a, b) => b.last_activity_ms - a.last_activity_ms);

return response.ok({
body,
});
Expand Down

0 comments on commit 1dafbab

Please sign in to comment.