Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/84000.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 84000
summary: Fix `GeoIpDownloader` startup during rolling upgrade
area: Ingest
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ public class GeoIpDownloader extends AllocatedPersistentTask {
Property.Dynamic,
Property.NodeScope
);

// for overriding in tests
private static final String DEFAULT_ENDPOINT = System.getProperty(
"ingest.geoip.downloader.endpoint.default",
"https://geoip.elastic.co/v1/database"
);
public static final Setting<String> ENDPOINT_SETTING = Setting.simpleString(
"ingest.geoip.downloader.endpoint",
"https://geoip.elastic.co/v1/database",
DEFAULT_ENDPOINT,
Property.NodeScope
);

Expand Down Expand Up @@ -258,6 +264,7 @@ void runDownloader() {
try {
updateDatabases();
} catch (Exception e) {
stats = stats.failedDownload();
logger.error("exception during geoip databases update", e);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.OriginSettingClient;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -29,6 +31,7 @@
import org.elasticsearch.persistent.PersistentTasksService;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.RemoteTransportException;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -128,14 +131,18 @@ public void clusterChanged(ClusterChangedEvent event) {
// wait for state recovered
return;
}
// bootstrap downloader after first cluster start

DiscoveryNode masterNode = event.state().nodes().getMasterNode();
if (masterNode == null || masterNode.getVersion().before(Version.V_7_14_0)) {
// wait for master to be upgraded so it understands geoip task
return;
}

clusterService.removeListener(this);
if (event.localNodeMaster()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this if check is safe here. Now any node will attempt to start the downloader task if it hasn't started all ready (the start task call is executed on elected master node). This addresses the problem that we was reported, since each time an upgraded node joins the cluster, the geoip downloader task is attempted to be started. So it is unlikely that geoip downloader doesn't exist if cluster has been upgraded.

if (ENABLED_SETTING.get(event.state().getMetadata().settings(), settings)) {
startTask(() -> clusterService.addListener(this));
} else {
stopTask(() -> clusterService.addListener(this));
}
if (ENABLED_SETTING.get(event.state().getMetadata().settings(), settings)) {
startTask(() -> clusterService.addListener(this));
} else {
stopTask(() -> clusterService.addListener(this));
}
}

Expand All @@ -144,8 +151,9 @@ private void startTask(Runnable onFailure) {
GEOIP_DOWNLOADER,
GEOIP_DOWNLOADER,
new GeoIpTaskParams(),
ActionListener.wrap(r -> {}, e -> {
if (e instanceof ResourceAlreadyExistsException == false) {
ActionListener.wrap(r -> logger.debug("Started geoip downloader task"), e -> {
Throwable t = e instanceof RemoteTransportException ? e.getCause() : e;
if (t instanceof ResourceAlreadyExistsException == false) {
logger.error("failed to create geoip downloader task", e);
onFailure.run();
}
Expand All @@ -154,18 +162,23 @@ private void startTask(Runnable onFailure) {
}

private void stopTask(Runnable onFailure) {
ActionListener<PersistentTasksCustomMetadata.PersistentTask<?>> listener = ActionListener.wrap(r -> {}, e -> {
if (e instanceof ResourceNotFoundException == false) {
logger.error("failed to remove geoip downloader task", e);
onFailure.run();
ActionListener<PersistentTasksCustomMetadata.PersistentTask<?>> listener = ActionListener.wrap(
r -> logger.debug("Stopped geoip downloader task"),
e -> {
Throwable t = e instanceof RemoteTransportException ? e.getCause() : e;
if (t instanceof ResourceNotFoundException == false) {
logger.error("failed to remove geoip downloader task", e);
onFailure.run();
}
}
});
);
persistentTasksService.sendRemoveRequest(
GEOIP_DOWNLOADER,
ActionListener.runAfter(
listener,
() -> client.admin().indices().prepareDelete(DATABASES_INDEX).execute(ActionListener.wrap(rr -> {}, e -> {
if (e instanceof ResourceNotFoundException == false) {
Throwable t = e instanceof RemoteTransportException ? e.getCause() : e;
if (t instanceof ResourceNotFoundException == false) {
logger.warn("failed to remove " + DATABASES_INDEX, e);
}
}))
Expand Down
7 changes: 7 additions & 0 deletions x-pack/qa/rolling-upgrade/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ BuildParams.bwcVersions.withWireCompatible { bwcVersion, baseName ->
versions = [oldVersion, project.version]
numberOfNodes = 3

systemProperty 'ingest.geoip.downloader.enabled.default', 'true'
//we don't want to hit real service from each test
systemProperty 'ingest.geoip.downloader.endpoint.default', 'http://invalid.endpoint'
if (bwcVersion.onOrAfter('7.14.0')) {
setting 'ingest.geoip.downloader.endpoint', 'http://invalid.endpoint'
}

setting 'repositories.url.allowed_urls', 'http://snapshot.test*'
setting 'path.repo', "['${buildDir}/cluster/shared/repo/${baseName}', '${searchableSnapshotRepository}']"
setting 'xpack.license.self_generated.type', 'trial'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.upgrades;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.hamcrest.Matchers;

import java.nio.charset.StandardCharsets;

public class GeoIpUpgradeIT extends AbstractUpgradeTestCase {

public void testGeoIpDownloader() throws Exception {
if (CLUSTER_TYPE == ClusterType.UPGRADED) {
assertBusy(() -> {
Response response = client().performRequest(new Request("GET", "_cat/tasks"));
String tasks = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
assertThat(tasks, Matchers.containsString("geoip-downloader"));
});
assertBusy(() -> {
Response response = client().performRequest(new Request("GET", "_ingest/geoip/stats"));
String tasks = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
assertThat(tasks, Matchers.containsString("failed_downloads\":1"));
});
}
}
}