-
Notifications
You must be signed in to change notification settings - Fork 113
/
ManagedIndexUtils.kt
571 lines (506 loc) · 23.6 KB
/
ManagedIndexUtils.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
@file:Suppress("TooManyFunctions", "MatchingDeclarationName")
@file:JvmName("ManagedIndexUtils")
package org.opensearch.indexmanagement.indexstatemanagement.util
// import inet.ipaddr.IPAddressString
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
// import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.opensearch.action.delete.DeleteRequest
import org.opensearch.action.get.GetRequest
import org.opensearch.action.get.GetResponse
import org.opensearch.action.index.IndexRequest
import org.opensearch.action.search.SearchRequest
import org.opensearch.action.support.WriteRequest
import org.opensearch.action.update.UpdateRequest
// import org.opensearch.alerting.destination.message.BaseMessage
import org.opensearch.client.Client
import org.opensearch.common.unit.ByteSizeValue
import org.opensearch.common.unit.TimeValue
import org.opensearch.common.xcontent.LoggingDeprecationHandler
import org.opensearch.core.xcontent.NamedXContentRegistry
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.common.xcontent.XContentFactory
import org.opensearch.common.xcontent.XContentHelper
import org.opensearch.common.xcontent.XContentType
import org.opensearch.index.query.BoolQueryBuilder
import org.opensearch.index.query.QueryBuilders
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.INDEX_MANAGEMENT_INDEX
import org.opensearch.indexmanagement.indexstatemanagement.ManagedIndexCoordinator
import org.opensearch.indexmanagement.indexstatemanagement.action.DeleteAction
import org.opensearch.indexmanagement.indexstatemanagement.action.RolloverAction
import org.opensearch.indexmanagement.indexstatemanagement.action.TransitionsAction
import org.opensearch.indexmanagement.indexstatemanagement.model.ChangePolicy
import org.opensearch.indexmanagement.indexstatemanagement.model.ISMTemplate
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.model.State
import org.opensearch.indexmanagement.indexstatemanagement.model.Transition
import org.opensearch.indexmanagement.indexstatemanagement.model.coordinator.SweptManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings
import org.opensearch.indexmanagement.opensearchapi.optionalISMTemplateField
import org.opensearch.indexmanagement.opensearchapi.optionalTimeField
import org.opensearch.indexmanagement.opensearchapi.parseWithType
import org.opensearch.indexmanagement.opensearchapi.suspendUntil
import org.opensearch.indexmanagement.spi.indexstatemanagement.Action
import org.opensearch.indexmanagement.spi.indexstatemanagement.Step
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionRetry
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.PolicyRetryInfoMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StateMetaData
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule
import org.opensearch.search.builder.SearchSourceBuilder
import java.time.Instant
import java.time.temporal.ChronoUnit
@Suppress("LongParameterList")
fun managedIndexConfigIndexRequest(
index: String,
uuid: String,
policyID: String,
jobInterval: Int,
policy: Policy? = null,
jobJitter: Double?
): IndexRequest {
val managedIndexConfig = ManagedIndexConfig(
jobName = index,
index = index,
indexUuid = uuid,
enabled = true,
jobSchedule = IntervalSchedule(Instant.now(), jobInterval, ChronoUnit.MINUTES),
jobLastUpdatedTime = Instant.now(),
jobEnabledTime = Instant.now(),
policyID = policyID,
policy = policy,
policySeqNo = policy?.seqNo,
policyPrimaryTerm = policy?.primaryTerm,
changePolicy = null,
jobJitter = jobJitter
)
return IndexRequest(INDEX_MANAGEMENT_INDEX)
.id(uuid)
.create(true)
.routing(managedIndexConfig.indexUuid)
.source(managedIndexConfig.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
}
fun managedIndexConfigIndexRequest(managedIndexConfig: ManagedIndexConfig): IndexRequest {
return IndexRequest(INDEX_MANAGEMENT_INDEX)
.id(managedIndexConfig.indexUuid)
.setIfPrimaryTerm(managedIndexConfig.primaryTerm)
.setIfSeqNo(managedIndexConfig.seqNo)
.routing(managedIndexConfig.indexUuid) // we want job doc and its metadata doc be routed to same shard
.source(managedIndexConfig.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
}
const val METADATA_POST_FIX = "#metadata"
fun managedIndexMetadataID(indexUuid: String) =
indexUuid + METADATA_POST_FIX
fun revertManagedIndexMetadataID(metadataID: String) =
metadataID.dropLast(METADATA_POST_FIX.length)
fun managedIndexMetadataIndexRequest(managedIndexMetadata: ManagedIndexMetaData, waitRefresh: Boolean = true, create: Boolean = false): IndexRequest {
// routing set using managed index's uuid
// so that metadata doc and managed-index doc are in the same place
val req = IndexRequest(INDEX_MANAGEMENT_INDEX)
.id(managedIndexMetadataID(managedIndexMetadata.indexUuid))
.setIfPrimaryTerm(managedIndexMetadata.primaryTerm)
.setIfSeqNo(managedIndexMetadata.seqNo)
.routing(managedIndexMetadata.indexUuid)
.create(create)
.source(managedIndexMetadata.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS, true))
if (waitRefresh)
return req.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL)
return req
}
private fun updateEnabledField(uuid: String, enabled: Boolean, enabledTime: Long?): UpdateRequest {
val builder = XContentFactory.jsonBuilder()
.startObject()
.startObject(ManagedIndexConfig.MANAGED_INDEX_TYPE)
.optionalTimeField(ManagedIndexConfig.LAST_UPDATED_TIME_FIELD, Instant.now())
.field(ManagedIndexConfig.ENABLED_FIELD, enabled)
.field(ManagedIndexConfig.ENABLED_TIME_FIELD, enabledTime)
.endObject()
.endObject()
return UpdateRequest(INDEX_MANAGEMENT_INDEX, uuid).doc(builder)
}
fun updateISMTemplateRequest(policyID: String, ismTemplates: List<ISMTemplate>, seqNo: Long, primaryTerm: Long): UpdateRequest {
val builder = XContentFactory.jsonBuilder()
.startObject()
.startObject(Policy.POLICY_TYPE)
.optionalISMTemplateField(Policy.ISM_TEMPLATE, ismTemplates)
.endObject()
.endObject()
return UpdateRequest(INDEX_MANAGEMENT_INDEX, policyID).doc(builder)
.setIfSeqNo(seqNo).setIfPrimaryTerm(primaryTerm)
}
fun updateDisableManagedIndexRequest(uuid: String): UpdateRequest {
return updateEnabledField(uuid, false, null)
}
fun updateEnableManagedIndexRequest(uuid: String): UpdateRequest {
return updateEnabledField(uuid, true, Instant.now().toEpochMilli())
}
fun deleteManagedIndexRequest(uuid: String): DeleteRequest {
return DeleteRequest(INDEX_MANAGEMENT_INDEX, uuid)
}
fun deleteManagedIndexMetadataRequest(uuid: String): DeleteRequest {
return DeleteRequest(INDEX_MANAGEMENT_INDEX, managedIndexMetadataID(uuid)).routing(uuid)
}
fun updateManagedIndexRequest(sweptManagedIndexConfig: SweptManagedIndexConfig): UpdateRequest {
return UpdateRequest(INDEX_MANAGEMENT_INDEX, sweptManagedIndexConfig.uuid)
.setIfPrimaryTerm(sweptManagedIndexConfig.primaryTerm)
.setIfSeqNo(sweptManagedIndexConfig.seqNo)
.doc(getPartialChangePolicyBuilder(sweptManagedIndexConfig.changePolicy))
}
/**
* Finds ManagedIndices that exist in [INDEX_MANAGEMENT_INDEX] that do not exist in the cluster state
* anymore which means we need to delete the [ManagedIndexConfig].
*
* @param currentIndexUuids List of current index uuids in cluster.
* @param currentManagedIndexUuids List of current managed index uuids in cluster.
* @return list of managedIndexUuids to delete.
*/
fun getManagedIndicesToDelete(
currentIndexUuids: List<String>,
currentManagedIndexUuids: List<String>
): List<String> {
return currentManagedIndexUuids.filter { currentManagedIndex ->
!currentIndexUuids.contains(currentManagedIndex)
}
}
fun getSweptManagedIndexSearchRequest(scroll: Boolean = false, size: Int = ManagedIndexCoordinator.MAX_HITS): SearchRequest {
val boolQueryBuilder = BoolQueryBuilder().filter(QueryBuilders.existsQuery(ManagedIndexConfig.MANAGED_INDEX_TYPE))
val req = SearchRequest().indices(INDEX_MANAGEMENT_INDEX)
.allowPartialSearchResults(false)
.source(
SearchSourceBuilder.searchSource()
.size(size)
.seqNoAndPrimaryTerm(true)
.fetchSource(emptyArray(), emptyArray())
.query(boolQueryBuilder)
)
if (scroll) req.scroll(TimeValue.timeValueMinutes(1))
return req
}
@Suppress("ReturnCount", "ComplexCondition")
fun Transition.evaluateConditions(
indexCreationDate: Instant,
numDocs: Long?,
indexSize: ByteSizeValue?,
transitionStartTime: Instant,
rolloverDate: Instant?,
): Boolean {
// If there are no conditions, treat as always true
if (this.conditions == null) return true
if (this.conditions.docCount != null && numDocs != null) {
return this.conditions.docCount <= numDocs
}
if (this.conditions.indexAge != null) {
val indexCreationDateMilli = indexCreationDate.toEpochMilli()
if (indexCreationDateMilli == -1L) return false // transitions cannot currently be ORd like rollover, so we must return here
val elapsedTime = Instant.now().toEpochMilli() - indexCreationDateMilli
return this.conditions.indexAge.millis <= elapsedTime
}
if (this.conditions.size != null && indexSize != null) {
return this.conditions.size <= indexSize
}
if (this.conditions.cron != null) {
// If a cron pattern matches the time between the start of "attempt_transition" to now then we consider it meeting the condition
return this.conditions.cron.getNextExecutionTime(transitionStartTime) <= Instant.now()
}
if (this.conditions.rolloverAge != null) {
val rolloverDateMilli = rolloverDate?.toEpochMilli() ?: return false
val elapsedTime = Instant.now().toEpochMilli() - rolloverDateMilli
return this.conditions.rolloverAge.millis <= elapsedTime
}
// We should never reach this
return false
}
fun Transition.hasStatsConditions(): Boolean = this.conditions?.docCount != null || this.conditions?.size != null
@Suppress("ReturnCount", "ComplexCondition")
fun RolloverAction.evaluateConditions(
indexAgeTimeValue: TimeValue,
numDocs: Long,
indexSize: ByteSizeValue,
primaryShardSize: ByteSizeValue
): Boolean {
if (this.minDocs == null &&
this.minAge == null &&
this.minSize == null &&
this.minPrimaryShardSize == null
) {
// If no conditions specified we default to true
return true
}
if (this.minDocs != null) {
if (this.minDocs <= numDocs) return true
}
if (this.minAge != null) {
if (this.minAge.millis <= indexAgeTimeValue.millis) return true
}
if (this.minSize != null) {
if (this.minSize <= indexSize) return true
}
if (this.minPrimaryShardSize != null) {
if (this.minPrimaryShardSize <= primaryShardSize) return true
}
// return false if none of the conditions were true.
return false
}
fun State.getUpdatedStateMetaData(managedIndexMetaData: ManagedIndexMetaData): StateMetaData {
// If the current ManagedIndexMetaData state does not match this state, it means we transitioned and need to update the startStartTime
val stateMetaData = managedIndexMetaData.stateMetaData
return when {
stateMetaData == null -> StateMetaData(this.name, Instant.now().toEpochMilli())
stateMetaData.name != this.name -> StateMetaData(this.name, Instant.now().toEpochMilli())
else -> stateMetaData
}
}
fun Action.shouldBackoff(actionMetaData: ActionMetaData?, actionRetry: ActionRetry?): Pair<Boolean, Long?>? {
return this.configRetry?.backoff?.shouldBackoff(actionMetaData, actionRetry)
}
@Suppress("ReturnCount")
fun Action.hasTimedOut(actionMetaData: ActionMetaData?): Boolean {
val startTime = actionMetaData?.startTime
val configTimeout = this.configTimeout
if (startTime == null || configTimeout == null) return false
return (Instant.now().toEpochMilli() - startTime) > configTimeout.timeout.millis
}
@Suppress("ReturnCount")
fun ManagedIndexMetaData.getStartingManagedIndexMetaData(
state: State?,
action: Action?,
step: Step?
): ManagedIndexMetaData {
// State can be null if the transition_to state or the current metadata state is not found in the policy
if (state == null) {
return this.copy(
policyRetryInfo = PolicyRetryInfoMetaData(true, 0),
info = mapOf("message" to "Failed to find state=${this.transitionTo ?: this.stateMetaData} in policy=${this.policyID}")
)
}
// Action can only be null if the metadata action type/actionIndex do not match in state.actions
// Step can only be null if Action is null
if (action == null || step == null) {
return this.copy(
policyRetryInfo = PolicyRetryInfoMetaData(true, 0),
info = mapOf("message" to "Failed to find action=${this.actionMetaData} in state=${this.stateMetaData}")
)
}
val updatedStateMetaData = state.getUpdatedStateMetaData(this)
val updatedActionMetaData = action.getUpdatedActionMetadata(this, state.name)
val updatedStepMetaData = step.getStartingStepMetaData(this)
return this.copy(
stateMetaData = updatedStateMetaData,
actionMetaData = updatedActionMetaData,
stepMetaData = updatedStepMetaData,
info = mapOf("message" to "Starting action ${action.type} and working on ${step.name}")
)
}
@Suppress("ReturnCount")
fun ManagedIndexMetaData.getCompletedManagedIndexMetaData(
action: Action,
step: Step
): ManagedIndexMetaData {
val updatedStepMetaData = step.getUpdatedManagedIndexMetadata(this)
val actionMetaData = updatedStepMetaData.actionMetaData ?: return this.copy(
policyRetryInfo = PolicyRetryInfoMetaData(true, 0),
info = mapOf("message" to "Failed due to ActionMetaData being null")
)
val updatedActionMetaData = if (updatedStepMetaData.stepMetaData?.stepStatus == Step.StepStatus.FAILED) {
when {
action.configRetry == null -> actionMetaData.copy(failed = true)
actionMetaData.consumedRetries >= action.configRetry!!.count -> actionMetaData.copy(failed = true)
else -> actionMetaData.copy(
failed = false,
consumedRetries = actionMetaData.consumedRetries + 1,
lastRetryTime = Instant.now().toEpochMilli()
)
}
} else {
actionMetaData
}
return this.copy(
policyCompleted = updatedStepMetaData.policyCompleted,
rolledOver = updatedStepMetaData.rolledOver,
actionMetaData = updatedActionMetaData,
stepMetaData = updatedStepMetaData.stepMetaData,
transitionTo = updatedStepMetaData.transitionTo,
policyRetryInfo = updatedStepMetaData.policyRetryInfo,
info = updatedStepMetaData.info
)
}
val ManagedIndexMetaData.isSuccessfulDelete: Boolean
get() = (this.actionMetaData?.name == DeleteAction.name && !this.actionMetaData!!.failed) &&
(this.stepMetaData?.name == DeleteAction.name && this.stepMetaData!!.stepStatus == Step.StepStatus.COMPLETED) &&
(this.policyRetryInfo?.failed != true)
val ManagedIndexMetaData.isFailed: Boolean
get() {
// If PolicyRetryInfo is failed then the ManagedIndex has failed.
if (this.policyRetryInfo?.failed == true) return true
// If ActionMetaData is not null and some action is failed. Then the ManagedIndex has failed.
if (this.actionMetaData?.failed == true) return true
return false
}
// Adding predicate extension to allow cleaner checks since policyCompleted is nullable
val ManagedIndexMetaData.isPolicyCompleted: Boolean
get() = this.policyCompleted == true
/**
* We will change the policy if a change policy exists and if we are currently in
* a Transitions action (which means we're safely at the end of a state). If a
* transitionTo exists on the [ManagedIndexMetaData] it should still be fine to
* change policy as we have not actually transitioned yet. If the next action is Transition
* or if the rest API determined it was "safe", meaning the new policy has the same structure
* of the current state, it should be safe to immediately change (even in the middle of the state).
*
* @param managedIndexMetaData current [ManagedIndexMetaData]
* @return {@code true} if we should change policy, {@code false} if not
*/
@Suppress("ReturnCount")
fun ManagedIndexConfig.shouldChangePolicy(managedIndexMetaData: ManagedIndexMetaData, actionToExecute: Action?): Boolean {
if (this.changePolicy == null) {
return false
}
if (this.changePolicy.isSafe) {
return true
}
// we need this in so that we can change policy before the first transition happens so policy doesnt get completed
// before we have a chance to change policy
if (actionToExecute?.type == TransitionsAction.name) {
return true
}
if (managedIndexMetaData.actionMetaData?.name != TransitionsAction.name) {
return false
}
return true
}
fun ManagedIndexMetaData.hasVersionConflict(managedIndexConfig: ManagedIndexConfig): Boolean =
this.policySeqNo != managedIndexConfig.policySeqNo || this.policyPrimaryTerm != managedIndexConfig.policyPrimaryTerm
fun ManagedIndexConfig.hasDifferentJobInterval(jobInterval: Int): Boolean {
val schedule = this.schedule
when (schedule) {
is IntervalSchedule -> {
return schedule.interval != jobInterval
}
}
return false
}
/**
* A policy is safe to change to a new policy when each policy has the current state
* the [ManagedIndexConfig] is in and that state has the same actions in the same order.
* This allows simple things like configuration updates to happen which won't break the execution/contract
* between [ManagedIndexMetaData] and [ManagedIndexConfig] as the metadata only knows about the current state.
* We never consider a policy safe to immediately change if the ChangePolicy contains a state to transition to
* as this could transition a user into a different state from the middle of the current state which we do not
* want to allow.
*
* @param stateName the name of the state the [ManagedIndexConfig] is currently in
* @param newPolicy the new (actual data model) policy we will eventually try to change to
* @param changePolicy the change policy to change to
* @return if its safe to change
*/
@Suppress("ReturnCount")
fun Policy.isSafeToChange(stateName: String?, newPolicy: Policy, changePolicy: ChangePolicy): Boolean {
// if stateName is null it means we either have not initialized the job (no metadata to pull stateName from)
// or we failed to load the initial policy, both cases its safe to change the policy
if (stateName == null) return true
if (changePolicy.state != null) return false
val currentState = this.states.find { it.name == stateName }
val newState = newPolicy.states.find { it.name == stateName }
if (currentState == null || newState == null) {
return false
}
if (currentState.actions.size != newState.actions.size) {
return false
}
currentState.actions.forEachIndexed { index, action ->
val newStateAction = newState.actions[index]
if (action.type != newStateAction.type) return@isSafeToChange false
}
return true
}
/**
* Allowed actions are ones that are specified in the [ManagedIndexSettings.ALLOW_LIST] setting.
*/
fun Action.isAllowed(allowList: List<String>): Boolean = allowList.contains(this.type)
/**
* Check if cluster state metadata has been moved to config index
*
* log warning if remaining cluster state metadata has newer last_updated_time
*/
@Suppress("ReturnCount", "ComplexCondition", "ComplexMethod")
fun checkMetadata(
clusterStateMetadata: ManagedIndexMetaData?,
configIndexMetadata: Any?,
currentIndexUuid: String?,
logger: Logger
): MetadataCheck {
// indexUuid saved in ISM metadata may be outdated
// if an index restored from snapshot
val indexUuid1 = clusterStateMetadata?.indexUuid
val indexUuid2 = when (configIndexMetadata) {
is ManagedIndexMetaData -> configIndexMetadata.indexUuid
is Map<*, *> -> configIndexMetadata["index_uuid"]
else -> null
} as String?
if ((indexUuid1 != null && indexUuid1 != currentIndexUuid) ||
(indexUuid2 != null && indexUuid2 != currentIndexUuid)
) {
return MetadataCheck.CORRUPT
}
if (clusterStateMetadata != null) {
if (configIndexMetadata == null) return MetadataCheck.PENDING
// compare last updated time between 2 metadatas
val t1 = clusterStateMetadata.stepMetaData?.startTime
val t2 = when (configIndexMetadata) {
is ManagedIndexMetaData -> configIndexMetadata.stepMetaData?.startTime
is Map<*, *> -> {
@Suppress("UNCHECKED_CAST")
val stepMetadata = configIndexMetadata["step"] as Map<String, Any>?
stepMetadata?.get("start_time")
}
else -> null
} as Long?
if (t1 != null && t2 != null && t1 > t2) {
logger.warn("Cluster state metadata get updates after moved for [${clusterStateMetadata.index}]")
}
}
return MetadataCheck.SUCCESS
}
enum class MetadataCheck {
PENDING, CORRUPT, SUCCESS
}
// private val baseMessageLogger = LogManager.getLogger(BaseMessage::class.java)
//
// fun BaseMessage.isHostInDenylist(networks: List<String>): Boolean {
// val ipStr = IPAddressString(this.uri.host)
// for (network in networks) {
// val netStr = IPAddressString(network)
// if (netStr.contains(ipStr)) {
// baseMessageLogger.error("Host: {} resolves to: {} which is in denylist: {}.", uri.host, InetAddress.getByName(uri.host), netStr)
// return true
// }
// }
// return false
// }
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun getManagedIndexConfig(indexUuid: String, client: Client): ManagedIndexConfig? {
val request = GetRequest().routing(indexUuid).index(INDEX_MANAGEMENT_INDEX).id(indexUuid)
val response: GetResponse = client.suspendUntil { get(request, it) }
var managedIndexConfig: ManagedIndexConfig? = null
val configSource = response.sourceAsBytesRef
// Intellij complains about createParser/parseWithType blocking because it sees they throw IOExceptions
configSource?.let {
withContext(Dispatchers.IO) {
val xcp = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, configSource, XContentType.JSON)
managedIndexConfig = xcp.parseWithType(response.id, response.seqNo, response.primaryTerm, ManagedIndexConfig.Companion::parse)
}
}
return managedIndexConfig
}
// extracts the job scheduler interval from the managed index config and returns the millisecond value
fun getIntervalFromManagedIndexConfig(managedIndexConfig: ManagedIndexConfig): Long {
val periodTuple = managedIndexConfig.jobSchedule.getPeriodStartingAt(Instant.now())
return periodTuple.v2().toEpochMilli() - periodTuple.v1().toEpochMilli()
}