|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | +package org.opensearch.action.admin.indices.segments; |
| 9 | + |
| 10 | +import org.opensearch.action.ActionListener; |
| 11 | +import org.opensearch.action.search.ListPitInfo; |
| 12 | +import org.opensearch.action.search.PitService; |
| 13 | +import org.opensearch.action.search.SearchContextId; |
| 14 | +import org.opensearch.action.search.SearchContextIdForNode; |
| 15 | +import org.opensearch.action.support.ActionFilters; |
| 16 | +import org.opensearch.action.support.DefaultShardOperationFailedException; |
| 17 | +import org.opensearch.action.support.broadcast.node.TransportBroadcastByNodeAction; |
| 18 | +import org.opensearch.cluster.ClusterState; |
| 19 | +import org.opensearch.cluster.block.ClusterBlockException; |
| 20 | +import org.opensearch.cluster.block.ClusterBlockLevel; |
| 21 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 22 | +import org.opensearch.cluster.routing.AllocationId; |
| 23 | +import org.opensearch.cluster.routing.PlainShardsIterator; |
| 24 | +import org.opensearch.cluster.routing.RecoverySource; |
| 25 | +import org.opensearch.cluster.routing.ShardRouting; |
| 26 | +import org.opensearch.cluster.routing.ShardRoutingState; |
| 27 | +import org.opensearch.cluster.routing.ShardsIterator; |
| 28 | +import org.opensearch.cluster.routing.UnassignedInfo; |
| 29 | +import org.opensearch.cluster.service.ClusterService; |
| 30 | +import org.opensearch.common.Strings; |
| 31 | +import org.opensearch.common.inject.Inject; |
| 32 | +import org.opensearch.common.io.stream.NamedWriteableRegistry; |
| 33 | +import org.opensearch.common.io.stream.StreamInput; |
| 34 | +import org.opensearch.common.io.stream.StreamOutput; |
| 35 | +import org.opensearch.common.xcontent.XContentBuilder; |
| 36 | +import org.opensearch.index.shard.ShardId; |
| 37 | +import org.opensearch.indices.IndicesService; |
| 38 | +import org.opensearch.search.SearchService; |
| 39 | +import org.opensearch.search.internal.PitReaderContext; |
| 40 | +import org.opensearch.tasks.Task; |
| 41 | +import org.opensearch.threadpool.ThreadPool; |
| 42 | +import org.opensearch.transport.TransportService; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Map; |
| 49 | +import java.util.stream.Collectors; |
| 50 | + |
| 51 | +import static org.opensearch.action.search.SearchContextId.decode; |
| 52 | + |
| 53 | +/** |
| 54 | + * Transport action for retrieving segment information of PITs |
| 55 | + */ |
| 56 | +public class TransportPitSegmentsAction extends TransportBroadcastByNodeAction<PitSegmentsRequest, IndicesSegmentResponse, ShardSegments> { |
| 57 | + private final ClusterService clusterService; |
| 58 | + private final IndicesService indicesService; |
| 59 | + private final SearchService searchService; |
| 60 | + private final NamedWriteableRegistry namedWriteableRegistry; |
| 61 | + private final TransportService transportService; |
| 62 | + private final PitService pitService; |
| 63 | + |
| 64 | + @Inject |
| 65 | + public TransportPitSegmentsAction( |
| 66 | + ClusterService clusterService, |
| 67 | + TransportService transportService, |
| 68 | + IndicesService indicesService, |
| 69 | + ActionFilters actionFilters, |
| 70 | + IndexNameExpressionResolver indexNameExpressionResolver, |
| 71 | + SearchService searchService, |
| 72 | + NamedWriteableRegistry namedWriteableRegistry, |
| 73 | + PitService pitService |
| 74 | + ) { |
| 75 | + super( |
| 76 | + PitSegmentsAction.NAME, |
| 77 | + clusterService, |
| 78 | + transportService, |
| 79 | + actionFilters, |
| 80 | + indexNameExpressionResolver, |
| 81 | + PitSegmentsRequest::new, |
| 82 | + ThreadPool.Names.MANAGEMENT |
| 83 | + ); |
| 84 | + this.clusterService = clusterService; |
| 85 | + this.indicesService = indicesService; |
| 86 | + this.searchService = searchService; |
| 87 | + this.namedWriteableRegistry = namedWriteableRegistry; |
| 88 | + this.transportService = transportService; |
| 89 | + this.pitService = pitService; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Execute PIT segments flow for all PITs or request PIT IDs |
| 94 | + */ |
| 95 | + @Override |
| 96 | + protected void doExecute(Task task, PitSegmentsRequest request, ActionListener<IndicesSegmentResponse> listener) { |
| 97 | + List<String> pitIds = request.getPitIds(); |
| 98 | + if (pitIds.size() == 1 && "_all".equals(pitIds.get(0))) { |
| 99 | + pitService.getAllPits(ActionListener.wrap(response -> { |
| 100 | + request.clearAndSetPitIds(response.getPitInfos().stream().map(ListPitInfo::getPitId).collect(Collectors.toList())); |
| 101 | + super.doExecute(task, request, listener); |
| 102 | + }, listener::onFailure)); |
| 103 | + } else { |
| 104 | + super.doExecute(task, request, listener); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * This adds list of shards on which we need to retrieve pit segments details |
| 110 | + * @param clusterState the cluster state |
| 111 | + * @param request the underlying request |
| 112 | + * @param concreteIndices the concrete indices on which to execute the operation |
| 113 | + */ |
| 114 | + @Override |
| 115 | + protected ShardsIterator shards(ClusterState clusterState, PitSegmentsRequest request, String[] concreteIndices) { |
| 116 | + final ArrayList<ShardRouting> iterators = new ArrayList<>(); |
| 117 | + for (String pitId : request.getPitIds()) { |
| 118 | + SearchContextId searchContext = decode(namedWriteableRegistry, pitId); |
| 119 | + for (Map.Entry<ShardId, SearchContextIdForNode> entry : searchContext.shards().entrySet()) { |
| 120 | + final SearchContextIdForNode perNode = entry.getValue(); |
| 121 | + // check if node is part of local cluster |
| 122 | + if (Strings.isEmpty(perNode.getClusterAlias())) { |
| 123 | + final ShardId shardId = entry.getKey(); |
| 124 | + iterators.add( |
| 125 | + new PitAwareShardRouting( |
| 126 | + pitId, |
| 127 | + shardId, |
| 128 | + perNode.getNode(), |
| 129 | + null, |
| 130 | + true, |
| 131 | + ShardRoutingState.STARTED, |
| 132 | + null, |
| 133 | + null, |
| 134 | + null, |
| 135 | + -1L |
| 136 | + ) |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return new PlainShardsIterator(iterators); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected ClusterBlockException checkGlobalBlock(ClusterState state, PitSegmentsRequest request) { |
| 146 | + return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected ClusterBlockException checkRequestBlock(ClusterState state, PitSegmentsRequest countRequest, String[] concreteIndices) { |
| 151 | + return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, concreteIndices); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected ShardSegments readShardResult(StreamInput in) throws IOException { |
| 156 | + return new ShardSegments(in); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected IndicesSegmentResponse newResponse( |
| 161 | + PitSegmentsRequest request, |
| 162 | + int totalShards, |
| 163 | + int successfulShards, |
| 164 | + int failedShards, |
| 165 | + List<ShardSegments> results, |
| 166 | + List<DefaultShardOperationFailedException> shardFailures, |
| 167 | + ClusterState clusterState |
| 168 | + ) { |
| 169 | + return new IndicesSegmentResponse( |
| 170 | + results.toArray(new ShardSegments[results.size()]), |
| 171 | + totalShards, |
| 172 | + successfulShards, |
| 173 | + failedShards, |
| 174 | + shardFailures |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + protected PitSegmentsRequest readRequestFrom(StreamInput in) throws IOException { |
| 180 | + return new PitSegmentsRequest(in); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public List<ShardRouting> getShardRoutingsFromInputStream(StreamInput in) throws IOException { |
| 185 | + return in.readList(PitAwareShardRouting::new); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * This retrieves segment details of PIT context |
| 190 | + * @param request the node-level request |
| 191 | + * @param shardRouting the shard on which to execute the operation |
| 192 | + */ |
| 193 | + @Override |
| 194 | + protected ShardSegments shardOperation(PitSegmentsRequest request, ShardRouting shardRouting) { |
| 195 | + assert shardRouting instanceof PitAwareShardRouting; |
| 196 | + PitAwareShardRouting pitAwareShardRouting = (PitAwareShardRouting) shardRouting; |
| 197 | + SearchContextIdForNode searchContextIdForNode = decode(namedWriteableRegistry, pitAwareShardRouting.getPitId()).shards() |
| 198 | + .get(shardRouting.shardId()); |
| 199 | + PitReaderContext pitReaderContext = searchService.getPitReaderContext(searchContextIdForNode.getSearchContextId()); |
| 200 | + if (pitReaderContext == null) { |
| 201 | + return new ShardSegments(shardRouting, Collections.emptyList()); |
| 202 | + } |
| 203 | + return new ShardSegments(pitReaderContext.getShardRouting(), pitReaderContext.getSegments()); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * This holds PIT id which is used to perform broadcast operation in PIT shards to retrieve segments information |
| 208 | + */ |
| 209 | + public class PitAwareShardRouting extends ShardRouting { |
| 210 | + |
| 211 | + private final String pitId; |
| 212 | + |
| 213 | + public PitAwareShardRouting(StreamInput in) throws IOException { |
| 214 | + super(in); |
| 215 | + this.pitId = in.readString(); |
| 216 | + } |
| 217 | + |
| 218 | + public PitAwareShardRouting( |
| 219 | + String pitId, |
| 220 | + ShardId shardId, |
| 221 | + String currentNodeId, |
| 222 | + String relocatingNodeId, |
| 223 | + boolean primary, |
| 224 | + ShardRoutingState state, |
| 225 | + RecoverySource recoverySource, |
| 226 | + UnassignedInfo unassignedInfo, |
| 227 | + AllocationId allocationId, |
| 228 | + long expectedShardSize |
| 229 | + ) { |
| 230 | + super( |
| 231 | + shardId, |
| 232 | + currentNodeId, |
| 233 | + relocatingNodeId, |
| 234 | + primary, |
| 235 | + state, |
| 236 | + recoverySource, |
| 237 | + unassignedInfo, |
| 238 | + allocationId, |
| 239 | + expectedShardSize |
| 240 | + ); |
| 241 | + this.pitId = pitId; |
| 242 | + } |
| 243 | + |
| 244 | + public String getPitId() { |
| 245 | + return pitId; |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public void writeTo(StreamOutput out) throws IOException { |
| 250 | + super.writeTo(out); |
| 251 | + out.writeString(pitId); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 256 | + super.toXContent(builder, params); |
| 257 | + builder.field("pit_id", pitId); |
| 258 | + return builder.endObject(); |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments