-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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] automatically update deprecated datafeed aggregations #55678
Merged
benwtrent
merged 4 commits into
elastic:master
from
benwtrent:feature/ml-df-auto-update
Apr 28, 2020
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderWireSerializationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.datafeed; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.search.SearchModule; | ||
import org.elasticsearch.search.aggregations.AggregatorFactories; | ||
import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase; | ||
import org.elasticsearch.xpack.core.ml.utils.XContentObjectTransformer; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public class AggProviderWireSerializationTests extends AbstractBWCWireSerializationTestCase<AggProvider> { | ||
|
||
@Override | ||
protected NamedWriteableRegistry writableRegistry() { | ||
SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); | ||
return new NamedWriteableRegistry(searchModule.getNamedWriteables()); | ||
} | ||
|
||
@Override | ||
protected NamedWriteableRegistry getNamedWriteableRegistry() { | ||
return writableRegistry(); | ||
} | ||
|
||
@Override | ||
protected AggProvider createTestInstance() { | ||
return createRandomValidAggProvider(); | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<AggProvider> instanceReader() { | ||
return AggProvider::fromStream; | ||
} | ||
|
||
public static AggProvider createRandomValidAggProvider() { | ||
Map<String, Object> agg = Collections.singletonMap(randomAlphaOfLengthBetween(1, 10), | ||
Collections.singletonMap("avg", Collections.singletonMap("field", randomAlphaOfLengthBetween(1, 10)))); | ||
try { | ||
SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); | ||
AggregatorFactories.Builder aggs = | ||
XContentObjectTransformer.aggregatorTransformer(new NamedXContentRegistry(searchModule.getNamedXContents())) | ||
.fromMap(agg); | ||
return new AggProvider(agg, aggs, null, false); | ||
} catch (IOException ex) { | ||
fail(ex.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected AggProvider mutateInstanceForVersion(AggProvider instance, Version version) { | ||
if (version.onOrBefore(Version.V_8_0_0)) { | ||
return new AggProvider(instance.getAggs(), instance.getParsedAggs(), instance.getParsingException(), false); | ||
} | ||
return instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAutoUpdateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.ClusterChangedEvent; | ||
import org.elasticsearch.cluster.ClusterStateListener; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
public class MlAutoUpdateService implements ClusterStateListener { | ||
private static final Logger logger = LogManager.getLogger(MlAutoUpdateService.class); | ||
|
||
public interface UpdateAction { | ||
boolean minNodeSupported(Version minNodeVersion); | ||
String getName(); | ||
void runUpdate(); | ||
} | ||
|
||
private final List<UpdateAction> updateActions; | ||
private final Set<String> currentlyUpdating; | ||
private final Set<String> completedUpdates; | ||
private final ThreadPool threadPool; | ||
|
||
public MlAutoUpdateService(ThreadPool threadPool, ClusterService clusterService, List<UpdateAction> updateActions) { | ||
clusterService.addListener(this); | ||
benwtrent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.updateActions = updateActions; | ||
this.completedUpdates = ConcurrentHashMap.newKeySet(); | ||
this.currentlyUpdating = ConcurrentHashMap.newKeySet(); | ||
this.threadPool = threadPool; | ||
} | ||
|
||
@Override | ||
public void clusterChanged(ClusterChangedEvent event) { | ||
if (event.localNodeMaster() == false) { | ||
return; | ||
} | ||
|
||
Version minNodeVersion = event.state().getNodes().getMinNodeVersion(); | ||
final List<UpdateAction> toRun = updateActions.stream() | ||
.filter(action -> action.minNodeSupported(minNodeVersion)) | ||
.filter(action -> completedUpdates.contains(action.getName()) == false) | ||
.filter(action -> currentlyUpdating.add(action.getName())) | ||
.collect(Collectors.toList()); | ||
threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( | ||
() -> toRun.forEach(this::runUpdate) | ||
); | ||
} | ||
|
||
private void runUpdate(UpdateAction action) { | ||
try { | ||
logger.debug(() -> new ParameterizedMessage("[{}] starting executing update action", action.getName())); | ||
action.runUpdate(); | ||
this.completedUpdates.add(action.getName()); | ||
logger.debug(() -> new ParameterizedMessage("[{}] succeeded executing update action", action.getName())); | ||
} catch (Exception ex) { | ||
logger.warn(new ParameterizedMessage("[{}] failure executing update action", action.getName()), ex); | ||
} finally { | ||
this.currentlyUpdating.remove(action.getName()); | ||
logger.debug(() -> new ParameterizedMessage("[{}] no longer executing update action", action.getName())); | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
toXContent
for the agg provider should only provide the agg map. This test is to make sure that we appropriate write between nodes of various versions.