Skip to content
Snippets Groups Projects
Commit f1f0c1ae authored by Chad Leong's avatar Chad Leong :speech_balloon:
Browse files

Merge branch 'cherry-pick-for-468' into 'release/0.19'

Cherry-pick 'Create index alias when index is created' into release/0.19

See merge request !484
parents f88c89b4 105957e4
No related branches found
No related tags found
5 merge requests!604Merge Delta changes from M16 master to M18 master,!600Merge Delta changes from M16 master to M18 master,!599Merge Delta changes from M16 to M18 master,!598Merged from M16,!484Cherry-pick 'Create index alias when index is created' into release/0.19
Pipeline #166516 failed
......@@ -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;
}
}
......@@ -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
}
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment