From f00c4e54d060cb3f8cefdbfc46ed58dd7ec31454 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Wed, 30 Aug 2023 16:52:00 -0400 Subject: [PATCH 1/3] [ResponseOps] experiment: make all alert doc updates partial work in progress --- x-pack/plugins/alerting/server/alerts_client/alerts_client.ts | 1 + .../rule_registry/server/utils/create_lifecycle_executor.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 03500c8b94575..06f4408a517e5 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -428,6 +428,7 @@ export class AlertsClient< ) { if (index && seqNo != null && primaryTerm != null) { return { + // this code will change to use update, and not the if_* fields index: { _id: uuid, _index: index, diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index f91f6dfdf72d0..da685bdeaebc3 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -353,6 +353,7 @@ export const createLifecycleExecutor = body: allEventsToIndex.flatMap(({ event, indexName, seqNo, primaryTerm }) => [ indexName ? { + // this code will change to use update, and not the if_* fields index: { _id: event[ALERT_UUID]!, _index: indexName, From 4e916f6974d0c2c6df827bad039c335f9c73cde0 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Wed, 30 Aug 2023 18:42:57 -0400 Subject: [PATCH 2/3] make the change to do partial updates instead of re-index --- .../alerting/server/alerts_client/alerts_client.ts | 12 ++++++++---- .../server/utils/create_lifecycle_executor.ts | 7 +++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 06f4408a517e5..af6ed961f92a2 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -388,7 +388,7 @@ export class AlertsClient< this.fetchedAlerts.primaryTerm[alert.kibana.alert.uuid], this.isUsingDataStreams() ), - alert, + getBulkDocument(this.fetchedAlerts.indices[alert.kibana.alert.uuid], alert), ]) ); @@ -419,6 +419,12 @@ export class AlertsClient< } } + function getBulkDocument(index: string | null | undefined, alert: Alert & AlertData) { + // if index provided, must be an update, so wrap in a { doc: {} } + if (index) return { doc: alert }; + return alert; + } + function getBulkMeta( uuid: string, index: string | undefined, @@ -429,11 +435,9 @@ export class AlertsClient< if (index && seqNo != null && primaryTerm != null) { return { // this code will change to use update, and not the if_* fields - index: { + update: { _id: uuid, _index: index, - if_seq_no: seqNo, - if_primary_term: primaryTerm, require_alias: false, }, }; diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index da685bdeaebc3..70e91627d7936 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -354,11 +354,9 @@ export const createLifecycleExecutor = indexName ? { // this code will change to use update, and not the if_* fields - index: { + update: { _id: event[ALERT_UUID]!, _index: indexName, - if_seq_no: seqNo, - if_primary_term: primaryTerm, require_alias: false, }, } @@ -367,7 +365,8 @@ export const createLifecycleExecutor = _id: event[ALERT_UUID]!, }, }, - event, + // if index provided, must be an update, so wrap in a { doc: {} } + ...(indexName ? { doc: event } : event), ]), refresh: 'wait_for', }); From fadf8a792ef2e1bdd1fd807bc376fd33c8c3f303 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Thu, 31 Aug 2023 19:33:21 -0400 Subject: [PATCH 3/3] oops, one of those expressions was a little off heh --- .../rule_registry/server/utils/create_lifecycle_executor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index 70e91627d7936..95933defbce8d 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -366,7 +366,7 @@ export const createLifecycleExecutor = }, }, // if index provided, must be an update, so wrap in a { doc: {} } - ...(indexName ? { doc: event } : event), + indexName ? { doc: event } : event, ]), refresh: 'wait_for', });