diff --git a/NOTICE b/NOTICE index fc5a1231f896210dfd34b46d3a1f35c86edd75a9..e3b1becbb65b4d8470742746d26fd69293e8c6b1 100644 --- a/NOTICE +++ b/NOTICE @@ -1112,6 +1112,7 @@ The following software have components provided under the terms of this license: - Elastic JNA Distribution (from https://github.com/java-native-access/jna) - Java Native Access (from https://github.com/java-native-access/jna, https://github.com/twall/jna) +- Java Native Access Platform (from https://github.com/java-native-access/jna) - Javassist (from http://www.javassist.org/) ======================================================================== diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java index db5a2f37a19857fd609da3fc3abdc953c5889fd8..bc8d633ac9be3a39336ea79a634709e1f34c2940 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java @@ -24,6 +24,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Request; @@ -51,10 +52,7 @@ import org.springframework.web.context.annotation.RequestScope; import java.io.IOException; import java.lang.reflect.Type; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; @Service @RequestScope @@ -107,6 +105,8 @@ public class IndicesServiceImpl implements IndicesService { if (indexStatus) { this.indexCache.put(index, true); this.log.info(String.format("Time taken to successfully create new index %s : %d milliseconds", request.index(), stopTime-startTime)); + + createIndexAlias(client, index); } return indexStatus; @@ -303,4 +303,48 @@ public class IndicesServiceImpl implements IndicesService { throw exception; } } -} \ No newline at end of file + + private void createIndexAlias(RestHighLevelClient client, String index) { + String kind = this.elasticIndexNameResolver.getKindFromIndexName(index); + if(!elasticIndexNameResolver.isIndexAliasSupported(kind)) + return; + + try { + List<String> kinds = new ArrayList<>(); + kinds.add(kind); + String kindWithMajorVersion = getKindWithMajorVersion(kind); + if(elasticIndexNameResolver.isIndexAliasSupported(kindWithMajorVersion)) { + kinds.add(kindWithMajorVersion); + } + for (String kd : kinds) { + index = elasticIndexNameResolver.getIndexNameFromKind(kd); + String alias = elasticIndexNameResolver.getIndexAliasFromKind(kd); + IndicesAliasesRequest addRequest = new IndicesAliasesRequest(); + IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD) + .index(index) + .alias(alias); + addRequest.addAliasAction(aliasActions); + AcknowledgedResponse response = client.indices().updateAliases(addRequest, RequestOptions.DEFAULT); + if (response.isAcknowledged()) { + this.log.info(String.format("Alias %s was created for index %s", alias, index)); + } + } + } + catch(Exception ex) { + // Failed to create alias is not the end. It should not affect the status of index creation + this.log.error(String.format("Fail to create aliases for index %s", index), ex); + } + } + + private String getKindWithMajorVersion(String kind) { + // If kind is common:welldb:wellbore:1.2.0, then kind with major version is common:welldb:wellbore:1.*.* + int idx = kind.lastIndexOf(":"); + String version = kind.substring(idx+1); + if(version.indexOf(".") > 0) { + String kindWithoutVersion = kind.substring(0, idx); + String majorVersion = version.substring(0, version.indexOf(".")); + return String.format("%s:%s.*.*", kindWithoutVersion, majorVersion); + } + return null; + } +} diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java index 8fb08c6e6ac0be3e3116cec674108af1a6f8231d..7f22b619a9364b308791473adbae5324a0687d9a 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java @@ -24,8 +24,11 @@ import org.apache.http.util.EntityUtils; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.*; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse; import org.elasticsearch.rest.RestStatus; @@ -47,10 +50,12 @@ import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.lang.reflect.Type; +import java.util.HashMap; import java.util.List; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; @@ -87,6 +92,52 @@ public class IndicesServiceTest { restHighLevelClient = PowerMockito.mock(RestHighLevelClient.class); } + @Test + public void create_elasticIndex() throws Exception { + String kind = "common:welldb:wellbore:1.2.0"; + String index = "common-welldb-wellbore-1.2.0"; + CreateIndexResponse indexResponse = new CreateIndexResponse(true, true, index); + AcknowledgedResponse acknowledgedResponse = new AcknowledgedResponse(true); + + when(elasticIndexNameResolver.getKindFromIndexName(any())).thenReturn(kind); + when(elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn(index); + when(elasticIndexNameResolver.getIndexAliasFromKind(any())).thenReturn("a12345678"); + when(elasticIndexNameResolver.isIndexAliasSupported(any())).thenReturn(true); + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenReturn(indexResponse); + when(indicesClient.updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class))).thenReturn(acknowledgedResponse); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertTrue(response); + when(this.indicesExistCache.get(index)).thenReturn(true); + verify(this.indicesClient, times(2)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + + @Test + public void create_elasticIndex_fail() throws Exception { + String index = "common-welldb-wellbore-1.2.0"; + CreateIndexResponse indexResponse = new CreateIndexResponse(false, false, index); + + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenReturn(indexResponse); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertFalse(response); + verify(this.indicesExistCache, times(0)).put(any(), any()); + verify(this.indicesClient, times(0)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + + @Test + public void create_existingElasticIndex() throws Exception { + String index = "common-welldb-wellbore-1.2.0"; + ElasticsearchStatusException elasticsearchStatusException = new ElasticsearchStatusException("resource_already_exists_exception", RestStatus.BAD_REQUEST); + + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenThrow(elasticsearchStatusException); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertTrue(response); + verify(this.indicesExistCache, times(1)).put(any(), any()); + verify(this.indicesClient, times(0)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + @Test public void delete_existingElasticIndex() throws Exception { AcknowledgedResponse indexResponse = new AcknowledgedResponse(true); @@ -290,4 +341,4 @@ public class IndicesServiceTest { verify(this.indicesExistCache, times(1)).get("anyIndex"); verify(this.indicesExistCache, times(1)).put("anyIndex", true); } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index ff8b2faea7ea659326a8150e160accbfb92969c4..42b096b3a8a73735ae42c63baeb32638a0456fdc 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> - <os-core-common.version>0.16.1</os-core-common.version> + <os-core-common.version>0.19.0-SNAPSHOT</os-core-common.version> <snakeyaml.version>1.33</snakeyaml.version> <hibernate-validator.version>6.1.5.Final</hibernate-validator.version> <jackson-databind.version>2.13.4.2</jackson-databind.version>