-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed how geoip cache is integrated with geoip processor. #68581
Conversation
This change helps facilitate allowing maxmind databases to be updated at runtime. This will make is easier to purge the cache if a database changes. Made the following changes: * Changed how geoip processor integrates with the cache. The cache is moved from the geoip processor to DatabaseReaderLazyLoader class. * Changed the cache key from ip + response class to ip + database_path. * Moved GeoIpCache from IngestGeoIpPlugin class to be a top level class.
Pinging @elastic/es-core-features (Team:Core/Features) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @martijnvg !
I left one comment about possible simplification
CityResponse getCity(InetAddress ipAddress) { | ||
SpecialPermission.check(); | ||
return AccessController.doPrivileged((PrivilegedAction<CityResponse>) () -> | ||
cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> { | ||
try { | ||
return get().city(ip); | ||
} catch (AddressNotFoundException e) { | ||
throw new GeoIpProcessor.AddressNotFoundRuntimeException(e); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
})); | ||
} | ||
|
||
CountryResponse getCountry(InetAddress ipAddress) { | ||
SpecialPermission.check(); | ||
return AccessController.doPrivileged((PrivilegedAction<CountryResponse>) () -> | ||
cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> { | ||
try { | ||
return get().country(ip); | ||
} catch (AddressNotFoundException e) { | ||
throw new GeoIpProcessor.AddressNotFoundRuntimeException(e); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
})); | ||
} | ||
|
||
AsnResponse getAsn(InetAddress ipAddress) { | ||
SpecialPermission.check(); | ||
return AccessController.doPrivileged((PrivilegedAction<AsnResponse>) () -> | ||
cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> { | ||
try { | ||
return get().asn(ip); | ||
} catch (AddressNotFoundException e) { | ||
throw new GeoIpProcessor.AddressNotFoundRuntimeException(e); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
})); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe extract common functionality to separate method:
private <T extends AbstractResponse> T getResponse(InetAddress ipAddress, CheckedBiFunction<DatabaseReader, InetAddress, T,
Exception> responseProvider) {
SpecialPermission.check();
return AccessController.doPrivileged((PrivilegedAction<T>) () ->
cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> {
try {
return responseProvider.apply(get(), ipAddress);
} catch (AddressNotFoundException e) {
throw new GeoIpProcessor.AddressNotFoundRuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
CityResponse getCity(InetAddress ipAddress) {
return getResponse(ipAddress, DatabaseReader::city);
}
CountryResponse getCountry(InetAddress ipAddress) {
return getResponse(ipAddress, DatabaseReader::country);
}
AsnResponse getAsn(InetAddress ipAddress) {
return getResponse(ipAddress, DatabaseReader::asn);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
Backport elastic#68581 of to 7.x branch. This change helps facilitate allowing maxmind databases to be updated at runtime. This will make is easier to purge the cache if a database changes. Made the following changes: * Changed how geoip processor integrates with the cache. The cache is moved from the geoip processor to DatabaseReaderLazyLoader class. * Changed the cache key from ip + response class to ip + database_path. * Moved GeoIpCache from IngestGeoIpPlugin class to be a top level class.
Backport #68581 of to 7.x branch. This change helps facilitate allowing maxmind databases to be updated at runtime. This will make is easier to purge the cache if a database changes. Made the following changes: * Changed how geoip processor integrates with the cache. The cache is moved from the geoip processor to DatabaseReaderLazyLoader class. * Changed the cache key from ip + response class to ip + database_path. * Moved GeoIpCache from IngestGeoIpPlugin class to be a top level class.
This change helps facilitate allowing maxmind databases to be updated at runtime.
This will make is easier to purge the cache if a database changes.
Made the following changes:
DatabaseReaderLazyLoader
instance a processor is using will change after a database update.