Repository navigation
Use binary serde for resource usage headers #21230
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
cwperks
merged 5 commits into
opensearch-project:main
from
dzane17:binary-resource-usage-header
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68fc2bf
Use binary serde for resource usage headers
dzane17 ffe1e5a
Add json option for bwc
dzane17 3f95642
Add dynamic enablement flag
dzane17 f416371
Extract generic Writeable Base64 codec into shared helper
dzane17 2e2537a
Gate binary resource usage header on 3.8.0
dzane17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
server/src/main/java/org/opensearch/common/io/stream/WriteableBase64.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.common.io.stream; | ||
|
|
||
| import org.opensearch.common.annotation.InternalApi; | ||
| import org.opensearch.core.common.bytes.BytesReference; | ||
| import org.opensearch.core.common.io.stream.StreamInput; | ||
| import org.opensearch.core.common.io.stream.Writeable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
|
|
||
| /** | ||
| * Utility for round-tripping a {@link Writeable} through its binary wire format encoded as a | ||
| * Base64 string. This is useful when a {@link Writeable} needs to travel over a channel that only | ||
| * carries strings (for example a {@code ThreadContext} response header), where the binary form is | ||
| * both cheaper to produce/consume than JSON and more compact once Base64-encoded. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| @InternalApi | ||
| public final class WriteableBase64 { | ||
|
|
||
| private WriteableBase64() {} | ||
|
|
||
| /** | ||
| * Serializes a {@link Writeable} to its binary wire format and Base64-encodes the result. | ||
| * | ||
| * @param writeable the object to serialize | ||
| * @return a Base64-encoded string of the object's binary representation | ||
| * @throws IOException if serialization fails | ||
| */ | ||
| public static String encode(Writeable writeable) throws IOException { | ||
| try (BytesStreamOutput out = new BytesStreamOutput()) { | ||
| writeable.writeTo(out); | ||
| byte[] bytes = BytesReference.toBytes(out.bytes()); | ||
| return Base64.getEncoder().encodeToString(bytes); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Decodes a Base64 string produced by {@link #encode(Writeable)} back into an object using the | ||
| * supplied reader. | ||
| * | ||
| * @param encoded the Base64-encoded string | ||
| * @param reader reconstructs the object from a {@link StreamInput} (for example | ||
| * {@code MyType::readFromStream} or {@code MyType::new}) | ||
| * @param <T> the type produced by {@code reader} | ||
| * @return the deserialized object | ||
| * @throws IOException if deserialization fails | ||
| * @throws IllegalArgumentException if the input is not valid Base64 | ||
| */ | ||
| public static <T> T decode(String encoded, Writeable.Reader<T> reader) throws IOException { | ||
| byte[] bytes = Base64.getDecoder().decode(encoded); | ||
| try (StreamInput in = StreamInput.wrap(bytes)) { | ||
| return reader.read(in); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
server/src/test/java/org/opensearch/common/io/stream/WriteableBase64Tests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.common.io.stream; | ||
|
|
||
| import org.opensearch.core.common.io.stream.StreamInput; | ||
| import org.opensearch.core.common.io.stream.StreamOutput; | ||
| import org.opensearch.core.common.io.stream.Writeable; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.Objects; | ||
|
|
||
| public class WriteableBase64Tests extends OpenSearchTestCase { | ||
|
|
||
| /** A minimal {@link Writeable} used to exercise the codec independently of any production type. */ | ||
| private static class Sample implements Writeable { | ||
| private final String name; | ||
| private final long value; | ||
|
|
||
| Sample(String name, long value) { | ||
| this.name = name; | ||
| this.value = value; | ||
| } | ||
|
|
||
| Sample(StreamInput in) throws IOException { | ||
| this.name = in.readString(); | ||
| this.value = in.readLong(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeString(name); | ||
| out.writeLong(value); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Sample sample = (Sample) o; | ||
| return value == sample.value && Objects.equals(name, sample.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, value); | ||
| } | ||
| } | ||
|
|
||
| public void testRoundTrip() throws IOException { | ||
| Sample original = new Sample(randomAlphaOfLength(12), randomLong()); | ||
|
|
||
| String encoded = WriteableBase64.encode(original); | ||
| Sample decoded = WriteableBase64.decode(encoded, Sample::new); | ||
|
|
||
| assertEquals(original, decoded); | ||
| } | ||
|
|
||
| public void testEncodeProducesValidBase64() throws IOException { | ||
| Sample original = new Sample("action", 42L); | ||
|
|
||
| String encoded = WriteableBase64.encode(original); | ||
|
|
||
| // Must decode as standard Base64 without throwing; the resulting bytes reconstruct the object. | ||
| byte[] decodedBytes = Base64.getDecoder().decode(encoded); | ||
| try (StreamInput in = StreamInput.wrap(decodedBytes)) { | ||
| assertEquals(original, new Sample(in)); | ||
| } | ||
| } | ||
|
|
||
| public void testEmptyStringThrows() { | ||
| // An empty payload has no bytes for the reader to consume. | ||
| expectThrows(Exception.class, () -> WriteableBase64.decode("", Sample::new)); | ||
| } | ||
|
|
||
| public void testInvalidBase64Throws() { | ||
| expectThrows(IllegalArgumentException.class, () -> WriteableBase64.decode("not-valid-base64!!!", Sample::new)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.