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

[Feature/extensions] Pass full RestResponse to user from Extension #4356

Merged

Conversation

dbwiddis
Copy link
Member

Companion PR: opensearch-project/opensearch-sdk-java#114

Description

In the initial SDK implementation, I simply returned a String from the Extension's REST handler to the user.

This PR updates the (SDK API) return value to a RestResponse to match what a user would have received from OpenSearch itself or a plugin, including the HTTP Status Code, and the ability to return formats other than text.

As a bonus, I figured out how to test the input and output streams in the Request/Response classes and have improved those tests as well.

Issues Resolved

Fixes SDK #112

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@codecov-commenter
Copy link

codecov-commenter commented Aug 31, 2022

Codecov Report

Merging #4356 (089ddbe) into feature/extensions (50f2d9e) will decrease coverage by 0.01%.
The diff coverage is 57.44%.

@@                   Coverage Diff                    @@
##             feature/extensions    #4356      +/-   ##
========================================================
- Coverage                 70.61%   70.59%   -0.02%     
- Complexity                57271    57301      +30     
========================================================
  Files                      4633     4633              
  Lines                    275817   275850      +33     
  Branches                  40337    40337              
========================================================
- Hits                     194763   194738      -25     
- Misses                    64722    64886     +164     
+ Partials                  16332    16226     -106     
Impacted Files Coverage Δ
...rch/extensions/rest/RestSendToExtensionAction.java 31.25% <0.00%> (-6.49%) ⬇️
...xtensions/rest/RestExecuteOnExtensionResponse.java 100.00% <100.00%> (+100.00%) ⬆️
...n/indices/forcemerge/ForceMergeRequestBuilder.java 0.00% <0.00%> (-75.00%) ⬇️
...adonly/AddIndexBlockClusterStateUpdateRequest.java 0.00% <0.00%> (-75.00%) ⬇️
...readonly/TransportVerifyShardIndexBlockAction.java 9.75% <0.00%> (-73.18%) ⬇️
.../indices/forcemerge/TransportForceMergeAction.java 25.00% <0.00%> (-58.34%) ⬇️
...adcast/BroadcastShardOperationFailedException.java 55.55% <0.00%> (-44.45%) ⬇️
...search/aggregations/pipeline/HoltWintersModel.java 21.47% <0.00%> (-42.96%) ⬇️
...indices/readonly/TransportAddIndexBlockAction.java 20.68% <0.00%> (-37.94%) ⬇️
...java/org/opensearch/threadpool/ThreadPoolInfo.java 56.25% <0.00%> (-37.50%) ⬇️
... and 488 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

assertEquals(2, fooList.size());
assertTrue(fooList.containsAll(headerValueList));

try (BytesStreamOutput out = new BytesStreamOutput()) {
Copy link
Member

Choose a reason for hiding this comment

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

This is great. Now we are testing input/output byte stream as well. Can we create an issue to test the other request/response as well we have for extensibility?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a great thing that we're testing it, as when my test initially failed I discovered write(byte[]) is different than writeByteArray(byte[]). :-)

Yes, we should test all the other request/response code similarly. There's a small list of other fixes I'd like (compiler warnings, etc.) that I'll put in an issue later this week.

final StringBuilder responseBuilder = new StringBuilder();
// Initialize response. Values will be changed in the handler.
final RestExecuteOnExtensionResponse restExecuteOnExtensionResponse = new RestExecuteOnExtensionResponse(
RestStatus.ACCEPTED,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to keep the initial status as ACCEPTED? Can we initialize with NOT_FOUND or something in the range of 4xx?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I scanned the list and thought this was a reasonable "current state" but on looking at the docs it seems to imply a "final" async call, so it's probably not right. Not sure if 404 is better than 500 as a default otherwise.

I think it's a moot point, though, as it's always overwritten. With the countdown latch it will only return from the two methods that count down, or from a timeout. So we could set it null and it probably wouldn't matter.

  • On success it's overwritten by the response sent by the extension's handler.
  • On exception it's a 500.
  • On timeout waiting for the response it's a 408.

Leaning toward 500 at this point, will see what other reviewers have to say.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with 500.

Copy link
Member Author

Choose a reason for hiding this comment

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

Switching to 500 which will save me having to overwrite it on the exception.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. So I remembered, recently I worked on a bug for OpenSearch which asked the API response to be changed from 500 to 4xx because it should be an exception rather server error. I'm still thinking around should we keep it 500 or something on 4xx line. Let's see what other reviewers have to say.

Copy link
Member

Choose a reason for hiding this comment

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

@saratvemulapalli any thoughts here?

Copy link
Member

Choose a reason for hiding this comment

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

500 sounds perfect, its designed for server errors.

Signed-off-by: Daniel Widdis <[email protected]>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

Copy link
Member

@saratvemulapalli saratvemulapalli left a comment

Choose a reason for hiding this comment

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

Thanks @dbwiddis for the change!

restExecuteOnExtensionResponse.setStatus(response.getStatus());
restExecuteOnExtensionResponse.setContentType(response.getContentType());
restExecuteOnExtensionResponse.setContent(response.getContent());
restExecuteOnExtensionResponse.setHeaders(response.getHeaders());
inProgressLatch.countDown();
Copy link
Member

Choose a reason for hiding this comment

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

We have to figure out a way to remove latches. I think OpenSearch does it via Listeners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants