Skip to content

Commit c7503fc

Browse files
authored
Explicit index resolution API (#18523)
* Introduced explicit index resolution API Signed-off-by: Nils Bandener <[email protected]> * Fix Signed-off-by: Nils Bandener <[email protected]> * Fix Signed-off-by: Nils Bandener <[email protected]> * Introduced OptionalResolvedIndices class Signed-off-by: Nils Bandener <[email protected]> * Fix Signed-off-by: Nils Bandener <[email protected]> * Fixes Signed-off-by: Nils Bandener <[email protected]> * Fixes Signed-off-by: Nils Bandener <[email protected]> * Fixes Signed-off-by: Nils Bandener <[email protected]> * Tests Signed-off-by: Nils Bandener <[email protected]> * Test fix Signed-off-by: Nils Bandener <[email protected]> * Additional tests Signed-off-by: Nils Bandener <[email protected]> * API simplifications Signed-off-by: Nils Bandener <[email protected]> * Test fix Signed-off-by: Nils Bandener <[email protected]> * Added resolveIndices() for composite index actions Signed-off-by: Nils Bandener <[email protected]> * Improvements Signed-off-by: Nils Bandener <[email protected]> * Fix Signed-off-by: Nils Bandener <[email protected]> * Additional actions Signed-off-by: Nils Bandener <[email protected]> * Fix Signed-off-by: Nils Bandener <[email protected]> * Do not cache the ResolvedIndices object Signed-off-by: Nils Bandener <[email protected]> * Do not cache the ResolvedIndices object Signed-off-by: Nils Bandener <[email protected]> * Added changelog Signed-off-by: Nils Bandener <[email protected]> * Moved to non-breaking transition of ActionFilter interface Signed-off-by: Nils Bandener <[email protected]> * Fixed JavaDoc Signed-off-by: Nils Bandener <[email protected]> * Fixes acc to PR comments Signed-off-by: Nils Bandener <[email protected]> --------- Signed-off-by: Nils Bandener <[email protected]>
1 parent 29b113a commit c7503fc

File tree

98 files changed

+3768
-332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+3768
-332
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
- Add pluggable gRPC interceptors with explicit ordering([#19005](https://github.com/opensearch-project/OpenSearch/pull/19005))
1515
- Add metrics for the merged segment warmer feature ([#18929](https://github.com/opensearch-project/OpenSearch/pull/18929))
1616
- Add pointer based lag metric in pull-based ingestion ([#19635](https://github.com/opensearch-project/OpenSearch/pull/19635))
17+
- Introduced internal API for retrieving metadata about requested indices from transport actions ([#18523](https://github.com/opensearch-project/OpenSearch/pull/18523))
1718

1819
### Changed
1920
- Faster `terms` query creation for `keyword` field with index and docValues enabled ([#19350](https://github.com/opensearch-project/OpenSearch/pull/19350))

modules/lang-mustache/src/main/java/org/opensearch/script/mustache/TransportRenderSearchTemplateAction.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.opensearch.script.mustache;
1010

11+
import org.opensearch.action.search.TransportSearchAction;
1112
import org.opensearch.action.support.ActionFilters;
1213
import org.opensearch.common.inject.Inject;
1314
import org.opensearch.core.xcontent.NamedXContentRegistry;
@@ -23,8 +24,17 @@ public TransportRenderSearchTemplateAction(
2324
ActionFilters actionFilters,
2425
ScriptService scriptService,
2526
NamedXContentRegistry xContentRegistry,
26-
NodeClient client
27+
NodeClient client,
28+
TransportSearchAction transportSearchAction
2729
) {
28-
super(RenderSearchTemplateAction.NAME, transportService, actionFilters, scriptService, xContentRegistry, client);
30+
super(
31+
RenderSearchTemplateAction.NAME,
32+
transportService,
33+
actionFilters,
34+
scriptService,
35+
xContentRegistry,
36+
client,
37+
transportSearchAction
38+
);
2939
}
3040
}

modules/lang-mustache/src/main/java/org/opensearch/script/mustache/TransportSearchTemplateAction.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434

3535
import org.opensearch.action.search.SearchRequest;
3636
import org.opensearch.action.search.SearchResponse;
37+
import org.opensearch.action.search.TransportSearchAction;
3738
import org.opensearch.action.support.ActionFilters;
3839
import org.opensearch.action.support.HandledTransportAction;
40+
import org.opensearch.action.support.TransportIndicesResolvingAction;
41+
import org.opensearch.cluster.metadata.OptionallyResolvedIndices;
42+
import org.opensearch.cluster.metadata.ResolvedIndices;
3943
import org.opensearch.common.inject.Inject;
4044
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
4145
import org.opensearch.core.action.ActionListener;
@@ -57,26 +61,30 @@
5761
import java.io.IOException;
5862
import java.util.Collections;
5963

60-
public class TransportSearchTemplateAction extends HandledTransportAction<SearchTemplateRequest, SearchTemplateResponse> {
61-
64+
public class TransportSearchTemplateAction extends HandledTransportAction<SearchTemplateRequest, SearchTemplateResponse>
65+
implements
66+
TransportIndicesResolvingAction<SearchTemplateRequest> {
6267
private static final String TEMPLATE_LANG = MustacheScriptEngine.NAME;
6368

6469
protected final ScriptService scriptService;
6570
protected final NamedXContentRegistry xContentRegistry;
6671
protected final NodeClient client;
72+
private final TransportSearchAction transportSearchAction;
6773

6874
@Inject
6975
public TransportSearchTemplateAction(
7076
TransportService transportService,
7177
ActionFilters actionFilters,
7278
ScriptService scriptService,
7379
NamedXContentRegistry xContentRegistry,
74-
NodeClient client
80+
NodeClient client,
81+
TransportSearchAction transportSearchAction
7582
) {
7683
super(SearchTemplateAction.NAME, transportService, actionFilters, SearchTemplateRequest::new);
7784
this.scriptService = scriptService;
7885
this.xContentRegistry = xContentRegistry;
7986
this.client = client;
87+
this.transportSearchAction = transportSearchAction;
8088
}
8189

8290
public TransportSearchTemplateAction(
@@ -85,12 +93,14 @@ public TransportSearchTemplateAction(
8593
ActionFilters actionFilters,
8694
ScriptService scriptService,
8795
NamedXContentRegistry xContentRegistry,
88-
NodeClient client
96+
NodeClient client,
97+
TransportSearchAction transportSearchAction
8998
) {
9099
super(actionName, transportService, actionFilters, SearchTemplateRequest::new);
91100
this.scriptService = scriptService;
92101
this.xContentRegistry = xContentRegistry;
93102
this.client = client;
103+
this.transportSearchAction = transportSearchAction;
94104
}
95105

96106
@Override
@@ -180,4 +190,14 @@ private static void checkRestTotalHitsAsInt(SearchRequest searchRequest, SearchS
180190
}
181191
}
182192
}
193+
194+
@Override
195+
public OptionallyResolvedIndices resolveIndices(SearchTemplateRequest request) {
196+
if (request.getRequest() != null) {
197+
return transportSearchAction.resolveIndices(request.getRequest());
198+
} else {
199+
// For the RenderSearchTemplateAction, request.getRequest() will be null.
200+
return ResolvedIndices.unknown();
201+
}
202+
}
183203
}

modules/reindex/src/main/java/org/opensearch/index/reindex/TransportDeleteByQueryAction.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232

3333
package org.opensearch.index.reindex;
3434

35+
import org.opensearch.action.search.TransportSearchAction;
3536
import org.opensearch.action.support.ActionFilters;
3637
import org.opensearch.action.support.HandledTransportAction;
38+
import org.opensearch.action.support.TransportIndicesResolvingAction;
39+
import org.opensearch.cluster.metadata.ResolvedIndices;
3740
import org.opensearch.cluster.service.ClusterService;
3841
import org.opensearch.common.inject.Inject;
3942
import org.opensearch.core.action.ActionListener;
@@ -45,12 +48,15 @@
4548
import org.opensearch.transport.client.Client;
4649
import org.opensearch.transport.client.ParentTaskAssigningClient;
4750

48-
public class TransportDeleteByQueryAction extends HandledTransportAction<DeleteByQueryRequest, BulkByScrollResponse> {
51+
public class TransportDeleteByQueryAction extends HandledTransportAction<DeleteByQueryRequest, BulkByScrollResponse>
52+
implements
53+
TransportIndicesResolvingAction<DeleteByQueryRequest> {
4954

5055
private final ThreadPool threadPool;
5156
private final Client client;
5257
private final ScriptService scriptService;
5358
private final ClusterService clusterService;
59+
private final TransportSearchAction transportSearchAction;
5460

5561
@Inject
5662
public TransportDeleteByQueryAction(
@@ -59,7 +65,8 @@ public TransportDeleteByQueryAction(
5965
Client client,
6066
TransportService transportService,
6167
ScriptService scriptService,
62-
ClusterService clusterService
68+
ClusterService clusterService,
69+
TransportSearchAction transportSearchAction
6370
) {
6471
super(
6572
DeleteByQueryAction.NAME,
@@ -71,6 +78,7 @@ public TransportDeleteByQueryAction(
7178
this.client = client;
7279
this.scriptService = scriptService;
7380
this.clusterService = clusterService;
81+
this.transportSearchAction = transportSearchAction;
7482
}
7583

7684
@Override
@@ -95,4 +103,9 @@ public void doExecute(Task task, DeleteByQueryRequest request, ActionListener<Bu
95103
}
96104
);
97105
}
106+
107+
@Override
108+
public ResolvedIndices resolveIndices(DeleteByQueryRequest request) {
109+
return transportSearchAction.resolveIndices(request.getSearchRequest());
110+
}
98111
}

modules/reindex/src/main/java/org/opensearch/index/reindex/TransportReindexAction.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232

3333
package org.opensearch.index.reindex;
3434

35+
import org.opensearch.action.index.IndexAction;
36+
import org.opensearch.action.search.TransportSearchAction;
3537
import org.opensearch.action.support.ActionFilters;
3638
import org.opensearch.action.support.AutoCreateIndex;
3739
import org.opensearch.action.support.HandledTransportAction;
40+
import org.opensearch.action.support.TransportIndicesResolvingAction;
3841
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
42+
import org.opensearch.cluster.metadata.ResolvedIndices;
3943
import org.opensearch.cluster.service.ClusterService;
4044
import org.opensearch.common.inject.Inject;
4145
import org.opensearch.common.settings.Setting;
@@ -56,7 +60,9 @@
5660

5761
import static java.util.Collections.emptyList;
5862

59-
public class TransportReindexAction extends HandledTransportAction<ReindexRequest, BulkByScrollResponse> {
63+
public class TransportReindexAction extends HandledTransportAction<ReindexRequest, BulkByScrollResponse>
64+
implements
65+
TransportIndicesResolvingAction<ReindexRequest> {
6066
public static final Setting<List<String>> REMOTE_CLUSTER_ALLOWLIST = Setting.listSetting(
6167
"reindex.remote.allowlist",
6268
emptyList(),
@@ -88,6 +94,8 @@ public class TransportReindexAction extends HandledTransportAction<ReindexReques
8894
private final Reindexer reindexer;
8995

9096
private final ClusterService clusterService;
97+
private final TransportSearchAction transportSearchAction;
98+
private final IndexNameExpressionResolver indexNameExpressionResolver;
9199

92100
@Inject
93101
public TransportReindexAction(
@@ -100,12 +108,15 @@ public TransportReindexAction(
100108
AutoCreateIndex autoCreateIndex,
101109
Client client,
102110
TransportService transportService,
103-
ReindexSslConfig sslConfig
111+
ReindexSslConfig sslConfig,
112+
TransportSearchAction transportSearchAction
104113
) {
105114
super(ReindexAction.NAME, transportService, actionFilters, ReindexRequest::new);
106115
this.reindexValidator = new ReindexValidator(settings, clusterService, indexNameExpressionResolver, autoCreateIndex);
107116
this.reindexer = new Reindexer(clusterService, client, threadPool, scriptService, sslConfig, remoteExtension);
108117
this.clusterService = clusterService;
118+
this.transportSearchAction = transportSearchAction;
119+
this.indexNameExpressionResolver = indexNameExpressionResolver;
109120
}
110121

111122
@Override
@@ -129,4 +140,13 @@ public void onFailure(Exception e) {
129140
}
130141
});
131142
}
143+
144+
@Override
145+
public ResolvedIndices resolveIndices(ReindexRequest request) {
146+
return transportSearchAction.resolveIndices(request.getSearchRequest())
147+
.withLocalSubActions(
148+
IndexAction.INSTANCE,
149+
ResolvedIndices.Local.of(indexNameExpressionResolver.resolveDateMathExpression(request.getDestination().index()))
150+
);
151+
}
132152
}

modules/reindex/src/main/java/org/opensearch/index/reindex/TransportUpdateByQueryAction.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434

3535
import org.apache.logging.log4j.Logger;
3636
import org.opensearch.action.index.IndexRequest;
37+
import org.opensearch.action.search.TransportSearchAction;
3738
import org.opensearch.action.support.ActionFilters;
3839
import org.opensearch.action.support.HandledTransportAction;
40+
import org.opensearch.action.support.TransportIndicesResolvingAction;
3941
import org.opensearch.cluster.ClusterState;
42+
import org.opensearch.cluster.metadata.ResolvedIndices;
4043
import org.opensearch.cluster.service.ClusterService;
4144
import org.opensearch.common.inject.Inject;
4245
import org.opensearch.core.action.ActionListener;
@@ -55,12 +58,15 @@
5558
import java.util.Map;
5659
import java.util.function.BiFunction;
5760

58-
public class TransportUpdateByQueryAction extends HandledTransportAction<UpdateByQueryRequest, BulkByScrollResponse> {
61+
public class TransportUpdateByQueryAction extends HandledTransportAction<UpdateByQueryRequest, BulkByScrollResponse>
62+
implements
63+
TransportIndicesResolvingAction<UpdateByQueryRequest> {
5964

6065
private final ThreadPool threadPool;
6166
private final Client client;
6267
private final ScriptService scriptService;
6368
private final ClusterService clusterService;
69+
private final TransportSearchAction transportSearchAction;
6470

6571
@Inject
6672
public TransportUpdateByQueryAction(
@@ -69,7 +75,8 @@ public TransportUpdateByQueryAction(
6975
Client client,
7076
TransportService transportService,
7177
ScriptService scriptService,
72-
ClusterService clusterService
78+
ClusterService clusterService,
79+
TransportSearchAction transportSearchAction
7380
) {
7481
super(
7582
UpdateByQueryAction.NAME,
@@ -81,6 +88,7 @@ public TransportUpdateByQueryAction(
8188
this.client = client;
8289
this.scriptService = scriptService;
8390
this.clusterService = clusterService;
91+
this.transportSearchAction = transportSearchAction;
8492
}
8593

8694
@Override
@@ -107,6 +115,11 @@ protected void doExecute(Task task, UpdateByQueryRequest request, ActionListener
107115
);
108116
}
109117

118+
@Override
119+
public ResolvedIndices resolveIndices(UpdateByQueryRequest request) {
120+
return transportSearchAction.resolveIndices(request.getSearchRequest());
121+
}
122+
110123
/**
111124
* Simple implementation of update-by-query using scrolling and bulk.
112125
*/

modules/reindex/src/test/java/org/opensearch/index/reindex/ReindexFromRemoteWithAuthTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.opensearch.action.search.SearchAction;
4040
import org.opensearch.action.support.ActionFilter;
4141
import org.opensearch.action.support.ActionFilterChain;
42+
import org.opensearch.action.support.ActionRequestMetadata;
4243
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
4344
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
4445
import org.opensearch.cluster.service.ClusterService;
@@ -233,6 +234,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
233234
Task task,
234235
String action,
235236
Request request,
237+
ActionRequestMetadata<Request, Response> actionRequestMetadata,
236238
ActionListener<Response> listener,
237239
ActionFilterChain<Request, Response> chain
238240
) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
9+
package org.opensearch.index.reindex;
10+
11+
import org.opensearch.action.index.IndexAction;
12+
import org.opensearch.action.index.IndexRequest;
13+
import org.opensearch.action.search.SearchRequest;
14+
import org.opensearch.action.search.TransportSearchAction;
15+
import org.opensearch.action.support.ActionFilters;
16+
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
17+
import org.opensearch.cluster.metadata.ResolvedIndices;
18+
import org.opensearch.cluster.service.ClusterService;
19+
import org.opensearch.common.settings.Settings;
20+
import org.opensearch.common.util.concurrent.ThreadContext;
21+
import org.opensearch.script.ScriptService;
22+
import org.opensearch.test.OpenSearchTestCase;
23+
import org.opensearch.threadpool.ThreadPool;
24+
import org.opensearch.transport.TransportService;
25+
import org.opensearch.transport.client.Client;
26+
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
public class TransportReindexActionTests extends OpenSearchTestCase {
32+
public void testResolveIndices() {
33+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
34+
ThreadPool threadPool = mock(ThreadPool.class);
35+
when(threadPool.getThreadContext()).thenReturn(threadContext);
36+
TransportSearchAction transportSearchAction = mock(TransportSearchAction.class);
37+
when(transportSearchAction.resolveIndices(any())).thenAnswer(i -> ResolvedIndices.of(((SearchRequest) i.getArgument(0)).indices()));
38+
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(threadContext);
39+
40+
TransportReindexAction action = new TransportReindexAction(
41+
Settings.EMPTY,
42+
threadPool,
43+
mock(ActionFilters.class),
44+
indexNameExpressionResolver,
45+
mock(ClusterService.class),
46+
mock(ScriptService.class),
47+
null,
48+
mock(Client.class),
49+
mock(TransportService.class),
50+
mock(ReindexSslConfig.class),
51+
transportSearchAction
52+
);
53+
54+
{
55+
ResolvedIndices resolvedIndices = action.resolveIndices(
56+
new ReindexRequest(new SearchRequest("searched-index"), new IndexRequest("target-index"))
57+
);
58+
assertEquals(
59+
ResolvedIndices.of("searched-index").withLocalSubActions(IndexAction.INSTANCE, ResolvedIndices.Local.of("target-index")),
60+
resolvedIndices
61+
);
62+
}
63+
}
64+
}

modules/reindex/src/test/java/org/opensearch/index/reindex/UpdateByQueryWithScriptTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected TransportUpdateByQueryAction.AsyncIndexBySearchAction action(ScriptSer
7979
null,
8080
transportService,
8181
scriptService,
82+
null,
8283
null
8384
);
8485
return new TransportUpdateByQueryAction.AsyncIndexBySearchAction(

0 commit comments

Comments
 (0)