@@ -7,17 +7,24 @@ package org.opensearch.commons.alerting.action
7
7
8
8
import org.opensearch.action.ActionRequest
9
9
import org.opensearch.action.ActionRequestValidationException
10
+ import org.opensearch.commons.alerting.model.DataSources
10
11
import org.opensearch.commons.alerting.model.IndexExecutionContext
12
+ import org.opensearch.commons.alerting.model.IntervalSchedule
11
13
import org.opensearch.commons.alerting.model.Monitor
14
+ import org.opensearch.commons.alerting.model.Monitor.Companion.NO_VERSION
12
15
import org.opensearch.commons.alerting.model.MonitorMetadata
13
16
import org.opensearch.commons.alerting.model.WorkflowRunContext
17
+ import org.opensearch.commons.alerting.util.IndexUtils.Companion.NO_SCHEMA_VERSION
14
18
import org.opensearch.core.common.io.stream.StreamInput
15
19
import org.opensearch.core.common.io.stream.StreamOutput
16
20
import org.opensearch.core.index.shard.ShardId
17
21
import org.opensearch.core.xcontent.ToXContent
18
22
import org.opensearch.core.xcontent.ToXContentObject
19
23
import org.opensearch.core.xcontent.XContentBuilder
24
+ import org.opensearch.index.seqno.SequenceNumbers
20
25
import java.io.IOException
26
+ import java.time.Instant
27
+ import java.time.temporal.ChronoUnit
21
28
22
29
class DocLevelMonitorFanOutRequest : ActionRequest , ToXContentObject {
23
30
val monitor: Monitor
@@ -29,6 +36,80 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
29
36
val concreteIndicesSeenSoFar: List <String >
30
37
val workflowRunContext: WorkflowRunContext ?
31
38
39
+ companion object {
40
+ private fun safeReadMonitor (sin : StreamInput ): Monitor =
41
+ try {
42
+ Monitor .readFrom(sin)!!
43
+ } catch (e: Exception ) {
44
+ Monitor (
45
+ " failed_serde" , NO_VERSION , " failed_serde" , true ,
46
+ IntervalSchedule (1 , ChronoUnit .MINUTES ), Instant .now(), Instant .now(), " " ,
47
+ null , NO_SCHEMA_VERSION , emptyList(), emptyList(), emptyMap(),
48
+ DataSources (), false , false , " failed"
49
+ )
50
+ }
51
+
52
+ private fun safeReadBoolean (sin : StreamInput ): Boolean =
53
+ try {
54
+ sin.readBoolean()
55
+ } catch (e: Exception ) {
56
+ false
57
+ }
58
+
59
+ private fun safeReadMonitorMetadata (sin : StreamInput ): MonitorMetadata =
60
+ try {
61
+ MonitorMetadata .readFrom(sin)
62
+ } catch (e: Exception ) {
63
+ MonitorMetadata (
64
+ " failed_serde" ,
65
+ SequenceNumbers .UNASSIGNED_SEQ_NO ,
66
+ SequenceNumbers .UNASSIGNED_PRIMARY_TERM ,
67
+ " failed_serde" ,
68
+ emptyList(),
69
+ emptyMap(),
70
+ mutableMapOf ()
71
+ )
72
+ }
73
+
74
+ private fun safeReadString (sin : StreamInput ): String =
75
+ try {
76
+ sin.readString()
77
+ } catch (e: Exception ) {
78
+ " "
79
+ }
80
+
81
+ private fun safeReadShardIds (sin : StreamInput ): List <ShardId > =
82
+ try {
83
+ sin.readList(::ShardId )
84
+ } catch (e: Exception ) {
85
+ listOf (ShardId (" failed_serde" , " failed_serde" , 999999 ))
86
+ }
87
+
88
+ private fun safeReadStringList (sin : StreamInput ): List <String > =
89
+ try {
90
+ sin.readStringList()
91
+ } catch (e: Exception ) {
92
+ emptyList()
93
+ }
94
+
95
+ private fun safeReadWorkflowRunContext (sin : StreamInput ): WorkflowRunContext ? =
96
+ try {
97
+ if (sin.readBoolean()) WorkflowRunContext (sin) else null
98
+ } catch (e: Exception ) {
99
+ null
100
+ }
101
+
102
+ private fun safeReadIndexExecutionContext (sin : StreamInput ): IndexExecutionContext =
103
+ try {
104
+ IndexExecutionContext (sin)
105
+ } catch (e: Exception ) {
106
+ IndexExecutionContext (
107
+ emptyList(), mutableMapOf (), mutableMapOf (), " failed_serde" , " failed_serde" ,
108
+ emptyList(), emptyList(), emptyList(), emptyList(), emptyList()
109
+ )
110
+ }
111
+ }
112
+
32
113
constructor (
33
114
monitor: Monitor ,
34
115
dryRun: Boolean ,
@@ -52,16 +133,14 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
52
133
53
134
@Throws(IOException ::class )
54
135
constructor (sin: StreamInput ) : this (
55
- monitor = Monitor .readFrom(sin)!! ,
56
- dryRun = sin.readBoolean(),
57
- monitorMetadata = MonitorMetadata .readFrom(sin),
58
- executionId = sin.readString(),
59
- shardIds = sin.readList(::ShardId ),
60
- concreteIndicesSeenSoFar = sin.readStringList(),
61
- workflowRunContext = if (sin.readBoolean()) {
62
- WorkflowRunContext (sin)
63
- } else { null },
64
- indexExecutionContext = IndexExecutionContext (sin)
136
+ monitor = safeReadMonitor(sin),
137
+ dryRun = safeReadBoolean(sin),
138
+ monitorMetadata = safeReadMonitorMetadata(sin),
139
+ executionId = safeReadString(sin),
140
+ shardIds = safeReadShardIds(sin),
141
+ concreteIndicesSeenSoFar = safeReadStringList(sin),
142
+ workflowRunContext = safeReadWorkflowRunContext(sin),
143
+ indexExecutionContext = safeReadIndexExecutionContext(sin)
65
144
)
66
145
67
146
@Throws(IOException ::class )
@@ -88,14 +167,14 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
88
167
89
168
@Throws(IOException ::class )
90
169
override fun toXContent (builder : XContentBuilder , params : ToXContent .Params ): XContentBuilder {
91
- builder.startObject()
170
+ return builder.startObject()
92
171
.field(" monitor" , monitor)
93
172
.field(" dry_run" , dryRun)
94
173
.field(" execution_id" , executionId)
95
174
.field(" index_execution_context" , indexExecutionContext)
96
175
.field(" shard_ids" , shardIds)
97
176
.field(" concrete_indices" , concreteIndicesSeenSoFar)
98
177
.field(" workflow_run_context" , workflowRunContext)
99
- return builder .endObject()
178
+ .endObject()
100
179
}
101
180
}
0 commit comments