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

[BUG] Deserialization of SearchRequest crashes #7829

Closed
Yury-Fridlyand opened this issue May 30, 2023 · 5 comments
Closed

[BUG] Deserialization of SearchRequest crashes #7829

Yury-Fridlyand opened this issue May 30, 2023 · 5 comments
Labels
bug Something isn't working distributed framework

Comments

@Yury-Fridlyand
Copy link

SQL plugin team presents...

Describe the bug

Deserialization of a serialized SearchRequest crashes.

To Reproduce

Run a unit test from the attached diff:
SearchRequest-deserialization-test.patch

    public void testRequestSerializationDeserializationRoundTrip() throws IOException {
        var request = new SearchRequest()
            .indices("account")
            .scroll(TimeValue.timeValueMinutes(1))
            .source(new SearchSourceBuilder()
                .from(0)
                .timeout(TimeValue.timeValueMinutes(1))
                .trackScores(false)
                .query(QueryBuilders.boolQuery()
                    .filter(QueryBuilders.matchQuery("address", "street"))
                    .filter(QueryBuilders.matchQuery("state", "OH")))
                .highlighter(new HighlightBuilder().field("*"))
                .fetchSource(new String[] { "firstmame", "lastname" }, new String[0])
                .size(2)
                .sort(DOC_FIELD_NAME, SortOrder.ASC));

        var outStream = new BytesStreamOutput();
        request.writeTo(outStream);
        outStream.flush();
        var deserialized = new SearchRequest(outStream.bytes().streamInput());
        // crashes here        ^^^^^^^^^^^^^
        assertEquals(request, deserialized);
    }

I added this test in server/src/test/java/org/opensearch/action/search/SearchRequestTests.java
Exception stack:

can't read named writeable from StreamInput
java.lang.UnsupportedOperationException: can't read named writeable from StreamInput
	at __randomizedtesting.SeedInfo.seed([C0164472CF724D04:AF8D8A47F3CD4769]:0)
	at org.opensearch.common.io.stream.StreamInput.readNamedWriteable(StreamInput.java:1111)
	at org.opensearch.common.io.stream.StreamInput.readOptionalNamedWriteable(StreamInput.java:1138)
	at org.opensearch.search.builder.SearchSourceBuilder.<init>(SearchSourceBuilder.java:239)
	at org.opensearch.common.io.stream.StreamInput.readOptionalWriteable(StreamInput.java:963)
	at org.opensearch.action.search.SearchRequest.<init>(SearchRequest.java:227)
	at org.opensearch.action.search.SearchRequestTests.testRequestSerializationDeserializationRoundTrip(SearchRequestTests.java:243)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.carrotsearch.randomizedtesting.RandomizedRunner.invoke(RandomizedRunner.java:1750)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$8.evaluate(RandomizedRunner.java:938)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$9.evaluate(RandomizedRunner.java:974)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$10.evaluate(RandomizedRunner.java:988)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.apache.lucene.tests.util.TestRuleSetupTeardownChained$1.evaluate(TestRuleSetupTeardownChained.java:48)
	at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
	at org.apache.lucene.tests.util.TestRuleThreadAndTestName$1.evaluate(TestRuleThreadAndTestName.java:45)
	at org.apache.lucene.tests.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
	at org.apache.lucene.tests.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:368)
	at com.carrotsearch.randomizedtesting.ThreadLeakControl.forkTimeoutingTask(ThreadLeakControl.java:817)
	at com.carrotsearch.randomizedtesting.ThreadLeakControl$3.evaluate(ThreadLeakControl.java:468)
	at com.carrotsearch.randomizedtesting.RandomizedRunner.runSingleTest(RandomizedRunner.java:947)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$5.evaluate(RandomizedRunner.java:832)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$6.evaluate(RandomizedRunner.java:883)
	at com.carrotsearch.randomizedtesting.RandomizedRunner$7.evaluate(RandomizedRunner.java:894)
	at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at org.apache.lucene.tests.util.TestRuleStoreClassName$1.evaluate(TestRuleStoreClassName.java:38)
	at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
	at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at org.apache.lucene.tests.util.TestRuleAssertionsRequired$1.evaluate(TestRuleAssertionsRequired.java:53)
	at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
	at org.apache.lucene.tests.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
	at org.apache.lucene.tests.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
	at org.apache.lucene.tests.util.TestRuleIgnoreTestSuites$1.evaluate(TestRuleIgnoreTestSuites.java:47)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
	at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:368)
	at java.base/java.lang.Thread.run(Thread.java:1589)

Expected behavior

Test should pass

Plugins

N/A

Screenshots

N/A

Host/Environment

main @ a201400

Additional context

Detected on SearchRequest, but scope could be much bigger.

@Yury-Fridlyand Yury-Fridlyand added bug Something isn't working untriaged labels May 30, 2023
@andrross
Copy link
Member

andrross commented May 30, 2023

Flagging #7780 as being potentially related to this.

/cc @nknize

@andrross
Copy link
Member

andrross commented Jun 8, 2023

@Yury-Fridlyand I think there is a problem with your test code. You should replace

var deserialized = new SearchRequest(outStream.bytes().streamInput());

with

var deserialized = new SearchRequest((new NamedWriteableAwareStreamInput(outStream.bytes().streamInput(), namedWriteableRegistry));

You must use a NamedWriteableAwareStreamInput with a NamedWriteableRegistry to be able to deserialize the named writables within a SearchRequest instance. (The base test class has a build-in method copyWriteable() that does this for you).

Does this answer solve your problem? If this doesn't help can you share more about the specific use case where you encountered this?

@Yury-Fridlyand
Copy link
Author

Hi @andrross,
Thank you, that works.
Fortunately, I found a way how to avoid deserializing a SearchRequest in my code, so it is not needed now. But good to know!

@kdh6429
Copy link

kdh6429 commented Jul 18, 2023

I am using SearchRequest in org.opensearch.client.opensearch.core . There is no writeTo method here.

@andrross
Copy link
Member

@kdh6429 You're using the class in opensearch-java, which is the recommended Java client. The class in question here is from the rest client built into the OpenSearch distribution, and is no longer recommended in favor of opensearch-java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working distributed framework
Projects
None yet
Development

No branches or pull requests

4 participants