|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.action.search; |
| 10 | + |
| 11 | +import org.elasticsearch.action.IndicesRequest; |
| 12 | +import org.elasticsearch.action.OriginalIndices; |
| 13 | +import org.elasticsearch.action.support.IndicesOptions; |
| 14 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 15 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 16 | +import org.elasticsearch.common.io.stream.Writeable; |
| 17 | +import org.elasticsearch.core.Nullable; |
| 18 | +import org.elasticsearch.core.TimeValue; |
| 19 | +import org.elasticsearch.index.shard.ShardId; |
| 20 | +import org.elasticsearch.search.Scroll; |
| 21 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 22 | +import org.elasticsearch.search.internal.AliasFilter; |
| 23 | +import org.elasticsearch.search.internal.ShardSearchContextId; |
| 24 | +import org.elasticsearch.search.internal.ShardSearchRequest; |
| 25 | +import org.elasticsearch.tasks.Task; |
| 26 | +import org.elasticsearch.tasks.TaskId; |
| 27 | +import org.elasticsearch.transport.TransportRequest; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * Node-level request used during can-match phase |
| 38 | + */ |
| 39 | +public class CanMatchNodeRequest extends TransportRequest implements IndicesRequest { |
| 40 | + |
| 41 | + private final SearchSourceBuilder source; |
| 42 | + private final List<Shard> shards; |
| 43 | + private final SearchType searchType; |
| 44 | + private final String[] types; |
| 45 | + private final Boolean requestCache; |
| 46 | + private final boolean allowPartialSearchResults; |
| 47 | + private final Scroll scroll; |
| 48 | + private final int numberOfShards; |
| 49 | + private final long nowInMillis; |
| 50 | + @Nullable |
| 51 | + private final String clusterAlias; |
| 52 | + private final String[] indices; |
| 53 | + private final IndicesOptions indicesOptions; |
| 54 | + private final TimeValue waitForCheckpointsTimeout; |
| 55 | + |
| 56 | + public static class Shard implements Writeable { |
| 57 | + private final String[] indices; |
| 58 | + private final ShardId shardId; |
| 59 | + private final int shardRequestIndex; |
| 60 | + private final AliasFilter aliasFilter; |
| 61 | + private final float indexBoost; |
| 62 | + private final ShardSearchContextId readerId; |
| 63 | + private final TimeValue keepAlive; |
| 64 | + private final long waitForCheckpoint; |
| 65 | + |
| 66 | + public Shard(String[] indices, |
| 67 | + ShardId shardId, |
| 68 | + int shardRequestIndex, |
| 69 | + AliasFilter aliasFilter, |
| 70 | + float indexBoost, |
| 71 | + ShardSearchContextId readerId, |
| 72 | + TimeValue keepAlive, |
| 73 | + long waitForCheckpoint) { |
| 74 | + this.indices = indices; |
| 75 | + this.shardId = shardId; |
| 76 | + this.shardRequestIndex = shardRequestIndex; |
| 77 | + this.aliasFilter = aliasFilter; |
| 78 | + this.indexBoost = indexBoost; |
| 79 | + this.readerId = readerId; |
| 80 | + this.keepAlive = keepAlive; |
| 81 | + this.waitForCheckpoint = waitForCheckpoint; |
| 82 | + assert keepAlive == null || readerId != null : "readerId: " + readerId + " keepAlive: " + keepAlive; |
| 83 | + } |
| 84 | + |
| 85 | + public Shard(StreamInput in) throws IOException { |
| 86 | + indices = in.readStringArray(); |
| 87 | + shardId = new ShardId(in); |
| 88 | + shardRequestIndex = in.readVInt(); |
| 89 | + aliasFilter = new AliasFilter(in); |
| 90 | + indexBoost = in.readFloat(); |
| 91 | + readerId = in.readOptionalWriteable(ShardSearchContextId::new); |
| 92 | + keepAlive = in.readOptionalTimeValue(); |
| 93 | + waitForCheckpoint = in.readLong(); |
| 94 | + assert keepAlive == null || readerId != null : "readerId: " + readerId + " keepAlive: " + keepAlive; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void writeTo(StreamOutput out) throws IOException { |
| 99 | + out.writeStringArray(indices); |
| 100 | + shardId.writeTo(out); |
| 101 | + out.writeVInt(shardRequestIndex); |
| 102 | + aliasFilter.writeTo(out); |
| 103 | + out.writeFloat(indexBoost); |
| 104 | + out.writeOptionalWriteable(readerId); |
| 105 | + out.writeOptionalTimeValue(keepAlive); |
| 106 | + out.writeLong(waitForCheckpoint); |
| 107 | + } |
| 108 | + |
| 109 | + public int getShardRequestIndex() { |
| 110 | + return shardRequestIndex; |
| 111 | + } |
| 112 | + |
| 113 | + public String[] getOriginalIndices() { |
| 114 | + return indices; |
| 115 | + } |
| 116 | + |
| 117 | + public ShardId shardId() { |
| 118 | + return shardId; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public CanMatchNodeRequest( |
| 123 | + SearchRequest searchRequest, |
| 124 | + IndicesOptions indicesOptions, |
| 125 | + List<Shard> shards, |
| 126 | + int numberOfShards, |
| 127 | + long nowInMillis, |
| 128 | + @Nullable String clusterAlias |
| 129 | + ) { |
| 130 | + this.source = searchRequest.source(); |
| 131 | + this.indicesOptions = indicesOptions; |
| 132 | + this.shards = new ArrayList<>(shards); |
| 133 | + this.searchType = searchRequest.searchType(); |
| 134 | + this.types = searchRequest.types(); |
| 135 | + this.requestCache = searchRequest.requestCache(); |
| 136 | + // If allowPartialSearchResults is unset (ie null), the cluster-level default should have been substituted |
| 137 | + // at this stage. Any NPEs in the above are therefore an error in request preparation logic. |
| 138 | + assert searchRequest.allowPartialSearchResults() != null; |
| 139 | + this.allowPartialSearchResults = searchRequest.allowPartialSearchResults(); |
| 140 | + this.scroll = searchRequest.scroll(); |
| 141 | + this.numberOfShards = numberOfShards; |
| 142 | + this.nowInMillis = nowInMillis; |
| 143 | + this.clusterAlias = clusterAlias; |
| 144 | + this.waitForCheckpointsTimeout = searchRequest.getWaitForCheckpointsTimeout(); |
| 145 | + indices = shards.stream().map(Shard::getOriginalIndices).flatMap(Arrays::stream).distinct() |
| 146 | + .toArray(String[]::new); |
| 147 | + } |
| 148 | + |
| 149 | + public CanMatchNodeRequest(StreamInput in) throws IOException { |
| 150 | + super(in); |
| 151 | + source = in.readOptionalWriteable(SearchSourceBuilder::new); |
| 152 | + indicesOptions = IndicesOptions.readIndicesOptions(in); |
| 153 | + searchType = SearchType.fromId(in.readByte()); |
| 154 | + types = in.readStringArray(); |
| 155 | + scroll = in.readOptionalWriteable(Scroll::new); |
| 156 | + requestCache = in.readOptionalBoolean(); |
| 157 | + allowPartialSearchResults = in.readBoolean(); |
| 158 | + numberOfShards = in.readVInt(); |
| 159 | + nowInMillis = in.readVLong(); |
| 160 | + clusterAlias = in.readOptionalString(); |
| 161 | + waitForCheckpointsTimeout = in.readTimeValue(); |
| 162 | + shards = in.readList(Shard::new); |
| 163 | + indices = shards.stream().map(Shard::getOriginalIndices).flatMap(Arrays::stream).distinct() |
| 164 | + .toArray(String[]::new); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void writeTo(StreamOutput out) throws IOException { |
| 169 | + super.writeTo(out); |
| 170 | + out.writeOptionalWriteable(source); |
| 171 | + indicesOptions.writeIndicesOptions(out); |
| 172 | + out.writeByte(searchType.id()); |
| 173 | + out.writeStringArray(types); |
| 174 | + out.writeOptionalWriteable(scroll); |
| 175 | + out.writeOptionalBoolean(requestCache); |
| 176 | + out.writeBoolean(allowPartialSearchResults); |
| 177 | + out.writeVInt(numberOfShards); |
| 178 | + out.writeVLong(nowInMillis); |
| 179 | + out.writeOptionalString(clusterAlias); |
| 180 | + out.writeTimeValue(waitForCheckpointsTimeout); |
| 181 | + out.writeList(shards); |
| 182 | + } |
| 183 | + |
| 184 | + public List<Shard> getShardLevelRequests() { |
| 185 | + return shards; |
| 186 | + } |
| 187 | + |
| 188 | + public List<ShardSearchRequest> createShardSearchRequests() { |
| 189 | + return shards.stream().map(this::createShardSearchRequest).collect(Collectors.toList()); |
| 190 | + } |
| 191 | + |
| 192 | + public ShardSearchRequest createShardSearchRequest(Shard r) { |
| 193 | + ShardSearchRequest shardSearchRequest = new ShardSearchRequest( |
| 194 | + new OriginalIndices(r.indices, indicesOptions), r.shardId, r.shardRequestIndex, numberOfShards, searchType, |
| 195 | + source, types, requestCache, r.aliasFilter, r.indexBoost, allowPartialSearchResults, scroll, |
| 196 | + nowInMillis, clusterAlias, r.readerId, r.keepAlive, r.waitForCheckpoint, waitForCheckpointsTimeout |
| 197 | + ); |
| 198 | + shardSearchRequest.setParentTask(getParentTask()); |
| 199 | + return shardSearchRequest; |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public String[] indices() { |
| 204 | + return indices; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public IndicesOptions indicesOptions() { |
| 209 | + return indicesOptions; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { |
| 214 | + return new SearchShardTask(id, type, action, getDescription(), parentTaskId, headers); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public String getDescription() { |
| 219 | + // Shard id is enough here, the request itself can be found by looking at the parent task description |
| 220 | + return "shardIds[" + shards.stream().map(slr -> slr.shardId).collect(Collectors.toList()) + "]"; |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments