Skip to content

Commit 66b55eb

Browse files
authored
Fix disabling GeoIP downloader through elasticsearch.yml (#76924) (#76964)
This change allows user to disable GeoIP downloader using elasticsearch.yml and it deletes .geoip_databases index if downloader is disabled. Closes #76586
1 parent 102cfe7 commit 66b55eb

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.action.index.IndexRequest;
1717
import org.elasticsearch.action.support.PlainActionFuture;
1818
import org.elasticsearch.client.Client;
19-
import org.elasticsearch.client.OriginSettingClient;
2019
import org.elasticsearch.cluster.service.ClusterService;
2120
import org.elasticsearch.common.hash.MessageDigests;
2221
import org.elasticsearch.common.settings.Setting;
@@ -32,7 +31,6 @@
3231
import org.elasticsearch.index.query.RangeQueryBuilder;
3332
import org.elasticsearch.index.reindex.DeleteByQueryAction;
3433
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
35-
import org.elasticsearch.ingest.IngestService;
3634
import org.elasticsearch.ingest.geoip.GeoIpTaskState.Metadata;
3735
import org.elasticsearch.ingest.geoip.stats.GeoIpDownloaderStats;
3836
import org.elasticsearch.persistent.AllocatedPersistentTask;
@@ -84,7 +82,7 @@ public class GeoIpDownloader extends AllocatedPersistentTask {
8482
long id, String type, String action, String description, TaskId parentTask, Map<String, String> headers) {
8583
super(id, type, action, description, parentTask, headers);
8684
this.httpClient = httpClient;
87-
this.client = new OriginSettingClient(client, IngestService.INGEST_ORIGIN);
85+
this.client = client;
8886
this.clusterService = clusterService;
8987
this.threadPool = threadPool;
9088
endpoint = ENDPOINT_SETTING.get(settings);

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
1313
import org.elasticsearch.ResourceAlreadyExistsException;
14+
import org.elasticsearch.ResourceNotFoundException;
1415
import org.elasticsearch.action.ActionListener;
1516
import org.elasticsearch.client.Client;
17+
import org.elasticsearch.client.OriginSettingClient;
1618
import org.elasticsearch.cluster.ClusterChangedEvent;
1719
import org.elasticsearch.cluster.ClusterStateListener;
1820
import org.elasticsearch.cluster.service.ClusterService;
1921
import org.elasticsearch.common.settings.Setting;
2022
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.gateway.GatewayService;
24+
import org.elasticsearch.ingest.IngestService;
2125
import org.elasticsearch.persistent.AllocatedPersistentTask;
2226
import org.elasticsearch.persistent.PersistentTaskState;
2327
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
@@ -29,6 +33,7 @@
2933
import java.util.Map;
3034
import java.util.concurrent.atomic.AtomicReference;
3135

36+
import static org.elasticsearch.ingest.geoip.GeoIpDownloader.DATABASES_INDEX;
3237
import static org.elasticsearch.ingest.geoip.GeoIpDownloader.GEOIP_DOWNLOADER;
3338

3439
/**
@@ -54,15 +59,14 @@ public final class GeoIpDownloaderTaskExecutor extends PersistentTasksExecutor<G
5459

5560
GeoIpDownloaderTaskExecutor(Client client, HttpClient httpClient, ClusterService clusterService, ThreadPool threadPool) {
5661
super(GEOIP_DOWNLOADER, ThreadPool.Names.GENERIC);
57-
this.client = client;
62+
this.client = new OriginSettingClient(client, IngestService.INGEST_ORIGIN);
5863
this.httpClient = httpClient;
5964
this.clusterService = clusterService;
6065
this.threadPool = threadPool;
6166
this.settings = clusterService.getSettings();
6267
persistentTasksService = new PersistentTasksService(clusterService, threadPool, client);
63-
if (ENABLED_SETTING.get(settings)) {
64-
clusterService.addListener(this);
65-
}
68+
clusterService.addListener(this);
69+
6670
clusterService.getClusterSettings().addSettingsUpdateConsumer(ENABLED_SETTING, this::setEnabled);
6771
}
6872

@@ -75,8 +79,8 @@ private void setEnabled(boolean enabled) {
7579
startTask(() -> {
7680
});
7781
} else {
78-
persistentTasksService.sendRemoveRequest(GEOIP_DOWNLOADER, ActionListener.wrap(r -> {
79-
}, e -> logger.error("failed to remove geoip task", e)));
82+
stopTask(() -> {
83+
});
8084
}
8185
}
8286

@@ -86,23 +90,33 @@ protected void nodeOperation(AllocatedPersistentTask task, GeoIpTaskParams param
8690
currentTask.set(downloader);
8791
GeoIpTaskState geoIpTaskState = state == null ? GeoIpTaskState.EMPTY : (GeoIpTaskState) state;
8892
downloader.setState(geoIpTaskState);
89-
downloader.runDownloader();
93+
if (ENABLED_SETTING.get(clusterService.state().metadata().settings(), settings)) {
94+
downloader.runDownloader();
95+
}
9096
}
9197

9298
@Override
9399
protected GeoIpDownloader createTask(long id, String type, String action, TaskId parentTaskId,
94-
PersistentTasksCustomMetadata.PersistentTask<GeoIpTaskParams> taskInProgress,
95-
Map<String, String> headers) {
100+
PersistentTasksCustomMetadata.PersistentTask<GeoIpTaskParams> taskInProgress,
101+
Map<String, String> headers) {
96102
return new GeoIpDownloader(client, httpClient, clusterService, threadPool, settings, id, type, action,
97103
getDescription(taskInProgress), parentTaskId, headers);
98104
}
99105

100106
@Override
101107
public void clusterChanged(ClusterChangedEvent event) {
108+
if(event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)){
109+
//wait for state recovered
110+
return;
111+
}
102112
//bootstrap downloader after first cluster start
103113
clusterService.removeListener(this);
104-
if (event.localNodeMaster() && ENABLED_SETTING.get(event.state().getMetadata().settings())) {
105-
startTask(() -> clusterService.addListener(this));
114+
if (event.localNodeMaster()) {
115+
if (ENABLED_SETTING.get(event.state().getMetadata().settings(), settings)) {
116+
startTask(() -> clusterService.addListener(this));
117+
} else {
118+
stopTask(() -> clusterService.addListener(this));
119+
}
106120
}
107121
}
108122

@@ -116,7 +130,24 @@ private void startTask(Runnable onFailure) {
116130
}));
117131
}
118132

119-
public GeoIpDownloader getCurrentTask(){
133+
private void stopTask(Runnable onFailure) {
134+
ActionListener<PersistentTasksCustomMetadata.PersistentTask<?>> listener = ActionListener.wrap(r -> {
135+
}, e -> {
136+
if (e instanceof ResourceNotFoundException == false) {
137+
logger.error("failed to remove geoip downloader task", e);
138+
onFailure.run();
139+
}
140+
});
141+
persistentTasksService.sendRemoveRequest(GEOIP_DOWNLOADER, ActionListener.runAfter(listener, () ->
142+
client.admin().indices().prepareDelete(DATABASES_INDEX).execute(ActionListener.wrap(rr -> {
143+
}, e -> {
144+
if (e instanceof ResourceNotFoundException == false) {
145+
logger.warn("failed to remove " + DATABASES_INDEX, e);
146+
}
147+
}))));
148+
}
149+
150+
public GeoIpDownloader getCurrentTask() {
120151
return currentTask.get();
121152
}
122153
}

0 commit comments

Comments
 (0)