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

Add support for indices exists to REST high level client #27384

Merged
merged 31 commits into from
Feb 2, 2018
Merged

Add support for indices exists to REST high level client #27384

merged 31 commits into from
Feb 2, 2018

Conversation

hariso
Copy link
Contributor

@hariso hariso commented Nov 14, 2017

This is obviously work in progress, but I opened the PR to ask a few questions:

  1. The small code contributed is a bit confused about the exact phrase to be used "indices exist" or "index exist".
  2. I couldn't find enough info here, w/r to how should the API behave when we check for existence of multiple existence. For now, I assume that if at least index in the list of indices provided is missing, the response is 404. It might be worth thinking about a response where details about each input index individually are included.
  3. Do we need the async version?
  4. I'll add docs soon.

@karmi
Copy link
Contributor

karmi commented Nov 14, 2017

Hi @hariso, we have found your signature in our records, but it seems like you have signed with a different e-mail than the one used in yout Git commit. Can you please add both of these e-mails into your Github profile (they can be hidden), so we can match your e-mails to your Github profile?

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@hariso hariso changed the title High-level REST client: Support for "indices exist" [WIP] High-level REST client: Support for "indices exist" Nov 14, 2017
@hariso
Copy link
Contributor Author

hariso commented Nov 14, 2017

@karmi Done!

return new IndicesExistsResponse(response.getStatusLine().getStatusCode() == 200);
}

public void existsAsync(IndicesExistsRequest indicesExistsRequest, ActionListener<IndicesExistsResponse> listener, Header... headers) 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.

Async version needed?

@@ -373,12 +377,12 @@ static Request search(SearchRequest searchRequest) throws IOException {

static Request searchScroll(SearchScrollRequest searchScrollRequest) throws IOException {
HttpEntity entity = createEntity(searchScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("GET", "/_search/scroll", Collections.emptyMap(), entity);
return new Request("GET", "/_search/scroll", emptyMap(), entity);
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 can remove this, if needed.

Copy link
Member

Choose a reason for hiding this comment

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

yes please, I commented above about it, before seeing your question ;)

@@ -410,10 +414,16 @@ static String endpoint(String... parts) {
* @return the {@link ContentType}
*/
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
public static ContentType createContentType(final XContentType xContentType) {
public static <Req extends ActionRequest> ContentType createContentType(final XContentType xContentType) {
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 will revert.

@@ -72,7 +72,7 @@ protected static RestHighLevelClient highLevelClient() {

@FunctionalInterface
protected interface AsyncMethod<Request, Response> {
void execute(Request request, ActionListener<Response> listener, Header... headers);
void execute(Request request, ActionListener<Response> listener, Header... headers) 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.

Most probably this is not needed, I'll remove.

return null;
}

public static IndicesExistsResponse fromXContent(XContentParser xContentParser) {
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 believe none of this is really needed...

);
}

private IndicesExistsResponse parseIndicesExistResp(Response response) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might have been better to move this method into IndicesExistsResponse, but Response is not visible from IndicesExistsResponse.

Copy link
Member

Choose a reason for hiding this comment

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

use the already existing convertExistsResponse from RestHighLevelClient, you can see how this is done in the ping method for instance.

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

hi @hariso , this looks pretty good, I will remove the WIP label. If you can add docs and address the few comments I left, I will merge this. Thanks!

On your questions: naming is good as-is. We do need the async version I think, which you have already added, so nothing to do there. The behaviour of the API when multiple indices are passed in can be controlled through indices options which are currently not supported in your PR, you'll see a comment about that. Once you support those you just have to return true if 200 and false if 404.

);
}

private IndicesExistsResponse parseIndicesExistResp(Response response) {
Copy link
Member

Choose a reason for hiding this comment

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

use the already existing convertExistsResponse from RestHighLevelClient, you can see how this is done in the ping method for instance.


import java.io.IOException;
import java.util.Collections;

import static java.util.Collections.emptySet;
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you revert the addition of the static import? It affects also other methods that you are not changing and that causes some noise in the PR.

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 👍

@@ -67,6 +69,8 @@
import java.util.Objects;
import java.util.StringJoiner;

import static java.util.Collections.emptyMap;
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you revert the addition of the static import? It affects also other methods that you are not changing and that causes some noise in the PR.

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 👍

@@ -373,12 +377,12 @@ static Request search(SearchRequest searchRequest) throws IOException {

static Request searchScroll(SearchScrollRequest searchScrollRequest) throws IOException {
HttpEntity entity = createEntity(searchScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("GET", "/_search/scroll", Collections.emptyMap(), entity);
return new Request("GET", "/_search/scroll", emptyMap(), entity);
Copy link
Member

Choose a reason for hiding this comment

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

yes please, I commented above about it, before seeing your question ;)

@@ -414,6 +418,12 @@ public static ContentType createContentType(final XContentType xContentType) {
return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null);
}

static Request indicesExist(IndicesExistsRequest request) {
String endpoint = endpoint(request.indices(), Strings.EMPTY_ARRAY, "");

Copy link
Member

Choose a reason for hiding this comment

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

can you remove this empty line please?

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 👍

import org.elasticsearch.rest.RestStatus;

import java.io.IOException;

public class IndicesClientIT extends ESRestHighLevelClientTestCase {

public void testIndexExists_IndexPresent() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer not using underscore as part of method names if possible. can you remove this one and the following ones please?

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!

final Request request = Request.indicesExist(indicesExistRequest);
assertEquals("/" + index1 + "," + index2, request.getEndpoint());
assertEquals("HEAD", request.getMethod());
}
Copy link
Member

Choose a reason for hiding this comment

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

I think that one test here is enough. test it with a random number of indices ?

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! Test is now using up to 10 indices with random names.

static Request indicesExist(IndicesExistsRequest request) {
String endpoint = endpoint(request.indices(), Strings.EMPTY_ARRAY, "");

return new Request(HttpHead.METHOD_NAME, endpoint, emptyMap(), null);
Copy link
Member

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.

@javanna I implemented this. Two values I could extract from IndicesExistsRequest I did. Please double check, since I have the feeling I missed something.

@javanna javanna changed the title [WIP] High-level REST client: Support for "indices exist" Add support for indices exists to REST high level client Dec 1, 2017
@javanna javanna removed the WIP label Dec 1, 2017
listener, emptySet(), headers);
}

public IndicesExistsResponse exists(IndicesExistsRequest indicesExistsRequest, Header... headers) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

can you make this final?

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 guess you mean the method and not parameters ("this" and not "these"), but I'd like to double check.: )

Copy link
Member

Choose a reason for hiding this comment

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

yes the method.

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 new IndicesExistsResponse(response.getStatusLine().getStatusCode() == 200);
}

public void existsAsync(IndicesExistsRequest indicesExistsRequest, ActionListener<IndicesExistsResponse> listener, Header... headers) {
Copy link
Member

Choose a reason for hiding this comment

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

can you make this final?

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.

@javanna
Copy link
Member

javanna commented Jan 16, 2018

hi @hariso do you think you'll have time to get back to this PR, merge master in and make the requested changes above? Thanks!

@elastic elastic deleted a comment from elasticmachine Jan 16, 2018
@hariso
Copy link
Contributor Author

hariso commented Jan 31, 2018

@javanna Thanks a lot for the feedback! I did a few small refactorings in the tests. I'll now address the other comments and let you once all of that is done.

@@ -1230,6 +1178,34 @@ private static void setRandomIndicesOptions(Consumer<IndicesOptions> setter, Sup
}
}

private static void setRandomIncludeDefaults(GetIndexRequest request, Map<String, String> expectedParams) {
if (randomBoolean()) {
request.includeDefaults(true);
Copy link
Member

Choose a reason for hiding this comment

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

can we also randomize the value, just to test what we do when the value is explicitly set to false? It is the default value but I would prefer to test that codepath too. Same for the other new methods.

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!

request.local(true);
expectedParams.put("local", Boolean.TRUE.toString());
}
}
Copy link
Member

Choose a reason for hiding this comment

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

good work!

@hariso
Copy link
Contributor Author

hariso commented Jan 31, 2018

@javanna Another batch of changes is in!
One question related to this ctor:

public GetIndexRequest(StreamInput in) throws IOException {
        super(in);
        int size = in.readVInt();
        features = new Feature[size];
        for (int i = 0; i < size; i++) {
            features[i] = Feature.fromId(in.readByte());
        }
        humanReadable = in.readBoolean();
    }

This is obviously the two new fields, flatSettings and includeDefaults. However, my understanding is that we have added those two fields because of the HLRC only, and this appears to be used by the transport, so I (kinda) intentionally left it out.


assertEquals(HttpHead.METHOD_NAME, request.getMethod());
assertEquals("/" + String.join(",", indices), request.getEndpoint());
assertThat(expectedParams, equalTo(request.getParameters()));
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you assert that request.getEntity is null please?

@@ -99,6 +98,8 @@ public static Feature fromId(byte id) {
private static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS };
private Feature[] features = DEFAULT_FEATURES;
private boolean humanReadable = false;
private boolean flatSettings = false;
private boolean includeDefaults = false;
Copy link
Member

Choose a reason for hiding this comment

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

the point that you make on these not being serializable is a good one. I think that is a good choice, if we want to make it more explicit we could mark these transient

<2> Return result in a format suitable for humans.
<3> Whether to return all default setting for each of the indices.
<4> Return settings in flat format.
<5> Controls how unavailable indices are resolved and how wildcard expressions are expanded.
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you remove the punctuation mark at the end of these notes?

include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[indices-exists-async]
--------------------------------------------------
<1> Called when the execution is successfully completed. The response is provided as an argument.
<2> Called in case of failure. The raised exception is provided as an argument.
Copy link
Member

Choose a reason for hiding this comment

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

remove the final punctuation marks?

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

this is ready @hariso . I left a couple of very minor comments. Docs look good. if you can address the last few comments I will merge this.

@hariso
Copy link
Contributor Author

hariso commented Feb 1, 2018

@javanna Docs and tests updated. I just realized one thing, and that is that it may be good to combine three different tests for Indices Exists API from IndicesClientIT into one, since it looks like that's how it's usually done in ES?

include::delete_index.asciidoc[]
>>>>>>> master
Copy link
Member

Choose a reason for hiding this comment

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

could you fix this? A merge gone wrong I think

Copy link
Member

Choose a reason for hiding this comment

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

include::deleteindex.asciidoc[] should go, it's been replaced by include::delete_index.asciidoc[]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks for noticing it!

@javanna javanna merged commit 897ef45 into elastic:master Feb 2, 2018
@javanna
Copy link
Member

javanna commented Feb 2, 2018

thanks a lot @hariso !

@hariso
Copy link
Contributor Author

hariso commented Feb 2, 2018

Thank you for all the stuff I learnt here! No wonder Elasticsearch is such a great piece of software!

@hariso hariso deleted the hlrc-indices-exist branch February 2, 2018 10:50
@jimczi jimczi added v7.0.0-beta1 and removed v7.0.0 labels Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants