Skip to content
Snippets Groups Projects
Commit e8f73e77 authored by Zhibin Mai's avatar Zhibin Mai Committed by David Diederich
Browse files

Create index alias when index is created

(cherry picked from commit bc582775)
parent f88c89b4
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
......@@ -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;
......@@ -107,6 +108,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 +306,30 @@ public class IndicesServiceImpl implements IndicesService {
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 @@
<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-ALIAS</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