Skip to content

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

Merged
owaiskazi19 merged 2 commits intoopensearch-project:feature/extensionsfrom
dbwiddis:restResponse
Sep 2, 2022
Merged

[Feature/extensions] Pass full RestResponse to user from Extension#4356
owaiskazi19 merged 2 commits intoopensearch-project:feature/extensionsfrom
dbwiddis:restResponse

Conversation

@dbwiddis
Copy link
Copy Markdown
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.

Signed-off-by: Daniel Widdis <widdis@gmail.com>
@github-actions
Copy link
Copy Markdown
Contributor

Gradle Check (Jenkins) Run Completed with:

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 31, 2022

Codecov Report

❌ Patch coverage is 57.44681% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.59%. Comparing base (50f2d9e) to head (089ddbe).
⚠️ Report is 350 commits behind head on feature/extensions.

Files with missing lines Patch % Lines
...rch/extensions/rest/RestSendToExtensionAction.java 0.00% 20 Missing ⚠️
Additional details and impacted files
@@                   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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

try (BytesStreamOutput out = new BytesStreamOutput()) {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 <widdis@gmail.com>
@github-actions
Copy link
Copy Markdown
Contributor

Gradle Check (Jenkins) Run Completed with:

Copy link
Copy Markdown
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.setContentType(response.getContentType());
restExecuteOnExtensionResponse.setContent(response.getContent());
restExecuteOnExtensionResponse.setHeaders(response.getHeaders());
inProgressLatch.countDown();
Copy link
Copy Markdown
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