From 98a62a6b2dafc4c6a602ec7ae99bdd4394e0b6ec Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Wed, 1 Jul 2020 11:14:01 -0500 Subject: [PATCH] Make DataStream instances explicitly immutable (#58688) (#58839) --- .../org/elasticsearch/cluster/metadata/DataStream.java | 7 +++---- .../elasticsearch/cluster/metadata/DataStreamTests.java | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java index 8238804094f..8220b8b430b 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java @@ -56,7 +56,7 @@ public final class DataStream extends AbstractDiffable implements To public DataStream(String name, TimestampField timeStampField, List indices, long generation) { this.name = name; this.timeStampField = timeStampField; - this.indices = indices; + this.indices = Collections.unmodifiableList(indices); this.generation = generation; assert indices.size() > 0; assert indices.get(indices.size() - 1).getName().equals(getDefaultBackingIndexName(name, generation)); @@ -236,12 +236,11 @@ public final class DataStream extends AbstractDiffable implements To "invalid type defined for mapping of timestamp_field"; this.name = name; - this.fieldMapping = fieldMapping; + this.fieldMapping = Collections.unmodifiableMap(fieldMapping); } public TimestampField(StreamInput in) throws IOException { - this.name = in.readString(); - this.fieldMapping = in.readMap(); + this(in.readString(), in.readMap()); } /** diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java index fcd363d6e5b..13ed09cd3b2 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java @@ -181,4 +181,10 @@ public class DataStreamTests extends AbstractSerializingTestCase { Map.of("properties", Map.of("@timestamp", Map.of("type", "date", "meta", Map.of("x", "y"))))))))); assertThat(mappings, equalTo(expectedMapping)); } + + public void testDataStreamsAreImmutable() { + DataStream ds = randomInstance(); + expectThrows(UnsupportedOperationException.class, () -> ds.getIndices().clear()); + expectThrows(UnsupportedOperationException.class, () -> ds.getTimeStampField().getFieldMapping().clear()); + } }