Skip to content

Commit 67eceaa

Browse files
authored
Serializing node attribute in discoveryNode only in scenarioes where it is required (#15617)
Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
1 parent bd57e5d commit 67eceaa

File tree

20 files changed

+91
-25
lines changed

20 files changed

+91
-25
lines changed

libs/core/src/main/java/org/opensearch/core/common/io/stream/StreamOutput.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,13 @@ public <T extends Writeable> void writeOptionalArray(@Nullable T[] array) throws
969969
}
970970

971971
public void writeOptionalWriteable(@Nullable Writeable writeable) throws IOException {
972+
writeOptionalWriteable((out, writable) -> writable.writeTo(out), writeable);
973+
}
974+
975+
public <T extends Writeable> void writeOptionalWriteable(final Writer<T> writer, @Nullable T writeable) throws IOException {
972976
if (writeable != null) {
973977
writeBoolean(true);
974-
writeable.writeTo(this);
978+
writer.write(this, writeable);
975979
} else {
976980
writeBoolean(false);
977981
}

server/src/main/java/org/opensearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public ClusterAllocationExplanation(StreamInput in) throws IOException {
9595
@Override
9696
public void writeTo(StreamOutput out) throws IOException {
9797
shardRouting.writeTo(out);
98-
out.writeOptionalWriteable(currentNode);
98+
out.writeOptionalWriteable((stream, node) -> node.writeToWithAttribute(stream), currentNode);
9999
out.writeOptionalWriteable(relocationTargetNode);
100100
out.writeOptionalWriteable(clusterInfo);
101101
shardAllocationDecision.writeTo(out);

server/src/main/java/org/opensearch/action/support/nodes/BaseNodeResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public DiscoveryNode getNode() {
6767

6868
@Override
6969
public void writeTo(StreamOutput out) throws IOException {
70-
node.writeTo(out);
70+
node.writeToWithAttribute(out);
7171
}
7272
}

server/src/main/java/org/opensearch/cluster/ClusterState.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public void writeTo(StreamOutput out) throws IOException {
781781
out.writeString(stateUUID);
782782
metadata.writeTo(out);
783783
routingTable.writeTo(out);
784-
nodes.writeTo(out);
784+
nodes.writeToWithAttribute(out);
785785
blocks.writeTo(out);
786786
// filter out custom states not supported by the other node
787787
int numberOfCustoms = 0;
@@ -859,13 +859,23 @@ public void writeTo(StreamOutput out) throws IOException {
859859
out.writeString(toUuid);
860860
out.writeLong(toVersion);
861861
routingTable.writeTo(out);
862-
nodes.writeTo(out);
862+
nodesWriteToWithAttributes(nodes, out);
863863
metadata.writeTo(out);
864864
blocks.writeTo(out);
865865
customs.writeTo(out);
866866
out.writeVInt(minimumClusterManagerNodesOnPublishingClusterManager);
867867
}
868868

869+
private void nodesWriteToWithAttributes(Diff<DiscoveryNodes> nodes, StreamOutput out) throws IOException {
870+
DiscoveryNodes part = nodes.apply(null);
871+
if (part != null) {
872+
out.writeBoolean(true);
873+
part.writeToWithAttribute(out);
874+
} else {
875+
out.writeBoolean(false);
876+
}
877+
}
878+
869879
@Override
870880
public ClusterState apply(ClusterState state) {
871881
Builder builder = new Builder(clusterName);

server/src/main/java/org/opensearch/cluster/coordination/Join.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public Join(StreamInput in) throws IOException {
7878

7979
@Override
8080
public void writeTo(StreamOutput out) throws IOException {
81-
sourceNode.writeTo(out);
82-
targetNode.writeTo(out);
81+
sourceNode.writeToWithAttribute(out);
82+
targetNode.writeToWithAttribute(out);
8383
out.writeLong(term);
8484
out.writeLong(lastAcceptedTerm);
8585
out.writeLong(lastAcceptedVersion);

server/src/main/java/org/opensearch/cluster/coordination/JoinRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public JoinRequest(StreamInput in) throws IOException {
8989
@Override
9090
public void writeTo(StreamOutput out) throws IOException {
9191
super.writeTo(out);
92-
sourceNode.writeTo(out);
92+
sourceNode.writeToWithAttribute(out);
9393
if (out.getVersion().onOrAfter(LegacyESVersion.V_7_7_0)) {
9494
out.writeLong(minimumTerm);
9595
}

server/src/main/java/org/opensearch/cluster/coordination/StartJoinRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public StartJoinRequest(StreamInput input) throws IOException {
6464
@Override
6565
public void writeTo(StreamOutput out) throws IOException {
6666
super.writeTo(out);
67-
sourceNode.writeTo(out);
67+
sourceNode.writeToWithAttribute(out);
6868
out.writeLong(term);
6969
}
7070

server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,34 @@ public DiscoveryNode(StreamInput in) throws IOException {
385385

386386
@Override
387387
public void writeTo(StreamOutput out) throws IOException {
388+
if (out.getVersion().onOrAfter(Version.V_2_17_0)) {
389+
writeToUtil(out, false);
390+
} else {
391+
writeToUtil(out, true);
392+
}
393+
}
394+
395+
public void writeToWithAttribute(StreamOutput out) throws IOException {
396+
writeToUtil(out, true);
397+
}
398+
399+
public void writeToUtil(StreamOutput out, boolean includeAllAttributes) throws IOException {
388400
out.writeString(nodeName);
389401
out.writeString(nodeId);
390402
out.writeString(ephemeralId);
391403
out.writeString(hostName);
392404
out.writeString(hostAddress);
393405
address.writeTo(out);
394-
out.writeVInt(attributes.size());
395-
for (Map.Entry<String, String> entry : attributes.entrySet()) {
396-
out.writeString(entry.getKey());
397-
out.writeString(entry.getValue());
406+
if (includeAllAttributes) {
407+
out.writeVInt(attributes.size());
408+
for (Map.Entry<String, String> entry : attributes.entrySet()) {
409+
out.writeString(entry.getKey());
410+
out.writeString(entry.getValue());
411+
}
412+
} else {
413+
out.writeVInt(0);
398414
}
415+
399416
if (out.getVersion().onOrAfter(LegacyESVersion.V_7_3_0)) {
400417
out.writeVInt(roles.size());
401418
for (final DiscoveryNodeRole role : roles) {

server/src/main/java/org/opensearch/cluster/node/DiscoveryNodes.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,14 @@ public String shortSummary() {
701701

702702
@Override
703703
public void writeTo(StreamOutput out) throws IOException {
704+
writeToUtil((output, value) -> value.writeTo(output), out);
705+
}
706+
707+
public void writeToWithAttribute(StreamOutput out) throws IOException {
708+
writeToUtil((output, value) -> value.writeToWithAttribute(output), out);
709+
}
710+
711+
private void writeToUtil(final Writer<DiscoveryNode> writer, StreamOutput out) throws IOException {
704712
if (clusterManagerNodeId == null) {
705713
out.writeBoolean(false);
706714
} else {
@@ -709,7 +717,7 @@ public void writeTo(StreamOutput out) throws IOException {
709717
}
710718
out.writeVInt(nodes.size());
711719
for (DiscoveryNode node : this) {
712-
node.writeTo(out);
720+
writer.write(out, node);
713721
}
714722
}
715723

server/src/main/java/org/opensearch/cluster/routing/allocation/AbstractAllocationDecision.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public List<NodeAllocationResult> getNodeDecisions() {
107107

108108
@Override
109109
public void writeTo(StreamOutput out) throws IOException {
110-
out.writeOptionalWriteable(targetNode);
110+
out.writeOptionalWriteable((stream, node) -> node.writeToWithAttribute(stream), targetNode);
111111
if (nodeDecisions != null) {
112112
out.writeBoolean(true);
113113
out.writeList(nodeDecisions);

0 commit comments

Comments
 (0)