-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
[ML] Snapshot ml configs before migrating #36645
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,14 @@ | |
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.DocWriteResponse; | ||
import org.elasticsearch.action.bulk.BulkItemResponse; | ||
import org.elasticsearch.action.bulk.BulkRequestBuilder; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.index.IndexRequestBuilder; | ||
import org.elasticsearch.action.index.IndexResponse; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterState; | ||
|
@@ -31,12 +35,14 @@ | |
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; | ||
import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings; | ||
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; | ||
import org.elasticsearch.xpack.ml.datafeed.persistence.DatafeedConfigProvider; | ||
import org.elasticsearch.xpack.ml.job.persistence.JobConfigProvider; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
|
@@ -90,12 +96,14 @@ public class MlConfigMigrator { | |
private final MlConfigMigrationEligibilityCheck migrationEligibilityCheck; | ||
|
||
private final AtomicBoolean migrationInProgress; | ||
private final AtomicBoolean firstTime; | ||
|
||
public MlConfigMigrator(Settings settings, Client client, ClusterService clusterService) { | ||
this.client = Objects.requireNonNull(client); | ||
this.clusterService = Objects.requireNonNull(clusterService); | ||
this.migrationEligibilityCheck = new MlConfigMigrationEligibilityCheck(settings, clusterService); | ||
this.migrationInProgress = new AtomicBoolean(false); | ||
this.firstTime = new AtomicBoolean(true); | ||
} | ||
|
||
/** | ||
|
@@ -127,9 +135,6 @@ public void migrateConfigsWithoutTasks(ClusterState clusterState, ActionListener | |
return; | ||
} | ||
|
||
|
||
logger.debug("migrating ml configurations"); | ||
|
||
Collection<DatafeedConfig> stoppedDatafeeds = stoppedDatafeedConfigs(clusterState); | ||
Map<String, Job> eligibleJobs = nonDeletingJobs(closedJobConfigs(clusterState)).stream() | ||
.map(MlConfigMigrator::updateJobForMigration) | ||
|
@@ -148,19 +153,36 @@ public void migrateConfigsWithoutTasks(ClusterState clusterState, ActionListener | |
} | ||
); | ||
|
||
if (firstTime.get()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the life time of this class? This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class has the same life time as the node and only runs on the master node. If it was a single node cluster or the master node never changed then this check would be sufficient to ensure the doc was never written more than once. The master node does change however, in which case the new master will have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, I did not know the class had the lifetime of the node :). |
||
snapshotMlMeta(MlMetadata.getMlMetadata(clusterState), ActionListener.wrap( | ||
response -> { | ||
firstTime.set(false); | ||
migrate(jobsAndDatafeedsToMigrate, unMarkMigrationInProgress); | ||
}, | ||
unMarkMigrationInProgress::onFailure | ||
)); | ||
return; | ||
} | ||
|
||
migrate(jobsAndDatafeedsToMigrate, unMarkMigrationInProgress); | ||
} | ||
|
||
private void migrate(JobsAndDatafeeds jobsAndDatafeedsToMigrate, ActionListener<Boolean> listener) { | ||
if (jobsAndDatafeedsToMigrate.totalCount() == 0) { | ||
unMarkMigrationInProgress.onResponse(Boolean.FALSE); | ||
listener.onResponse(Boolean.FALSE); | ||
return; | ||
} | ||
|
||
logger.debug("migrating ml configurations"); | ||
|
||
writeConfigToIndex(jobsAndDatafeedsToMigrate.datafeedConfigs, jobsAndDatafeedsToMigrate.jobs, ActionListener.wrap( | ||
failedDocumentIds -> { | ||
List<String> successfulJobWrites = filterFailedJobConfigWrites(failedDocumentIds, jobsAndDatafeedsToMigrate.jobs); | ||
List<String> successfulDatafeedWrites = | ||
filterFailedDatafeedConfigWrites(failedDocumentIds, jobsAndDatafeedsToMigrate.datafeedConfigs); | ||
removeFromClusterState(successfulJobWrites, successfulDatafeedWrites, unMarkMigrationInProgress); | ||
removeFromClusterState(successfulJobWrites, successfulDatafeedWrites, listener); | ||
}, | ||
unMarkMigrationInProgress::onFailure | ||
listener::onFailure | ||
)); | ||
} | ||
|
||
|
@@ -300,6 +322,45 @@ private IndexRequest indexRequest(ToXContentObject source, String documentId, To | |
return indexRequest; | ||
} | ||
|
||
|
||
// public for testing | ||
public void snapshotMlMeta(MlMetadata mlMetadata, ActionListener<Boolean> listener) { | ||
|
||
if (mlMetadata.getJobs().isEmpty() && mlMetadata.getDatafeeds().isEmpty()) { | ||
listener.onResponse(Boolean.TRUE); | ||
return; | ||
} | ||
|
||
logger.debug("taking a snapshot of mlmetadata"); | ||
String documentId = "ml-config"; | ||
IndexRequestBuilder indexRequest = client.prepareIndex(AnomalyDetectorsIndex.jobStateIndexName(), | ||
ElasticsearchMappings.DOC_TYPE, documentId) | ||
.setOpType(DocWriteRequest.OpType.CREATE); | ||
|
||
ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")); | ||
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { | ||
builder.startObject(); | ||
mlMetadata.toXContent(builder, params); | ||
builder.endObject(); | ||
|
||
indexRequest.setSource(builder); | ||
} catch (IOException e) { | ||
logger.error("failed to serialise mlmetadata", e); | ||
listener.onFailure(e); | ||
return; | ||
} | ||
|
||
executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, indexRequest.request(), | ||
ActionListener.<IndexResponse>wrap( | ||
indexResponse -> { | ||
listener.onResponse(indexResponse.getResult() == DocWriteResponse.Result.CREATED); | ||
}, | ||
listener::onFailure), | ||
client::index | ||
); | ||
} | ||
|
||
|
||
public static Job updateJobForMigration(Job job) { | ||
Job.Builder builder = new Job.Builder(job); | ||
Map<String, Object> custom = job.getCustomSettings() == null ? new HashMap<>() : new HashMap<>(job.getCustomSettings()); | ||
|
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.
The rolling upgrade tests occasionally fail because the migration does not get triggered. I suspect the cause could have been that the
onMaster
method ofLocalNodeMasterListener
which enables migration is called after the vital cluster state change event caused by master election. I changed this so it is no longer aLocalNodeMasterListener
instead it ignores events if it is not the master.