Skip to content

Commit

Permalink
Fix NPE when deleting multiple backing indices on a data stream (elas…
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann authored Sep 14, 2020
1 parent f29c743 commit f7e5ed5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,10 @@ public Builder removeIndexTemplate(String name) {
return this;
}

public DataStream dataStream(String dataStreamName) {
return ((DataStreamMetadata) customs.get(DataStreamMetadata.TYPE)).dataStreams().get(dataStreamName);
}

public Builder dataStreams(Map<String, DataStream> dataStreams) {
this.customs.put(DataStreamMetadata.TYPE, new DataStreamMetadata(dataStreams));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
clusterBlocksBuilder.removeIndexBlocks(indexName);
metadataBuilder.remove(indexName);
if (backingIndices.containsKey(index)) {
DataStream parent = backingIndices.get(index);
DataStream parent = metadataBuilder.dataStream(backingIndices.get(index).getName());
metadataBuilder.put(parent.removeBackingIndex(index));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
import org.junit.Before;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -129,6 +132,32 @@ public void testDeleteBackingIndexForDataStream() {
DataStream.getDefaultBackingIndexName(dataStreamName, numIndexToDelete)), IsNull.nullValue());
}

public void testDeleteMultipleBackingIndexForDataStream() {
int numBackingIndices = randomIntBetween(3, 5);
int numBackingIndicesToDelete = randomIntBetween(2, numBackingIndices - 1);
String dataStreamName = randomAlphaOfLength(6).toLowerCase(Locale.ROOT);
ClusterState before = DataStreamTestHelper.getClusterStateWithDataStreams(
List.of(new Tuple<>(dataStreamName, numBackingIndices)), List.of());

List<Integer> indexNumbersToDelete =
randomSubsetOf(numBackingIndicesToDelete, IntStream.rangeClosed(1, numBackingIndices - 1).boxed().collect(Collectors.toList()));

Set<Index> indicesToDelete = new HashSet<>();
for (int k : indexNumbersToDelete) {
indicesToDelete.add(before.metadata().index(DataStream.getDefaultBackingIndexName(dataStreamName, k)).getIndex());
}
ClusterState after = service.deleteIndices(before, indicesToDelete);

DataStream dataStream = after.metadata().dataStreams().get(dataStreamName);
assertThat(dataStream, IsNull.notNullValue());
assertThat(dataStream.getIndices().size(), equalTo(numBackingIndices - indexNumbersToDelete.size()));
for (Index i : indicesToDelete) {
assertThat(after.metadata().getIndices().get(i.getName()), IsNull.nullValue());
assertFalse(dataStream.getIndices().contains(i));
}
assertThat(after.metadata().getIndices().size(), equalTo(numBackingIndices - indexNumbersToDelete.size()));
}

public void testDeleteCurrentWriteIndexForDataStream() {
int numBackingIndices = randomIntBetween(1, 5);
String dataStreamName = randomAlphaOfLength(6).toLowerCase(Locale.ROOT);
Expand Down

0 comments on commit f7e5ed5

Please sign in to comment.