-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Changes from 5 commits
a47e432
ff3ddd1
1af129c
b6683d1
bcb8f38
0deb3c1
e63914f
0e67a92
a71d5c8
0c14987
30a1694
37fbf88
77875e4
614e711
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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 { | ||
return restHighLevelClient.performRequestAndParseEntity(request, MigrationRequestConverters::submitMigrateTask, options, | ||
IndexUpgradeSubmissionResponse::fromXContent, Collections.emptySet()); | ||
} | ||
|
||
|
||
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. 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 |
---|---|---|
|
@@ -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 { | ||
|
@@ -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") | ||
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. you can comma separate these instead... i know the likelihood of us not using |
||
.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 |
---|---|---|
|
@@ -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)); | ||
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. that need to be to set false and pass through to the server. 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. Ahh, this is due to the fact that 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. exactly. See RestIndexUpgradeAction:60 |
||
} | ||
|
||
Params withNodes(String[] nodes) { | ||
|
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 { | ||
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. some javadoc here pls 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. done 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. I think this should be just a 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. agree, great suggestion |
||
|
||
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. 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 { | ||
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. Is this used anywhere? 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. nope, removing |
||
super(in); | ||
indices = in.readStringArray(); | ||
indicesOptions = IndicesOptions.readIndicesOptions(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
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. Is this used anywhere? 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. 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() { | ||
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. we can remove this once we make it a 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. done |
||
return null; | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
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. is this used anywhere? 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. 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; | ||
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. 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 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. 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? 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. 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 :) 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 @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. 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. 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. 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. I am convinced too. Will refactor this to use TaskSubmissionResponse with taskId field of type String 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. 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 |
---|---|---|
|
@@ -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())); | ||
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. pls put a note saying why this is low level rest (the tasks api has not been finished yet etc). 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. will do- will leave a TODO note to revisit once the high level api for getTaskByID is finished 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. <3 |
||
return (boolean) entityAsMap(response).get("completed"); | ||
} catch (IOException e) { | ||
fail(e.getMessage()); | ||
return false; | ||
} | ||
}; | ||
} | ||
} |
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.
as discussed, I want to refactor that later to return TaskSubmissionResponse and use across other methods where wait_for_completion=false was used
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.
++.
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.
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