Skip to content

Commit c3bb68d

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 b24b73c commit c3bb68d

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

0 commit comments

Comments
 (0)