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

Added support for headers for ExtensionRestRequest #643

Merged
merged 12 commits into from
Apr 7, 2023

Conversation

nassipkali
Copy link
Contributor

@nassipkali nassipkali commented Apr 3, 2023

Description

Adds SDKRestRequest, SDKHttpRequest

Companion PR: opensearch-project/OpenSearch#6826

Issues Resolved

#431

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.

@nassipkali
Copy link
Contributor Author

@dbwiddis first option mentioned in previous pr is more complicated because protected constructor of RestRequest requires also to fill HttpRequest. We can create sdkHttpRequest instead sdkRestRequest. sdkHttpRequest can replace anonymous HttpRequest class.

@dbwiddis
Copy link
Member

dbwiddis commented Apr 3, 2023

@dbwiddis first option mentioned in previous pr is more complicated because protected constructor of RestRequest requires also to fill HttpRequest. We can create sdkHttpRequest instead sdkRestRequest. sdkHttpRequest can replace anonymous HttpRequest class.

We still need both. In particular we really need the params() in the RestRequest() and the only way to get them in there from the static HttpRequest is by hacking at the uri (which I did in the test class). Take a look at TestBaseExtensionRestHandler.createTestRestRequest() in particular the uri() method. That works for combining path + params into a uri, but that's not the original uri.

But we should be able to use an anonymous RestRequest subclass instead of the static method to create the RestRequest. Change RestRequest.request(xContentRegistry, new HttpRequest() { ... }, null); to new RestRequest(xContentRegistry, params, path, headers, new HttpRequest() { ... existing overrides ... } , null) {};.

@nassipkali
Copy link
Contributor Author

@dbwiddis first option mentioned in previous pr is more complicated because protected constructor of RestRequest requires also to fill HttpRequest. We can create sdkHttpRequest instead sdkRestRequest. sdkHttpRequest can replace anonymous HttpRequest class.

We still need both. In particular we really need the params() in the RestRequest() and the only way to get them in there from the static HttpRequest is by hacking at the uri (which I did in the test class). Take a look at TestBaseExtensionRestHandler.createTestRestRequest() in particular the uri() method. That works for combining path + params into a uri, but that's not the original uri.

But we should be able to use an anonymous RestRequest subclass instead of the static method to create the RestRequest. Change RestRequest.request(xContentRegistry, new HttpRequest() { ... }, null); to new RestRequest(xContentRegistry, params, path, headers, new HttpRequest() { ... existing overrides ... } , null) {};.

https://github.com/opensearch-project/OpenSearch/blob/bcbb561b51c60776257e68786fcc411eebf4fab4/server/src/main/java/org/opensearch/rest/RestRequest.java#LL170C42-L170C42
Constuctor gets params from uri. Is RestRequest subclass needed in future? If yes then I try implement that class

@nassipkali
Copy link
Contributor Author

@dbwiddis I can add path to ExtensionRestRequest. If I keep path, uri, params in ExtensionRestRequest then I can create instance of RestRequest subclass without overhead(converting uri to path, getting params from uri)

@dbwiddis
Copy link
Member

dbwiddis commented Apr 3, 2023

@dbwiddis I can add path to ExtensionRestRequest. If I keep path, uri, params in ExtensionRestRequest then I can create instance of RestRequest subclass without overhead(converting uri to path, getting params from uri)

The "params from uri" happens in OpenSearch inside the RestController. It has already:

  • Decoded params from the URI ( such as the name/blah pair in /restcommand/foo?name=blah)
  • Decoded named params from registered REST actions (such as the id/123 pair in a registered rest path /detector/{id} when a user types /detector/123.

It's that second part that we do not have the ability to decode in the Extension, which is why we must pass the params() map in the ExtensionRestRequest and use it to recreate the RestRequest with those same params. What I did in the test class was a hack around the way we should do it.

Since we have the params, we don't really need the uri, since the path is the interesting part. But since we are recreating the whole HttpRequest it doesn't hurt to include both (by putting the uri where it belongs, the user can get either the path or the uri). It looks like you've handled these in your PR, so I'll go review it now!

Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

This looks great!

I've made a few suggestions for improvements, mostly in the documentation.

One more request: our org.opensearch.sdk package is getting cluttered and we should move stuff out of it when we can. Please create a new org.opensearch.sdk.rest package and put your two new SDK classes into it.

Might also be a good home for these classes (we can address in a later PR):

  • ExtensionRestHandler
  • BaseExtensionRestHandler (will break dependent extensions like AD but we can synchronize fix)
  • RouteHandler (will break dependent extensions like AD but we can synchronize fix)
  • ExtensionRestPathRegistry

@dbwiddis dbwiddis added the CCI Part of the College Contributor Initiative label Apr 4, 2023
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

In addition to the comment changes and package change requested above, also please update the tests:

  • the TestBaseExtensionRestHandler class contains a method createTestRestRequest(). Replace the code there to use your new SDK classes, so that test code relying on them will use your new classes.

@nassipkali nassipkali requested a review from dbwiddis April 4, 2023 17:41
dbwiddis
dbwiddis previously approved these changes Apr 4, 2023
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

LGTM!

Tests should pass following merge of opensearch-project/OpenSearch#6826

joshpalis
joshpalis previously approved these changes Apr 4, 2023
@nassipkali nassipkali dismissed stale reviews from joshpalis and dbwiddis via 3b5cac9 April 5, 2023 17:41
@nassipkali nassipkali requested a review from dbwiddis April 5, 2023 17:42
Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

Looks good, some suggestions to improve it!

src/test/java/org/opensearch/sdk/TestSDKRestRequest.java Outdated Show resolved Hide resolved
src/test/java/org/opensearch/sdk/TestSDKRestRequest.java Outdated Show resolved Hide resolved
@nassipkali
Copy link
Contributor Author

nassipkali commented Apr 5, 2023

@dbwiddis Map<String, Object> source = request.contentParser().map();. Can I use there mapStrings()? mapStrings returns Map<String, String> and doesnt need cast/convert Object to String

@dbwiddis
Copy link
Member

dbwiddis commented Apr 5, 2023

@dbwiddis Map<String, Object> source = request.contentParser().map();. Can I use there mapStrings()? mapStrings returns Map<String, String> and doesnt need cast/convert Object to String

Yep!

@nassipkali
Copy link
Contributor Author

@dbwiddis When snapshot that contains latest commits of OpenSearch is will be used? My PR from OpenSearch side is merged but this PR checks didn't see changes from OpenSearch side.

dbwiddis
dbwiddis previously approved these changes Apr 7, 2023
@dbwiddis dbwiddis self-requested a review April 7, 2023 17:28
@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

OK, new snapshot is being read but there's a missing method signature we're trying to read:

/home/runner/work/opensearch-sdk-java/opensearch-sdk-java/src/main/java/org/opensearch/sdk/rest/SDKHttpRequest.java:42: error: cannot find symbol

    this.httpVersion = request.httpVersion();

@nassipkali
Copy link
Contributor Author

OK, new snapshot is being read but there's a missing method signature we're trying to read:

/home/runner/work/opensearch-sdk-java/opensearch-sdk-java/src/main/java/org/opensearch/sdk/rest/SDKHttpRequest.java:42: error: cannot find symbol

    this.httpVersion = request.httpVersion();

Yes, there should be protocolVersion()

@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

Yes, there should be protocolVersion()

Just found that myself :)

Should be a quick fix and we can merge this!

@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

@nassipkali are you going to be fixing that method?

Signed-off-by: Daulet Nassipkali <[email protected]>
@nassipkali
Copy link
Contributor Author

nassipkali commented Apr 7, 2023

@dbwiddis I have a problem with git push. I made change from github web editor

@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

@dbwiddis I have a problem with git push. I made change from github web editor

Lots of different errors in the build now.

Locally you should be able to pull the changes down from your branch.

@dbwiddis
Copy link
Member

dbwiddis commented Apr 7, 2023

@nassipkali I'm going to go ahead and fix up the issues here, since the SDK build is now failing other PRs.

One challenge with these companion PRs is finding out what's broken before we merge the companion. You should be using ./gradlew publishToMavenLocal on the OpenSearch PR branch and then running ./gradlew check on this PR branch to verify that things work.

Signed-off-by: Daniel Widdis <[email protected]>
owaiskazi19
owaiskazi19 previously approved these changes Apr 7, 2023
@dbwiddis dbwiddis merged commit dfdb050 into opensearch-project:main Apr 7, 2023
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 7, 2023
* added uri and httpVersion to HttpRequest

Signed-off-by: Daulet <[email protected]>

* added SDKHttpRequest, SDKRestRequest

Signed-off-by: Daulet <[email protected]>

* spotless applied

Signed-off-by: Daulet <[email protected]>

* moved SDKHttpRequest and SDKRestRequest to org.opensearch.sdk.rest

Signed-off-by: Daulet <[email protected]>

* tests changed for new ExtensionRestRequest

Signed-off-by: Daulet <[email protected]>

* TestSDKRestRequest created

Signed-off-by: Daulet <[email protected]>

* corrected test for xContentType, added test for contentParser()

Signed-off-by: Daulet <[email protected]>

* replaced .map() with mapStrings() in contentParser()

Signed-off-by: Daulet <[email protected]>

* Update SDKHttpRequest.java

Signed-off-by: Daulet Nassipkali <[email protected]>

* Fix tests for new format

Signed-off-by: Daniel Widdis <[email protected]>

* Use correct Test annotation so tests actually run

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Daulet <[email protected]>
Signed-off-by: Daulet Nassipkali <[email protected]>
Signed-off-by: Daniel Widdis <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
(cherry picked from commit dfdb050)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
dbwiddis added a commit that referenced this pull request Apr 8, 2023
* added uri and httpVersion to HttpRequest



* added SDKHttpRequest, SDKRestRequest



* spotless applied



* moved SDKHttpRequest and SDKRestRequest to org.opensearch.sdk.rest



* tests changed for new ExtensionRestRequest



* TestSDKRestRequest created



* corrected test for xContentType, added test for contentParser()



* replaced .map() with mapStrings() in contentParser()



* Update SDKHttpRequest.java



* Fix tests for new format



* Use correct Test annotation so tests actually run



---------





(cherry picked from commit dfdb050)

Signed-off-by: Daulet <[email protected]>
Signed-off-by: Daulet Nassipkali <[email protected]>
Signed-off-by: Daniel Widdis <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Daniel Widdis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.x CCI Part of the College Contributor Initiative repeat-contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants