Skip to content

Commit e1fb408

Browse files
backslashtywelsch
authored andcommitted
Fixing the custom object serialization bug in diffable utils. (#39544)
While serializing custom objects, the length of the list is computed after filtering out the unsupported objects but while writing objects the filter is not applied thus resulting in writing unsupported objects which will fail to deserialize by the receiever. Adding the condition to filter out unsupported custom objects.
1 parent 46392f2 commit e1fb408

File tree

2 files changed

+148
-2
lines changed

2 files changed

+148
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,10 @@ public void writeTo(StreamOutput out) throws IOException {
474474
}
475475
out.writeVInt(upsertsCount);
476476
for (Map.Entry<K, T> entry : upserts.entrySet()) {
477-
keySerializer.writeKey(entry.getKey(), out);
478-
valueSerializer.write(entry.getValue(), out);
477+
if(valueSerializer.supportsVersion(entry.getValue(), version)) {
478+
keySerializer.writeKey(entry.getKey(), out);
479+
valueSerializer.write(entry.getValue(), out);
480+
}
479481
}
480482
}
481483
}

server/src/test/java/org/elasticsearch/cluster/serialization/ClusterSerializationTests.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
package org.elasticsearch.cluster.serialization;
2121

2222
import org.elasticsearch.Version;
23+
import org.elasticsearch.cluster.AbstractNamedDiffable;
2324
import org.elasticsearch.cluster.ClusterModule;
2425
import org.elasticsearch.cluster.ClusterName;
2526
import org.elasticsearch.cluster.ClusterState;
27+
import org.elasticsearch.cluster.ClusterState.Custom;
2628
import org.elasticsearch.cluster.Diff;
2729
import org.elasticsearch.cluster.ESAllocationTestCase;
30+
import org.elasticsearch.cluster.NamedDiff;
2831
import org.elasticsearch.cluster.RestoreInProgress;
2932
import org.elasticsearch.cluster.SnapshotDeletionsInProgress;
3033
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -39,14 +42,17 @@
3942
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
4043
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
4144
import org.elasticsearch.common.io.stream.StreamInput;
45+
import org.elasticsearch.common.io.stream.StreamOutput;
4246
import org.elasticsearch.common.settings.Settings;
47+
import org.elasticsearch.common.xcontent.XContentBuilder;
4348
import org.elasticsearch.snapshots.Snapshot;
4449
import org.elasticsearch.snapshots.SnapshotId;
4550
import org.elasticsearch.test.VersionUtils;
4651

4752
import java.io.IOException;
4853
import java.util.Arrays;
4954
import java.util.Collections;
55+
import java.util.List;
5056

5157
import static org.hamcrest.Matchers.equalTo;
5258
import static org.hamcrest.Matchers.notNullValue;
@@ -205,4 +211,142 @@ public void testObjectReuseWhenApplyingClusterStateDiff() throws Exception {
205211
assertSame("template", serializedClusterState2.metaData().templates().get("test-template"),
206212
serializedClusterState3.metaData().templates().get("test-template"));
207213
}
214+
215+
public static class TestCustomOne extends AbstractNamedDiffable<Custom> implements Custom {
216+
217+
public static final String TYPE = "test_custom_one";
218+
private final String strObject;
219+
220+
public TestCustomOne(String strObject) {
221+
this.strObject = strObject;
222+
}
223+
224+
public TestCustomOne(StreamInput in) throws IOException {
225+
this.strObject = in.readString();
226+
}
227+
228+
@Override
229+
public void writeTo(StreamOutput out) throws IOException {
230+
out.writeString(strObject);
231+
}
232+
233+
@Override
234+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
235+
builder.startObject();
236+
{
237+
builder.field("custom_string_object", strObject);
238+
}
239+
builder.endObject();
240+
return builder;
241+
}
242+
243+
@Override
244+
public String getWriteableName() {
245+
return TYPE;
246+
}
247+
248+
public static NamedDiff<Custom> readDiffFrom(StreamInput in) throws IOException {
249+
return readDiffFrom(Custom.class, TYPE, in);
250+
}
251+
252+
@Override
253+
public Version getMinimalSupportedVersion() {
254+
return Version.CURRENT;
255+
}
256+
257+
}
258+
259+
public static class TestCustomTwo extends AbstractNamedDiffable<Custom> implements Custom {
260+
261+
public static final String TYPE = "test_custom_two";
262+
private final Integer intObject;
263+
264+
public TestCustomTwo(Integer intObject) {
265+
this.intObject = intObject;
266+
}
267+
268+
public TestCustomTwo(StreamInput in) throws IOException {
269+
this.intObject = in.readInt();
270+
}
271+
272+
@Override
273+
public void writeTo(StreamOutput out) throws IOException {
274+
out.writeInt(intObject);
275+
}
276+
277+
@Override
278+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
279+
builder.startObject();
280+
{
281+
builder.field("custom_integer_object", intObject);
282+
}
283+
builder.endObject();
284+
return builder;
285+
}
286+
287+
@Override
288+
public String getWriteableName() {
289+
return TYPE;
290+
}
291+
292+
public static NamedDiff<Custom> readDiffFrom(StreamInput in) throws IOException {
293+
return readDiffFrom(Custom.class, TYPE, in);
294+
}
295+
296+
@Override
297+
public Version getMinimalSupportedVersion() {
298+
return Version.CURRENT.minimumCompatibilityVersion();
299+
}
300+
301+
}
302+
303+
public void testCustomSerialization() throws Exception {
304+
ClusterState.Builder builder = ClusterState.builder(ClusterState.EMPTY_STATE)
305+
.putCustom(TestCustomOne.TYPE, new TestCustomOne("test_custom_one"))
306+
.putCustom(TestCustomTwo.TYPE, new TestCustomTwo(10));
307+
308+
ClusterState clusterState = builder.incrementVersion().build();
309+
310+
Diff<ClusterState> diffs = clusterState.diff(ClusterState.EMPTY_STATE);
311+
312+
// Add the new customs to named writeables
313+
final List<NamedWriteableRegistry.Entry> entries = ClusterModule.getNamedWriteables();
314+
entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TestCustomOne.TYPE, TestCustomOne::new));
315+
entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, TestCustomOne.TYPE, TestCustomOne::readDiffFrom));
316+
entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TestCustomTwo.TYPE, TestCustomTwo::new));
317+
entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, TestCustomTwo.TYPE, TestCustomTwo::readDiffFrom));
318+
319+
// serialize with current version
320+
BytesStreamOutput outStream = new BytesStreamOutput();
321+
Version version = Version.CURRENT;
322+
outStream.setVersion(version);
323+
diffs.writeTo(outStream);
324+
StreamInput inStream = outStream.bytes().streamInput();
325+
326+
inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(entries));
327+
inStream.setVersion(version);
328+
Diff<ClusterState> serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
329+
ClusterState stateAfterDiffs = serializedDiffs.apply(ClusterState.EMPTY_STATE);
330+
331+
// Current version - Both the customs are non null
332+
assertThat(stateAfterDiffs.custom(TestCustomOne.TYPE), notNullValue());
333+
assertThat(stateAfterDiffs.custom(TestCustomTwo.TYPE), notNullValue());
334+
335+
// serialize with minimum compatibile version
336+
outStream = new BytesStreamOutput();
337+
version = Version.CURRENT.minimumCompatibilityVersion();
338+
outStream.setVersion(version);
339+
diffs.writeTo(outStream);
340+
inStream = outStream.bytes().streamInput();
341+
342+
inStream = new NamedWriteableAwareStreamInput(inStream, new NamedWriteableRegistry(entries));
343+
inStream.setVersion(version);
344+
serializedDiffs = ClusterState.readDiffFrom(inStream, clusterState.nodes().getLocalNode());
345+
stateAfterDiffs = serializedDiffs.apply(ClusterState.EMPTY_STATE);
346+
347+
// Old version - TestCustomOne is null and TestCustomTwo is not null
348+
assertThat(stateAfterDiffs.custom(TestCustomOne.TYPE), nullValue());
349+
assertThat(stateAfterDiffs.custom(TestCustomTwo.TYPE), notNullValue());
350+
}
351+
208352
}

0 commit comments

Comments
 (0)