Skip to content

Commit b3ddec7

Browse files
authored
Add warning header after 25 days after last db update (#75311)
Add warning header after 25 days after last db update
1 parent cc25792 commit b3ddec7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
import com.maxmind.geoip2.record.Country;
1818
import com.maxmind.geoip2.record.Location;
1919
import com.maxmind.geoip2.record.Subdivision;
20+
2021
import org.elasticsearch.ElasticsearchParseException;
2122
import org.elasticsearch.ResourceNotFoundException;
2223
import org.elasticsearch.cluster.ClusterState;
2324
import org.elasticsearch.cluster.service.ClusterService;
2425
import org.elasticsearch.common.CheckedSupplier;
26+
import org.elasticsearch.common.logging.HeaderWarning;
2527
import org.elasticsearch.common.network.InetAddresses;
2628
import org.elasticsearch.common.network.NetworkAddress;
2729
import org.elasticsearch.ingest.AbstractProcessor;
@@ -65,10 +67,11 @@ public final class GeoIpProcessor extends AbstractProcessor {
6567

6668
/**
6769
* Construct a geo-IP processor.
70+
*
6871
* @param tag the processor tag
6972
* @param description the processor description
7073
* @param field the source field to geo-IP map
71-
* @param supplier a supplier of a geo-IP database reader; ideally this is lazily-loaded once on first use
74+
* @param supplier a supplier of a geo-IP database reader; ideally this is lazily-loaded once on first use
7275
* @param isValid
7376
* @param targetField the target field
7477
* @param properties the properties; ideally this is lazily-loaded once on first use
@@ -104,7 +107,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws IOException
104107
Object ip = ingestDocument.getFieldValue(field, Object.class, ignoreMissing);
105108

106109
if (isValid.get() == false) {
107-
ingestDocument.appendFieldValue("tags","_geoip_expired_database", false);
110+
ingestDocument.appendFieldValue("tags", "_geoip_expired_database", false);
108111
return ingestDocument;
109112
} else if (ip == null && ignoreMissing) {
110113
return ingestDocument;
@@ -367,9 +370,9 @@ public Factory(DatabaseRegistry databaseRegistry, ClusterService clusterService)
367370

368371
@Override
369372
public GeoIpProcessor create(
370-
final Map<String, Processor.Factory> registry,
371-
final String processorTag,
372-
final String description, final Map<String, Object> config) throws IOException {
373+
final Map<String, Processor.Factory> registry,
374+
final String processorTag,
375+
final String description, final Map<String, Object> config) throws IOException {
373376
String ipField = readStringProperty(TYPE, processorTag, config, "field");
374377
String targetField = readStringProperty(TYPE, processorTag, config, "target_field", "geoip");
375378
String databaseFile = readStringProperty(TYPE, processorTag, config, "database_file", "GeoLite2-City.mmdb");
@@ -438,7 +441,17 @@ public GeoIpProcessor create(
438441
GeoIpTaskState state = (GeoIpTaskState) task.getState();
439442
GeoIpTaskState.Metadata metadata = state.getDatabases().get(databaseFile);
440443
// we never remove metadata from cluster state, if metadata is null we deal with built-in database, which is always valid
441-
return metadata == null || metadata.isValid(currentState.metadata().settings());
444+
if (metadata == null) {
445+
return true;
446+
}
447+
448+
boolean valid = metadata.isValid(currentState.metadata().settings());
449+
if (valid && metadata.isCloseToExpiration()) {
450+
HeaderWarning.addWarning("database [{}] was not updated for over 25 days, geoip processor will stop working if there " +
451+
"is no update for 30 days", databaseFile);
452+
}
453+
454+
return valid;
442455
};
443456
return new GeoIpProcessor(processorTag, description, ipField, supplier, isValid, targetField, properties, ignoreMissing,
444457
firstOnly);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ public long getLastUpdate() {
189189
return lastUpdate;
190190
}
191191

192+
public boolean isCloseToExpiration(){
193+
return Instant.ofEpochMilli(lastCheck).isBefore(Instant.now().minus(25, ChronoUnit.DAYS));
194+
}
195+
192196
public boolean isValid(Settings settings) {
193197
TimeValue valid = settings.getAsTime("ingest.geoip.database_validity", TimeValue.timeValueDays(30));
194198
return Instant.ofEpochMilli(lastCheck).isAfter(Instant.now().minus(valid.getMillis(), ChronoUnit.MILLIS));

0 commit comments

Comments
 (0)