Cleanup and Optimize Multiple Serialization Spots (#59626) (#59936)

Follow up to #59606 using some of the new infrastructure and making similar cleanups (and due to at times better handling of size hints and empty collections also optimizations in the stream utility methods this also means speedups) in various spots in the core codebase.
This commit is contained in:
Armin Braun 2020-07-21 10:06:56 +02:00 committed by GitHub
parent 8647872a1e
commit 5b92596fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 100 additions and 554 deletions

View File

@ -111,10 +111,7 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
MultiSearchTemplateResponse(StreamInput in) throws IOException {
super(in);
items = new Item[in.readVInt()];
for (int i = 0; i < items.length; i++) {
items[i] = new Item(in);
}
items = in.readArray(Item::new, Item[]::new);
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
tookInMillis = in.readVLong();
} else {
@ -148,10 +145,7 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(items.length);
for (Item item : items) {
item.writeTo(out);
}
out.writeArray(items);
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
out.writeVLong(tookInMillis);
}

View File

@ -27,7 +27,6 @@ import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
@ -62,15 +61,7 @@ public class ClusterHealthRequest extends MasterNodeReadRequest<ClusterHealthReq
public ClusterHealthRequest(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
if (size == 0) {
indices = Strings.EMPTY_ARRAY;
} else {
indices = new String[size];
for (int i = 0; i < indices.length; i++) {
indices[i] = in.readString();
}
}
indices = in.readStringArray();
timeout = in.readTimeValue();
if (in.readBoolean()) {
waitForStatus = ClusterHealthStatus.fromValue(in.readByte());
@ -97,10 +88,7 @@ public class ClusterHealthRequest extends MasterNodeReadRequest<ClusterHealthReq
if (indices == null) {
out.writeVInt(0);
} else {
out.writeVInt(indices.length);
for (String index : indices) {
out.writeString(index);
}
out.writeStringArray(indices);
}
out.writeTimeValue(timeout);
if (waitForStatus == null) {

View File

@ -41,19 +41,13 @@ public class ClusterSearchShardsGroup implements Writeable, ToXContentObject {
ClusterSearchShardsGroup(StreamInput in) throws IOException {
shardId = new ShardId(in);
shards = new ShardRouting[in.readVInt()];
for (int i = 0; i < shards.length; i++) {
shards[i] = new ShardRouting(shardId, in);
}
shards = in.readArray(i -> new ShardRouting(shardId, i), ShardRouting[]::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
shardId.writeTo(out);
out.writeVInt(shards.length);
for (ShardRouting shardRouting : shards) {
shardRouting.writeToThin(out);
}
out.writeArray((o, s) -> s.writeToThin(o), shards);
}
public ShardId getShardId() {

View File

@ -50,10 +50,7 @@ public class ClusterSearchShardsRequest extends MasterNodeReadRequest<ClusterSea
public ClusterSearchShardsRequest(StreamInput in) throws IOException {
super(in);
indices = new String[in.readVInt()];
for (int i = 0; i < indices.length; i++) {
indices[i] = in.readString();
}
indices = in.readStringArray();
routing = in.readOptionalString();
preference = in.readOptionalString();
@ -64,12 +61,7 @@ public class ClusterSearchShardsRequest extends MasterNodeReadRequest<ClusterSea
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(indices.length);
for (String index : indices) {
out.writeString(index);
}
out.writeStringArray(indices);
out.writeOptionalString(routing);
out.writeOptionalString(preference);

View File

@ -29,7 +29,6 @@ import org.elasticsearch.search.internal.AliasFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ClusterSearchShardsResponse extends ActionResponse implements ToXContentObject {
@ -40,38 +39,16 @@ public class ClusterSearchShardsResponse extends ActionResponse implements ToXCo
public ClusterSearchShardsResponse(StreamInput in) throws IOException {
super(in);
groups = new ClusterSearchShardsGroup[in.readVInt()];
for (int i = 0; i < groups.length; i++) {
groups[i] = new ClusterSearchShardsGroup(in);
}
nodes = new DiscoveryNode[in.readVInt()];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = new DiscoveryNode(in);
}
int size = in.readVInt();
indicesAndFilters = new HashMap<>();
for (int i = 0; i < size; i++) {
String index = in.readString();
AliasFilter aliasFilter = new AliasFilter(in);
indicesAndFilters.put(index, aliasFilter);
}
groups = in.readArray(ClusterSearchShardsGroup::new, ClusterSearchShardsGroup[]::new);
nodes = in.readArray(DiscoveryNode::new, DiscoveryNode[]::new);
indicesAndFilters = in.readMap(StreamInput::readString, AliasFilter::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(groups.length);
for (ClusterSearchShardsGroup response : groups) {
response.writeTo(out);
}
out.writeVInt(nodes.length);
for (DiscoveryNode node : nodes) {
node.writeTo(out);
}
out.writeVInt(indicesAndFilters.size());
for (Map.Entry<String, AliasFilter> entry : indicesAndFilters.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
}
out.writeArray(groups);
out.writeArray(nodes);
out.writeMap(indicesAndFilters, StreamOutput::writeString, (o, s) -> s.writeTo(o));
}
public ClusterSearchShardsResponse(ClusterSearchShardsGroup[] groups, DiscoveryNode[] nodes,

View File

@ -70,15 +70,13 @@ public class SnapshotStatus implements ToXContentObject, Writeable {
private SnapshotStats stats;
@Nullable
private Boolean includeGlobalState;
private final Boolean includeGlobalState;
SnapshotStatus(StreamInput in) throws IOException {
snapshot = new Snapshot(in);
state = State.fromValue(in.readByte());
shards = Collections.unmodifiableList(in.readList(SnapshotIndexShardStatus::new));
if (in.getVersion().onOrAfter(Version.V_6_2_0)) {
includeGlobalState = in.readOptionalBoolean();
}
includeGlobalState = in.readOptionalBoolean();
final long startTime;
final long time;
if (in.getVersion().onOrAfter(Version.V_7_4_0)) {
@ -181,9 +179,7 @@ public class SnapshotStatus implements ToXContentObject, Writeable {
snapshot.writeTo(out);
out.writeByte(state.value());
out.writeList(shards);
if (out.getVersion().onOrAfter(Version.V_6_2_0)) {
out.writeOptionalBoolean(includeGlobalState);
}
out.writeOptionalBoolean(includeGlobalState);
if (out.getVersion().onOrAfter(Version.V_7_4_0)) {
out.writeLong(stats.getStartTime());
out.writeLong(stats.getTime());

View File

@ -29,7 +29,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@ -45,12 +44,7 @@ public class SnapshotsStatusResponse extends ActionResponse implements ToXConten
public SnapshotsStatusResponse(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
List<SnapshotStatus> builder = new ArrayList<>();
for (int i = 0; i < size; i++) {
builder.add(new SnapshotStatus(in));
}
snapshots = Collections.unmodifiableList(builder);
snapshots = Collections.unmodifiableList(in.readList(SnapshotStatus::new));
}
SnapshotsStatusResponse(List<SnapshotStatus> snapshots) {
@ -68,10 +62,7 @@ public class SnapshotsStatusResponse extends ActionResponse implements ToXConten
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(snapshots.size());
for (SnapshotStatus snapshotInfo : snapshots) {
snapshotInfo.writeTo(out);
}
out.writeList(snapshots);
}
@Override

View File

@ -33,9 +33,9 @@ import java.io.IOException;
public class ClusterStatsNodeResponse extends BaseNodeResponse {
private NodeInfo nodeInfo;
private NodeStats nodeStats;
private ShardStats[] shardsStats;
private final NodeInfo nodeInfo;
private final NodeStats nodeStats;
private final ShardStats[] shardsStats;
private ClusterHealthStatus clusterStatus;
public ClusterStatsNodeResponse(StreamInput in) throws IOException {
@ -46,11 +46,7 @@ public class ClusterStatsNodeResponse extends BaseNodeResponse {
}
this.nodeInfo = new NodeInfo(in);
this.nodeStats = new NodeStats(in);
int size = in.readVInt();
shardsStats = new ShardStats[size];
for (int i = 0; i < size; i++) {
shardsStats[i] = new ShardStats(in);
}
shardsStats = in.readArray(ShardStats::new, ShardStats[]::new);
}
public ClusterStatsNodeResponse(DiscoveryNode node, @Nullable ClusterHealthStatus clusterStatus,
@ -97,9 +93,6 @@ public class ClusterStatsNodeResponse extends BaseNodeResponse {
}
nodeInfo.writeTo(out);
nodeStats.writeTo(out);
out.writeVInt(shardsStats.length);
for (ShardStats ss : shardsStats) {
ss.writeTo(out);
}
out.writeArray(shardsStats);
}
}

View File

@ -27,21 +27,16 @@ import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PendingClusterTasksResponse extends ActionResponse implements Iterable<PendingClusterTask>, ToXContentObject {
private List<PendingClusterTask> pendingTasks;
private final List<PendingClusterTask> pendingTasks;
public PendingClusterTasksResponse(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
pendingTasks = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
pendingTasks.add(new PendingClusterTask(in));
}
pendingTasks = in.readList(PendingClusterTask::new);
}
PendingClusterTasksResponse(List<PendingClusterTask> pendingTasks) {
@ -108,10 +103,7 @@ public class PendingClusterTasksResponse extends ActionResponse implements Itera
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(pendingTasks.size());
for (PendingClusterTask task : pendingTasks) {
task.writeTo(out);
}
out.writeList(pendingTasks);
}
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.admin.indices.alias.get;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
@ -27,14 +26,12 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class GetAliasesResponse extends ActionResponse {
private ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.of();
private final ImmutableOpenMap<String, List<AliasMetadata>> aliases;
public GetAliasesResponse(ImmutableOpenMap<String, List<AliasMetadata>> aliases) {
this.aliases = aliases;
@ -42,18 +39,7 @@ public class GetAliasesResponse extends ActionResponse {
public GetAliasesResponse(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliasesBuilder = ImmutableOpenMap.builder();
for (int i = 0; i < size; i++) {
String key = in.readString();
int valueSize = in.readVInt();
List<AliasMetadata> value = new ArrayList<>(valueSize);
for (int j = 0; j < valueSize; j++) {
value.add(new AliasMetadata(in));
}
aliasesBuilder.put(key, Collections.unmodifiableList(value));
}
aliases = aliasesBuilder.build();
aliases = in.readImmutableMap(StreamInput::readString, i -> i.readList(AliasMetadata::new));
}
public ImmutableOpenMap<String, List<AliasMetadata>> getAliases() {
@ -62,14 +48,7 @@ public class GetAliasesResponse extends ActionResponse {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(aliases.size());
for (ObjectObjectCursor<String, List<AliasMetadata>> entry : aliases) {
out.writeString(entry.key);
out.writeVInt(entry.value.size());
for (AliasMetadata aliasMetadata : entry.value) {
aliasMetadata.writeTo(out);
}
}
out.writeMap(aliases, StreamOutput::writeString, StreamOutput::writeList);
}
@Override

View File

@ -59,7 +59,7 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
private ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of();
private ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
private ImmutableOpenMap<String, String> dataStreams = ImmutableOpenMap.of();
private String[] indices;
private final String[] indices;
public GetIndexResponse(String[] indices,
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings,
@ -126,11 +126,9 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
settings = settingsMapBuilder.build();
ImmutableOpenMap.Builder<String, Settings> defaultSettingsMapBuilder = ImmutableOpenMap.builder();
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
int defaultSettingsSize = in.readVInt();
for (int i = 0; i < defaultSettingsSize ; i++) {
defaultSettingsMapBuilder.put(in.readString(), Settings.readSettingsFromStream(in));
}
int defaultSettingsSize = in.readVInt();
for (int i = 0; i < defaultSettingsSize; i++) {
defaultSettingsMapBuilder.put(in.readString(), Settings.readSettingsFromStream(in));
}
defaultSettings = defaultSettingsMapBuilder.build();
@ -245,12 +243,10 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
out.writeString(indexEntry.key);
Settings.writeSettingsToStream(indexEntry.value, out);
}
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeVInt(defaultSettings.size());
for (ObjectObjectCursor<String, Settings> indexEntry : defaultSettings) {
out.writeString(indexEntry.key);
Settings.writeSettingsToStream(indexEntry.value, out);
}
out.writeVInt(defaultSettings.size());
for (ObjectObjectCursor<String, Settings> indexEntry : defaultSettings) {
out.writeString(indexEntry.key);
Settings.writeSettingsToStream(indexEntry.value, out);
}
if (out.getVersion().onOrAfter(Version.V_7_8_0)) {
out.writeVInt(dataStreams.size());

View File

@ -223,8 +223,8 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
}, MAPPING, ObjectParser.ValueType.OBJECT);
}
private String fullName;
private BytesReference source;
private final String fullName;
private final BytesReference source;
public FieldMappingMetadata(String fullName, BytesReference source) {
this.fullName = fullName;

View File

@ -386,21 +386,9 @@ public class ResolveIndexAction extends ActionType<ResolveIndexAction.Response>
}
public Response(StreamInput in) throws IOException {
this.indices = new ArrayList<>();
int count = in.readVInt();
for (int k = 0; k < count; k++) {
indices.add(new ResolvedIndex(in));
}
this.aliases = new ArrayList<>();
count = in.readVInt();
for (int k = 0; k < count; k++) {
aliases.add(new ResolvedAlias(in));
}
this.dataStreams = new ArrayList<>();
count = in.readVInt();
for (int k = 0; k < count; k++) {
dataStreams.add(new ResolvedDataStream(in));
}
this.indices = in.readList(ResolvedIndex::new);
this.aliases = in.readList(ResolvedAlias::new);
this.dataStreams = in.readList(ResolvedDataStream::new);
}
public List<ResolvedIndex> getIndices() {
@ -417,18 +405,9 @@ public class ResolveIndexAction extends ActionType<ResolveIndexAction.Response>
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(indices.size());
for (ResolvedIndex index : indices) {
index.writeTo(out);
}
out.writeVInt(aliases.size());
for (ResolvedAlias alias : aliases) {
alias.writeTo(out);
}
out.writeVInt(dataStreams.size());
for (ResolvedDataStream dataStream : dataStreams) {
dataStream.writeTo(out);
}
out.writeList(indices);
out.writeList(aliases);
out.writeList(dataStreams);
}
@Override

View File

@ -26,16 +26,14 @@ import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.index.engine.Segment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ShardSegments implements Writeable, Iterable<Segment> {
private ShardRouting shardRouting;
private final ShardRouting shardRouting;
private List<Segment> segments;
private final List<Segment> segments;
ShardSegments(ShardRouting shardRouting, List<Segment> segments) {
this.shardRouting = shardRouting;
@ -44,15 +42,7 @@ public class ShardSegments implements Writeable, Iterable<Segment> {
ShardSegments(StreamInput in) throws IOException {
shardRouting = new ShardRouting(in);
int size = in.readVInt();
if (size == 0) {
segments = Collections.emptyList();
} else {
segments = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
segments.add(new Segment(in));
}
}
segments = in.readList(Segment::new);
}
@Override
@ -91,9 +81,6 @@ public class ShardSegments implements Writeable, Iterable<Segment> {
@Override
public void writeTo(StreamOutput out) throws IOException {
shardRouting.writeTo(out);
out.writeVInt(segments.size());
for (Segment segment : segments) {
segment.writeTo(out);
}
out.writeList(segments);
}
}

View File

@ -41,8 +41,8 @@ import java.util.Objects;
public class GetSettingsResponse extends ActionResponse implements ToXContentObject {
private ImmutableOpenMap<String, Settings> indexToSettings = ImmutableOpenMap.of();
private ImmutableOpenMap<String, Settings> indexToDefaultSettings = ImmutableOpenMap.of();
private final ImmutableOpenMap<String, Settings> indexToSettings;
private final ImmutableOpenMap<String, Settings> indexToDefaultSettings;
public GetSettingsResponse(ImmutableOpenMap<String, Settings> indexToSettings,
ImmutableOpenMap<String, Settings> indexToDefaultSettings) {
@ -52,22 +52,8 @@ public class GetSettingsResponse extends ActionResponse implements ToXContentObj
public GetSettingsResponse(StreamInput in) throws IOException {
super(in);
int settingsSize = in.readVInt();
ImmutableOpenMap.Builder<String, Settings> settingsBuilder = ImmutableOpenMap.builder();
for (int i = 0; i < settingsSize; i++) {
settingsBuilder.put(in.readString(), Settings.readSettingsFromStream(in));
}
ImmutableOpenMap.Builder<String, Settings> defaultSettingsBuilder = ImmutableOpenMap.builder();
if (in.getVersion().onOrAfter(org.elasticsearch.Version.V_6_4_0)) {
int defaultSettingsSize = in.readVInt();
for (int i = 0; i < defaultSettingsSize ; i++) {
defaultSettingsBuilder.put(in.readString(), Settings.readSettingsFromStream(in));
}
}
indexToSettings = settingsBuilder.build();
indexToDefaultSettings = defaultSettingsBuilder.build();
indexToSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream);
indexToDefaultSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream);
}
/**
@ -118,18 +104,8 @@ public class GetSettingsResponse extends ActionResponse implements ToXContentObj
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(indexToSettings.size());
for (ObjectObjectCursor<String, Settings> cursor : indexToSettings) {
out.writeString(cursor.key);
Settings.writeSettingsToStream(cursor.value, out);
}
if (out.getVersion().onOrAfter(org.elasticsearch.Version.V_6_4_0)) {
out.writeVInt(indexToDefaultSettings.size());
for (ObjectObjectCursor<String, Settings> cursor : indexToDefaultSettings) {
out.writeString(cursor.key);
Settings.writeSettingsToStream(cursor.value, out);
}
}
out.writeMap(indexToSettings, StreamOutput::writeString, (o, s) -> Settings.writeSettingsToStream(s, o));
out.writeMap(indexToDefaultSettings, StreamOutput::writeString, (o, s) -> Settings.writeSettingsToStream(s, o));
}
private static void parseSettingsField(XContentParser parser, String currentIndexName, Map<String, Settings> indexToSettings,

View File

@ -30,17 +30,13 @@ import java.util.Iterator;
public class IndexShardStats implements Iterable<ShardStats>, Writeable {
private ShardId shardId;
private final ShardId shardId;
private ShardStats[] shards;
private final ShardStats[] shards;
public IndexShardStats(StreamInput in) throws IOException {
shardId = new ShardId(in);
int shardSize = in.readVInt();
shards = new ShardStats[shardSize];
for (int i = 0; i < shardSize; i++) {
shards[i] = new ShardStats(in);
}
shards = in.readArray(ShardStats::new, ShardStats[]::new);
}
public IndexShardStats(ShardId shardId, ShardStats[] shards) {
@ -98,9 +94,6 @@ public class IndexShardStats implements Iterable<ShardStats>, Writeable {
@Override
public void writeTo(StreamOutput out) throws IOException {
shardId.writeTo(out);
out.writeVInt(shards.length);
for (ShardStats stats : shards) {
stats.writeTo(out);
}
out.writeArray(shards);
}
}

View File

@ -88,10 +88,7 @@ public class BulkRequest extends ActionRequest implements CompositeIndicesReques
public BulkRequest(StreamInput in) throws IOException {
super(in);
waitForActiveShards = ActiveShardCount.readFrom(in);
int size = in.readVInt();
for (int i = 0; i < size; i++) {
requests.add(DocWriteRequest.readDocumentRequest(null, in));
}
requests.addAll(in.readList(i -> DocWriteRequest.readDocumentRequest(null, i)));
refreshPolicy = RefreshPolicy.readFrom(in);
timeout = in.readTimeValue();
}
@ -418,10 +415,7 @@ public class BulkRequest extends ActionRequest implements CompositeIndicesReques
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
waitForActiveShards.writeTo(out);
out.writeVInt(requests.size());
for (DocWriteRequest<?> request : requests) {
DocWriteRequest.writeDocumentRequest(out, request);
}
out.writeCollection(requests, DocWriteRequest::writeDocumentRequest);
refreshPolicy.writeTo(out);
out.writeTimeValue(timeout);
}

View File

@ -52,18 +52,13 @@ public class BulkResponse extends ActionResponse implements Iterable<BulkItemRes
public static final long NO_INGEST_TOOK = -1L;
private BulkItemResponse[] responses;
private long tookInMillis;
private long ingestTookInMillis;
BulkResponse() {}
private final BulkItemResponse[] responses;
private final long tookInMillis;
private final long ingestTookInMillis;
public BulkResponse(StreamInput in) throws IOException {
super(in);
responses = new BulkItemResponse[in.readVInt()];
for (int i = 0; i < responses.length; i++) {
responses[i] = new BulkItemResponse(in);
}
responses = in.readArray(BulkItemResponse::new, BulkItemResponse[]::new);
tookInMillis = in.readVLong();
ingestTookInMillis = in.readZLong();
}
@ -140,10 +135,7 @@ public class BulkResponse extends ActionResponse implements Iterable<BulkItemRes
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(responses.length);
for (BulkItemResponse response : responses) {
response.writeTo(out);
}
out.writeArray(responses);
out.writeVLong(tookInMillis);
out.writeZLong(ingestTookInMillis);
}

View File

@ -42,13 +42,8 @@ public class BulkShardRequest extends ReplicatedWriteRequest<BulkShardRequest> i
public BulkShardRequest(StreamInput in) throws IOException {
super(in);
items = new BulkItemRequest[in.readVInt()];
final ShardId itemShardId = in.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION) ? shardId : null;
for (int i = 0; i < items.length; i++) {
if (in.readBoolean()) {
items[i] = new BulkItemRequest(itemShardId, in);
}
}
items = in.readArray(i -> i.readOptionalWriteable(inpt -> new BulkItemRequest(itemShardId, inpt)), BulkItemRequest[]::new);
}
public BulkShardRequest(ShardId shardId, RefreshPolicy refreshPolicy, BulkItemRequest[] items) {
@ -81,21 +76,14 @@ public class BulkShardRequest extends ReplicatedWriteRequest<BulkShardRequest> i
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(items.length);
if (out.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION)) {
for (BulkItemRequest item : items) {
if (item != null) {
out.writeBoolean(true);
item.writeThin(out);
} else {
out.writeBoolean(false);
}
out.writeArray(out.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION) ? (o, item) -> {
if (item != null) {
o.writeBoolean(true);
item.writeThin(o);
} else {
o.writeBoolean(false);
}
} else {
for (BulkItemRequest item : items) {
out.writeOptionalWriteable(item);
}
}
} : StreamOutput::writeOptionalWriteable, items);
}
@Override

View File

@ -39,16 +39,8 @@ public class BulkShardResponse extends ReplicationResponse implements WriteRespo
BulkShardResponse(StreamInput in) throws IOException {
super(in);
shardId = new ShardId(in);
responses = new BulkItemResponse[in.readVInt()];
if (in.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION)) {
for (int i = 0; i < responses.length; i++) {
responses[i] = new BulkItemResponse(shardId, in);
}
} else {
for (int i = 0; i < responses.length; i++) {
responses[i] = new BulkItemResponse(in);
}
}
responses = in.readArray(in.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION)
? i -> new BulkItemResponse(shardId, i) : BulkItemResponse::new, BulkItemResponse[]::new);
}
// NOTE: public for testing only
@ -83,15 +75,7 @@ public class BulkShardResponse extends ReplicationResponse implements WriteRespo
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
shardId.writeTo(out);
out.writeVInt(responses.length);
if (out.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION)) {
for (BulkItemResponse response : responses) {
response.writeThin(out);
}
} else {
for (BulkItemResponse response : responses) {
response.writeTo(out);
}
}
out.writeArray(out.getVersion().onOrAfter(COMPACT_SHARD_ID_VERSION)
? (o, item) -> item.writeThin(out) : (o, item) -> item.writeTo(o), responses);
}
}

View File

@ -279,12 +279,7 @@ public class MultiGetRequest extends ActionRequest
preference = in.readOptionalString();
refresh = in.readBoolean();
realtime = in.readBoolean();
int size = in.readVInt();
items = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
items.add(new Item(in));
}
items = in.readList(Item::new);
}
public List<Item> getItems() {
@ -536,10 +531,6 @@ public class MultiGetRequest extends ActionRequest
}
}
public static void parseIds(XContentParser parser, List<Item> items) throws IOException {
parseIds(parser, items, null, null, null, null, null);
}
@Override
public Iterator<Item> iterator() {
return Collections.unmodifiableCollection(items).iterator();
@ -551,11 +542,7 @@ public class MultiGetRequest extends ActionRequest
out.writeOptionalString(preference);
out.writeBoolean(refresh);
out.writeBoolean(realtime);
out.writeVInt(items.size());
for (Item item : items) {
item.writeTo(out);
}
out.writeList(items);
}
@Override

View File

@ -129,10 +129,7 @@ public class MultiGetResponse extends ActionResponse implements Iterable<MultiGe
MultiGetResponse(StreamInput in) throws IOException {
super(in);
responses = new MultiGetItemResponse[in.readVInt()];
for (int i = 0; i < responses.length; i++) {
responses[i] = new MultiGetItemResponse(in);
}
responses = in.readArray(MultiGetItemResponse::new, MultiGetItemResponse[]::new);
}
public MultiGetItemResponse[] getResponses() {
@ -240,9 +237,6 @@ public class MultiGetResponse extends ActionResponse implements Iterable<MultiGe
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(responses.length);
for (MultiGetItemResponse response : responses) {
response.writeTo(out);
}
out.writeArray(responses);
}
}

View File

@ -275,29 +275,16 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
@Override
public void writeTo(StreamOutput out) throws IOException {
writeBlockSet(global, out);
out.writeVInt(indicesBlocks.size());
for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : indicesBlocks) {
out.writeString(entry.key);
writeBlockSet(entry.value, out);
}
out.writeMap(indicesBlocks, StreamOutput::writeString, (o, s) -> writeBlockSet(s, o));
}
private static void writeBlockSet(Set<ClusterBlock> blocks, StreamOutput out) throws IOException {
out.writeVInt(blocks.size());
for (ClusterBlock block : blocks) {
block.writeTo(out);
}
out.writeCollection(blocks);
}
public ClusterBlocks(StreamInput in) throws IOException {
Set<ClusterBlock> global = readBlockSet(in);
int size = in.readVInt();
ImmutableOpenMap.Builder<String, Set<ClusterBlock>> indicesBuilder = ImmutableOpenMap.builder(size);
for (int j = 0; j < size; j++) {
indicesBuilder.put(in.readString().intern(), readBlockSet(in));
}
this.global = global;
this.indicesBlocks = indicesBuilder.build();
this.global = readBlockSet(in);
this.indicesBlocks = in.readImmutableMap(i -> i.readString().intern(), ClusterBlocks::readBlockSet);
levelHolders = generateLevelHolders(global, indicesBlocks);
}
@ -339,9 +326,9 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
public static class Builder {
private Set<ClusterBlock> global = new HashSet<>();
private final Set<ClusterBlock> global = new HashSet<>();
private Map<String, Set<ClusterBlock>> indices = new HashMap<>();
private final Map<String, Set<ClusterBlock>> indices = new HashMap<>();
public Builder() {
}

View File

@ -104,18 +104,8 @@ public class DiffableStringMap extends AbstractMap<String, String> implements Di
}
private DiffableStringMapDiff(StreamInput in) throws IOException {
deletes = new ArrayList<>();
upserts = new HashMap<>();
int deletesCount = in.readVInt();
for (int i = 0; i < deletesCount; i++) {
deletes.add(in.readString());
}
int upsertsCount = in.readVInt();
for (int i = 0; i < upsertsCount; i++) {
String key = in.readString();
String newValue = in.readString();
upserts.put(key, newValue);
}
deletes = in.readStringList();
upserts = in.readMap(StreamInput::readString, StreamInput::readString);
}
public List<String> getDeletes() {
@ -132,15 +122,8 @@ public class DiffableStringMap extends AbstractMap<String, String> implements Di
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(deletes.size());
for (String delete : deletes) {
out.writeString(delete);
}
out.writeVInt(upserts.size());
for (Map.Entry<String, String> entry : upserts.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeStringCollection(deletes);
out.writeMap(upserts, StreamOutput::writeString, StreamOutput::writeString);
}
@Override

View File

@ -37,18 +37,12 @@ class CompositeKey implements Writeable {
}
CompositeKey(StreamInput in) throws IOException {
values = new Comparable[in.readVInt()];
for (int i = 0; i < values.length; i++) {
values[i] = (Comparable) in.readGenericValue();
}
values = in.readArray(i -> (Comparable) i.readGenericValue(), Comparable[]::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(values.length);
for (int i = 0; i < values.length; i++) {
out.writeGenericValue(values[i]);
}
out.writeArray(StreamOutput::writeGenericValue, values);
}
Comparable[] values() {

View File

@ -304,28 +304,14 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
*/
public SnapshotInfo(final StreamInput in) throws IOException {
snapshotId = new SnapshotId(in);
int size = in.readVInt();
List<String> indicesListBuilder = new ArrayList<>();
for (int i = 0; i < size; i++) {
indicesListBuilder.add(in.readString());
}
indices = Collections.unmodifiableList(indicesListBuilder);
indices = Collections.unmodifiableList(in.readStringList());
state = in.readBoolean() ? SnapshotState.fromValue(in.readByte()) : null;
reason = in.readOptionalString();
startTime = in.readVLong();
endTime = in.readVLong();
totalShards = in.readVInt();
successfulShards = in.readVInt();
size = in.readVInt();
if (size > 0) {
List<SnapshotShardFailure> failureBuilder = new ArrayList<>();
for (int i = 0; i < size; i++) {
failureBuilder.add(new SnapshotShardFailure(in));
}
shardFailures = Collections.unmodifiableList(failureBuilder);
} else {
shardFailures = Collections.emptyList();
}
shardFailures = Collections.unmodifiableList(in.readList(SnapshotShardFailure::new));
version = in.readBoolean() ? Version.readVersion(in) : null;
if (in.getVersion().onOrAfter(INCLUDE_GLOBAL_STATE_INTRODUCED)) {
includeGlobalState = in.readOptionalBoolean();
@ -735,10 +721,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
@Override
public void writeTo(final StreamOutput out) throws IOException {
snapshotId.writeTo(out);
out.writeVInt(indices.size());
for (String index : indices) {
out.writeString(index);
}
out.writeStringCollection(indices);
if (state != null) {
out.writeBoolean(true);
out.writeByte(state.value());
@ -750,10 +733,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
out.writeVLong(endTime);
out.writeVInt(totalShards);
out.writeVInt(successfulShards);
out.writeVInt(shardFailures.size());
for (SnapshotShardFailure failure : shardFailures) {
failure.writeTo(out);
}
out.writeList(shardFailures);
if (version != null) {
out.writeBoolean(true);
Version.writeVersion(version, out);

View File

@ -20,15 +20,11 @@
package org.elasticsearch.action.admin.indices.get;
import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponseTests;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponseTests;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
@ -36,34 +32,18 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.RandomCreateIndexGenerator;
import org.elasticsearch.rest.BaseRestHandler;
import org.junit.Assert;
import org.elasticsearch.test.AbstractSerializingTestCase;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.Locale;
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.index.IndexSettings.INDEX_REFRESH_INTERVAL_SETTING;
import java.util.function.Predicate;
public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexResponse> {
/**
* The following byte response was generated from the v6.3.0 tag
*/
private static final String TEST_6_3_0_RESPONSE_BYTES =
"AQhteV9pbmRleAEIbXlfaW5kZXgBA2RvYwNkb2OePID6KURGTACqVkrLTM1JiTdUsqpWKqksSFWyUiouKcrMS1eqrQUAAAD//" +
"wMAAAABCG15X2luZGV4AgZhbGlhczEAAQJyMQECcjEGYWxpYXMyAX8jNXYiREZMAKpWKkktylWyqlaqTE0sUrIyMjA0q60FAAAA//" +
"8DAAAAAQhteV9pbmRleAIYaW5kZXgubnVtYmVyX29mX3JlcGxpY2FzAAExFmluZGV4Lm51bWJlcl9vZl9zaGFyZHMAATI=";
private static final GetIndexResponse TEST_6_3_0_RESPONSE_INSTANCE = getExpectedTest630Response();
@Override
protected GetIndexResponse doParseInstance(XContentParser parser) throws IOException {
return GetIndexResponse.fromXContent(parser);
@ -122,85 +102,6 @@ public class GetIndexResponseTests extends AbstractSerializingTestCase<GetIndexR
f.contains(".aliases");
}
private static ImmutableOpenMap<String, List<AliasMetadata>> getTestAliases(String indexName) {
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
List<AliasMetadata> indexAliases = new ArrayList<>();
indexAliases.add(new AliasMetadata.Builder("alias1").routing("r1").build());
indexAliases.add(new AliasMetadata.Builder("alias2").filter("{\"term\": {\"year\": 2016}}").build());
aliases.put(indexName, Collections.unmodifiableList(indexAliases));
return aliases.build();
}
private static ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> getTestMappings(String indexName) {
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetadata>> mappings = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, MappingMetadata> indexMappings = ImmutableOpenMap.builder();
try {
indexMappings.put(
"doc",
new MappingMetadata("doc",
Collections.singletonMap("field_1", Collections.singletonMap("type", "string"))
)
);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
mappings.put(indexName, indexMappings.build());
return mappings.build();
}
private static ImmutableOpenMap<String, Settings> getTestSettings(String indexName) {
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
Settings.Builder indexSettings = Settings.builder();
indexSettings.put(SETTING_NUMBER_OF_SHARDS, 2);
indexSettings.put(SETTING_NUMBER_OF_REPLICAS, 1);
settings.put(indexName, indexSettings.build());
return settings.build();
}
private static GetIndexResponse getExpectedTest630Response() {
// The only difference between this snippet and the one used for generation TEST_6_3_0_RESPONSE_BYTES is the
// constructor for GetIndexResponse which also takes defaultSettings now.
String indexName = "my_index";
String indices[] = { indexName };
return
new GetIndexResponse(
indices, getTestMappings(indexName), getTestAliases(indexName), getTestSettings(indexName),
ImmutableOpenMap.of(), ImmutableOpenMap.of()
);
}
private static GetIndexResponse getResponseWithDefaultSettings() {
String indexName = "my_index";
String indices[] = { indexName };
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
Settings.Builder indexDefaultSettings = Settings.builder();
indexDefaultSettings.put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "1s");
defaultSettings.put(indexName, indexDefaultSettings.build());
return
new GetIndexResponse(
indices, getTestMappings(indexName), getTestAliases(indexName), getTestSettings(indexName),
defaultSettings.build(), ImmutableOpenMap.of()
);
}
public void testCanDecode622Response() throws IOException {
StreamInput si = StreamInput.wrap(Base64.getDecoder().decode(TEST_6_3_0_RESPONSE_BYTES));
si.setVersion(Version.V_6_3_0);
GetIndexResponse response = new GetIndexResponse(si);
Assert.assertEquals(TEST_6_3_0_RESPONSE_INSTANCE, response);
}
public void testCanOutput622Response() throws IOException {
GetIndexResponse responseWithExtraFields = getResponseWithDefaultSettings();
BytesStreamOutput bso = new BytesStreamOutput();
bso.setVersion(Version.V_6_3_0);
responseWithExtraFields.writeTo(bso);
String base64OfResponse = Base64.getEncoder().encodeToString(BytesReference.toBytes(bso.bytes()));
Assert.assertEquals(TEST_6_3_0_RESPONSE_BYTES, base64OfResponse);
}
/**
* For xContent roundtrip testing we force the xContent output to still contain types because the parser still expects them.
* The new typeless parsing is implemented in the client side GetIndexResponse.

View File

@ -19,42 +19,22 @@
package org.elasticsearch.action.admin.indices.settings.get;
import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.RandomCreateIndexGenerator;
import org.junit.Assert;
import org.elasticsearch.test.AbstractSerializingTestCase;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.index.IndexSettings.INDEX_REFRESH_INTERVAL_SETTING;
public class GetSettingsResponseTests extends AbstractSerializingTestCase<GetSettingsResponse> {
/*
index.number_of_shards=2,index.number_of_replicas=1. The below base64'd bytes were generated by
code from the 6.2.2 tag.
*/
private static final String TEST_6_2_2_RESPONSE_BYTES =
"AQppbmRleF9uYW1lAhhpbmRleC5udW1iZXJfb2ZfcmVwbGljYXMAATEWaW5kZXgubnVtYmVyX29mX3NoYXJkcwABMg==";
/* This response object was generated using similar code to the code used to create the above bytes */
private static final GetSettingsResponse TEST_6_2_2_RESPONSE_INSTANCE = getExpectedTest622Response();
@Override
protected GetSettingsResponse createTestInstance() {
HashMap<String, Settings> indexToSettings = new HashMap<>();
@ -111,49 +91,4 @@ public class GetSettingsResponseTests extends AbstractSerializingTestCase<GetSet
//we do not want to add new fields at the root (index-level), or inside settings blocks
return f -> f.equals("") || f.contains(".settings") || f.contains(".defaults");
}
private static GetSettingsResponse getExpectedTest622Response() {
/* This is a fairly direct copy of the code used to generate the base64'd response above -- with the caveat that the constructor
has been modified so that the code compiles on this version of elasticsearch
*/
HashMap<String, Settings> indexToSettings = new HashMap<>();
Settings.Builder builder = Settings.builder();
builder.put(SETTING_NUMBER_OF_SHARDS, 2);
builder.put(SETTING_NUMBER_OF_REPLICAS, 1);
indexToSettings.put("index_name", builder.build());
GetSettingsResponse response = new GetSettingsResponse(ImmutableOpenMap.<String, Settings>builder().putAll(indexToSettings).build
(), ImmutableOpenMap.of());
return response;
}
private static GetSettingsResponse getResponseWithNewFields() {
HashMap<String, Settings> indexToDefaultSettings = new HashMap<>();
Settings.Builder builder = Settings.builder();
builder.put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "1s");
indexToDefaultSettings.put("index_name", builder.build());
ImmutableOpenMap<String, Settings> defaultsMap = ImmutableOpenMap.<String, Settings>builder().putAll(indexToDefaultSettings)
.build();
return new GetSettingsResponse(getExpectedTest622Response().getIndexToSettings(), defaultsMap);
}
public void testCanDecode622Response() throws IOException {
StreamInput si = StreamInput.wrap(Base64.getDecoder().decode(TEST_6_2_2_RESPONSE_BYTES));
si.setVersion(Version.V_6_2_2);
GetSettingsResponse response = new GetSettingsResponse(si);
Assert.assertEquals(TEST_6_2_2_RESPONSE_INSTANCE, response);
}
public void testCanOutput622Response() throws IOException {
GetSettingsResponse responseWithExtraFields = getResponseWithNewFields();
BytesStreamOutput bso = new BytesStreamOutput();
bso.setVersion(Version.V_6_2_2);
responseWithExtraFields.writeTo(bso);
String base64OfResponse = Base64.getEncoder().encodeToString(BytesReference.toBytes(bso.bytes()));
Assert.assertEquals(TEST_6_2_2_RESPONSE_BYTES, base64OfResponse);
}
}