-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Compress and cache cluster state during validate join request #7321
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
andrross
merged 19 commits into
opensearch-project:main
from
amkhar:cluster-state-node-join-optimizations
Jun 6, 2023
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9481716
Compress and cache cluster state during validate join request
94d16ec
Add changelog and license
1d29ab7
Add javadoc and correct styling
78db6b0
Add new handler for sending compressed cluster state in validate join…
41c475e
Refactor util method
8ff8cbc
optimize imports
7003af6
Use cluster state version based cache instead of time based cache
772d7d3
style fix
ac7bdd6
fix styling 2
0f3aaa8
Use concurrent hashmap instead of cache, add UT class for ClusterStat…
e2ab91e
style fix
ccdd009
Merge remote-tracking branch 'upstream/main' into cluster-state-node-…
8b3197f
Use AtomicReference instead of ConcurrentHashMap
4767c5a
Use method overloading to simplify the caller code
872e6c6
Resolve merge conflicts
d346a71
Resolve conflicts
d9092d5
Change code structure to separate the flow for JoinHelper and Publica…
ad2bdcc
resolve conflicts
5605928
Remove unnecessary input.setVersion line
amkhar 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
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
98 changes: 98 additions & 0 deletions
98
server/src/main/java/org/opensearch/cluster/coordination/ClusterStateUtils.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,98 @@ | ||
| /* | ||
| * 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.cluster.coordination; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.cluster.ClusterState; | ||
| import org.opensearch.cluster.Diff; | ||
| import org.opensearch.cluster.node.DiscoveryNode; | ||
| import org.opensearch.common.bytes.BytesReference; | ||
| import org.opensearch.common.compress.Compressor; | ||
| import org.opensearch.common.compress.CompressorFactory; | ||
| import org.opensearch.common.io.stream.BytesStreamOutput; | ||
| import org.opensearch.common.io.stream.InputStreamStreamInput; | ||
| import org.opensearch.common.io.stream.NamedWriteableAwareStreamInput; | ||
| import org.opensearch.common.io.stream.NamedWriteableRegistry; | ||
| import org.opensearch.common.io.stream.OutputStreamStreamOutput; | ||
| import org.opensearch.common.io.stream.StreamInput; | ||
| import org.opensearch.common.io.stream.StreamOutput; | ||
| import org.opensearch.common.io.stream.Writeable; | ||
| import org.opensearch.transport.BytesTransportRequest; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * A helper class to utilize the compressed stream. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public final class ClusterStateUtils { | ||
|
amkhar marked this conversation as resolved.
Outdated
amkhar marked this conversation as resolved.
Outdated
|
||
| private static final Logger logger = LogManager.getLogger(ClusterStateUtils.class); | ||
|
|
||
| /** | ||
| * Serialize the given cluster state or diff. It'll always use compression before writing on a newly created output | ||
| * stream. | ||
| * | ||
| * @param writer Object which is going to write the content | ||
| * @param node version of cluster node | ||
| * @param isFullClusterState flag used at receiver end to make intelligent decisions. For example, ClusterState | ||
| * assumes full state of diff of the states based on this flag. | ||
| * @return reference to serialized bytes | ||
| * @throws IOException if writing on the compressed stream is failed. | ||
| */ | ||
| public static BytesReference serializeClusterState(Writeable writer, DiscoveryNode node, boolean isFullClusterState) | ||
|
amkhar marked this conversation as resolved.
Outdated
|
||
| throws IOException { | ||
| final BytesStreamOutput bStream = new BytesStreamOutput(); | ||
| try (StreamOutput stream = new OutputStreamStreamOutput(CompressorFactory.COMPRESSOR.threadLocalOutputStream(bStream))) { | ||
| stream.setVersion(node.getVersion()); | ||
| stream.writeBoolean(isFullClusterState); | ||
| writer.writeTo(stream); | ||
| } | ||
| final BytesReference serializedByteRef = bStream.bytes(); | ||
| logger.trace("serialized writable object for node version [{}] with size [{}]", node.getVersion(), serializedByteRef.length()); | ||
| return serializedByteRef; | ||
| } | ||
|
|
||
| /** | ||
| * Decompress the incoming compressed BytesTransportRequest into StreamInput which can be deserialized. | ||
| * @param request incoming compressed request in bytes form | ||
| * @param namedWriteableRegistry existing registry of readers which contains ClusterState writable | ||
| * @return StreamInput object containing uncompressed request sent by sender | ||
| * @throws IOException if creating StreamInput object fails due to EOF | ||
| */ | ||
| public static StreamInput decompressClusterState(BytesTransportRequest request, NamedWriteableRegistry namedWriteableRegistry) | ||
| throws IOException { | ||
| final Compressor compressor = CompressorFactory.compressor(request.bytes()); | ||
| StreamInput in = request.bytes().streamInput(); | ||
| if (compressor != null) { | ||
| in = new InputStreamStreamInput(compressor.threadLocalInputStream(in)); | ||
| } | ||
| in = new NamedWriteableAwareStreamInput(in, namedWriteableRegistry); | ||
| in.setVersion(request.version()); | ||
| return in; | ||
| } | ||
|
|
||
| public static ClusterState deserializeFullClusterState(StreamInput in, DiscoveryNode localNode) throws IOException { | ||
| final ClusterState incomingState; | ||
| try (StreamInput input = in) { | ||
| incomingState = ClusterState.readFrom(input, localNode); | ||
| } | ||
| return incomingState; | ||
| } | ||
|
|
||
| public static Diff<ClusterState> deserializeClusterStateDiff(StreamInput in, DiscoveryNode localNode) throws IOException { | ||
| final Diff<ClusterState> incomingStateDiff; | ||
| // Close stream early to release resources used by the de-compression as early as possible | ||
| try (StreamInput input = in) { | ||
| incomingStateDiff = ClusterState.readDiffFrom(input, localNode); | ||
| } | ||
| return incomingStateDiff; | ||
| } | ||
| } | ||
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
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.