Skip to content
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: add migrate anomalies assistant #36643

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7effc73
ML: add migrate anomalies assistant
benwtrent Dec 5, 2018
d578d1b
adjusting failure handling for reindex
benwtrent Dec 14, 2018
a74bcfe
Fixing request and tests
benwtrent Dec 14, 2018
9a43a54
Adding tests to blacklist
benwtrent Dec 14, 2018
b3fccc6
adjusting test
benwtrent Dec 14, 2018
07ed2a1
test fix: posting data directly to the job instead of relying on data…
benwtrent Dec 17, 2018
bf9e223
adjusting API usage
benwtrent Dec 17, 2018
4a92d35
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 2, 2019
064ac69
adding Todos and adjusting endpoint
benwtrent Jan 2, 2019
ba0df3a
Adding types to reindexRequest
benwtrent Jan 2, 2019
a4c5e86
removing unreliable "live" data test
benwtrent Jan 2, 2019
28576fd
adding index refresh to test
benwtrent Jan 3, 2019
8e9451e
adding index refresh to test
benwtrent Jan 3, 2019
cb9607d
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 3, 2019
1d6c66c
adding index refresh to yaml test
benwtrent Jan 3, 2019
fa56850
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 4, 2019
b341424
fixing bad exists call
benwtrent Jan 4, 2019
3f47ce0
removing todo
benwtrent Jan 7, 2019
5c1683c
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 7, 2019
02603ef
Addressing remove comments
benwtrent Jan 7, 2019
4e926f9
Adjusting rest endpoint name
benwtrent Jan 7, 2019
1661c45
making service have its own logger
benwtrent Jan 7, 2019
cd9c747
adjusting validity check for newindex names
benwtrent Jan 7, 2019
1594b49
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 8, 2019
0650294
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 8, 2019
674463c
fixing typos
benwtrent Jan 8, 2019
c4cf94f
Merge branch 'master' into feature/ml-migrate-anomalies-assistent
benwtrent Jan 9, 2019
2d67895
fixing renaming
benwtrent Jan 9, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.elasticsearch.xpack.core.ml.action.PutDatafeedAction;
import org.elasticsearch.xpack.core.ml.action.PutFilterAction;
import org.elasticsearch.xpack.core.ml.action.PutJobAction;
import org.elasticsearch.xpack.core.ml.action.MlUpgradeAction;
import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction;
import org.elasticsearch.xpack.core.ml.action.StartDatafeedAction;
import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction;
Expand Down Expand Up @@ -289,6 +290,7 @@ public List<Action<? extends ActionResponse>> getClientActions() {
PostCalendarEventsAction.INSTANCE,
PersistJobAction.INSTANCE,
FindFileStructureAction.INSTANCE,
MlUpgradeAction.INSTANCE,
// security
ClearRealmCacheAction.INSTANCE,
ClearRolesCacheAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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.action;

import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequestBuilder;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;


public class MlUpgradeAction extends Action<AcknowledgedResponse> {
public static final MlUpgradeAction INSTANCE = new MlUpgradeAction();
public static final String NAME = "cluster:admin/xpack/ml/upgrade";

private MlUpgradeAction() {
super(NAME);
}

@Override
public AcknowledgedResponse newResponse() {
return new AcknowledgedResponse();
}

public static class Request extends MasterNodeReadRequest<Request> implements ToXContentObject {

private static final ParseField REINDEX_BATCH_SIZE = new ParseField("reindex_batch_size");

public static ObjectParser<Request, Void> PARSER = new ObjectParser<>("ml_results_upgrade", true, Request::new);
static {
PARSER.declareInt(Request::setReindexBatchSize, REINDEX_BATCH_SIZE);
}

static final String INDEX = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*";
private int reindexBatchSize = 1000;

/**
* Should this task store its result?
*/
private boolean shouldStoreResult;

// for serialization
public Request() {
}

public Request(StreamInput in) throws IOException {
super(in);
reindexBatchSize = in.readInt();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeInt(reindexBatchSize);
}

public String[] indices() {
return new String[]{INDEX};
}

public IndicesOptions indicesOptions() {
return IndicesOptions.strictExpandOpenAndForbidClosed();
}

/**
* Should this task store its result after it has finished?
*/
public Request setShouldStoreResult(boolean shouldStoreResult) {
this.shouldStoreResult = shouldStoreResult;
return this;
}

@Override
public boolean getShouldStoreResult() {
return shouldStoreResult;
}

public Request setReindexBatchSize(int reindexBatchSize) {
this.reindexBatchSize = reindexBatchSize;
return this;
}

public int getReindexBatchSize() {
return reindexBatchSize;
}

@Override
public ActionRequestValidationException validate() {
if (reindexBatchSize <= 0) {

Choose a reason for hiding this comment

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

if we test the lower limit, we should also test the upper limit which is default 10000 but can be changed (index.max_result_window)

(That's one reason why I am thinking it's better not to expose the batch size but rather use a good default)

ActionRequestValidationException validationException = new ActionRequestValidationException();
validationException.addValidationError("["+ REINDEX_BATCH_SIZE.getPreferredName()+"] must be greater than 0.");
return validationException;
}
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Request request = (Request) o;
return Objects.equals(reindexBatchSize, request.reindexBatchSize);
}

@Override
public int hashCode() {
return Objects.hash(reindexBatchSize);
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, getDescription(), parentTaskId, headers) {
Copy link
Member

Choose a reason for hiding this comment

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

I can't see where getDescription is overridden? The default implementation in TaskAwareRequest returns an empty string

@Override
public boolean shouldCancelChildrenOnCancellation() {
return true;
}
};
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(REINDEX_BATCH_SIZE.getPreferredName(), reindexBatchSize);
builder.endObject();
return builder;
}
}

public static class RequestBuilder extends MasterNodeReadOperationRequestBuilder<Request, AcknowledgedResponse, RequestBuilder> {

public RequestBuilder(ElasticsearchClient client) {
super(client, INSTANCE, new Request());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.action;

import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase;


public class MlUpgradeRequestTests extends AbstractWireSerializingTestCase<MlUpgradeAction.Request> {

@Override
protected MlUpgradeAction.Request createTestInstance() {
MlUpgradeAction.Request request = new MlUpgradeAction.Request();
if (randomBoolean()) {
request.setReindexBatchSize(randomIntBetween(1, 10_000));
}
return request;
}

@Override
protected Writeable.Reader<MlUpgradeAction.Request> instanceReader() {
return MlUpgradeAction.Request::new;
}

}
4 changes: 3 additions & 1 deletion x-pack/plugin/ml/qa/ml-with-security/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ integTestRunner {
'ml/validate/Test job config that is invalid only because of the job ID',
'ml/validate_detector/Test invalid detector',
'ml/delete_forecast/Test delete on _all forecasts not allow no forecasts',
'ml/delete_forecast/Test delete forecast on missing forecast'
'ml/delete_forecast/Test delete forecast on missing forecast',
'ml/results_indices_upgrade/Upgrade results when there is nothing to upgrade',
benwtrent marked this conversation as resolved.
Show resolved Hide resolved
'ml/results_indices_upgrade/Upgrade results when there is nothing to upgrade not waiting for results'
].join(',')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.ReindexPlugin;
import org.elasticsearch.persistent.PersistentTaskParams;
import org.elasticsearch.persistent.PersistentTaskState;
import org.elasticsearch.plugins.Plugin;
Expand Down Expand Up @@ -120,7 +121,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {

@Override
protected Collection<Class<? extends Plugin>> transportClientPlugins() {
return Arrays.asList(XPackClientPlugin.class, Netty4Plugin.class);
return Arrays.asList(XPackClientPlugin.class, Netty4Plugin.class, ReindexPlugin.class);
}

@Override
Expand Down
Loading