forked from opendistro-for-elasticsearch/index-management
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
schema mapping change (opendistro-for-elasticsearch#198) (opendistro-…
…for-elasticsearch#202) * check schema version for IndexConfig mapping acknowledge false in case indexmetadata does not exist
1 parent
03ab689
commit 5f22b5b
Showing
12 changed files
with
384 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...main/kotlin/com/amazon/opendistroforelasticsearch/indexstatemanagement/util/IndexUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.util | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementIndices | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementPlugin | ||
import org.apache.logging.log4j.LogManager | ||
import org.elasticsearch.action.ActionListener | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse | ||
import org.elasticsearch.client.IndicesAdminClient | ||
import org.elasticsearch.cluster.ClusterState | ||
import org.elasticsearch.cluster.metadata.IndexMetaData | ||
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry | ||
import org.elasticsearch.common.xcontent.XContentParser | ||
import org.elasticsearch.common.xcontent.XContentType | ||
|
||
class IndexUtils { | ||
companion object { | ||
@Suppress("ObjectPropertyNaming") | ||
const val _META = "_meta" | ||
const val SCHEMA_VERSION = "schema_version" | ||
const val DEFAULT_SCHEMA_VERSION = 1L | ||
val logger = LogManager.getLogger(IndexUtils::class.java) | ||
|
||
var indexManagementSchemaVersion: Long | ||
private set | ||
var indexManagementHistoryVersion: Long | ||
private set | ||
|
||
init { | ||
indexManagementSchemaVersion = getSchemaVersion(IndexStateManagementIndices.indexStateManagementMappings) | ||
indexManagementHistoryVersion = getSchemaVersion(IndexStateManagementIndices.indexStateManagementHistoryMappings) | ||
} | ||
|
||
fun getSchemaVersion(mapping: String): Long { | ||
val xcp = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, | ||
LoggingDeprecationHandler.INSTANCE, mapping) | ||
|
||
while (!xcp.isClosed) { | ||
val token = xcp.currentToken() | ||
if (token != null && token != XContentParser.Token.END_OBJECT && token != XContentParser.Token.START_OBJECT) { | ||
if (xcp.currentName() != _META) { | ||
xcp.nextToken() | ||
xcp.skipChildren() | ||
} else { | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
when (xcp.currentName()) { | ||
SCHEMA_VERSION -> { | ||
val version = xcp.longValue() | ||
require(version > -1) | ||
return version | ||
} | ||
else -> xcp.nextToken() | ||
} | ||
} | ||
} | ||
} | ||
xcp.nextToken() | ||
} | ||
return DEFAULT_SCHEMA_VERSION | ||
} | ||
|
||
fun shouldUpdateIndex(index: IndexMetaData, newVersion: Long): Boolean { | ||
var oldVersion = DEFAULT_SCHEMA_VERSION | ||
|
||
val indexMapping = index.mapping()?.sourceAsMap() | ||
if (indexMapping != null && indexMapping.containsKey(_META) && indexMapping[_META] is HashMap<*, *>) { | ||
val metaData = indexMapping[_META] as HashMap<*, *> | ||
if (metaData.containsKey(SCHEMA_VERSION)) { | ||
oldVersion = (metaData[SCHEMA_VERSION] as Int).toLong() | ||
} | ||
} | ||
return newVersion > oldVersion | ||
} | ||
|
||
fun checkAndUpdateConfigIndexMapping( | ||
clusterState: ClusterState, | ||
client: IndicesAdminClient, | ||
actionListener: ActionListener<AcknowledgedResponse> | ||
) { | ||
checkAndUpdateIndexMapping( | ||
IndexStateManagementPlugin.INDEX_STATE_MANAGEMENT_INDEX, | ||
indexManagementSchemaVersion, | ||
IndexStateManagementIndices.indexStateManagementMappings, | ||
clusterState, | ||
client, | ||
actionListener | ||
) | ||
} | ||
|
||
@OpenForTesting | ||
fun checkAndUpdateIndexMapping( | ||
index: String, | ||
schemaVersion: Long, | ||
mapping: String, | ||
clusterState: ClusterState, | ||
client: IndicesAdminClient, | ||
actionListener: ActionListener<AcknowledgedResponse> | ||
) { | ||
if (clusterState.metaData.indices.containsKey(index)) { | ||
if (shouldUpdateIndex(clusterState.metaData.indices[index], schemaVersion)) { | ||
val putMappingRequest: PutMappingRequest = PutMappingRequest(index).type(_DOC).source(mapping, XContentType.JSON) | ||
client.putMapping(putMappingRequest, actionListener) | ||
} else { | ||
actionListener.onResponse(AcknowledgedResponse(true)) | ||
} | ||
} else { | ||
logger.error("IndexMetaData does not exist for $index") | ||
actionListener.onResponse(AcknowledgedResponse(false)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"_meta" : { | ||
"schema_version": 2 | ||
}, | ||
"dynamic": "strict", | ||
"properties": { | ||
"policy": { | ||
|
81 changes: 81 additions & 0 deletions
81
...m/amazon/opendistroforelasticsearch/indexstatemanagement/IndexStateManagementIndicesIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.amazon.opendistroforelasticsearch.indexstatemanagement | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementIndices.Companion.indexStateManagementMappings | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementPlugin.Companion.INDEX_STATE_MANAGEMENT_INDEX | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementPlugin.Companion.POLICY_BASE_URI | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.ChangePolicy | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.resthandler.RestChangePolicyAction | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.util.FAILED_INDICES | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.util.FAILURES | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.util.UPDATED_INDICES | ||
import org.apache.http.entity.ContentType | ||
import org.apache.http.entity.StringEntity | ||
import org.elasticsearch.common.settings.Settings | ||
import org.elasticsearch.rest.RestRequest | ||
import org.elasticsearch.test.ESTestCase | ||
import java.util.Locale | ||
|
||
class IndexStateManagementIndicesIT : IndexStateManagementRestTestCase() { | ||
|
||
private val testIndexName = javaClass.simpleName.toLowerCase(Locale.ROOT) | ||
|
||
fun `test create index management`() { | ||
val policy = randomPolicy() | ||
val policyId = ESTestCase.randomAlphaOfLength(10) | ||
client().makeRequest("PUT", "$POLICY_BASE_URI/$policyId", emptyMap(), policy.toHttpEntity()) | ||
assertIndexExists(INDEX_STATE_MANAGEMENT_INDEX) | ||
verifyIndexSchemaVersion(INDEX_STATE_MANAGEMENT_INDEX, 2) | ||
} | ||
|
||
fun `test update management index mapping with new schema version`() { | ||
assertIndexDoesNotExist(INDEX_STATE_MANAGEMENT_INDEX) | ||
|
||
val mapping = indexStateManagementMappings.trimStart('{').trimEnd('}') | ||
.replace("\"schema_version\": 2", "\"schema_version\": 0") | ||
|
||
createIndex(INDEX_STATE_MANAGEMENT_INDEX, Settings.EMPTY, mapping) | ||
assertIndexExists(INDEX_STATE_MANAGEMENT_INDEX) | ||
verifyIndexSchemaVersion(INDEX_STATE_MANAGEMENT_INDEX, 0) | ||
client().makeRequest("DELETE", "*") | ||
|
||
val policy = randomPolicy() | ||
val policyId = ESTestCase.randomAlphaOfLength(10) | ||
client().makeRequest("PUT", "$POLICY_BASE_URI/$policyId", emptyMap(), policy.toHttpEntity()) | ||
|
||
assertIndexExists(INDEX_STATE_MANAGEMENT_INDEX) | ||
verifyIndexSchemaVersion(INDEX_STATE_MANAGEMENT_INDEX, 2) | ||
} | ||
|
||
fun `test changing policy on an index that hasn't initialized yet check schema version`() { | ||
val policy = createRandomPolicy() | ||
val newPolicy = createPolicy(randomPolicy(), "new_policy", true) | ||
val indexName = "${testIndexName}_computer" | ||
val (index) = createIndex(indexName, policy.id) | ||
|
||
val managedIndexConfig = getExistingManagedIndexConfig(index) | ||
assertNull("Change policy is not null", managedIndexConfig.changePolicy) | ||
assertNull("Policy has already initialized", managedIndexConfig.policy) | ||
assertEquals("Policy id does not match", policy.id, managedIndexConfig.policyID) | ||
|
||
val mapping = "{" + indexStateManagementMappings.trimStart('{').trimEnd('}') | ||
.replace("\"schema_version\": 2", "\"schema_version\": 0") | ||
|
||
val entity = StringEntity(mapping, ContentType.APPLICATION_JSON) | ||
client().makeRequest(RestRequest.Method.PUT.toString(), | ||
"/$INDEX_STATE_MANAGEMENT_INDEX/_mapping", emptyMap(), entity) | ||
|
||
verifyIndexSchemaVersion(INDEX_STATE_MANAGEMENT_INDEX, 0) | ||
|
||
// if we try to change policy now, it'll have no ManagedIndexMetaData yet and should succeed | ||
val changePolicy = ChangePolicy(newPolicy.id, null, emptyList(), false) | ||
val response = client().makeRequest( | ||
RestRequest.Method.POST.toString(), | ||
"${RestChangePolicyAction.CHANGE_POLICY_BASE_URI}/$index", emptyMap(), changePolicy.toHttpEntity()) | ||
|
||
verifyIndexSchemaVersion(INDEX_STATE_MANAGEMENT_INDEX, 2) | ||
|
||
assertAffectedIndicesResponseIsEqual(mapOf(FAILURES to false, FAILED_INDICES to emptyList<Any>(), UPDATED_INDICES to 1), response.asMap()) | ||
|
||
waitFor { assertEquals(newPolicy.id, getManagedIndexConfig(index)?.changePolicy?.policyID) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...kotlin/com/amazon/opendistroforelasticsearch/indexstatemanagement/util/IndexUtilsTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.util | ||
|
||
import org.elasticsearch.cluster.metadata.IndexMetaData | ||
import org.elasticsearch.common.xcontent.XContentType | ||
import org.elasticsearch.test.ESTestCase | ||
import kotlin.test.assertFailsWith | ||
|
||
class IndexUtilsTests : ESTestCase() { | ||
|
||
fun `test get schema version`() { | ||
val message = "{\"user\":{ \"name\":\"test\"},\"_meta\":{\"schema_version\": 1}}" | ||
|
||
val schemaVersion = IndexUtils.getSchemaVersion(message) | ||
assertEquals(1, schemaVersion) | ||
} | ||
|
||
fun `test get schema version without _meta`() { | ||
val message = "{\"user\":{ \"name\":\"test\"}}" | ||
|
||
val schemaVersion = IndexUtils.getSchemaVersion(message) | ||
assertEquals(1, schemaVersion) | ||
} | ||
|
||
fun `test get schema version without schema_version`() { | ||
val message = "{\"user\":{ \"name\":\"test\"},\"_meta\":{\"test\": 1}}" | ||
|
||
val schemaVersion = IndexUtils.getSchemaVersion(message) | ||
assertEquals(1, schemaVersion) | ||
} | ||
|
||
fun `test get schema version with negative schema_version`() { | ||
val message = "{\"user\":{ \"name\":\"test\"},\"_meta\":{\"schema_version\": -1}}" | ||
|
||
assertFailsWith(IllegalArgumentException::class, "Expected IllegalArgumentException") { | ||
IndexUtils.getSchemaVersion(message) | ||
} | ||
} | ||
|
||
fun `test get schema version with wrong schema_version`() { | ||
val message = "{\"user\":{ \"name\":\"test\"},\"_meta\":{\"schema_version\": \"wrong\"}}" | ||
|
||
assertFailsWith(IllegalArgumentException::class, "Expected IllegalArgumentException") { | ||
IndexUtils.getSchemaVersion(message) | ||
} | ||
} | ||
|
||
fun `test should update index without original version`() { | ||
val indexContent = "{\"testIndex\":{\"settings\":{\"index\":{\"creation_date\":\"1558407515699\"," + | ||
"\"number_of_shards\":\"1\",\"number_of_replicas\":\"1\",\"uuid\":\"t-VBBW6aR6KpJ3XP5iISOA\"," + | ||
"\"version\":{\"created\":\"6040399\"},\"provided_name\":\"data_test\"}},\"mapping_version\":123," + | ||
"\"settings_version\":123,\"mappings\":{\"_doc\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}" | ||
|
||
val parser = createParser(XContentType.JSON.xContent(), indexContent) | ||
val index: IndexMetaData = IndexMetaData.fromXContent(parser) | ||
|
||
val shouldUpdateIndex = IndexUtils.shouldUpdateIndex(index, 10) | ||
assertTrue(shouldUpdateIndex) | ||
} | ||
|
||
fun `test should update index with lagged version`() { | ||
val indexContent = "{\"testIndex\":{\"settings\":{\"index\":{\"creation_date\":\"1558407515699\"," + | ||
"\"number_of_shards\":\"1\",\"number_of_replicas\":\"1\",\"uuid\":\"t-VBBW6aR6KpJ3XP5iISOA\"," + | ||
"\"version\":{\"created\":\"6040399\"},\"provided_name\":\"data_test\"}},\"mapping_version\":123," + | ||
"\"settings_version\":123,\"mappings\":{\"_doc\":{\"_meta\":{\"schema_version\":1},\"properties\":" + | ||
"{\"name\":{\"type\":\"keyword\"}}}}}}" | ||
|
||
val parser = createParser(XContentType.JSON.xContent(), indexContent) | ||
val index: IndexMetaData = IndexMetaData.fromXContent(parser) | ||
|
||
val shouldUpdateIndex = IndexUtils.shouldUpdateIndex(index, 10) | ||
assertTrue(shouldUpdateIndex) | ||
} | ||
|
||
fun `test should update index with same version`() { | ||
val indexContent = "{\"testIndex\":{\"settings\":{\"index\":{\"creation_date\":\"1558407515699\"," + | ||
"\"number_of_shards\":\"1\",\"number_of_replicas\":\"1\",\"uuid\":\"t-VBBW6aR6KpJ3XP5iISOA\"," + | ||
"\"version\":{\"created\":\"6040399\"},\"provided_name\":\"data_test\"}},\"mappings\":" + | ||
"{\"_doc\":{\"_meta\":{\"schema_version\":1},\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}" | ||
|
||
val parser = createParser(XContentType.JSON.xContent(), indexContent) | ||
val index: IndexMetaData = IndexMetaData.fromXContent(parser) | ||
|
||
val shouldUpdateIndex = IndexUtils.shouldUpdateIndex(index, 1) | ||
assertFalse(shouldUpdateIndex) | ||
} | ||
} |