Skip to content
Snippets Groups Projects
Commit bc582775 authored by Zhibin Mai's avatar Zhibin Mai
Browse files

Create index alias when index is created

parent aeb23b3d
No related branches found
No related tags found
1 merge request!468Create index alias when index is created
Pipeline #162422 failed
...@@ -24,6 +24,7 @@ import org.elasticsearch.ElasticsearchException; ...@@ -24,6 +24,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; 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.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
...@@ -107,6 +108,8 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -107,6 +108,8 @@ public class IndicesServiceImpl implements IndicesService {
if (indexStatus) { if (indexStatus) {
this.indexCache.put(index, true); this.indexCache.put(index, true);
this.log.info(String.format("Time taken to successfully create new index %s : %d milliseconds", request.index(), stopTime-startTime)); this.log.info(String.format("Time taken to successfully create new index %s : %d milliseconds", request.index(), stopTime-startTime));
createIndexAlias(client, index);
} }
return indexStatus; return indexStatus;
...@@ -303,4 +306,30 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -303,4 +306,30 @@ public class IndicesServiceImpl implements IndicesService {
throw exception; throw exception;
} }
} }
}
\ No newline at end of file private void createIndexAlias(RestHighLevelClient client, String index) throws IOException {
String kind = this.elasticIndexNameResolver.getKindFromIndexName(index);
String kindWithMajorVersion = getKindWithMajorVersion(kind);
for (String kd : Arrays.asList(kind, kindWithMajorVersion)) {
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));
}
}
}
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);
String majorVersion = version.substring(0, version.indexOf("."));
return String.format("%s:%s.*.*", kind.substring(0, idx), majorVersion);
}
}
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version> <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-ALIAS</os-core-common.version>
<snakeyaml.version>1.33</snakeyaml.version> <snakeyaml.version>1.33</snakeyaml.version>
<hibernate-validator.version>6.1.5.Final</hibernate-validator.version> <hibernate-validator.version>6.1.5.Final</hibernate-validator.version>
<jackson-databind.version>2.13.4.2</jackson-databind.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