Skip to content

Collect config for XContentParser #79814

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

Merged
merged 16 commits into from
Oct 27, 2021

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Oct 26, 2021

This shifts the internal API for building an XContentParser to make
maintenance easier.

To build an XContentParser you need three things:

  1. An XContentType that knows whether you are reading json, smile,
    yaml, or cbor
  2. Configuration information
  3. A place to get the bytes

Years ago, there wasn't any configuration. But we've since added stuff
like NamedXContentRegistry and DeprecationHandler and
RestApiVersion and, more lately, filtering configuration. Each time we
added one we modified the method signature to add it. Each "place to get
the bytes" also needs its own method. All of which are implemented by
each XContentType. All together we had dozens of these methods for
different ways of building parser. All because of the combinatorial
explosion of config x type x source.

This tames the combinatorial explosion somewhat by bundling all of the
configuration into an immutable XContentParserConfiguration. Sadly, it
has to add createParser methods that consume the configuration objects.
Luckily, we can remove some of the old methods. We could remove all of
them, but that'd make this change huge. Instead we deprecate the old
methods and point them to the new ones.

This shifts the internal API for building an `XContentParser` to make
maintenance easier.

To build an `XContentParser` you need three things:
1. An `XContentType` that knows whether you are reading json, smile,
   yaml, or cbor
2. Configuration information
3. A place to get the bytes

Years ago, there wasn't any configration. But we've since added stuff
like `NamedXContentRegistry` and `DeprecationHandler` and
`RestApiVersion` and, more lately, filtering configuration. Each time we
added one we modified the method signature to add it. Each "place to get
the bytes" also needs its own method. All of which are implemented by
each `XContentType`. All together we had dozens of these methods for
different ways of building parser. All because of the combinatorial
explosion of `config x type x source`.

This tames the combinatorial explosion somewhat by bundling all of the
configuration into an immutable `XContentParserConfiguration`. Sadly, it
has to add `createParser` methods that consume the configuration objects.
Luckilly, we can remove some of the old methods. We *could* remove all of
them, but that'd make this change *huge*. Instead we deprecate the old
methods and point them to the new ones.
@nik9000 nik9000 added >non-issue :Core/Infra/Core Core issues without another label v8.0.0 labels Oct 26, 2021
@nik9000 nik9000 requested a review from pgomulka October 26, 2021 14:20
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Oct 26, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

Copy link
Contributor

@pgomulka pgomulka left a comment

Choose a reason for hiding this comment

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

LGTM - great work :)
the only real concern I have is with the PLAIN name

btw we have a similar issue with XContentBuilder constructors :)

@@ -207,7 +215,7 @@ public static void readMultiLineFormat(BytesReference data,
break;
}
// support first line with \n
if (restApiVersion == RestApiVersion.V_7 && nextMarker == 0) {
if (config.restApiVersion() == RestApiVersion.V_7 && nextMarker == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

might be worth calling it parserConfig as it is in other places. Not because of consistency, but because I think it reads better

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

public RestApiVersion getRestApiVersion() {
return restApiVersion;
return parserConfig.restApiVersion();
Copy link
Contributor

Choose a reason for hiding this comment

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

just a thought..
restApiVersion is used for both XContentParser and XContentBuilder
we create a builder with

 new XContentBuilder(XContentFactory.xContent(responseContentType), unclosableOutputStream,
                includes, excludes, responseMediaType, request.getRestApiVersion());

in AbstractRestChannel. I wonder if it would be confusing that the version is taken from a request which takes it from parserConfig? Obviously we now know that it only wrapps parameters..

I wonder if maybe for RestRequest it would make sense to keep the restApiVersion field together with parserConfig. Or would it be more complex ?
I am a little biased, because when working on RestApiCompatibility I got used to thinking that RestRequest keeps the version.
How does this read to you?

tldr ;] I am not convinced myself about this, but might be worth taking a second look at this class

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's ok to keep it separate. It'd be worried if they didn't line up but it'd be fine to add an assertion I think.

* Configuration for {@link XContentParser}.
*/
public class XContentParserConfiguration {
public static final XContentParserConfiguration PLAIN = new XContentParserConfiguration(
Copy link
Contributor

Choose a reason for hiding this comment

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

how about DEFAULT? or EMPTY?
PLAIN reminds me of plaint/text and I think it would be better away from that

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a pretty compelling reason not to call it PLAIN. I think EMPTY is pretty good though. It sort of implies at least a chunk of the configuration it encodes.

@nik9000
Copy link
Member Author

nik9000 commented Oct 27, 2021

@elasticmachine update branch

@nik9000 nik9000 added v8.1.0 auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) and removed v8.0.0 labels Oct 27, 2021
@elasticsearchmachine elasticsearchmachine merged commit a8258bd into elastic:master Oct 27, 2021
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Oct 27, 2021
In elastic#79814 I deprecated many variants of `XContent#createParser` in favor
of some new variants that bundle many of the arguments. This removes one
of the deprecated variants by converting all call sites to the new
variant.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Oct 30, 2021
In elastic#79814 I deprecated many variants of XContent#createParser in favor
of some new variants that bundle many of the arguments. This replaces
the variants used by the search execution context with the new variants.
nik9000 added a commit that referenced this pull request Nov 1, 2021
In #79814 I deprecated many variants of `XContent#createParser` in favor
of some new variants that bundle many of the arguments. This removes one
of the deprecated variants by converting all call sites to the new
variant.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Nov 1, 2021
In elastic#79814 I deprecated many variants of XContent#createParser in favor
of some new variants that bundle many of the arguments. This replaces
the variants used by the search execution context with the new variants.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Feb 23, 2022
This removes one of the `createParser` methods that I deprecated in
 elastic#79814, migrating callers to one of the new methods that it created.
nik9000 added a commit that referenced this pull request Feb 24, 2022
This removes one of the `createParser` methods that I deprecated in
 #79814, migrating callers to one of the new methods that it created.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Feb 24, 2022
This removes one of the `createParser` methods that I deprecated in
 elastic#79814, migrating callers to one of the new methods that it created.
elasticsearchmachine pushed a commit that referenced this pull request Feb 28, 2022
This removes one of the `createParser` methods that I deprecated in 
#79814, migrating callers to one of the new methods that it created.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Mar 7, 2022
This removes many calls to the last remaining `createParser` method that
I deprecated in elastic#79814, migrating callers to one of the new methods that
it created.
elasticsearchmachine pushed a commit that referenced this pull request Aug 1, 2022
This removes many calls to the last remaining `createParser` method that
I deprecated in #79814, migrating callers to one of the new methods that
it created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) :Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants