Skip to content

Conversation

@jnturton
Copy link
Contributor

DRILL-8307: Ensure thread safety in the Druid plugin HTTP client

Description

Replaces the Apache HttpClient in the Druid storage plugin with the thread-safe OkHttp3 client.

Documentation

N/A

Testing

Existing unit tests.

@jnturton jnturton added bug backport-to-stable This bug fix is applicable to the latest stable release and should be considered for inclusion there labels Sep 15, 2022
@jnturton jnturton marked this pull request as draft September 15, 2022 14:56
@jnturton
Copy link
Contributor Author

I see I missed a spot, converting to draft.

@jnturton jnturton force-pushed the 8307-druid-http-threads branch from bbfb4e7 to 57be903 Compare September 16, 2022 10:05
@jnturton jnturton marked this pull request as ready for review September 16, 2022 10:07
@jnturton jnturton requested a review from luocooong September 16, 2022 13:37
}
try (Response response = restClient.post(queryUrl, query)) {
if (!response.isSuccessful()) {
throw UserException
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to get the errorContext from the DruidBatchReader so that we get good error messages?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that these objects are created in the DruidPlguin but maybe we could add a method addErrorContext or something like that and call it in the constructor of the Batch Reader.

Copy link
Contributor Author

@jnturton jnturton Sep 16, 2022

Choose a reason for hiding this comment

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

@cgivre thanks, I'll check that out. I'd have thought that any error information available in the batch reader would have to also be present here in the HTTP response. Even if that's true, there might be some more response body unpacking to do to expose it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cgivre I wasn't aware of the CustomErrorContext, thanks for pointing it out. It looks to me like it's an EVF construct that isn't accessible to pre-EVF plugins like storage-druid. If I've got that right then will we only be able to start it using in this plugin's error messages when its conversion to EVF arrives?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh... you're right. In that case, would you please put some TODO comments in the rest clients and then we can update once the EVF conversion is 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'm good with merging this once that's done. +1

@jnturton jnturton force-pushed the 8307-druid-http-threads branch from e523491 to 529b510 Compare September 17, 2022 05:36
Copy link
Contributor

@cgivre cgivre left a comment

Choose a reason for hiding this comment

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

LGTM +1

@jnturton jnturton merged commit 612f789 into apache:master Sep 18, 2022
HttpEntity entity = new StringEntity(body, DEFAULT_ENCODING);
httppost.setEntity(entity);
public Response post(String url, String body) throws IOException {
RequestBody postBody = RequestBody.create(body.getBytes(StandardCharsets.UTF_8));
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to use the RequestBody.create method that takes a String instead of converting to a byte[]?

https://square.github.io/okhttp/#post-to-a-server

Copy link
Contributor Author

@jnturton jnturton Sep 18, 2022

Choose a reason for hiding this comment

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

Would it be possible to use the RequestBody.create method that takes a String instead of converting to a byte[]?

https://square.github.io/okhttp/#post-to-a-server

Ugh I missed that. @cgivre please may we incorporate @pjfanning's suggestion here in the EVF conversion PR.

public Response get(String url) throws IOException {
Request get = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json")
Copy link
Member

Choose a reason for hiding this comment

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

Why would a get request need a content-type? Content-Type on a request refers to the type of the request body but for GETs, this is empty. Maybe I've misunderstood the intent, but shouldn't this be an Accept header? An Accept header is used to tell the HTTP server that the client (us) would prefer to get the response with a particular content-type. See https://en.wikipedia.org/wiki/List_of_HTTP_header_fields which has a description of what the headers mean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pjfanning: also a valid question, but remember that this PR was a thread safety fix intended for backporting to stable so I strove to disturb as little possible while fixing the bug. Since the plugin specified this content-type here before, and we have no bug report related to it doing so, it continues to do that after this PR.

Maybe when @cgivre sends in the EVF version of this plugin we can see if Druid is indeed happy for this content-type header to disappear. I'm almost sure you're right and that will be the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-stable This bug fix is applicable to the latest stable release and should be considered for inclusion there bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants