Skip to content

Commit dfb1939

Browse files
committed
Slight refactor
1 parent d10d09d commit dfb1939

File tree

5 files changed

+104
-128
lines changed

5 files changed

+104
-128
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlAnomaliesIndexUtils.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAlias.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
2121
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
2222
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
23+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
2324
import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction;
2425
import org.elasticsearch.action.support.ActiveShardCount;
2526
import org.elasticsearch.action.support.IndicesOptions;
@@ -33,9 +34,13 @@
3334
import org.elasticsearch.index.Index;
3435
import org.elasticsearch.index.IndexVersion;
3536
import org.elasticsearch.index.IndexVersions;
37+
import org.elasticsearch.index.query.QueryBuilders;
3638
import org.elasticsearch.indices.SystemIndexDescriptor;
3739
import org.elasticsearch.xcontent.XContentParserConfiguration;
3840
import org.elasticsearch.xcontent.json.JsonXContent;
41+
import org.elasticsearch.xpack.core.ml.job.config.Job;
42+
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
43+
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;
3944
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
4045

4146
import java.io.IOException;
@@ -504,4 +509,88 @@ public static String latestIndexMatchingBaseName(
504509

505510
return MlIndexAndAlias.latestIndex(filtered);
506511
}
512+
513+
public static void rollover(Client client, RolloverRequest rolloverRequest, ActionListener<String> listener) {
514+
client.admin()
515+
.indices()
516+
.rolloverIndex(rolloverRequest, ActionListener.wrap(response -> listener.onResponse(response.getNewIndex()), e -> {
517+
if (e instanceof ResourceAlreadyExistsException alreadyExistsException) {
518+
// The destination index already exists possibly because it has been rolled over already.
519+
listener.onResponse(alreadyExistsException.getIndex().getName());
520+
} else {
521+
listener.onFailure(e);
522+
}
523+
}));
524+
}
525+
526+
public static void createAliasForRollover(
527+
org.elasticsearch.logging.Logger logger,
528+
Client client,
529+
String indexName,
530+
String aliasName,
531+
ActionListener<IndicesAliasesResponse> listener
532+
) {
533+
logger.warn("creating rollover [{}] alias for [{}]", aliasName, indexName);
534+
client.admin()
535+
.indices()
536+
.prepareAliases(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS)
537+
.addAliasAction(IndicesAliasesRequest.AliasActions.add().index(indexName).alias(aliasName).isHidden(true))
538+
.execute(listener);
539+
}
540+
541+
public static void updateAliases(IndicesAliasesRequestBuilder request, ActionListener<Boolean> listener) {
542+
request.execute(listener.delegateFailure((l, response) -> l.onResponse(Boolean.TRUE)));
543+
}
544+
545+
public static IndicesAliasesRequestBuilder addIndexAliasesRequests(
546+
IndicesAliasesRequestBuilder aliasRequestBuilder,
547+
String oldIndex,
548+
String newIndex,
549+
ClusterState clusterState
550+
) {
551+
// Multiple jobs can share the same index each job
552+
// has a read and write alias that needs updating
553+
// after the rollover
554+
var meta = clusterState.metadata().getProject().index(oldIndex);
555+
assert meta != null;
556+
if (meta == null) {
557+
return aliasRequestBuilder;
558+
}
559+
560+
for (var alias : meta.getAliases().values()) {
561+
if (isAnomaliesWriteAlias(alias.alias())) {
562+
aliasRequestBuilder.addAliasAction(
563+
IndicesAliasesRequest.AliasActions.add().index(newIndex).alias(alias.alias()).isHidden(true).writeIndex(true)
564+
);
565+
aliasRequestBuilder.addAliasAction(IndicesAliasesRequest.AliasActions.remove().index(oldIndex).alias(alias.alias()));
566+
} else if (isAnomaliesReadAlias(alias.alias())) {
567+
String jobId = AnomalyDetectorsIndex.jobIdFromAlias(alias.alias());
568+
aliasRequestBuilder.addAliasAction(
569+
IndicesAliasesRequest.AliasActions.add()
570+
.index(newIndex)
571+
.alias(alias.alias())
572+
.isHidden(true)
573+
.filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))
574+
);
575+
}
576+
}
577+
578+
return aliasRequestBuilder;
579+
}
580+
581+
public static boolean isAnomaliesWriteAlias(String aliasName) {
582+
return aliasName.startsWith(AnomalyDetectorsIndexFields.RESULTS_INDEX_WRITE_PREFIX);
583+
}
584+
585+
public static boolean isAnomaliesReadAlias(String aliasName) {
586+
if (aliasName.startsWith(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX) == false) {
587+
return false;
588+
}
589+
590+
// See {@link AnomalyDetectorsIndex#jobResultsAliasedName}
591+
String jobIdPart = aliasName.substring(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX.length());
592+
// If this is a write alias it will start with a `.` character
593+
// which is not a valid job id.
594+
return MlStrings.isValidId(jobIdPart);
595+
}
507596
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAnomaliesIndexUpdate.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.logging.Logger;
2929
import org.elasticsearch.rest.RestStatus;
3030
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
31-
import org.elasticsearch.xpack.core.ml.utils.MlAnomaliesIndexUtils;
3231
import org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias;
3332

3433
import java.util.ArrayList;
@@ -167,18 +166,18 @@ private void rollAndUpdateAliases(ClusterState clusterState, String index, Actio
167166
).<String>andThen((l, success) -> {
168167
rollover(rolloverAlias, newIndexName, l);
169168
}).<Boolean>andThen((l, newIndexNameResponse) -> {
170-
MlAnomaliesIndexUtils.addIndexAliasesRequests(aliasRequestBuilder, index, newIndexNameResponse, clusterState);
169+
MlIndexAndAlias.addIndexAliasesRequests(aliasRequestBuilder, index, newIndexNameResponse, clusterState);
171170
// Delete the new alias created for the rollover action
172171
aliasRequestBuilder.removeAlias(newIndexNameResponse, rolloverAlias);
173-
MlAnomaliesIndexUtils.updateAliases(aliasRequestBuilder, l);
172+
MlIndexAndAlias.updateAliases(aliasRequestBuilder, l);
174173
}).addListener(listener);
175174
}
176175

177176
private void rollover(String alias, @Nullable String newIndexName, ActionListener<String> listener) {
178-
MlAnomaliesIndexUtils.rollover(client, new RolloverRequest(alias, newIndexName), listener);
177+
MlIndexAndAlias.rollover(client, new RolloverRequest(alias, newIndexName), listener);
179178
}
180179

181180
private void createAliasForRollover(String indexName, String aliasName, ActionListener<IndicesAliasesResponse> listener) {
182-
MlAnomaliesIndexUtils.createAliasForRollover(logger, client, indexName, aliasName, listener);
181+
MlIndexAndAlias.createAliasForRollover(logger, client, indexName, aliasName, listener);
183182
}
184183
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.elasticsearch.xpack.core.ml.action.ResetJobAction;
5151
import org.elasticsearch.xpack.core.ml.job.config.Job;
5252
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
53-
import org.elasticsearch.xpack.core.ml.utils.MlAnomaliesIndexUtils;
5453
import org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias;
5554
import org.elasticsearch.xpack.ml.utils.TypedChainTaskExecutor;
5655

@@ -285,7 +284,7 @@ void removeRolloverAlias(
285284
ActionListener<Boolean> listener
286285
) {
287286
aliasRequestBuilder.removeAlias(index, alias);
288-
MlAnomaliesIndexUtils.updateAliases(aliasRequestBuilder, listener);
287+
MlIndexAndAlias.updateAliases(aliasRequestBuilder, listener);
289288
}
290289

291290
private void rollAndUpdateAliases(ClusterState clusterState, String index, ActionListener<Boolean> listener) {
@@ -336,7 +335,7 @@ private void rollAndUpdateAliases(ClusterState clusterState, String index, Actio
336335

337336
// 3 Update aliases
338337
ActionListener<String> rolloverListener = ActionListener.wrap(newIndexNameResponse -> {
339-
MlAnomaliesIndexUtils.addIndexAliasesRequests(aliasRequestBuilder, index, newIndexNameResponse, clusterState);
338+
MlIndexAndAlias.addIndexAliasesRequests(aliasRequestBuilder, index, newIndexNameResponse, clusterState);
340339
// On success, the rollover alias may have been moved to the new index, so we attempt to remove it from there.
341340
// Note that the rollover request is considered "successful" even if it didn't occur due to a condition not being met
342341
// (no exception will be thrown). In which case the attempt to remove the alias here will fail with an
@@ -352,7 +351,7 @@ private void rollAndUpdateAliases(ClusterState clusterState, String index, Actio
352351

353352
// 2 rollover the index alias to the new index name
354353
ActionListener<IndicesAliasesResponse> getIndicesAliasesListener = ActionListener.wrap(getIndicesAliasesResponse -> {
355-
MlAnomaliesIndexUtils.rollover(
354+
MlIndexAndAlias.rollover(
356355
client,
357356
new RolloverRequestBuilder(client).setRolloverTarget(rolloverAlias)
358357
.setNewIndexName(newIndexName)
@@ -364,7 +363,7 @@ private void rollAndUpdateAliases(ClusterState clusterState, String index, Actio
364363
}, rolloverListener::onFailure);
365364

366365
// 1. Create necessary aliases
367-
MlAnomaliesIndexUtils.createAliasForRollover(logger, client, index, rolloverAlias, getIndicesAliasesListener);
366+
MlIndexAndAlias.createAliasForRollover(logger, client, index, rolloverAlias, getIndicesAliasesListener);
368367
}
369368

370369
// TODO make public for testing?

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlAnomaliesIndexUpdateTests.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.test.ESTestCase;
3333
import org.elasticsearch.threadpool.ThreadPool;
3434
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
35-
import org.elasticsearch.xpack.core.ml.utils.MlAnomaliesIndexUtils;
3635
import org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias;
3736

3837
import java.util.List;
@@ -53,15 +52,15 @@
5352
public class MlAnomaliesIndexUpdateTests extends ESTestCase {
5453

5554
public void testIsAnomaliesWriteAlias() {
56-
assertTrue(MlAnomaliesIndexUtils.isAnomaliesWriteAlias(AnomalyDetectorsIndex.resultsWriteAlias("foo")));
57-
assertFalse(MlAnomaliesIndexUtils.isAnomaliesWriteAlias(AnomalyDetectorsIndex.jobResultsAliasedName("foo")));
58-
assertFalse(MlAnomaliesIndexUtils.isAnomaliesWriteAlias("some-index"));
55+
assertTrue(MlIndexAndAlias.isAnomaliesWriteAlias(AnomalyDetectorsIndex.resultsWriteAlias("foo")));
56+
assertFalse(MlIndexAndAlias.isAnomaliesWriteAlias(AnomalyDetectorsIndex.jobResultsAliasedName("foo")));
57+
assertFalse(MlIndexAndAlias.isAnomaliesWriteAlias("some-index"));
5958
}
6059

6160
public void testIsAnomaliesAlias() {
62-
assertTrue(MlAnomaliesIndexUtils.isAnomaliesReadAlias(AnomalyDetectorsIndex.jobResultsAliasedName("foo")));
63-
assertFalse(MlAnomaliesIndexUtils.isAnomaliesReadAlias(AnomalyDetectorsIndex.resultsWriteAlias("foo")));
64-
assertFalse(MlAnomaliesIndexUtils.isAnomaliesReadAlias("some-index"));
61+
assertTrue(MlIndexAndAlias.isAnomaliesReadAlias(AnomalyDetectorsIndex.jobResultsAliasedName("foo")));
62+
assertFalse(MlIndexAndAlias.isAnomaliesReadAlias(AnomalyDetectorsIndex.resultsWriteAlias("foo")));
63+
assertFalse(MlIndexAndAlias.isAnomaliesReadAlias("some-index"));
6564
}
6665

6766
public void testIsAbleToRun_IndicesDoNotExist() {
@@ -115,7 +114,7 @@ public void testBuildIndexAliasesRequest() {
115114
);
116115

117116
var newIndex = anomaliesIndex + "-000001";
118-
var request = MlAnomaliesIndexUtils.addIndexAliasesRequests(aliasRequestBuilder, anomaliesIndex, newIndex, csBuilder.build());
117+
var request = MlIndexAndAlias.addIndexAliasesRequests(aliasRequestBuilder, anomaliesIndex, newIndex, csBuilder.build());
119118
var actions = request.request().getAliasActions();
120119
assertThat(actions, hasSize(6));
121120

0 commit comments

Comments
 (0)