Skip to content

Commit 48a4d55

Browse files
Do a horrible thing as an experimental workaround for the logstash issue
1 parent 0f1738d commit 48a4d55

File tree

8 files changed

+40
-13
lines changed

8 files changed

+40
-13
lines changed

modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private void assertValidDatabase(DatabaseNodeService databaseNodeService, String
9393
IpDatabase database = databaseNodeService.getDatabase(databaseFileName);
9494
assertNotNull(database);
9595
assertThat(database.getDatabaseType(), equalTo(databaseType));
96-
GeoIpTestUtils.SimpleCountry countryResponse = database.getResponse("89.160.20.128", GeoIpTestUtils::getCountry);
96+
GeoIpTestUtils.SimpleCountry countryResponse = database.getTypedResponse("89.160.20.128", GeoIpTestUtils::getCountry);
9797
assertNotNull(countryResponse);
9898
assertThat(countryResponse.countryName(), equalTo("Sweden"));
9999
}

modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ private static DatabaseNodeService createRegistry(Path geoIpConfigDir, Path geoI
213213
private static void lazyLoadReaders(DatabaseNodeService databaseNodeService) throws IOException {
214214
if (databaseNodeService.get("GeoLite2-City.mmdb") != null) {
215215
databaseNodeService.get("GeoLite2-City.mmdb").getDatabaseType();
216-
databaseNodeService.get("GeoLite2-City.mmdb").getResponse("2.125.160.216", GeoIpTestUtils::getCity);
216+
databaseNodeService.get("GeoLite2-City.mmdb").getTypedResponse("2.125.160.216", GeoIpTestUtils::getCity);
217217
}
218218
databaseNodeService.get("GeoLite2-City-Test.mmdb").getDatabaseType();
219-
databaseNodeService.get("GeoLite2-City-Test.mmdb").getResponse("2.125.160.216", GeoIpTestUtils::getCity);
219+
databaseNodeService.get("GeoLite2-City-Test.mmdb").getTypedResponse("2.125.160.216", GeoIpTestUtils::getCity);
220220
}
221221

222222
}

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ int current() {
105105
return currentUsages.get();
106106
}
107107

108-
@Override
109108
@Nullable
110109
@FixForMultiProject // do not use ProjectId.DEFAULT
111-
public <RESPONSE extends Response> RESPONSE getResponse(
110+
public <RESPONSE extends Response> RESPONSE getTypedResponse(
112111
String ipAddress,
113112
CheckedBiFunction<Reader, String, RESPONSE, Exception> responseProvider
114113
) {

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IpDatabase.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,38 @@ public interface IpDatabase extends AutoCloseable {
3535
* @return a possibly-null response
3636
*/
3737
@Nullable
38-
<RESPONSE extends Response> RESPONSE getResponse(
38+
@Deprecated // use getTypedResponse instead
39+
default <RESPONSE> RESPONSE getResponse(String ipAddress, CheckedBiFunction<Reader, String, RESPONSE, Exception> responseProvider) {
40+
// TODO(pete): Decide what to do here
41+
// TODO(pete): This is one option, which makes this method work so long as it is invoked with a type which satisfies the constraint,
42+
// but fails nastily if it doesn't.
43+
// @SuppressWarnings("unchecked") // TODO(pete): Put some words of wisdom here
44+
// RESPONSE typedResponse = (RESPONSE) getTypedResponse(
45+
// ipAddress,
46+
// (CheckedBiFunction<Reader, String, ? extends Response, Exception>) responseProvider
47+
// );
48+
// return typedResponse;
49+
// TODO(pete): This is the other option, which fails fast. Which is okay as nothing calls this method.
50+
// TODO(pete): If we do this, add a message to the exception.
51+
throw new UnsupportedOperationException();
52+
}
53+
54+
// TODO(pete): If we end up doing this, pick a better name for getTypedResponse() and document why we do this weird thing.
55+
56+
/**
57+
* Returns a response from this database's reader for the given IP address.
58+
*
59+
* @param ipAddress the address to lookup
60+
* @param responseProvider a method for extracting a response from a {@link Reader}, usually this will be a method reference
61+
* @return a possibly-null response
62+
*/
63+
@Nullable
64+
default <RESPONSE extends Response> RESPONSE getTypedResponse(
3965
String ipAddress,
4066
CheckedBiFunction<Reader, String, RESPONSE, Exception> responseProvider
41-
);
67+
) {
68+
return getResponse(ipAddress, responseProvider);
69+
}
4270

4371
/**
4472
* Releases the current database object. Called after processing a single document. Databases should be closed or returned to a

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IpinfoIpDataLookups.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public Set<Database.Property> getProperties() {
479479

480480
@Override
481481
public final Map<String, Object> getData(final IpDatabase ipDatabase, final String ipAddress) {
482-
final Result<RESPONSE> response = ipDatabase.getResponse(ipAddress, this::lookup);
482+
final Result<RESPONSE> response = ipDatabase.getTypedResponse(ipAddress, this::lookup);
483483
return (response == null || response.result() == null) ? Map.of() : transform(response);
484484
}
485485

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ public Set<Database.Property> getProperties() {
938938

939939
@Override
940940
public final Map<String, Object> getData(final IpDatabase ipDatabase, final String ipAddress) {
941-
final RECORD response = ipDatabase.getResponse(ipAddress, this::lookup);
941+
final RECORD response = ipDatabase.getTypedResponse(ipAddress, this::lookup);
942942
return (response == null) ? Map.of() : transform(response);
943943
}
944944

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/ConfigDatabasesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void testDatabasesUpdateExistingConfDatabase() throws Exception {
124124

125125
DatabaseReaderLazyLoader loader = configDatabases.getDatabase("GeoLite2-City.mmdb");
126126
assertThat(loader.getDatabaseType(), equalTo("GeoLite2-City"));
127-
GeoIpTestUtils.SimpleCity cityResponse = loader.getResponse("89.160.20.128", GeoIpTestUtils::getCity);
127+
GeoIpTestUtils.SimpleCity cityResponse = loader.getTypedResponse("89.160.20.128", GeoIpTestUtils::getCity);
128128
assertThat(cityResponse.cityName(), equalTo("Tumba"));
129129
assertThat(cache.count(), equalTo(1));
130130
}
@@ -136,7 +136,7 @@ public void testDatabasesUpdateExistingConfDatabase() throws Exception {
136136

137137
DatabaseReaderLazyLoader loader = configDatabases.getDatabase("GeoLite2-City.mmdb");
138138
assertThat(loader.getDatabaseType(), equalTo("GeoLite2-City"));
139-
GeoIpTestUtils.SimpleCity cityResponse = loader.getResponse("89.160.20.128", GeoIpTestUtils::getCity);
139+
GeoIpTestUtils.SimpleCity cityResponse = loader.getTypedResponse("89.160.20.128", GeoIpTestUtils::getCity);
140140
assertThat(cityResponse.cityName(), equalTo("Linköping"));
141141
assertThat(cache.count(), equalTo(1));
142142
});

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void copyDefaultDatabases(final Path directory, ConfigDatabases configDat
7070
public record SimpleCity(String cityName) implements IpDatabase.Response {}
7171

7272
/**
73-
* A static city-specific responseProvider for use with {@link IpDatabase#getResponse(String, CheckedBiFunction)} in
73+
* A static city-specific responseProvider for use with {@link IpDatabase#getTypedResponse(String, CheckedBiFunction)} in
7474
* tests.
7575
* <p>
7676
* Like this: {@code SimpleCity city = loader.getResponse("some.ip.address", GeoIpTestUtils::getCity);}
@@ -84,7 +84,7 @@ public static SimpleCity getCity(Reader reader, String ip) throws IOException {
8484
public record SimpleCountry(String countryName) implements IpDatabase.Response {}
8585

8686
/**
87-
* A static country-specific responseProvider for use with {@link IpDatabase#getResponse(String, CheckedBiFunction)} in
87+
* A static country-specific responseProvider for use with {@link IpDatabase#getTypedResponse(String, CheckedBiFunction)} in
8888
* tests.
8989
* <p>
9090
* Like this: {@code SimpleCountry country = loader.getResponse("some.ip.address", GeoIpTestUtils::getCountry);}

0 commit comments

Comments
 (0)