49
49
import org .opensearch .core .xcontent .XContentParser ;
50
50
import org .opensearch .geospatial .ip2geo .jobscheduler .Datasource ;
51
51
import org .opensearch .geospatial .ip2geo .jobscheduler .DatasourceExtension ;
52
+ import org .opensearch .geospatial .shared .StashedThreadContext ;
52
53
import org .opensearch .index .IndexNotFoundException ;
53
54
import org .opensearch .index .query .QueryBuilders ;
54
55
import org .opensearch .search .SearchHit ;
@@ -89,7 +90,7 @@ public void createIndexIfNotExists(final StepListener<Void> stepListener) {
89
90
indexSettings .put (INDEX_SETTING_HIDDEN .v1 (), INDEX_SETTING_HIDDEN .v2 ());
90
91
final CreateIndexRequest createIndexRequest = new CreateIndexRequest (DatasourceExtension .JOB_INDEX_NAME ).mapping (getIndexMapping ())
91
92
.settings (indexSettings );
92
- client .admin ().indices ().create (createIndexRequest , new ActionListener <>() {
93
+ StashedThreadContext . run ( client , () -> client .admin ().indices ().create (createIndexRequest , new ActionListener <>() {
93
94
@ Override
94
95
public void onResponse (final CreateIndexResponse createIndexResponse ) {
95
96
stepListener .onResponse (null );
@@ -104,7 +105,7 @@ public void onFailure(final Exception e) {
104
105
}
105
106
stepListener .onFailure (e );
106
107
}
107
- });
108
+ })) ;
108
109
}
109
110
110
111
private String getIndexMapping () {
@@ -123,34 +124,44 @@ private String getIndexMapping() {
123
124
* Update datasource in an index {@code DatasourceExtension.JOB_INDEX_NAME}
124
125
* @param datasource the datasource
125
126
* @return index response
126
- * @throws IOException exception
127
127
*/
128
- public IndexResponse updateDatasource (final Datasource datasource ) throws IOException {
128
+ public IndexResponse updateDatasource (final Datasource datasource ) {
129
129
datasource .setLastUpdateTime (Instant .now ());
130
- return client .prepareIndex (DatasourceExtension .JOB_INDEX_NAME )
131
- .setId (datasource .getName ())
132
- .setOpType (DocWriteRequest .OpType .INDEX )
133
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
134
- .setSource (datasource .toXContent (XContentFactory .jsonBuilder (), ToXContent .EMPTY_PARAMS ))
135
- .execute ()
136
- .actionGet (clusterSettings .get (Ip2GeoSettings .TIMEOUT ));
130
+ return StashedThreadContext .run (client , () -> {
131
+ try {
132
+ return client .prepareIndex (DatasourceExtension .JOB_INDEX_NAME )
133
+ .setId (datasource .getName ())
134
+ .setOpType (DocWriteRequest .OpType .INDEX )
135
+ .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
136
+ .setSource (datasource .toXContent (XContentFactory .jsonBuilder (), ToXContent .EMPTY_PARAMS ))
137
+ .execute ()
138
+ .actionGet (clusterSettings .get (Ip2GeoSettings .TIMEOUT ));
139
+ } catch (IOException e ) {
140
+ throw new RuntimeException (e );
141
+ }
142
+ });
137
143
}
138
144
139
145
/**
140
146
* Put datasource in an index {@code DatasourceExtension.JOB_INDEX_NAME}
141
147
*
142
148
* @param datasource the datasource
143
149
* @param listener the listener
144
- * @throws IOException exception
145
150
*/
146
- public void putDatasource (final Datasource datasource , final ActionListener listener ) throws IOException {
151
+ public void putDatasource (final Datasource datasource , final ActionListener listener ) {
147
152
datasource .setLastUpdateTime (Instant .now ());
148
- client .prepareIndex (DatasourceExtension .JOB_INDEX_NAME )
149
- .setId (datasource .getName ())
150
- .setOpType (DocWriteRequest .OpType .CREATE )
151
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
152
- .setSource (datasource .toXContent (XContentFactory .jsonBuilder (), ToXContent .EMPTY_PARAMS ))
153
- .execute (listener );
153
+ StashedThreadContext .run (client , () -> {
154
+ try {
155
+ client .prepareIndex (DatasourceExtension .JOB_INDEX_NAME )
156
+ .setId (datasource .getName ())
157
+ .setOpType (DocWriteRequest .OpType .CREATE )
158
+ .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
159
+ .setSource (datasource .toXContent (XContentFactory .jsonBuilder (), ToXContent .EMPTY_PARAMS ))
160
+ .execute (listener );
161
+ } catch (IOException e ) {
162
+ new RuntimeException (e );
163
+ }
164
+ });
154
165
}
155
166
156
167
/**
@@ -163,7 +174,7 @@ public Datasource getDatasource(final String name) throws IOException {
163
174
GetRequest request = new GetRequest (DatasourceExtension .JOB_INDEX_NAME , name );
164
175
GetResponse response ;
165
176
try {
166
- response = client .get (request ).actionGet (clusterSettings .get (Ip2GeoSettings .TIMEOUT ));
177
+ response = StashedThreadContext . run ( client , () -> client .get (request ).actionGet (clusterSettings .get (Ip2GeoSettings .TIMEOUT ) ));
167
178
if (response .isExists () == false ) {
168
179
log .error ("Datasource[{}] does not exist in an index[{}]" , name , DatasourceExtension .JOB_INDEX_NAME );
169
180
return null ;
@@ -188,7 +199,7 @@ public Datasource getDatasource(final String name) throws IOException {
188
199
*/
189
200
public void getDatasource (final String name , final ActionListener <Datasource > actionListener ) {
190
201
GetRequest request = new GetRequest (DatasourceExtension .JOB_INDEX_NAME , name );
191
- client .get (request , new ActionListener <GetResponse >() {
202
+ StashedThreadContext . run ( client , () -> client .get (request , new ActionListener <>() {
192
203
@ Override
193
204
public void onResponse (final GetResponse response ) {
194
205
if (response .isExists () == false ) {
@@ -212,7 +223,7 @@ public void onResponse(final GetResponse response) {
212
223
public void onFailure (final Exception e ) {
213
224
actionListener .onFailure (e );
214
225
}
215
- });
226
+ })) ;
216
227
}
217
228
218
229
/**
@@ -221,20 +232,26 @@ public void onFailure(final Exception e) {
221
232
* @param actionListener the action listener
222
233
*/
223
234
public void getDatasources (final String [] names , final ActionListener <List <Datasource >> actionListener ) {
224
- client .prepareMultiGet ()
225
- .add (DatasourceExtension .JOB_INDEX_NAME , names )
226
- .execute (createGetDataSourceQueryActionLister (MultiGetResponse .class , actionListener ));
235
+ StashedThreadContext .run (
236
+ client ,
237
+ () -> client .prepareMultiGet ()
238
+ .add (DatasourceExtension .JOB_INDEX_NAME , names )
239
+ .execute (createGetDataSourceQueryActionLister (MultiGetResponse .class , actionListener ))
240
+ );
227
241
}
228
242
229
243
/**
230
244
* Get all datasources up to {@code MAX_SIZE} from an index {@code DatasourceExtension.JOB_INDEX_NAME}
231
245
* @param actionListener the action listener
232
246
*/
233
247
public void getAllDatasources (final ActionListener <List <Datasource >> actionListener ) {
234
- client .prepareSearch (DatasourceExtension .JOB_INDEX_NAME )
235
- .setQuery (QueryBuilders .matchAllQuery ())
236
- .setSize (MAX_SIZE )
237
- .execute (createGetDataSourceQueryActionLister (SearchResponse .class , actionListener ));
248
+ StashedThreadContext .run (
249
+ client ,
250
+ () -> client .prepareSearch (DatasourceExtension .JOB_INDEX_NAME )
251
+ .setQuery (QueryBuilders .matchAllQuery ())
252
+ .setSize (MAX_SIZE )
253
+ .execute (createGetDataSourceQueryActionLister (SearchResponse .class , actionListener ))
254
+ );
238
255
}
239
256
240
257
private <T > ActionListener <T > createGetDataSourceQueryActionLister (
0 commit comments