Skip to content

Commit d1027da

Browse files
fix for MapperException[the [enabled] parameter can't be updated for the object mapping [metadata.source_to_query_index_mapping] (#1432) (#1434)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent ae6b964 commit d1027da

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt

+42-25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlinx.coroutines.SupervisorJob
1212
import org.apache.logging.log4j.LogManager
1313
import org.opensearch.ExceptionsHelper
1414
import org.opensearch.OpenSearchSecurityException
15+
import org.opensearch.OpenSearchStatusException
1516
import org.opensearch.action.DocWriteRequest
1617
import org.opensearch.action.DocWriteResponse
1718
import org.opensearch.action.admin.indices.get.GetIndexRequest
@@ -78,35 +79,51 @@ object MonitorMetadataService :
7879
@Suppress("ComplexMethod", "ReturnCount")
7980
suspend fun upsertMetadata(metadata: MonitorMetadata, updating: Boolean): MonitorMetadata {
8081
try {
81-
val indexRequest = IndexRequest(ScheduledJob.SCHEDULED_JOBS_INDEX)
82-
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
83-
.source(metadata.toXContent(XContentFactory.jsonBuilder(), ToXContent.MapParams(mapOf("with_type" to "true"))))
84-
.id(metadata.id)
85-
.routing(metadata.monitorId)
86-
.setIfSeqNo(metadata.seqNo)
87-
.setIfPrimaryTerm(metadata.primaryTerm)
88-
.timeout(indexTimeout)
82+
if (clusterService.state().routingTable.hasIndex(ScheduledJob.SCHEDULED_JOBS_INDEX)) {
83+
val indexRequest = IndexRequest(ScheduledJob.SCHEDULED_JOBS_INDEX)
84+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
85+
.source(
86+
metadata.toXContent(
87+
XContentFactory.jsonBuilder(),
88+
ToXContent.MapParams(mapOf("with_type" to "true"))
89+
)
90+
)
91+
.id(metadata.id)
92+
.routing(metadata.monitorId)
93+
.setIfSeqNo(metadata.seqNo)
94+
.setIfPrimaryTerm(metadata.primaryTerm)
95+
.timeout(indexTimeout)
8996

90-
if (updating) {
91-
indexRequest.id(metadata.id).setIfSeqNo(metadata.seqNo).setIfPrimaryTerm(metadata.primaryTerm)
92-
} else {
93-
indexRequest.opType(DocWriteRequest.OpType.CREATE)
94-
}
95-
val response: IndexResponse = client.suspendUntil { index(indexRequest, it) }
96-
when (response.result) {
97-
DocWriteResponse.Result.DELETED, DocWriteResponse.Result.NOOP, DocWriteResponse.Result.NOT_FOUND, null -> {
98-
val failureReason = "The upsert metadata call failed with a ${response.result?.lowercase} result"
99-
log.error(failureReason)
100-
throw AlertingException(failureReason, RestStatus.INTERNAL_SERVER_ERROR, IllegalStateException(failureReason))
97+
if (updating) {
98+
indexRequest.id(metadata.id).setIfSeqNo(metadata.seqNo).setIfPrimaryTerm(metadata.primaryTerm)
99+
} else {
100+
indexRequest.opType(DocWriteRequest.OpType.CREATE)
101101
}
102-
DocWriteResponse.Result.CREATED, DocWriteResponse.Result.UPDATED -> {
103-
log.debug("Successfully upserted MonitorMetadata:${metadata.id} ")
102+
val response: IndexResponse = client.suspendUntil { index(indexRequest, it) }
103+
when (response.result) {
104+
DocWriteResponse.Result.DELETED, DocWriteResponse.Result.NOOP, DocWriteResponse.Result.NOT_FOUND, null -> {
105+
val failureReason =
106+
"The upsert metadata call failed with a ${response.result?.lowercase} result"
107+
log.error(failureReason)
108+
throw AlertingException(
109+
failureReason,
110+
RestStatus.INTERNAL_SERVER_ERROR,
111+
IllegalStateException(failureReason)
112+
)
113+
}
114+
115+
DocWriteResponse.Result.CREATED, DocWriteResponse.Result.UPDATED -> {
116+
log.debug("Successfully upserted MonitorMetadata:${metadata.id} ")
117+
}
104118
}
119+
return metadata.copy(
120+
seqNo = response.seqNo,
121+
primaryTerm = response.primaryTerm
122+
)
123+
} else {
124+
val failureReason = "Job index ${ScheduledJob.SCHEDULED_JOBS_INDEX} does not exist to update monitor metadata"
125+
throw OpenSearchStatusException(failureReason, RestStatus.INTERNAL_SERVER_ERROR)
105126
}
106-
return metadata.copy(
107-
seqNo = response.seqNo,
108-
primaryTerm = response.primaryTerm
109-
)
110127
} catch (e: Exception) {
111128
throw AlertingException.wrap(e)
112129
}

0 commit comments

Comments
 (0)