Skip to content

Commit 0064d5e

Browse files
committed
fix integration test
1 parent 1ad69b3 commit 0064d5e

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,14 @@ private void deleteModelSnapshots(List<ModelSnapshot> modelSnapshots, String job
271271
}
272272

273273
// Remove read-only indices
274-
var indicesToQuery = writableIndexExpander.getWritableIndices(indices);
274+
List<String> indicesToQuery = new ArrayList<>();
275+
try {
276+
indicesToQuery = writableIndexExpander.getWritableIndices(indices);
277+
} catch (Exception e) {
278+
LOGGER.error("Failed to get writable indices for [{}] job: {}", jobId, e.getMessage(), e);
279+
listener.onFailure(e);
280+
return;
281+
}
275282
if (indicesToQuery.isEmpty()) {
276283
LOGGER.info("No writable model snapshot indices found for [{}] job. No expired model snapshots to remove.", jobId);
277284
listener.onResponse(true);

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/WritableIndexExpander.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.util.Collection;
18+
import java.util.List;
1819
import java.util.Objects;
1920
import java.util.stream.Collectors;
2021

@@ -33,22 +34,29 @@ public WritableIndexExpander(ClusterService clusterService, IndexNameExpressionR
3334
}
3435

3536
protected ArrayList<String> getWritableIndices(String indexPattern) {
36-
var clusterState = clusterService.state();
37-
var concreteIndices = indexNameExpressionResolver.concreteIndexNames(
38-
clusterState,
39-
IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN,
40-
indexPattern
41-
);
42-
return Arrays.stream(concreteIndices)
43-
.filter(index -> isIndexReadOnly(index) == false)
44-
.collect(Collectors.toCollection(ArrayList::new));
37+
return getWritableIndices(List.of(indexPattern));
4538
}
4639

4740
protected ArrayList<String> getWritableIndices(Collection<String> indices) {
48-
return indices.stream().filter(index -> isIndexReadOnly(index) == false).collect(Collectors.toCollection(ArrayList::new));
41+
if (indices == null || indices.isEmpty()) {
42+
return new ArrayList<>();
43+
}
44+
var clusterState = clusterService.state();
45+
return indices.stream()
46+
.map(index -> indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN, index))
47+
.flatMap(Arrays::stream)
48+
.filter(index -> (isIndexReadOnly(index) == false))
49+
.collect(Collectors.toCollection(ArrayList::new));
4950
}
5051

5152
private Boolean isIndexReadOnly(String indexName) {
52-
return IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(clusterService.state().metadata().getProject().index(indexName).getSettings());
53+
IndexMetadata indexMetadata = clusterService.state().metadata().getProject().index(indexName);
54+
if (indexMetadata == null) {
55+
throw new IllegalArgumentException("Failed to identify if index is read-only: index [" + indexName + "] not found");
56+
}
57+
if (indexMetadata.getSettings() == null) {
58+
throw new IllegalStateException("Settings for index [" + indexName + "] are unavailable");
59+
}
60+
return IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(indexMetadata.getSettings());
5361
}
5462
}

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemoverTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ private void givenBucket(Bucket bucket) {
201201
}).when(client).execute(eq(TransportSearchAction.TYPE), any(), any());
202202
}
203203

204-
205-
206204
private ExpiredResultsRemover createExpiredResultsRemover(Iterator<Job> jobIterator) {
207205
return createExpiredResultsRemover(jobIterator, true);
208206
}

0 commit comments

Comments
 (0)