diff --git a/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.test.ts b/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.test.ts index bc7b08fa719c1..86bfc73fd1e00 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.test.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.test.ts @@ -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, diff --git a/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.ts b/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.ts index 221693944b8a5..7f120ee1db06e 100644 --- a/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.ts +++ b/x-pack/solutions/observability/plugins/slo/server/services/management/clean_up_temp_summary.ts @@ -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; @@ -29,6 +32,12 @@ export class CleanUpTempSummary { constructor(private readonly esClient: ElasticsearchClient, private readonly logger: Logger) {} public async execute(): Promise { + 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( @@ -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({