|
20 | 20 | package org.elasticsearch.cluster.serialization;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.Version;
|
| 23 | +import org.elasticsearch.cluster.AbstractNamedDiffable; |
23 | 24 | import org.elasticsearch.cluster.ClusterModule;
|
24 | 25 | import org.elasticsearch.cluster.ClusterName;
|
25 | 26 | import org.elasticsearch.cluster.ClusterState;
|
| 27 | +import org.elasticsearch.cluster.ClusterState.Custom; |
26 | 28 | import org.elasticsearch.cluster.Diff;
|
27 | 29 | import org.elasticsearch.cluster.ESAllocationTestCase;
|
| 30 | +import org.elasticsearch.cluster.NamedDiff; |
28 | 31 | import org.elasticsearch.cluster.RestoreInProgress;
|
29 | 32 | import org.elasticsearch.cluster.SnapshotDeletionsInProgress;
|
30 | 33 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
39 | 42 | import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
|
40 | 43 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
41 | 44 | import org.elasticsearch.common.io.stream.StreamInput;
|
| 45 | +import org.elasticsearch.common.io.stream.StreamOutput; |
42 | 46 | import org.elasticsearch.common.settings.Settings;
|
| 47 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
43 | 48 | import org.elasticsearch.snapshots.Snapshot;
|
44 | 49 | import org.elasticsearch.snapshots.SnapshotId;
|
45 | 50 | import org.elasticsearch.test.VersionUtils;
|
46 | 51 |
|
47 | 52 | import java.io.IOException;
|
48 | 53 | import java.util.Arrays;
|
49 | 54 | import java.util.Collections;
|
| 55 | +import java.util.List; |
50 | 56 |
|
51 | 57 | import static org.hamcrest.Matchers.equalTo;
|
52 | 58 | import static org.hamcrest.Matchers.notNullValue;
|
@@ -218,4 +224,142 @@ public void testObjectReuseWhenApplyingClusterStateDiff() throws Exception {
|
218 | 224 | assertSame("template", serializedClusterState2.metaData().templates().get("test-template"),
|
219 | 225 | serializedClusterState3.metaData().templates().get("test-template"));
|
220 | 226 | }
|
| 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 | + |
221 | 365 | }
|
0 commit comments