Skip to content

Conversation

@jordan-powers
Copy link
Contributor

This PR is a precursor to #126492.

It does three things:

  1. Move org.elasticsearch.common.text.Text from :server to org.elasticsearch.xcontent.Text in :libs:x-content.
  2. Refactor the Text class to use a java-native ByteBuffer instead of the elasticsearch BytesReference.
  3. Add the XContentString interface, with the Text class implement that interface.

@jordan-powers jordan-powers added >non-issue :Core/Infra/Core Core issues without another label v9.1.0 labels May 2, 2025
@jordan-powers jordan-powers requested a review from ldematte May 2, 2025 21:13
@jordan-powers jordan-powers self-assigned this May 2, 2025
@jordan-powers jordan-powers requested a review from a team as a code owner May 2, 2025 21:13
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label May 2, 2025
@elasticsearchmachine
Copy link
Collaborator

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

Copy link
Contributor

@ldematte ldematte 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, thanks for separating this, made it easier to focus the review.
I left a couple of comments

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

I agree with Lorenzo's comments, and have one myself.

write(spare.bytes(), 0, spare.length());
} else {
BytesReference bytes = text.bytes();
BytesReference bytes = BytesReference.fromByteBuffer(text.bytes());
Copy link
Member

Choose a reason for hiding this comment

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

nit: we could support reading/writing ByteBuffer directly in StreamInput/Output so that conversions are not necessary here. That could allow optimizing in the ByteArrayStreamInput case to create a ByteBuffer that wraps the underlying byte array with appropriate offset/length, without needing to copy bytes to a new array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into that, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'm thinking this might be a bigger change than we want to incorporate into this PR. It might be better to leave it as a follow-up, since the current implementation will still work, it'll just be less optimized.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm OK with leaving this in a follow-up, it will help with reviews too

@jordan-powers jordan-powers added auto-backport Automatically create backport pull requests when merged v8.19.0 labels May 6, 2025
Copy link
Contributor

@ldematte ldematte left a comment

Choose a reason for hiding this comment

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

LGTM

@jordan-powers jordan-powers merged commit db0c3c7 into elastic:main May 8, 2025
17 checks passed
@jordan-powers jordan-powers deleted the move-text-class branch May 8, 2025 15:19
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
8.19 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 127666

@jordan-powers
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
8.19

Questions ?

Please refer to the Backport tool documentation

elasticsearchmachine pushed a commit that referenced this pull request May 8, 2025
This PR is a precursor to #126492.

It does three things:
- Move org.elasticsearch.common.text.Text from :server to
  org.elasticsearch.xcontent.Text in :libs:x-content.
- Refactor the Text class to use a java-native ByteBuffer instead
  of the elasticsearch BytesReference.
- Add the XContentString interface, with the Text class implementing
  that interface.

(cherry picked from commit db0c3c7)

# Conflicts:
#	server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/DefaultHighlighter.java
#	server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java
#	server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java
#	test/framework/src/main/java/org/elasticsearch/search/SearchResponseUtils.java
ywangd pushed a commit to ywangd/elasticsearch that referenced this pull request May 9, 2025
This PR is a precursor to elastic#126492.

It does three things:
- Move org.elasticsearch.common.text.Text from :server to
  org.elasticsearch.xcontent.Text in :libs:x-content.
- Refactor the Text class to use a java-native ByteBuffer instead
  of the elasticsearch BytesReference. 
- Add the XContentString interface, with the Text class implementing
  that interface.
jfreden pushed a commit to jfreden/elasticsearch that referenced this pull request May 12, 2025
This PR is a precursor to elastic#126492.

It does three things:
- Move org.elasticsearch.common.text.Text from :server to
  org.elasticsearch.xcontent.Text in :libs:x-content.
- Refactor the Text class to use a java-native ByteBuffer instead
  of the elasticsearch BytesReference. 
- Add the XContentString interface, with the Text class implementing
  that interface.
cbuescher added a commit to cbuescher/elasticsearch that referenced this pull request May 23, 2025
After updating the Text class to use ByteBuffer in elastic#127666 we saw test
failures where similar Text instances are shared between different
threads and tested for equals(). The reason is that calling bytes()
lazily materializes the internal ByteBuffer. That method is what the
equals method calls on both instances it tests. Apparently this can
leads to race conditions when instances are shared across threads.
Making the internal `bytes` representation volatile fixes the problem.

Closes elastic#128029
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request May 26, 2025
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request May 26, 2025
JVerwolf pushed a commit that referenced this pull request May 26, 2025
* Revert "Fix the Text class package change in example plugins (#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (#127666)"

This reverts commit db0c3c7.
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request May 26, 2025
* Revert "Fix the Text class package change in example plugins (elastic#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (elastic#127666)"

This reverts commit db0c3c7.
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request May 26, 2025
* Revert "Fix the Text class package change in example plugins (elastic#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (elastic#127666)"

This reverts commit db0c3c7.
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request May 26, 2025
* Revert "Fix the Text class package change in example plugins (elastic#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (elastic#127666)"

This reverts commit db0c3c7.
elasticsearchmachine pushed a commit that referenced this pull request May 26, 2025
* Revert changes to Text class (#128483)

* Revert "Fix the Text class package change in example plugins (#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (#127666)"

This reverts commit db0c3c7.

* [CI] Auto commit changes from spotless

---------

Co-authored-by: elasticsearchmachine <[email protected]>
elasticsearchmachine pushed a commit that referenced this pull request May 27, 2025
* Revert "Fix the Text class package change in example plugins (#128316)"

This reverts commit cc48648.

* Revert "Update Text class to use native java ByteBuffer (#127666)"

This reverts commit db0c3c7.

Co-authored-by: Lorenzo Dematté <[email protected]>
jordan-powers added a commit that referenced this pull request Jun 4, 2025
This PR is a precursor to #126492.

It does three things:
1. Move org.elasticsearch.common.text.Text from :server to
   org.elasticsearch.xcontent.Text in :libs:x-content.
2. Refactor the Text class to use a new EncodedBytes record instead of
   the elasticsearch BytesReference.
3. Add the XContentString interface, with the Text class implementing
   that interface.

These changes were originally implemented in #127666 and #128316,
however they were reverted in #128484 due to problems caused by the
mutable nature of java ByteBuffers. This is resolved by instead using a
new immutable EncodedBytes record.
elasticsearchmachine pushed a commit that referenced this pull request Jun 4, 2025
This PR is a precursor to #126492.

It does three things:
1. Move org.elasticsearch.common.text.Text from :server to
   org.elasticsearch.xcontent.Text in :libs:x-content.
2. Refactor the Text class to use a new EncodedBytes record instead of
   the elasticsearch BytesReference.
3. Add the XContentString interface, with the Text class implementing
   that interface.

These changes were originally implemented in #127666 and #128316,
however they were reverted in #128484 due to problems caused by the
mutable nature of java ByteBuffers. This is resolved by instead using a
new immutable EncodedBytes record.

(cherry picked from commit de40ac4)

# Conflicts:
#	server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/DefaultHighlighter.java
#	server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java
#	server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java
#	test/framework/src/main/java/org/elasticsearch/search/SearchResponseUtils.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged :Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v8.19.0 v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants