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

HLRC: migration api - upgrade #34898

Merged
merged 14 commits into from
Nov 13, 2018
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

import org.elasticsearch.client.migration.IndexUpgradeInfoRequest;
import org.elasticsearch.client.migration.IndexUpgradeInfoResponse;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.migration.IndexUpgradeSubmissionResponse;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.client.migration.IndexUpgradeRequest;


import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -52,4 +57,20 @@ public IndexUpgradeInfoResponse getAssistance(IndexUpgradeInfoRequest request, R
return restHighLevelClient.performRequestAndParseEntity(request, MigrationRequestConverters::getMigrationAssistance, options,
IndexUpgradeInfoResponse::fromXContent, Collections.emptySet());
}

public BulkByScrollResponse upgrade(IndexUpgradeRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, MigrationRequestConverters::migrate, options,
BulkByScrollResponse::fromXContent, Collections.emptySet());
}

public IndexUpgradeSubmissionResponse submitUpgradeTask(IndexUpgradeRequest request, RequestOptions options) throws IOException {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as discussed, I want to refactor that later to return TaskSubmissionResponse and use across other methods where wait_for_completion=false was used

Copy link
Contributor

Choose a reason for hiding this comment

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

++.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is another PR where we discussed if it wouldn't be better to return just TaskID:
https://github.com/elastic/elasticsearch/pull/35202/files
I might get back to this one and refactor to this TaskID

return restHighLevelClient.performRequestAndParseEntity(request, MigrationRequestConverters::submitMigrateTask, options,
IndexUpgradeSubmissionResponse::fromXContent, Collections.emptySet());
}


Copy link
Contributor

Choose a reason for hiding this comment

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

spaces

public void upgradeAsync(IndexUpgradeRequest request, RequestOptions options, ActionListener<BulkByScrollResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, MigrationRequestConverters::migrate, options,
BulkByScrollResponse::fromXContent, listener, Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.elasticsearch.client.migration.IndexUpgradeRequest;
import org.elasticsearch.client.migration.IndexUpgradeInfoRequest;

final class MigrationRequestConverters {
Expand All @@ -36,4 +38,25 @@ static Request getMigrationAssistance(IndexUpgradeInfoRequest indexUpgradeInfoRe
parameters.withIndicesOptions(indexUpgradeInfoRequest.indicesOptions());
return request;
}

static Request migrate(IndexUpgradeRequest indexUpgradeRequest) {
return prepareMigrateRequest(indexUpgradeRequest, true);
}

static Request submitMigrateTask(IndexUpgradeRequest indexUpgradeRequest) {
return prepareMigrateRequest(indexUpgradeRequest, false);
}

private static Request prepareMigrateRequest(IndexUpgradeRequest indexUpgradeRequest, boolean waitForCompletion) {
RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack/migration/upgrade")
Copy link
Contributor

Choose a reason for hiding this comment

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

you can comma separate these instead... i know the likelihood of us not using / is low, but its best to not have them in this.

.addCommaSeparatedPathParts(indexUpgradeRequest.indices());
String endpoint = endpointBuilder.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);

RequestConverters.Params params = new RequestConverters.Params(request);
params.withWaitForCompletion(waitForCompletion);

return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,7 @@ Params withDetailed(boolean detailed) {
}

Params withWaitForCompletion(boolean waitForCompletion) {
if (waitForCompletion) {
return putParam("wait_for_completion", Boolean.TRUE.toString());
}
return this;
return putParam("wait_for_completion", Boolean.toString(waitForCompletion));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

that need to be to set false and pass through to the server.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, this is due to the fact that wait_for_completion defaults to true in this API, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly. See RestIndexUpgradeAction:60
if (request.paramAsBoolean("wait_for_completion", true)) {

}

Params withNodes(String[] nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import java.util.Arrays;
import java.util.Objects;

/**
* A request for retrieving upgrade information
* Part of Migration API
*/
public class IndexUpgradeInfoRequest extends TimedRequest implements IndicesRequest.Replaceable {

private String[] indices = Strings.EMPTY_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

/**
* Response object that contains information about indices to be upgraded
*/
public class IndexUpgradeInfoResponse {

private static final ParseField INDICES = new ParseField("indices");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.client.migration;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

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

/**
* A request for performing Upgrade on Index
* Part of Migration API
*/
public class IndexUpgradeRequest extends MasterNodeReadRequest<IndexUpgradeRequest> implements IndicesRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

some javadoc here pls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be just a TimedRequest instead of a MasterNodeReadRequest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, great suggestion


Copy link
Contributor

Choose a reason for hiding this comment

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

remove empty line

private String[] indices ;
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, true, true, true);

public IndexUpgradeRequest(String index) {
indices = new String[]{index};
}

public IndexUpgradeRequest(StreamInput in) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, removing

super(in);
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, removing

super.writeTo(out);
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
}

@Override
public String[] indices() {
return indices;
}


@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}

public void indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
}

@Override
public ActionRequestValidationException validate() {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can remove this once we make it a TimedRequest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return null;
}

@Override
public void readFrom(StreamInput in) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, removed

throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IndexUpgradeRequest request = (IndexUpgradeRequest) o;
return Arrays.equals(indices, request.indices) &&
Objects.equals(indicesOptions.toString(), request.indicesOptions.toString());
}

@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(indices), indicesOptions.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.client.migration;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.tasks.TaskId;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a TaskId? Can we just use a string here instead? Id love to not pull things in from server here if possible

Copy link
Contributor Author

@pgomulka pgomulka Nov 6, 2018

Choose a reason for hiding this comment

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

true, that is not ideal that it uses a class from server, but maybe we could copy or make our own TaskId class in HLRC?
I am a big fan of strong typed apis, and that abstracts nicely the concept of task identifier. I could refactor this class now, but since this is used in reindex and in getTask PRs I would prefer to refactor later

Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO if we always pass it to server as a single string and never need to modify it or make decisions based on some portion of the ID (as i understand it has > 1 bit of information in the string separated by a delimiter), then making it a string is fine by me.

That being said, if it is easier for a user to create a object with those 3 values rather than just passing in a string, Im all for using an object.

I dont know all the uses, but im a fan of keeping it simple. If a user is never going to do anything with those 3 different parts of the string, and they only store it and give it back to us as a single string, it does not make much sense in my mind to create a class representation for the 3 strings. Ofc it might make sense to have this on the server, since those 3 strings are useful as individual pieces :D

All that being said, im ok with duplicating a TaskId class in HLRC. Im not too picky :)

Copy link
Contributor

Choose a reason for hiding this comment

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

What @hub-cap said. I too am unclear on the uses of TaskIDs and whether clients would need to break out component parts. I suppose even if they don't there's a potential upside to a typed object just because we could make them have private constructors so a client would only ever be able to give us IDs we'd previously given them, rather than any arbitrary string of their choosing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Im still leaning toward string. we have to have a way to deserialize bits from the wire into a TaskId, so there will be a way for a user to create their own TaskId, if they want to go that far.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am convinced too. Will refactor this to use TaskSubmissionResponse with taskId field of type String

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. In retrospect, typed TaskIDs might only be useful if the world ran on Java but Java apps will typically hand off control to a Javascript front-end where it would become untyped strings in subsequent comms.


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

/**
* Response object that contains the taskID from submitted IndexUpgradeRequest
*/
public class IndexUpgradeSubmissionResponse {

private static final ParseField TASK = new ParseField("task");

public static final ConstructingObjectParser<IndexUpgradeSubmissionResponse, Void> PARSER = new ConstructingObjectParser<>(
"upgrade_submission_response",
true, a-> new IndexUpgradeSubmissionResponse( (TaskId) a[0]));

static {
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), TaskId.parser(), TASK, ObjectParser.ValueType.STRING);
}

public static IndexUpgradeSubmissionResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

private final TaskId task;

IndexUpgradeSubmissionResponse(@Nullable TaskId task) {
this.task = task;
}

/**
* Get the task id
* @return the id of the upgrade task.
*/
public TaskId getTask() {
return task;
}

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

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

IndexUpgradeSubmissionResponse that = (IndexUpgradeSubmissionResponse) other;
return Objects.equals(task, that.task);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,73 @@

package org.elasticsearch.client;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.client.migration.IndexUpgradeInfoRequest;
import org.elasticsearch.client.migration.IndexUpgradeInfoResponse;
import org.elasticsearch.client.migration.IndexUpgradeRequest;
import org.elasticsearch.client.migration.IndexUpgradeSubmissionResponse;
import org.elasticsearch.common.settings.Settings;

import java.io.IOException;
import java.util.function.BooleanSupplier;

import static org.hamcrest.Matchers.containsString;

public class MigrationIT extends ESRestHighLevelClientTestCase {

public void testGetAssistance() throws IOException {
RestHighLevelClient client = highLevelClient();
{
IndexUpgradeInfoResponse response = client.migration().getAssistance(new IndexUpgradeInfoRequest(), RequestOptions.DEFAULT);
IndexUpgradeInfoResponse response = highLevelClient().migration()
.getAssistance(new IndexUpgradeInfoRequest(), RequestOptions.DEFAULT);
assertEquals(0, response.getActions().size());
}
{
client.indices().create(new CreateIndexRequest("test"), RequestOptions.DEFAULT);
IndexUpgradeInfoResponse response = client.migration().getAssistance(
createIndex("test", Settings.EMPTY);
IndexUpgradeInfoResponse response = highLevelClient().migration().getAssistance(
new IndexUpgradeInfoRequest("test"), RequestOptions.DEFAULT);
assertEquals(0, response.getActions().size());
}
}

public void testUpgradeWhenIndexCannotBeUpgraded() throws IOException {
createIndex("test", Settings.EMPTY);

ThrowingRunnable execute = () -> execute(new IndexUpgradeRequest("test"),
highLevelClient().migration()::upgrade,
highLevelClient().migration()::upgradeAsync);

ElasticsearchStatusException responseException = expectThrows(ElasticsearchStatusException.class, execute);

assertThat(responseException.getDetailedMessage(), containsString("cannot be upgraded"));
}

public void testUpgradeWithTaskApi() throws IOException, InterruptedException {
createIndex("test", Settings.EMPTY);

IndexUpgradeRequest request = new IndexUpgradeRequest("test");

IndexUpgradeSubmissionResponse upgrade = highLevelClient().migration()
.submitUpgradeTask(request, RequestOptions.DEFAULT);

assertNotNull(upgrade.getTask());

BooleanSupplier hasUpgradeCompleted = checkCompletionStatus(upgrade);
awaitBusy(hasUpgradeCompleted);
}

/**
* Using low-level api as high-level-rest-client's getTaskById work is in progress.
* TODO revisit once that work is finished
*/
private BooleanSupplier checkCompletionStatus(IndexUpgradeSubmissionResponse upgrade) {
return () -> {
try {
Response response = client().performRequest(new Request("GET", "/_tasks/" + upgrade.getTask().toString()));
Copy link
Contributor

Choose a reason for hiding this comment

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

pls put a note saying why this is low level rest (the tasks api has not been finished yet etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do- will leave a TODO note to revisit once the high level api for getTaskByID is finished

Copy link
Contributor

Choose a reason for hiding this comment

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

<3

return (boolean) entityAsMap(response).get("completed");
} catch (IOException e) {
fail(e.getMessage());
return false;
}
};
}
}
Loading