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 UpdateRequest support to High Level Rest client #23266

Merged

Conversation

tlrx
Copy link
Member

@tlrx tlrx commented Feb 20, 2017

This pull request adds support for UpdateRequest to the High Level Rest client.

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.

left a couple of comments, looks good though

XContentType xContentType = null;
if (updateRequest.doc() != null) {
xContentType = updateRequest.doc().getContentType();
} else if (updateRequest.upsertRequest() != null) {
Copy link
Member

Choose a reason for hiding this comment

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

can the upsert document only be present when no partial document is provided? I think the confusing part is that we have content type coming from both, but the header is one :) maybe we just need to introduce validation that they come in the same content-type, thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

can the upsert document only be present when no partial document is provided?

Yes

maybe we just need to introduce validation that they come in the same content-type, thoughts?

Yes, I'll do that in a separate PR with the ToXContent method.


BytesRef requestBody = builder.bytes().toBytesRef();
ContentType contentType = ContentType.create(xContentType.mediaType());
return new ByteArrayEntity(requestBody.bytes, requestBody.offset, requestBody.length, contentType);
Copy link
Member

Choose a reason for hiding this comment

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

should UpdateRequest implement ToXContent instead? we will find this in other requests too (e.g. bulk), we should decide where to put this code for all those.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Created #23289

ContentType contentType = ContentType.create(xContentType.mediaType());
return new ByteArrayEntity(requestBody.bytes, requestBody.offset, requestBody.length, contentType);
} catch (IOException e) {
throw new IllegalStateException("Failed to build HTTP entity from update request", e);
Copy link
Member

Choose a reason for hiding this comment

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

why wrapping the exception?

@tlrx tlrx force-pushed the add-update-request-to-high-level-rest-client branch from a5c3424 to 0edc183 Compare February 21, 2017 15:14
@tlrx
Copy link
Member Author

tlrx commented Feb 21, 2017

@javanna Now #23289 is merged, would you like to have another look to this pull request? Thanks

@javanna javanna removed the v5.4.0 label Feb 21, 2017
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.

left a couple of comments.

@@ -118,6 +122,48 @@ static Request index(IndexRequest indexRequest) {
return new Request(method, endpoint, parameters.getParams(), entity);
}

static Request update(UpdateRequest updateRequest) 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.

wondering if we should rather catch IOException and rethrow UncheckedIOException. getting tired of all these checked exceptions, which force us to move to CheckedFunction.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I'm on the fence on this. We already use CheckedFunction for response parsers I think we could also use CheckedFunction for request converters? I don't feel like it adds a lot of boling code.

Copy link
Member

Choose a reason for hiding this comment

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

ok fine with me

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks

if (updateRequest.upsertRequest() != null) {
XContentType upsertContentType = updateRequest.upsertRequest().getContentType();
if ((xContentType != null) && (xContentType != upsertContentType)) {
throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" +
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 the assumption up until now was that these methods wouldn't throw exceptions. if they do, we have to modify the async methods to catch and call the listener. I think users wouldn't expect to have exceptions thrown with async methods, even if this happens synchronously before the request gets sent :)

Copy link
Member Author

Choose a reason for hiding this comment

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

if they do, we have to modify the async methods to catch and call the listener.

I think I did it in performRequestAsync() ?

Copy link
Member

Choose a reason for hiding this comment

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

oh I had missed this. But we only catch IOException, while we can also throw IllegalStateException now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right - it should catch Exception instead (and also ensure that the listener get called anyway because I just notive that performRequestAsync throws an IllegalArgumentException too.

Copy link
Member

Choose a reason for hiding this comment

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

oh oh, I think those exceptions thrown in RestClient#performRequestAsync are a problem to be handled in the low level client. the bug is there I'm afraid.

Copy link
Member Author

Choose a reason for hiding this comment

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

oh right - I didn't notice I was looking at the RestClient itself. I'll take care of changing it


ResponseListener responseListener = wrapResponseListener(responseConverter, listener, ignores);
client.performRequestAsync(req.method, req.endpoint, req.params, req.entity, responseListener, headers);
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure here, I think for correctness we should only wrap the requestConverter call? the wrapResponseListener and performRequestAsync should really never throw exception. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that what I did first but then I wrapped everything because I mixed up the performRequestAsync methods.

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.

LGTM

@tlrx tlrx merged commit 09734e7 into elastic:master Feb 22, 2017
@tlrx tlrx deleted the add-update-request-to-high-level-rest-client branch February 22, 2017 10:55
@tlrx
Copy link
Member Author

tlrx commented Feb 22, 2017

Thanks @javanna

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.

2 participants