Skip to content

Commit 137ad57

Browse files
Fix for closed indices
1 parent 3257cec commit 137ad57

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,28 @@ Table buildTable(RestRequest request, IndexMetaData[] indicesMetaData, ClusterHe
409409
}
410410
}
411411

412-
// the index is present in the cluster state but is not returned in the indices stats API
413-
if (indexStats == null) {
412+
// the open index is present in the cluster state but is not returned in the indices stats API
413+
if (indexStats == null && state != IndexMetaData.State.CLOSE) {
414414
// the index stats API is called last, after cluster state and cluster health. If the index stats
415-
// has not resolved the same indices as the initial cluster state call, then the indices might
415+
// has not resolved the same open indices as the initial cluster state call, then the indices might
416416
// have been removed in the meantime or, more likely, are unauthorized. This is because the cluster
417417
// state exposes everything, even unauthorized indices, which are not exposed in APIs.
418418
// We ignore such an index instead of displaying it with an empty stats.
419419
continue;
420420
}
421421

422-
final CommonStats primaryStats = indexStats.getPrimaries();
423-
final CommonStats totalStats = indexStats.getTotal();
422+
final CommonStats primaryStats;
423+
final CommonStats totalStats;
424+
425+
if (state == IndexMetaData.State.CLOSE) {
426+
// empty stats for closed indices, but their names are displayed
427+
assert indexStats == null;
428+
primaryStats = new CommonStats();
429+
totalStats = new CommonStats();
430+
} else {
431+
primaryStats = indexStats.getPrimaries();
432+
totalStats = indexStats.getTotal();
433+
}
424434

425435
table.startRow();
426436
table.addCell(state == IndexMetaData.State.OPEN ?
@@ -609,8 +619,8 @@ Table buildTable(RestRequest request, IndexMetaData[] indicesMetaData, ClusterHe
609619
table.addCell(totalStats.getSearch() == null ? null : totalStats.getSearch().getTotal().getSuggestCount());
610620
table.addCell(primaryStats.getSearch() == null ? null : primaryStats.getSearch().getTotal().getSuggestCount());
611621

612-
table.addCell(indexStats.getTotal().getTotalMemory());
613-
table.addCell(indexStats.getPrimaries().getTotalMemory());
622+
table.addCell(totalStats.getTotalMemory());
623+
table.addCell(primaryStats.getTotalMemory());
614624

615625
table.addCell(searchThrottled);
616626

server/src/test/java/org/elasticsearch/rest/action/cat/RestIndicesActionTests.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
import org.elasticsearch.test.rest.FakeRestRequest;
6161
import org.elasticsearch.usage.UsageService;
6262

63-
import java.lang.reflect.Array;
6463
import java.nio.file.Path;
6564
import java.util.ArrayList;
65+
import java.util.Arrays;
6666
import java.util.Collections;
6767
import java.util.List;
6868

@@ -189,18 +189,13 @@ private IndicesStatsResponse randomIndicesStatsResponse(final IndexMetaData[] in
189189
);
190190
}
191191

192-
private <T> T[] removeRandomElement(T[] array) {
192+
private IndexMetaData[] removeRandomElement(IndexMetaData[] array) {
193193
assert array != null;
194194
assert array.length > 0;
195-
@SuppressWarnings("unchecked")
196-
final T[] smallerArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1);
197-
final int index = randomIntBetween(0, array.length - 1);
198-
if (index > 0) {
199-
System.arraycopy(array, 0, smallerArray, 0, index);
200-
}
201-
if (index < array.length - 1) {
202-
System.arraycopy(array, index + 1, smallerArray, index, array.length - index - 1);
203-
}
204-
return smallerArray;
195+
final List<IndexMetaData> collectionLessAnItem = new ArrayList<>();
196+
collectionLessAnItem.addAll(Arrays.asList(array));
197+
final int toRemoveIndex = randomIntBetween(0, array.length - 1);
198+
collectionLessAnItem.remove(toRemoveIndex);
199+
return collectionLessAnItem.toArray(new IndexMetaData[0]);
205200
}
206201
}

0 commit comments

Comments
 (0)