Skip to content

Commit 3822628

Browse files
committed
Try to maintain backward compatibility of APIs
Instead of extracting an interface from QueryRewriteContext, we can keep it as a base class and subclass it for QueryCoordinatorContext. Signed-off-by: Michael Froh <[email protected]>
1 parent 0688385 commit 3822628

File tree

11 files changed

+121
-233
lines changed

11 files changed

+121
-233
lines changed

server/src/main/java/org/opensearch/index/query/BaseQueryRewriteContext.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

server/src/main/java/org/opensearch/index/query/QueryCoordinatorContext.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88

99
package org.opensearch.index.query;
1010

11-
import org.opensearch.client.Client;
1211
import org.opensearch.common.annotation.PublicApi;
13-
import org.opensearch.core.action.ActionListener;
14-
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
15-
import org.opensearch.core.xcontent.NamedXContentRegistry;
1612
import org.opensearch.search.pipeline.PipelinedRequest;
1713

1814
import java.util.HashMap;
1915
import java.util.Map;
20-
import java.util.function.BiConsumer;
2116

2217
/**
2318
* The QueryCoordinatorContext class implements the QueryRewriteContext interface and provides
@@ -29,53 +24,12 @@
2924
* @since 2.19.0
3025
*/
3126
@PublicApi(since = "2.19.0")
32-
public class QueryCoordinatorContext implements QueryRewriteContext {
33-
private final QueryRewriteContext rewriteContext;
27+
public class QueryCoordinatorContext extends QueryRewriteContext {
3428
private final PipelinedRequest searchRequest;
3529

36-
public QueryCoordinatorContext(QueryRewriteContext rewriteContext, PipelinedRequest searchRequest) {
37-
this.rewriteContext = rewriteContext;
38-
this.searchRequest = searchRequest;
39-
}
40-
41-
@Override
42-
public NamedXContentRegistry getXContentRegistry() {
43-
return rewriteContext.getXContentRegistry();
44-
}
45-
46-
@Override
47-
public long nowInMillis() {
48-
return rewriteContext.nowInMillis();
49-
}
50-
51-
@Override
52-
public NamedWriteableRegistry getWriteableRegistry() {
53-
return rewriteContext.getWriteableRegistry();
54-
}
55-
56-
@Override
57-
public QueryShardContext convertToShardContext() {
58-
return rewriteContext.convertToShardContext();
59-
}
60-
61-
@Override
62-
public void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncAction) {
63-
rewriteContext.registerAsyncAction(asyncAction);
64-
}
65-
66-
@Override
67-
public boolean hasAsyncActions() {
68-
return rewriteContext.hasAsyncActions();
69-
}
70-
71-
@Override
72-
public void executeAsyncActions(ActionListener listener) {
73-
rewriteContext.executeAsyncActions(listener);
74-
}
75-
76-
@Override
77-
public boolean validate() {
78-
return rewriteContext.validate();
30+
public QueryCoordinatorContext(QueryRewriteContext parent, PipelinedRequest pipelinedRequest) {
31+
super(parent.getXContentRegistry(), parent.getWriteableRegistry(), parent.client, parent.nowInMillis, parent.validate());
32+
this.searchRequest = pipelinedRequest;
7933
}
8034

8135
@Override

server/src/main/java/org/opensearch/index/query/QueryRewriteContext.java

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,85 @@
3333

3434
import org.opensearch.client.Client;
3535
import org.opensearch.common.annotation.PublicApi;
36+
import org.opensearch.common.util.concurrent.CountDown;
3637
import org.opensearch.core.action.ActionListener;
3738
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
3839
import org.opensearch.core.xcontent.NamedXContentRegistry;
3940
import org.opensearch.core.xcontent.XContentParser;
4041

42+
import java.util.ArrayList;
43+
import java.util.List;
4144
import java.util.function.BiConsumer;
45+
import java.util.function.LongSupplier;
4246

4347
/**
4448
* Context object used to rewrite {@link QueryBuilder} instances into simplified version.
4549
*
4650
* @opensearch.api
4751
*/
4852
@PublicApi(since = "1.0.0")
49-
public interface QueryRewriteContext {
53+
public class QueryRewriteContext {
54+
private final NamedXContentRegistry xContentRegistry;
55+
private final NamedWriteableRegistry writeableRegistry;
56+
protected final Client client;
57+
protected final LongSupplier nowInMillis;
58+
private final List<BiConsumer<Client, ActionListener<?>>> asyncActions = new ArrayList<>();
59+
private final boolean validate;
60+
61+
public QueryRewriteContext(
62+
NamedXContentRegistry xContentRegistry,
63+
NamedWriteableRegistry writeableRegistry,
64+
Client client,
65+
LongSupplier nowInMillis
66+
) {
67+
this(xContentRegistry, writeableRegistry, client, nowInMillis, false);
68+
}
69+
70+
public QueryRewriteContext(
71+
NamedXContentRegistry xContentRegistry,
72+
NamedWriteableRegistry writeableRegistry,
73+
Client client,
74+
LongSupplier nowInMillis,
75+
boolean validate
76+
) {
77+
78+
this.xContentRegistry = xContentRegistry;
79+
this.writeableRegistry = writeableRegistry;
80+
this.client = client;
81+
this.nowInMillis = nowInMillis;
82+
this.validate = validate;
83+
}
84+
5085
/**
5186
* The registry used to build new {@link XContentParser}s. Contains registered named parsers needed to parse the query.
5287
*/
53-
NamedXContentRegistry getXContentRegistry();
88+
public NamedXContentRegistry getXContentRegistry() {
89+
return xContentRegistry;
90+
}
5491

5592
/**
5693
* Returns the time in milliseconds that is shared across all resources involved. Even across shards and nodes.
5794
*/
58-
long nowInMillis();
95+
public long nowInMillis() {
96+
return nowInMillis.getAsLong();
97+
}
5998

60-
NamedWriteableRegistry getWriteableRegistry();
99+
public NamedWriteableRegistry getWriteableRegistry() {
100+
return writeableRegistry;
101+
}
61102

62103
/**
63-
* Returns an instance of {@link QueryShardContext} if available of null otherwise
104+
* Returns an instance of {@link QueryShardContext} if available or null otherwise
64105
*/
65-
default QueryShardContext convertToShardContext() {
106+
public QueryShardContext convertToShardContext() {
66107
return null;
67108
}
68109

69-
default QueryCoordinatorContext convertToCoordinatorContext() {
110+
/**
111+
* Returns an instance of {@link QueryCoordinatorContext} if available or null otherwise
112+
* @return
113+
*/
114+
public QueryCoordinatorContext convertToCoordinatorContext() {
70115
return null;
71116
}
72117

@@ -75,18 +120,52 @@ default QueryCoordinatorContext convertToCoordinatorContext() {
75120
* This should be used if a rewriteabel needs to fetch some external resources in order to be executed ie. a document
76121
* from an index.
77122
*/
78-
void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncAction);
123+
public void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncAction) {
124+
asyncActions.add(asyncAction);
125+
}
79126

80127
/**
81128
* Returns <code>true</code> if there are any registered async actions.
82129
*/
83-
boolean hasAsyncActions();
130+
public boolean hasAsyncActions() {
131+
return asyncActions.isEmpty() == false;
132+
}
84133

85134
/**
86135
* Executes all registered async actions and notifies the listener once it's done. The value that is passed to the listener is always
87136
* <code>null</code>. The list of registered actions is cleared once this method returns.
88137
*/
89-
void executeAsyncActions(ActionListener listener);
138+
public void executeAsyncActions(ActionListener listener) {
139+
if (asyncActions.isEmpty()) {
140+
listener.onResponse(null);
141+
return;
142+
}
90143

91-
boolean validate();
144+
CountDown countDown = new CountDown(asyncActions.size());
145+
ActionListener<?> internalListener = new ActionListener() {
146+
@Override
147+
public void onResponse(Object o) {
148+
if (countDown.countDown()) {
149+
listener.onResponse(null);
150+
}
151+
}
152+
153+
@Override
154+
public void onFailure(Exception e) {
155+
if (countDown.fastForward()) {
156+
listener.onFailure(e);
157+
}
158+
}
159+
};
160+
// make a copy to prevent concurrent modification exception
161+
List<BiConsumer<Client, ActionListener<?>>> biConsumers = new ArrayList<>(asyncActions);
162+
asyncActions.clear();
163+
for (BiConsumer<Client, ActionListener<?>> action : biConsumers) {
164+
action.accept(client, internalListener);
165+
}
166+
}
167+
168+
public boolean validate() {
169+
return validate;
170+
}
92171
}

server/src/main/java/org/opensearch/index/query/QueryShardContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
* @opensearch.api
102102
*/
103103
@PublicApi(since = "1.0.0")
104-
public class QueryShardContext extends BaseQueryRewriteContext {
104+
public class QueryShardContext extends QueryRewriteContext {
105105

106106
private final ScriptService scriptService;
107107
private final IndexSettings indexSettings;

0 commit comments

Comments
 (0)