Data stream should refer to the backing indices using the Index class (#54199)

Backport of #54189
This commit is contained in:
Martijn van Groningen 2020-03-25 20:18:15 +01:00 committed by GitHub
parent d14f170093
commit 66861a82a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 14 deletions

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.Index;
import java.io.IOException;
import java.util.List;
@ -36,9 +37,9 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
private final String name;
private final String timeStampField;
private final List<String> indices;
private final List<Index> indices;
public DataStream(String name, String timeStampField, List<String> indices) {
public DataStream(String name, String timeStampField, List<Index> indices) {
this.name = name;
this.timeStampField = timeStampField;
this.indices = indices;
@ -52,12 +53,12 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
return timeStampField;
}
public List<String> getIndices() {
public List<Index> getIndices() {
return indices;
}
public DataStream(StreamInput in) throws IOException {
this(in.readString(), in.readString(), in.readStringList());
this(in.readString(), in.readString(), in.readList(Index::new));
}
public static Diff<DataStream> readDiffFrom(StreamInput in) throws IOException {
@ -68,7 +69,7 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeString(timeStampField);
out.writeStringCollection(indices);
out.writeList(indices);
}
public static final ParseField NAME_FIELD = new ParseField("name");
@ -77,12 +78,12 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<DataStream, Void> PARSER = new ConstructingObjectParser<>("data_stream",
args -> new DataStream((String) args[0], (String) args[1], (List<String>) args[2]));
args -> new DataStream((String) args[0], (String) args[1], (List<Index>) args[2]));
static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME_FIELD);
PARSER.declareString(ConstructingObjectParser.constructorArg(), TIMESTAMP_FIELD_FIELD);
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), INDICES_FIELD);
PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> Index.fromXContent(p), INDICES_FIELD);
}
public static DataStream fromXContent(XContentParser parser) throws IOException {

View File

@ -27,9 +27,10 @@ import org.elasticsearch.test.AbstractSerializingTestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.elasticsearch.cluster.metadata.DataStreamTests.randomIndexInstances;
public class GetDataStreamsResponseTests extends AbstractSerializingTestCase<Response> {
@Override
@ -53,8 +54,7 @@ public class GetDataStreamsResponseTests extends AbstractSerializingTestCase<Res
int numDataStreams = randomIntBetween(0, 8);
List<DataStream> dataStreams = new ArrayList<>();
for (int i = 0; i < numDataStreams; i++) {
dataStreams.add(new DataStream(randomAlphaOfLength(4), randomAlphaOfLength(4),
Arrays.asList(generateRandomStringArray(8, 4, false))));
dataStreams.add(new DataStream(randomAlphaOfLength(4), randomAlphaOfLength(4), randomIndexInstances()));
}
return new Response(dataStreams);
}

View File

@ -18,8 +18,10 @@
*/
package org.elasticsearch.cluster.metadata;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.Index;
import org.elasticsearch.test.AbstractSerializingTestCase;
import java.io.IOException;
@ -28,13 +30,17 @@ import java.util.List;
public class DataStreamTests extends AbstractSerializingTestCase<DataStream> {
public static DataStream randomInstance() {
public static List<Index> randomIndexInstances() {
int numIndices = randomIntBetween(0, 128);
List<String> indices = new ArrayList<>(numIndices);
List<Index> indices = new ArrayList<>(numIndices);
for (int i = 0; i < numIndices; i++) {
indices.add(randomAlphaOfLength(10));
indices.add(new Index(randomAlphaOfLength(10), UUIDs.randomBase64UUID(random())));
}
return new DataStream(randomAlphaOfLength(10), randomAlphaOfLength(10), indices);
return indices;
}
public static DataStream randomInstance() {
return new DataStream(randomAlphaOfLength(10), randomAlphaOfLength(10), randomIndexInstances());
}
@Override