use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class TransportSearchAction method executeRequest.
private void executeRequest(Task task, SearchRequest searchRequest, SearchAsyncActionProvider searchAsyncActionProvider, ActionListener<SearchResponse> listener) {
final long relativeStartNanos = System.nanoTime();
final SearchTimeProvider timeProvider = new SearchTimeProvider(searchRequest.getOrCreateAbsoluteStartMillis(), relativeStartNanos, System::nanoTime);
ActionListener<SearchSourceBuilder> rewriteListener = ActionListener.wrap(source -> {
if (source != searchRequest.source()) {
// only set it if it changed - we don't allow null values to be set but it might be already null. this way we catch
// situations when source is rewritten to null due to a bug
searchRequest.source(source);
}
final ClusterState clusterState = clusterService.state();
final SearchContextId searchContext;
final Map<String, OriginalIndices> remoteClusterIndices;
if (searchRequest.pointInTimeBuilder() != null) {
searchContext = SearchContextId.decode(namedWriteableRegistry, searchRequest.pointInTimeBuilder().getId());
remoteClusterIndices = getIndicesFromSearchContexts(searchContext, searchRequest.indicesOptions());
} else {
searchContext = null;
remoteClusterIndices = remoteClusterService.groupIndices(searchRequest.indicesOptions(), searchRequest.indices(), idx -> indexNameExpressionResolver.hasIndexAbstraction(idx, clusterState));
}
OriginalIndices localIndices = remoteClusterIndices.remove(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY);
if (remoteClusterIndices.isEmpty()) {
executeLocalSearch(task, timeProvider, searchRequest, localIndices, clusterState, listener, searchContext, searchAsyncActionProvider);
} else {
if (shouldMinimizeRoundtrips(searchRequest)) {
ccsRemoteReduce(searchRequest, localIndices, remoteClusterIndices, timeProvider, searchService.aggReduceContextBuilder(searchRequest), remoteClusterService, threadPool, listener, (r, l) -> executeLocalSearch(task, timeProvider, r, localIndices, clusterState, l, searchContext, searchAsyncActionProvider));
} else {
AtomicInteger skippedClusters = new AtomicInteger(0);
collectSearchShards(searchRequest.indicesOptions(), searchRequest.preference(), searchRequest.routing(), skippedClusters, remoteClusterIndices, remoteClusterService, threadPool, ActionListener.wrap(searchShardsResponses -> {
final BiFunction<String, String, DiscoveryNode> clusterNodeLookup = getRemoteClusterNodeLookup(searchShardsResponses);
final Map<String, AliasFilter> remoteAliasFilters;
final List<SearchShardIterator> remoteShardIterators;
if (searchContext != null) {
remoteAliasFilters = searchContext.aliasFilter();
remoteShardIterators = getRemoteShardsIteratorFromPointInTime(searchShardsResponses, searchContext, searchRequest.pointInTimeBuilder().getKeepAlive(), remoteClusterIndices);
} else {
remoteAliasFilters = getRemoteAliasFilters(searchShardsResponses);
remoteShardIterators = getRemoteShardsIterator(searchShardsResponses, remoteClusterIndices, remoteAliasFilters);
}
int localClusters = localIndices == null ? 0 : 1;
int totalClusters = remoteClusterIndices.size() + localClusters;
int successfulClusters = searchShardsResponses.size() + localClusters;
executeSearch((SearchTask) task, timeProvider, searchRequest, localIndices, remoteShardIterators, clusterNodeLookup, clusterState, remoteAliasFilters, listener, new SearchResponse.Clusters(totalClusters, successfulClusters, skippedClusters.get()), searchContext, searchAsyncActionProvider);
}, listener::onFailure));
}
}
}, listener::onFailure);
if (searchRequest.source() == null) {
rewriteListener.onResponse(searchRequest.source());
} else {
Rewriteable.rewriteAndFetch(searchRequest.source(), searchService.getRewriteContext(timeProvider::getAbsoluteStartMillis), rewriteListener);
}
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class RestReindexActionTests method testPipelineQueryParameterIsError.
public void testPipelineQueryParameterIsError() throws IOException {
FakeRestRequest.Builder request = new FakeRestRequest.Builder(xContentRegistry());
try (XContentBuilder body = JsonXContent.contentBuilder().prettyPrint()) {
body.startObject();
{
body.startObject("source");
{
body.field("index", "source");
}
body.endObject();
body.startObject("dest");
{
body.field("index", "dest");
}
body.endObject();
}
body.endObject();
request.withContent(BytesReference.bytes(body), body.contentType());
}
request.withParams(singletonMap("pipeline", "doesn't matter"));
Exception e = expectThrows(IllegalArgumentException.class, () -> action.buildRequest(request.build(), new NamedWriteableRegistry(Collections.emptyList())));
assertEquals("_reindex doesn't support [pipeline] as a query parameter. Specify it in the [dest] object instead.", e.getMessage());
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class QueryBuilderBWCIT method testQueryBuilderBWC.
public void testQueryBuilderBWC() throws Exception {
final String type = getOldClusterVersion().before(LegacyESVersion.V_7_0_0) ? "doc" : "_doc";
String index = "queries";
if (isRunningAgainstOldCluster()) {
XContentBuilder mappingsAndSettings = jsonBuilder();
mappingsAndSettings.startObject();
{
mappingsAndSettings.startObject("settings");
mappingsAndSettings.field("number_of_shards", 1);
mappingsAndSettings.field("number_of_replicas", 0);
mappingsAndSettings.endObject();
}
{
mappingsAndSettings.startObject("mappings");
if (isRunningAgainstAncientCluster()) {
mappingsAndSettings.startObject(type);
}
mappingsAndSettings.startObject("properties");
{
mappingsAndSettings.startObject("query");
mappingsAndSettings.field("type", "percolator");
mappingsAndSettings.endObject();
}
{
mappingsAndSettings.startObject("keyword_field");
mappingsAndSettings.field("type", "keyword");
mappingsAndSettings.endObject();
}
{
mappingsAndSettings.startObject("long_field");
mappingsAndSettings.field("type", "long");
mappingsAndSettings.endObject();
}
mappingsAndSettings.endObject();
mappingsAndSettings.endObject();
if (isRunningAgainstAncientCluster()) {
mappingsAndSettings.endObject();
}
}
mappingsAndSettings.endObject();
Request request = new Request("PUT", "/" + index);
request.setOptions(allowTypesRemovalWarnings());
request.setJsonEntity(Strings.toString(mappingsAndSettings));
Response rsp = client().performRequest(request);
assertEquals(200, rsp.getStatusLine().getStatusCode());
for (int i = 0; i < CANDIDATES.size(); i++) {
request = new Request("PUT", "/" + index + "/" + type + "/" + Integer.toString(i));
request.setJsonEntity((String) CANDIDATES.get(i)[0]);
rsp = client().performRequest(request);
assertEquals(201, rsp.getStatusLine().getStatusCode());
}
} else {
NamedWriteableRegistry registry = new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables());
for (int i = 0; i < CANDIDATES.size(); i++) {
QueryBuilder expectedQueryBuilder = (QueryBuilder) CANDIDATES.get(i)[1];
Request request = new Request("GET", "/" + index + "/_search");
request.setJsonEntity("{\"query\": {\"ids\": {\"values\": [\"" + Integer.toString(i) + "\"]}}, " + "\"docvalue_fields\": [{\"field\":\"query.query_builder_field\"}]}");
Response rsp = client().performRequest(request);
assertEquals(200, rsp.getStatusLine().getStatusCode());
Map<?, ?> hitRsp = (Map<?, ?>) ((List<?>) ((Map<?, ?>) toMap(rsp).get("hits")).get("hits")).get(0);
String queryBuilderStr = (String) ((List<?>) ((Map<?, ?>) hitRsp.get("fields")).get("query.query_builder_field")).get(0);
byte[] qbSource = Base64.getDecoder().decode(queryBuilderStr);
try (InputStream in = new ByteArrayInputStream(qbSource, 0, qbSource.length)) {
try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), registry)) {
input.setVersion(getOldClusterVersion());
QueryBuilder queryBuilder = input.readNamedWriteable(QueryBuilder.class);
assert in.read() == -1;
assertEquals(expectedQueryBuilder, queryBuilder);
}
}
}
}
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class TransportResyncReplicationActionTests method testResyncDoesNotBlockOnPrimaryAction.
public void testResyncDoesNotBlockOnPrimaryAction() throws Exception {
try (ClusterService clusterService = createClusterService(threadPool)) {
final String indexName = randomAlphaOfLength(5);
setState(clusterService, state(indexName, true, ShardRoutingState.STARTED));
setState(clusterService, ClusterState.builder(clusterService.state()).blocks(ClusterBlocks.builder().addGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ALL).addIndexBlock(indexName, IndexMetadata.INDEX_WRITE_BLOCK)));
try (MockNioTransport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(emptyList()), new NoneCircuitBreakerService())) {
final MockTransportService transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
transportService.start();
transportService.acceptIncomingRequests();
final ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool);
final IndexMetadata indexMetadata = clusterService.state().metadata().index(indexName);
final Index index = indexMetadata.getIndex();
final ShardId shardId = new ShardId(index, 0);
final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().shardRoutingTable(shardId);
final ShardRouting primaryShardRouting = clusterService.state().routingTable().shardRoutingTable(shardId).primaryShard();
final String allocationId = primaryShardRouting.allocationId().getId();
final long primaryTerm = indexMetadata.primaryTerm(shardId.id());
final AtomicInteger acquiredPermits = new AtomicInteger();
final IndexShard indexShard = mock(IndexShard.class);
when(indexShard.indexSettings()).thenReturn(new IndexSettings(indexMetadata, Settings.EMPTY));
when(indexShard.shardId()).thenReturn(shardId);
when(indexShard.routingEntry()).thenReturn(primaryShardRouting);
when(indexShard.getPendingPrimaryTerm()).thenReturn(primaryTerm);
when(indexShard.getOperationPrimaryTerm()).thenReturn(primaryTerm);
when(indexShard.getActiveOperationsCount()).then(i -> acquiredPermits.get());
doAnswer(invocation -> {
ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
acquiredPermits.incrementAndGet();
callback.onResponse(acquiredPermits::decrementAndGet);
return null;
}).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any(), eq(true));
when(indexShard.getReplicationGroup()).thenReturn(new ReplicationGroup(shardRoutingTable, clusterService.state().metadata().index(index).inSyncAllocationIds(shardId.id()), shardRoutingTable.getAllAllocationIds(), 0));
final IndexService indexService = mock(IndexService.class);
when(indexService.getShard(eq(shardId.id()))).thenReturn(indexShard);
final IndicesService indexServices = mock(IndicesService.class);
when(indexServices.indexServiceSafe(eq(index))).thenReturn(indexService);
final TransportResyncReplicationAction action = new TransportResyncReplicationAction(Settings.EMPTY, transportService, clusterService, indexServices, threadPool, shardStateAction, new ActionFilters(new HashSet<>()), new IndexingPressureService(Settings.EMPTY, clusterService), new SystemIndices(emptyMap()));
assertThat(action.globalBlockLevel(), nullValue());
assertThat(action.indexBlockLevel(), nullValue());
final Task task = mock(Task.class);
when(task.getId()).thenReturn(randomNonNegativeLong());
final byte[] bytes = "{}".getBytes(Charset.forName("UTF-8"));
final ResyncReplicationRequest request = new ResyncReplicationRequest(shardId, 42L, 100, new Translog.Operation[] { new Translog.Index("type", "id", 0, primaryTerm, 0L, bytes, null, -1) });
final PlainActionFuture<ResyncReplicationResponse> listener = new PlainActionFuture<>();
action.sync(request, task, allocationId, primaryTerm, listener);
assertThat(listener.get().getShardInfo().getFailed(), equalTo(0));
assertThat(listener.isDone(), is(true));
}
}
}
use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.
the class ExplainRequestTests method setUp.
public void setUp() throws Exception {
super.setUp();
IndicesModule indicesModule = new IndicesModule(Collections.emptyList());
SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
entries.addAll(indicesModule.getNamedWriteables());
entries.addAll(searchModule.getNamedWriteables());
namedWriteableRegistry = new NamedWriteableRegistry(entries);
}
Aggregations