Skip to content

Commit d87d83e

Browse files
committed
initialize serde failed flag to false
Signed-off-by: Surya Sashank Nistala <[email protected]>
1 parent d30dac5 commit d87d83e

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

src/main/kotlin/org/opensearch/commons/alerting/action/DocLevelMonitorFanOutRequest.kt

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
4040
val workflowRunContext: WorkflowRunContext?
4141
val hasSerializationFailed: Boolean
4242

43+
init {
44+
serializationFailedFlag = false
45+
}
4346
companion object {
4447
// flag flipped to true whenever a safeRead*() method fails to serialize a field correctly
4548
private var serializationFailedFlag: Boolean = false
@@ -99,7 +102,7 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
99102
} catch (e: Exception) {
100103
serializationFailedFlag = true
101104
log.error("Error parsing shardId list in Doc level monitor fanout request", e)
102-
listOf(ShardId("failed_serde", "failed_serde", 999999))
105+
emptyList()
103106
}
104107

105108
private fun safeReadStringList(sin: StreamInput): List<String> =
@@ -124,7 +127,8 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
124127
var indexExecutionContext: IndexExecutionContext? = null
125128
return try {
126129
indexExecutionContext = IndexExecutionContext(sin)
127-
while (sin.read() != -1) {
130+
while (sin.read() != 0) {
131+
serializationFailedFlag = true
128132
// read and discard bytes until stream is entirely consumed
129133
try {
130134
sin.readByte()
@@ -137,7 +141,7 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
137141
} catch (e: Exception) {
138142
serializationFailedFlag = true
139143
log.error("Error parsing index execution context in Doc level monitor fanout request", e)
140-
while (sin.read() != -1) {
144+
while (sin.read() != 0) {
141145
try { // read and throw bytes until stream is entirely consumed
142146
sin.readByte()
143147
} catch (_: EOFException) {
@@ -148,7 +152,7 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
148152
}
149153
}
150154

151-
constructor(
155+
private constructor(
152156
monitor: Monitor,
153157
dryRun: Boolean,
154158
monitorMetadata: MonitorMetadata,
@@ -157,7 +161,7 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
157161
shardIds: List<ShardId>,
158162
concreteIndicesSeenSoFar: List<String>,
159163
workflowRunContext: WorkflowRunContext?,
160-
hasSerializationFailed: Boolean? = null
164+
hasSerializationFailed: Boolean
161165
) : super() {
162166
this.monitor = monitor
163167
this.dryRun = dryRun
@@ -167,10 +171,30 @@ class DocLevelMonitorFanOutRequest : ActionRequest, ToXContentObject {
167171
this.shardIds = shardIds
168172
this.concreteIndicesSeenSoFar = concreteIndicesSeenSoFar
169173
this.workflowRunContext = workflowRunContext
170-
require(false == shardIds.isEmpty()) { }
171174
this.hasSerializationFailed = hasSerializationFailed ?: false
172175
}
173176

177+
constructor(
178+
monitor: Monitor,
179+
dryRun: Boolean,
180+
monitorMetadata: MonitorMetadata,
181+
executionId: String,
182+
indexExecutionContext: IndexExecutionContext?,
183+
shardIds: List<ShardId>,
184+
concreteIndicesSeenSoFar: List<String>,
185+
workflowRunContext: WorkflowRunContext?
186+
) : super() {
187+
this.monitor = monitor
188+
this.dryRun = dryRun
189+
this.monitorMetadata = monitorMetadata
190+
this.executionId = executionId
191+
this.indexExecutionContext = indexExecutionContext
192+
this.shardIds = shardIds
193+
this.concreteIndicesSeenSoFar = concreteIndicesSeenSoFar
194+
this.workflowRunContext = workflowRunContext
195+
this.hasSerializationFailed = false
196+
}
197+
174198
@Throws(IOException::class)
175199
constructor(sin: StreamInput) : this(
176200
monitor = safeReadMonitor(sin),

src/test/kotlin/org/opensearch/commons/alerting/action/DocLevelMonitorFanOutRequestTests.kt

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class DocLevelMonitorFanOutRequestTests {
9191
assertEquals(docLevelMonitorFanOutRequest.indexExecutionContext, newDocLevelMonitorFanOutRequest.indexExecutionContext)
9292
assertEquals(docLevelMonitorFanOutRequest.shardIds, newDocLevelMonitorFanOutRequest.shardIds)
9393
assertEquals(docLevelMonitorFanOutRequest.workflowRunContext, newDocLevelMonitorFanOutRequest.workflowRunContext)
94-
assertEquals(sin.read(), -1)
94+
assertEquals(sin.read(), 0)
9595
assertFalse(newDocLevelMonitorFanOutRequest.hasSerializationFailed)
9696
}
9797

@@ -156,7 +156,7 @@ class DocLevelMonitorFanOutRequestTests {
156156
assertEquals(docLevelMonitorFanOutRequest.shardIds, newDocLevelMonitorFanOutRequest.shardIds)
157157
assertEquals(docLevelMonitorFanOutRequest.workflowRunContext, newDocLevelMonitorFanOutRequest.workflowRunContext)
158158
assertFalse(newDocLevelMonitorFanOutRequest.hasSerializationFailed)
159-
assertEquals(sin.read(), -1)
159+
assertEquals(sin.read().toString(), sin.read(), 0)
160160
}
161161

162162
@Test
@@ -217,4 +217,68 @@ class DocLevelMonitorFanOutRequestTests {
217217
assertTrue(newDocLevelMonitorFanOutRequest.hasSerializationFailed)
218218
assertEquals(sin.read(), -1)
219219
}
220+
221+
@Test
222+
fun `test doc level monitor fan out request as stream when there are additional bytes left to handle`() {
223+
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", fields = listOf(), name = "3")
224+
val docLevelInput = DocLevelMonitorInput("description", listOf("test-index"), listOf(docQuery))
225+
226+
val trigger = randomDocumentLevelTrigger(condition = Script("return true"))
227+
val monitor = randomDocumentLevelMonitor(
228+
inputs = listOf(docLevelInput),
229+
triggers = listOf(trigger),
230+
enabled = true,
231+
schedule = IntervalSchedule(1, ChronoUnit.MINUTES)
232+
)
233+
val monitorMetadata = MonitorMetadata(
234+
"test",
235+
SequenceNumbers.UNASSIGNED_SEQ_NO,
236+
SequenceNumbers.UNASSIGNED_PRIMARY_TERM,
237+
Monitor.NO_ID,
238+
listOf(ActionExecutionTime("", Instant.now())),
239+
mutableMapOf("index" to mutableMapOf("1" to "1")),
240+
mutableMapOf("test-index" to ".opensearch-sap-test_windows-queries-000001")
241+
)
242+
val indexExecutionContext = IndexExecutionContext(
243+
listOf(docQuery),
244+
mutableMapOf("index" to mutableMapOf("1" to "1")),
245+
mutableMapOf("index" to mutableMapOf("1" to "1")),
246+
"test-index",
247+
"test-index",
248+
listOf("test-index"),
249+
listOf("test-index"),
250+
listOf("test-field"),
251+
listOf("1", "2")
252+
)
253+
val workflowRunContext = WorkflowRunContext(
254+
Workflow.NO_ID,
255+
Workflow.NO_ID,
256+
Monitor.NO_ID,
257+
mutableMapOf("index" to listOf("1")),
258+
true
259+
)
260+
val docLevelMonitorFanOutRequest = DocLevelMonitorFanOutRequest(
261+
monitor,
262+
false,
263+
monitorMetadata,
264+
UUID.randomUUID().toString(),
265+
indexExecutionContext,
266+
listOf(ShardId("test-index", UUID.randomUUID().toString(), 0)),
267+
listOf("test-index"),
268+
workflowRunContext
269+
)
270+
val out = BytesStreamOutput()
271+
docLevelMonitorFanOutRequest.writeTo(out)
272+
out.writeByte(Byte.MIN_VALUE)
273+
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
274+
val newDocLevelMonitorFanOutRequest = DocLevelMonitorFanOutRequest(sin)
275+
assertEquals(docLevelMonitorFanOutRequest.monitor, newDocLevelMonitorFanOutRequest.monitor)
276+
assertEquals(docLevelMonitorFanOutRequest.executionId, newDocLevelMonitorFanOutRequest.executionId)
277+
assertEquals(docLevelMonitorFanOutRequest.monitorMetadata, newDocLevelMonitorFanOutRequest.monitorMetadata)
278+
assertEquals(docLevelMonitorFanOutRequest.indexExecutionContext, newDocLevelMonitorFanOutRequest.indexExecutionContext)
279+
assertEquals(docLevelMonitorFanOutRequest.shardIds, newDocLevelMonitorFanOutRequest.shardIds)
280+
assertEquals(docLevelMonitorFanOutRequest.workflowRunContext, newDocLevelMonitorFanOutRequest.workflowRunContext)
281+
assertEquals(sin.read(), 0)
282+
assertTrue(newDocLevelMonitorFanOutRequest.hasSerializationFailed)
283+
}
220284
}

0 commit comments

Comments
 (0)