Skip to content

Commit

Permalink
Return even faster if there is nothing to do
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Feb 10, 2025
1 parent 68d3d55 commit 526979b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ describe('CleanUpTempSummary', () => {
jest.useRealTimers();
});

it('returns early if there is no temporary documents', async () => {
esClientMock.count.mockResolvedValueOnce({
count: 0,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
});

await service.execute();

expect(esClientMock.search).not.toHaveBeenCalled();
expect(esClientMock.deleteByQuery).not.toHaveBeenCalled();
});

it("deletes nothing when there isn't a duplicate temporary documents", async () => {
esClientMock.search.mockResolvedValueOnce({
...commonEsResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { SUMMARY_DESTINATION_INDEX_PATTERN } from '../../../common/constants';
import {
SUMMARY_DESTINATION_INDEX_PATTERN,
SUMMARY_TEMP_INDEX_NAME,
} from '../../../common/constants';

interface AggBucketKey {
spaceId: string;
Expand All @@ -29,6 +32,12 @@ export class CleanUpTempSummary {
constructor(private readonly esClient: ElasticsearchClient, private readonly logger: Logger) {}

public async execute(): Promise<void> {
const openCircuitBreaker = await this.shouldOpenCircuitBreaker();
if (openCircuitBreaker) {
this.logger.info('No temporary documents found, skipping.');
return;
}

let searchAfterKey: AggBucketKey | undefined;
do {
const { buckets, nextSearchAfterKey } = await this.findDuplicateTemporaryDocuments(
Expand All @@ -42,6 +51,11 @@ export class CleanUpTempSummary {
} while (searchAfterKey);
}

private async shouldOpenCircuitBreaker() {
const results = await this.esClient.count({ index: SUMMARY_TEMP_INDEX_NAME });
return results.count === 0;
}

private async findDuplicateTemporaryDocuments(searchAfterKey: AggBucketKey | undefined) {
this.logger.info('Searching for duplicate temporary documents');
const results = await this.esClient.search<unknown, AggResults>({
Expand Down

0 comments on commit 526979b

Please sign in to comment.