-
Notifications
You must be signed in to change notification settings - Fork 113
/
RestStopRollupActionIT.kt
322 lines (284 loc) · 14.4 KB
/
RestStopRollupActionIT.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.indexmanagement.rollup.resthandler
import org.opensearch.client.Request
import org.opensearch.client.ResponseException
import org.opensearch.common.settings.Settings
import org.opensearch.core.rest.RestStatus
import org.opensearch.indexmanagement.IndexManagementIndices
import org.opensearch.indexmanagement.IndexManagementPlugin
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.INDEX_MANAGEMENT_INDEX
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.ROLLUP_JOBS_BASE_URI
import org.opensearch.indexmanagement.common.model.dimension.DateHistogram
import org.opensearch.indexmanagement.common.model.dimension.Terms
import org.opensearch.indexmanagement.indexstatemanagement.util.INDEX_HIDDEN
import org.opensearch.indexmanagement.indexstatemanagement.util.INDEX_NUMBER_OF_SHARDS
import org.opensearch.indexmanagement.makeRequest
import org.opensearch.indexmanagement.randomInstant
import org.opensearch.indexmanagement.rollup.model.Rollup
import org.opensearch.indexmanagement.rollup.model.RollupMetadata
import org.opensearch.indexmanagement.rollup.randomRollup
import org.opensearch.indexmanagement.waitFor
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Locale
class RestStopRollupActionIT : RollupRestAPITestCase() {
private val testName = javaClass.simpleName.lowercase(Locale.ROOT)
@Throws(Exception::class)
fun `test stopping a started rollup`() {
val rollup = createRollup(randomRollup().copy(enabled = true, jobEnabledTime = randomInstant(), metadataID = null), rollupId = "$testName-1")
assertTrue("Rollup was not enabled", rollup.enabled)
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
val updatedRollup = getRollup(rollup.id)
assertFalse("Rollup was not disabled", updatedRollup.enabled)
}
@Throws(Exception::class)
fun `test stopping a stopped rollup`() {
val rollup = createRollup(randomRollup().copy(enabled = true, jobEnabledTime = randomInstant(), metadataID = null), rollupId = "$testName-2")
assertTrue("Rollup was not enabled", rollup.enabled)
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
val updatedRollup = getRollup(rollup.id)
assertFalse("Rollup was not disabled", updatedRollup.enabled)
val secondResponse = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, secondResponse.restStatus())
val expectedSecondResponse = mapOf("acknowledged" to true)
assertEquals(expectedSecondResponse, secondResponse.asMap())
val updatedSecondRollup = getRollup(rollup.id)
assertFalse("Rollup was not disabled", updatedSecondRollup.enabled)
}
@Throws(Exception::class)
fun `test stopping a finished rollup`() {
// Create a rollup that finishes
val rollup =
createRollup(
randomRollup()
.copy(
continuous = false,
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
enabled = true,
jobEnabledTime = Instant.now(),
metadataID = null,
),
rollupId = "$testName-3",
)
createRollupSourceIndex(rollup)
updateRollupStartTime(rollup)
// Assert it finished
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup never finished", RollupMetadata.Status.FINISHED, metadata.status)
// Waiting for job to be disabled here to avoid version conflict exceptions later on
assertFalse("Job was not disabled", updatedRollup.enabled)
}
// Try to stop a finished rollup
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
// Assert it is still in finished status
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup should have stayed finished", RollupMetadata.Status.FINISHED, metadata.status)
}
}
@Throws(Exception::class)
fun `test stopping a failed rollup`() {
// Create a rollup that will fail because no source index
val rollup =
randomRollup().copy(
id = "test_stopping_a_failed_rollup",
continuous = false,
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
enabled = true,
jobEnabledTime = Instant.now(),
metadataID = null,
).let { createRollup(it, it.id) }
updateRollupStartTime(rollup)
// Assert its in failed
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup never failed", RollupMetadata.Status.FAILED, metadata.status)
}
// Stop rollup
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
// Assert rollup still failed status
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup should have stayed failed", RollupMetadata.Status.FAILED, metadata.status)
}
}
@Throws(Exception::class)
fun `test stopping a retry rollup`() {
// Create a rollup job
val rollup =
createRollup(
randomRollup()
.copy(
continuous = false,
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
enabled = true,
jobEnabledTime = Instant.now(),
metadataID = null,
),
rollupId = "$testName-4",
)
// Force rollup to execute which should fail as we did not create a source index
updateRollupStartTime(rollup)
// Assert rollup is in failed status
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup never failed (no source index)", RollupMetadata.Status.FAILED, metadata.status)
}
// Start job to set it into retry status
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_start")
assertEquals("Start rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
// Assert the job is in retry status
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup is not in RETRY", RollupMetadata.Status.RETRY, metadata.status)
}
// Stop the job which is currently in retry status
val responseTwo = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, responseTwo.restStatus())
val expectedResponseTwo = mapOf("acknowledged" to true)
assertEquals(expectedResponseTwo, responseTwo.asMap())
// Assert the job correctly went back to failed and not stopped
waitFor {
val updatedRollup = getRollup(rollup.id)
val metadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup should have stayed finished", RollupMetadata.Status.FAILED, metadata.status)
}
}
@Throws(Exception::class)
fun `test stopping rollup with metadata`() {
generateNYCTaxiData("source")
val rollup =
Rollup(
id = "basic_term_query",
schemaVersion = 1L,
enabled = true,
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
jobLastUpdatedTime = Instant.now(),
jobEnabledTime = Instant.now(),
description = "basic search test",
sourceIndex = "source",
targetIndex = "target",
metadataID = null,
roles = emptyList(),
pageSize = 10,
delay = 0,
continuous = true,
dimensions =
listOf(
DateHistogram(sourceField = "tpep_pickup_datetime", fixedInterval = "1h"),
Terms("RatecodeID", "RatecodeID"),
Terms("PULocationID", "PULocationID"),
),
metrics = emptyList(),
).let { createRollup(it, it.id) }
updateRollupStartTime(rollup)
waitFor {
val rollupJob = getRollup(rollupId = rollup.id)
assertNotNull("Rollup job doesn't have metadata set", rollupJob.metadataID)
val rollupMetadata = getRollupMetadata(rollupJob.metadataID!!)
assertEquals("Rollup is not STARTED", RollupMetadata.Status.STARTED, rollupMetadata.status)
// There are two calls to _stop happening serially which is prone to version conflicts during an ongoing job
// so including it in a waitFor to ensure it can retry a few times
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
}
val updatedRollup = getRollup(rollup.id)
assertFalse("Rollup was not disabled", updatedRollup.enabled)
val rollupMetadata = getRollupMetadata(updatedRollup.metadataID!!)
assertEquals("Rollup is not STOPPED", RollupMetadata.Status.STOPPED, rollupMetadata.status)
}
@Throws(Exception::class)
fun `test stop a rollup with no id fails`() {
try {
client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI//_stop")
fail("Expected 400 Method BAD_REQUEST response")
} catch (e: ResponseException) {
assertEquals("Unexpected status", RestStatus.BAD_REQUEST, e.response.restStatus())
}
}
fun `test stop rollup when multiple shards configured for IM config index`() {
// setup ism-config index with multiple primary shards
val deleteISMIndexRequest = Request("DELETE", "/$INDEX_MANAGEMENT_INDEX")
adminClient().performRequest(deleteISMIndexRequest)
val mapping = IndexManagementIndices.indexManagementMappings.trim().trimStart('{').trimEnd('}')
val settings =
Settings.builder()
.put(INDEX_HIDDEN, true)
.put(INDEX_NUMBER_OF_SHARDS, 5)
.build()
createIndex(IndexManagementPlugin.INDEX_MANAGEMENT_INDEX, settings, mapping)
assertIndexExists(IndexManagementPlugin.INDEX_MANAGEMENT_INDEX)
generateNYCTaxiData("source_multi_shard_stop")
val rollup =
Rollup(
id = "multi_shard_stop",
schemaVersion = 1L,
enabled = true,
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
jobLastUpdatedTime = Instant.now(),
jobEnabledTime = Instant.now(),
description = "basic search test",
sourceIndex = "source_multi_shard_stop",
targetIndex = "target_multi_shard_stop",
metadataID = null,
roles = emptyList(),
pageSize = 1,
delay = 0,
continuous = true,
dimensions =
listOf(
DateHistogram(sourceField = "tpep_pickup_datetime", fixedInterval = "1h"),
Terms("RatecodeID", "RatecodeID"),
Terms("PULocationID", "PULocationID"),
),
metrics = emptyList(),
).let { createRollup(it, it.id) }
// The updateRollupStartTime call can be missed if the job scheduler hasn't started listening to the new index yet,
// sleep a bit to let it initialize
Thread.sleep(2000L)
updateRollupStartTime(rollup)
waitFor {
val rollupJob = getRollup(rollupId = rollup.id)
assertNotNull("Rollup job doesn't have metadata set", rollupJob.metadataID)
}
val response = client().makeRequest("POST", "$ROLLUP_JOBS_BASE_URI/${rollup.id}/_stop")
assertEquals("Stop rollup failed", RestStatus.OK, response.restStatus())
val expectedResponse = mapOf("acknowledged" to true)
assertEquals(expectedResponse, response.asMap())
val updatedRollup = getRollup(rollup.id)
assertFalse("Rollup was not disabled", updatedRollup.enabled)
val rollupMetadata = getRollupMetadataWithRoutingId(rollup.id, updatedRollup.metadataID!!)
assertEquals("Rollup is not STOPPED", RollupMetadata.Status.STOPPED, rollupMetadata.status)
// clearing the config index to prevent other tests using this multi shard index
Thread.sleep(2000L)
adminClient().performRequest(deleteISMIndexRequest)
Thread.sleep(2000L)
}
}