17
17
import java .util .function .BiConsumer ;
18
18
import java .util .stream .Collectors ;
19
19
20
+ import lombok .AllArgsConstructor ;
20
21
import lombok .Getter ;
21
22
import lombok .extern .log4j .Log4j2 ;
22
23
23
24
import org .opensearch .action .ActionListener ;
24
25
import org .opensearch .common .settings .ClusterSettings ;
25
26
import org .opensearch .geospatial .annotation .VisibleForTesting ;
27
+ import org .opensearch .geospatial .ip2geo .cache .Ip2GeoCache ;
26
28
import org .opensearch .geospatial .ip2geo .common .DatasourceFacade ;
27
29
import org .opensearch .geospatial .ip2geo .common .DatasourceState ;
28
30
import org .opensearch .geospatial .ip2geo .common .GeoIpDataFacade ;
29
- import org .opensearch .geospatial .ip2geo .jobscheduler .Datasource ;
30
- import org .opensearch .index .IndexNotFoundException ;
31
31
import org .opensearch .ingest .AbstractProcessor ;
32
32
import org .opensearch .ingest .IngestDocument ;
33
33
import org .opensearch .ingest .IngestService ;
@@ -57,6 +57,7 @@ public final class Ip2GeoProcessor extends AbstractProcessor {
57
57
private final ClusterSettings clusterSettings ;
58
58
private final DatasourceFacade datasourceFacade ;
59
59
private final GeoIpDataFacade geoIpDataFacade ;
60
+ private final Ip2GeoCache ip2GeoCache ;
60
61
61
62
/**
62
63
* Ip2Geo processor type
@@ -75,6 +76,7 @@ public final class Ip2GeoProcessor extends AbstractProcessor {
75
76
* @param clusterSettings the cluster settings
76
77
* @param datasourceFacade the datasource facade
77
78
* @param geoIpDataFacade the geoip data facade
79
+ * @param ip2GeoCache the cache
78
80
*/
79
81
public Ip2GeoProcessor (
80
82
final String tag ,
@@ -86,7 +88,8 @@ public Ip2GeoProcessor(
86
88
final boolean ignoreMissing ,
87
89
final ClusterSettings clusterSettings ,
88
90
final DatasourceFacade datasourceFacade ,
89
- final GeoIpDataFacade geoIpDataFacade
91
+ final GeoIpDataFacade geoIpDataFacade ,
92
+ final Ip2GeoCache ip2GeoCache
90
93
) {
91
94
super (tag , description );
92
95
this .field = field ;
@@ -97,6 +100,7 @@ public Ip2GeoProcessor(
97
100
this .clusterSettings = clusterSettings ;
98
101
this .datasourceFacade = datasourceFacade ;
99
102
this .geoIpDataFacade = geoIpDataFacade ;
103
+ this .ip2GeoCache = ip2GeoCache ;
100
104
}
101
105
102
106
/**
@@ -149,42 +153,18 @@ protected void executeInternal(
149
153
final BiConsumer <IngestDocument , Exception > handler ,
150
154
final String ip
151
155
) {
152
- datasourceFacade .getDatasource (datasourceName , new ActionListener <>() {
153
- @ Override
154
- public void onResponse (final Datasource datasource ) {
155
- if (datasource == null ) {
156
- handler .accept (null , new IllegalStateException ("datasource is not available" ));
157
- return ;
158
- }
159
-
160
- if (DatasourceState .AVAILABLE .equals (datasource .getState ()) == false ) {
161
- handler .accept (null , new IllegalStateException ("datasource is not in an available state" ));
162
- return ;
163
- }
164
-
165
- String indexName = datasource .currentIndexName ();
166
- if (indexName == null ) {
167
- ingestDocument .setFieldValue (targetField , DATA_EXPIRED );
168
- handler .accept (ingestDocument , null );
169
- return ;
170
- }
171
-
172
- try {
173
- geoIpDataFacade .getGeoIpData (indexName , ip , getSingleGeoIpDataListener (ingestDocument , handler ));
174
- } catch (Exception e ) {
175
- handler .accept (null , e );
176
- }
177
- }
156
+ validateDatasourceIsInAvailableState (datasourceName );
157
+ String indexName = ip2GeoCache .getIndexName (datasourceName );
158
+ if (ip2GeoCache .isExpired (datasourceName ) || indexName == null ) {
159
+ handleExpiredData (ingestDocument , handler );
160
+ return ;
161
+ }
178
162
179
- @ Override
180
- public void onFailure (final Exception e ) {
181
- if (e instanceof IndexNotFoundException ) {
182
- handler .accept (null , new IllegalStateException ("datasource is not available" ));
183
- return ;
184
- }
185
- handler .accept (null , e );
186
- }
187
- });
163
+ try {
164
+ geoIpDataFacade .getGeoIpData (indexName , ip , getSingleGeoIpDataListener (ingestDocument , handler ));
165
+ } catch (Exception e ) {
166
+ handler .accept (null , e );
167
+ }
188
168
}
189
169
190
170
@ VisibleForTesting
@@ -228,6 +208,21 @@ private List<Map<String, Object>> filteredGeoData(final List<Map<String, Object>
228
208
return geoData .stream ().map (this ::filteredGeoData ).collect (Collectors .toList ());
229
209
}
230
210
211
+ private void validateDatasourceIsInAvailableState (final String datasourceName ) {
212
+ if (ip2GeoCache .has (datasourceName ) == false ) {
213
+ throw new IllegalStateException ("datasource does not exist" );
214
+ }
215
+
216
+ if (DatasourceState .AVAILABLE .equals (ip2GeoCache .getState (datasourceName )) == false ) {
217
+ throw new IllegalStateException ("datasource is not in an available state" );
218
+ }
219
+ }
220
+
221
+ private void handleExpiredData (final IngestDocument ingestDocument , final BiConsumer <IngestDocument , Exception > handler ) {
222
+ ingestDocument .setFieldValue (targetField , DATA_EXPIRED );
223
+ handler .accept (ingestDocument , null );
224
+ }
225
+
231
226
/**
232
227
* Handle multiple ips
233
228
*
@@ -246,37 +241,15 @@ protected void executeInternal(
246
241
throw new IllegalArgumentException ("array in field [" + field + "] should only contain strings" );
247
242
}
248
243
}
249
- datasourceFacade .getDatasource (datasourceName , new ActionListener <>() {
250
- @ Override
251
- public void onResponse (final Datasource datasource ) {
252
- if (datasource == null || DatasourceState .AVAILABLE .equals (datasource .getState ()) == false ) {
253
- handler .accept (null , new IllegalStateException ("datasource is not available" ));
254
- return ;
255
- }
256
244
257
- String indexName = datasource .currentIndexName ();
258
- if (indexName == null ) {
259
- ingestDocument .setFieldValue (targetField , DATA_EXPIRED );
260
- handler .accept (ingestDocument , null );
261
- return ;
262
- }
263
-
264
- try {
265
- geoIpDataFacade .getGeoIpData (indexName , (List <String >) ips , getMultiGeoIpDataListener (ingestDocument , handler ));
266
- } catch (Exception e ) {
267
- handler .accept (null , e );
268
- }
269
- }
245
+ validateDatasourceIsInAvailableState (datasourceName );
246
+ String indexName = ip2GeoCache .getIndexName (datasourceName );
247
+ if (ip2GeoCache .isExpired (datasourceName ) || indexName == null ) {
248
+ handleExpiredData (ingestDocument , handler );
249
+ return ;
250
+ }
270
251
271
- @ Override
272
- public void onFailure (final Exception e ) {
273
- if (e instanceof IndexNotFoundException ) {
274
- handler .accept (null , new IllegalStateException ("datasource is not available" ));
275
- return ;
276
- }
277
- handler .accept (null , e );
278
- }
279
- });
252
+ geoIpDataFacade .getGeoIpData (indexName , (List <String >) ips , getMultiGeoIpDataListener (ingestDocument , handler ));
280
253
}
281
254
282
255
@ VisibleForTesting
@@ -312,23 +285,12 @@ public String getType() {
312
285
/**
313
286
* Ip2Geo processor factory
314
287
*/
288
+ @ AllArgsConstructor
315
289
public static final class Factory implements Processor .Factory {
316
290
private final IngestService ingestService ;
317
291
private final DatasourceFacade datasourceFacade ;
318
292
private final GeoIpDataFacade geoIpDataFacade ;
319
-
320
- /**
321
- * Default constructor
322
- *
323
- * @param ingestService the ingest service
324
- * @param datasourceFacade the datasource facade
325
- * @param geoIpDataFacade the geoip data facade
326
- */
327
- public Factory (final IngestService ingestService , final DatasourceFacade datasourceFacade , final GeoIpDataFacade geoIpDataFacade ) {
328
- this .ingestService = ingestService ;
329
- this .datasourceFacade = datasourceFacade ;
330
- this .geoIpDataFacade = geoIpDataFacade ;
331
- }
293
+ private final Ip2GeoCache ip2GeoCache ;
332
294
333
295
/**
334
296
* Within this method, blocking request cannot be called because this method is executed in a transport thread.
@@ -357,7 +319,8 @@ public Ip2GeoProcessor create(
357
319
ignoreMissing ,
358
320
ingestService .getClusterService ().getClusterSettings (),
359
321
datasourceFacade ,
360
- geoIpDataFacade
322
+ geoIpDataFacade ,
323
+ ip2GeoCache
361
324
);
362
325
}
363
326
}
0 commit comments