diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index 17df06dbf4b84..eb232d47010ba 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -46,6 +46,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import static org.elasticsearch.action.ValidateActions.addValidationError; @@ -525,4 +526,38 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(updateAllTypes); waitForActiveShards.writeTo(out); } + + @Override + public boolean equals(Object obj) { + if (getClass() != obj.getClass()) return false; + CreateIndexRequest other = (CreateIndexRequest) obj; + return Objects.equals(cause, other.cause) + && Objects.equals(index, other.index) + && Objects.equals(settings, other.settings) + && Objects.equals(timeout, other.timeout) + && Objects.equals(mappings, other.mappings) + && Objects.equals(customs, other.customs) + && Objects.equals(aliases, other.aliases) + && Objects.equals(updateAllTypes, other.updateAllTypes) + && Objects.equals(waitForActiveShards, other.waitForActiveShards); + } + + @Override + public int hashCode() { + return Objects.hash(cause, index, settings, timeout, mappings, customs, aliases, updateAllTypes, waitForActiveShards); + } + + @Override + public String toString() { + return "CreateIndex[" + + "cause=" + cause + + ",index=" + index + + ",settings=" + settings + + ",timeout=" + timeout + + ",mappings=" + mappings + + ",customs=" + customs + + ",aliases=" + aliases + + ",updateAllTypes=" + updateAllTypes + + ",waitForActiveShards=" + waitForActiveShards + "]"; + } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequest.java index bb74f2c7947db..a200789831ba9 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequest.java @@ -38,8 +38,8 @@ */ public class MigrateIndexRequest extends AcknowledgedRequest implements IndicesRequest { private String sourceIndex; - private Script script; private CreateIndexRequest createIndexRequest; + private Script script; /** * Build an empty request. @@ -75,18 +75,20 @@ public ActionRequestValidationException validate() { @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); + readTimeout(in); sourceIndex = in.readString(); - script = in.readOptionalWriteable(Script::new); createIndexRequest = new CreateIndexRequest(); createIndexRequest.readFrom(in); + script = in.readOptionalWriteable(Script::new); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); + writeTimeout(out); out.writeString(sourceIndex); - out.writeOptionalWriteable(script); createIndexRequest.writeTo(out); + out.writeOptionalWriteable(script); } @Override @@ -145,4 +147,32 @@ public String[] indices() { public IndicesOptions indicesOptions() { return IndicesOptions.strictSingleIndexNoExpandForbidClosed(); } + + @Override + public String toString() { + return "MigrateIndex[" + + "source=" + sourceIndex + + ",create=" + createIndexRequest + + ",script=" + script + + ",timeout=" + timeout + + ",masterNodeTimeout=" + masterNodeTimeout + + ",parentTask=" + getParentTask() + "]"; + } + + @Override + public boolean equals(Object obj) { + if (getClass() != obj.getClass()) return false; + MigrateIndexRequest other = (MigrateIndexRequest) obj; + return Objects.equals(sourceIndex, other.sourceIndex) + && Objects.equals(createIndexRequest, other.createIndexRequest) + && Objects.equals(script, other.script) + && Objects.equals(timeout, other.timeout) + && Objects.equals(masterNodeTimeout, other.masterNodeTimeout) + && Objects.equals(getParentTask(), other.getParentTask()); + } + + @Override + public int hashCode() { + return Objects.hash(sourceIndex, script, createIndexRequest, timeout, masterNodeTimeout, getParentTask()); + } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponse.java b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponse.java index b6d32e771ab2a..4c75befe0526b 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponse.java @@ -20,12 +20,14 @@ package org.elasticsearch.action.admin.indices.migrate; import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; +import java.util.Objects; public class MigrateIndexResponse extends AcknowledgedResponse implements ToXContent { private boolean noop; // NOCOMMIT we can do better. Probably like reindex, make the task status useful and capture that in the response. @@ -65,4 +67,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("noop", isNoop()); return builder; } + + @Override + public String toString() { + return Strings.toString(this, true); + } + + @Override + public boolean equals(Object obj) { + if (getClass() != obj.getClass()) return false; + MigrateIndexResponse other = (MigrateIndexResponse) obj; + return Objects.equals(isAcknowledged(), other.isAcknowledged()) + && Objects.equals(noop, other.noop); + } + + @Override + public int hashCode() { + return Objects.hash(isAcknowledged(), noop); + } + } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexTask.java b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexTask.java index 26f1549faea9c..627feec1c5e23 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexTask.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexTask.java @@ -23,6 +23,9 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; +/** + * Task subclass that supports getting information about the currently running migration. + */ public class MigrateIndexTask extends Task { private volatile Operation operation; diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequestTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequestTests.java new file mode 100644 index 0000000000000..70209b6983b4d --- /dev/null +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexRequestTests.java @@ -0,0 +1,123 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.indices.migrate; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptService.ScriptType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.Matchers.containsString; + +public class MigrateIndexRequestTests extends ESTestCase { + public void testRoundTripThroughTransport() throws IOException { + MigrateIndexRequest original = randomRequest(); + try (BytesStreamOutput out = new BytesStreamOutput()) { + original.writeTo(out); + try (StreamInput in = out.bytes().streamInput()) { + MigrateIndexRequest read = new MigrateIndexRequest(); + read.readFrom(in); + assertEquals(original, read); + } + } + } + + public void testToStringIsSane() { + String string = randomRequest().toString(); + assertThat(string, containsString("MigrateIndex[")); + assertThat(string, containsString("source=")); + assertThat(string, containsString("create=")); + assertThat(string, containsString("script=")); + assertThat(string, containsString("timeout=")); + assertThat(string, containsString("masterNodeTimeout=")); + assertThat(string, containsString("parentTask=")); + } + + private MigrateIndexRequest randomRequest() { + MigrateIndexRequest request = new MigrateIndexRequest(randomAsciiOfLength(5), randomAsciiOfLength(5)); + int settingsCount = between(0, 5); + if (settingsCount > 0) { + Settings.Builder settings = Settings.builder(); + for (int i = 0; i < settingsCount; i++) { + settings.put(randomAsciiOfLength(5), randomAsciiOfLength(5)); + } + request.getCreateIndexRequest().settings(settings); + } + int typesCount = between(0, 5); + if (typesCount > 0) { + for (int i = 0; i < typesCount; i++) { + Map mapping = new HashMap<>(); + int mappingSize = between(0, 5); + for (int p = 0; p < mappingSize; p++) { + mapping.put(randomAsciiOfLength(5), randomAsciiOfLength(5)); + } + request.getCreateIndexRequest().mapping(randomAsciiOfLength(5), mapping); + } + } + int aliasCount = between(0, 5); + for (int i = 0; i < aliasCount; i++) { + request.getCreateIndexRequest().alias(randomAlias()); + } + if (randomBoolean()) { + request.setScript(randomScript()); + } + if (randomBoolean()) { + request.timeout(randomPositiveTimeValue()); + } + if (randomBoolean()) { + request.masterNodeTimeout(randomPositiveTimeValue()); + } + if (randomBoolean()) { + request.setParentTask(randomAsciiOfLength(5), randomLong()); + } + return request; + } + + private Alias randomAlias() { + Alias alias = new Alias(randomAsciiOfLength(5)); + if (randomBoolean()) { + // We don't need a valid query for this test which is nice because random queries are hard + alias.filter(randomAsciiOfLength(10)); + } + if (randomBoolean()) { + alias.indexRouting(randomAsciiOfLength(5)); + } + if (randomBoolean()) { + alias.searchRouting(randomAsciiOfLength(5)); + } + return alias; + } + + private Script randomScript() { + int paramsLength = between(0, 10); + Map params = new HashMap<>(paramsLength); + for (int i = 0; i < paramsLength; i++) { + params.put(randomAsciiOfLength(5), randomAsciiOfLength(5)); + } + return new Script(randomAsciiOfLength(5), randomFrom(ScriptType.values()), randomAsciiOfLength(5), params); + } +} diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponseTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponseTests.java new file mode 100644 index 0000000000000..31d39bb081977 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/migrate/MigrateIndexResponseTests.java @@ -0,0 +1,52 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.indices.migrate; + +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +import static org.hamcrest.Matchers.containsString; + +public class MigrateIndexResponseTests extends ESTestCase { + public void testRoundTripThroughTransport() throws IOException { + MigrateIndexResponse original = randomResponse(); + try (BytesStreamOutput out = new BytesStreamOutput()) { + original.writeTo(out); + try (StreamInput in = out.bytes().streamInput()) { + MigrateIndexResponse read = new MigrateIndexResponse(); + read.readFrom(in); + assertEquals(original, read); + } + } + } + + public void testToStringIsSane() { + String string = randomResponse().toString(); + assertThat(string, containsString("\"acknowledged\":")); + assertThat(string, containsString("\"noop\":")); + } + + private MigrateIndexResponse randomResponse() { + return new MigrateIndexResponse(randomBoolean(), randomBoolean()); + } +}