Skip to content

Commit 4516065

Browse files
authored
Limiting node attribute serialisation in DiscoveryNode (#15341)
Signed-off-by: RS146BIJAY <rishavsagar4b1@gmail.com>
1 parent 7a9cb35 commit 4516065

File tree

20 files changed

+93
-25
lines changed

20 files changed

+93
-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;
@@ -887,13 +887,23 @@ public void writeTo(StreamOutput out) throws IOException {
887887
out.writeString(toUuid);
888888
out.writeLong(toVersion);
889889
routingTable.writeTo(out);
890-
nodes.writeTo(out);
890+
nodesWriteToWithAttributes(nodes, out);
891891
metadata.writeTo(out);
892892
blocks.writeTo(out);
893893
customs.writeTo(out);
894894
out.writeVInt(minimumClusterManagerNodesOnPublishingClusterManager);
895895
}
896896

897+
private void nodesWriteToWithAttributes(Diff<DiscoveryNodes> nodes, StreamOutput out) throws IOException {
898+
DiscoveryNodes part = nodes.apply(null);
899+
if (part != null) {
900+
out.writeBoolean(true);
901+
part.writeToWithAttribute(out);
902+
} else {
903+
out.writeBoolean(false);
904+
}
905+
}
906+
897907
@Override
898908
public ClusterState apply(ClusterState state) {
899909
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
@@ -84,7 +84,7 @@ public JoinRequest(StreamInput in) throws IOException {
8484
@Override
8585
public void writeTo(StreamOutput out) throws IOException {
8686
super.writeTo(out);
87-
sourceNode.writeTo(out);
87+
sourceNode.writeToWithAttribute(out);
8888
out.writeLong(minimumTerm);
8989
out.writeOptionalWriteable(optionalJoin.orElse(null));
9090
}

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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ public DiscoveryNode(StreamInput in) throws IOException {
330330
for (int i = 0; i < size; i++) {
331331
this.attributes.put(in.readString(), in.readString());
332332
}
333+
333334
int rolesSize = in.readVInt();
334335
final Set<DiscoveryNodeRole> roles = new HashSet<>(rolesSize);
335336
for (int i = 0; i < rolesSize; i++) {
@@ -359,13 +360,31 @@ public DiscoveryNode(StreamInput in) throws IOException {
359360

360361
@Override
361362
public void writeTo(StreamOutput out) throws IOException {
363+
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
364+
writeToUtil(out, false);
365+
} else {
366+
writeToUtil(out, true);
367+
}
368+
369+
}
370+
371+
public void writeToWithAttribute(StreamOutput out) throws IOException {
372+
writeToUtil(out, true);
373+
}
374+
375+
public void writeToUtil(StreamOutput out, boolean includeAllAttributes) throws IOException {
362376
writeNodeDetails(out);
363377

364-
out.writeVInt(attributes.size());
365-
for (Map.Entry<String, String> entry : attributes.entrySet()) {
366-
out.writeString(entry.getKey());
367-
out.writeString(entry.getValue());
378+
if (includeAllAttributes) {
379+
out.writeVInt(attributes.size());
380+
for (Map.Entry<String, String> entry : attributes.entrySet()) {
381+
out.writeString(entry.getKey());
382+
out.writeString(entry.getValue());
383+
}
384+
} else {
385+
out.writeVInt(0);
368386
}
387+
369388
writeRolesAndVersion(out);
370389
}
371390

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,18 @@ public String shortSummary() {
690690

691691
@Override
692692
public void writeTo(StreamOutput out) throws IOException {
693+
writeToUtil((output, value) -> value.writeTo(output), out);
694+
}
695+
696+
public void writeToWithAttribute(StreamOutput out) throws IOException {
697+
writeToUtil((output, value) -> value.writeToWithAttribute(output), out);
698+
}
699+
700+
public void writeToUtil(final Writer<DiscoveryNode> writer, StreamOutput out) throws IOException {
693701
writeClusterManager(out);
694702
out.writeVInt(nodes.size());
695703
for (DiscoveryNode node : this) {
696-
node.writeTo(out);
704+
writer.write(out, node);
697705
}
698706
}
699707

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)