|
26 | 26 | import org.elasticsearch.cluster.metadata.MetadataTests; |
27 | 27 | import org.elasticsearch.cluster.metadata.ProjectId; |
28 | 28 | import org.elasticsearch.cluster.metadata.ProjectMetadata; |
| 29 | +import org.elasticsearch.cluster.metadata.ReservedStateHandlerMetadata; |
| 30 | +import org.elasticsearch.cluster.metadata.ReservedStateMetadata; |
29 | 31 | import org.elasticsearch.cluster.node.DiscoveryNode; |
30 | 32 | import org.elasticsearch.cluster.node.DiscoveryNodeUtils; |
31 | 33 | import org.elasticsearch.cluster.node.DiscoveryNodes; |
|
44 | 46 | import org.elasticsearch.common.bytes.BytesArray; |
45 | 47 | import org.elasticsearch.common.bytes.BytesReference; |
46 | 48 | import org.elasticsearch.common.collect.Iterators; |
| 49 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 50 | +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; |
| 51 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
47 | 52 | import org.elasticsearch.common.settings.Setting; |
48 | 53 | import org.elasticsearch.common.settings.Settings; |
49 | 54 | import org.elasticsearch.common.transport.TransportAddress; |
|
85 | 90 | import static java.util.Collections.singletonMap; |
86 | 91 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED; |
87 | 92 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; |
| 93 | +import static org.hamcrest.Matchers.aMapWithSize; |
| 94 | +import static org.hamcrest.Matchers.contains; |
| 95 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
88 | 96 | import static org.hamcrest.Matchers.containsString; |
89 | 97 | import static org.hamcrest.Matchers.equalTo; |
| 98 | +import static org.hamcrest.Matchers.hasKey; |
90 | 99 | import static org.hamcrest.Matchers.instanceOf; |
91 | 100 | import static org.hamcrest.Matchers.not; |
92 | 101 | import static org.hamcrest.Matchers.notNullValue; |
@@ -515,8 +524,7 @@ public void testToXContentWithMultipleProjects() throws IOException { |
515 | 524 | "event_ingested_range": { "shards": [] } |
516 | 525 | } |
517 | 526 | }, |
518 | | - "index-graveyard": { "tombstones": [] }, |
519 | | - "reserved_state": {} |
| 527 | + "index-graveyard": { "tombstones": [] } |
520 | 528 | }, |
521 | 529 | { |
522 | 530 | "id": "3LftaL7hgfXAsF60Gm6jcD", |
@@ -573,15 +581,13 @@ public void testToXContentWithMultipleProjects() throws IOException { |
573 | 581 | "event_ingested_range": { "shards": [] } |
574 | 582 | } |
575 | 583 | }, |
576 | | - "index-graveyard": { "tombstones": [] }, |
577 | | - "reserved_state": {} |
| 584 | + "index-graveyard": { "tombstones": [] } |
578 | 585 | }, |
579 | 586 | { |
580 | 587 | "id": "WHyuJ0uqBYOPgHX9kYUXlZ", |
581 | 588 | "templates": {}, |
582 | 589 | "indices": {}, |
583 | | - "index-graveyard": { "tombstones": [] }, |
584 | | - "reserved_state": {} |
| 590 | + "index-graveyard": { "tombstones": [] } |
585 | 591 | } |
586 | 592 | ], |
587 | 593 | "reserved_state": {} |
@@ -2241,4 +2247,62 @@ public static int expectedChunkCount(ToXContent.Params params, ClusterState clus |
2241 | 2247 |
|
2242 | 2248 | return Math.toIntExact(chunkCount); |
2243 | 2249 | } |
| 2250 | + |
| 2251 | + public void testSerialization() throws IOException { |
| 2252 | + ClusterState clusterState = buildClusterState(); |
| 2253 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 2254 | + clusterState.writeTo(out); |
| 2255 | + |
| 2256 | + // check it deserializes ok |
| 2257 | + NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables()); |
| 2258 | + ClusterState deserialisedClusterState = ClusterState.readFrom(new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry), null); |
| 2259 | + |
| 2260 | + // check it matches the original object |
| 2261 | + Metadata deserializedMetadata = deserialisedClusterState.metadata(); |
| 2262 | + assertThat(deserializedMetadata.projects(), aMapWithSize(1)); |
| 2263 | + assertThat(deserializedMetadata.projects(), hasKey(ProjectId.DEFAULT)); |
| 2264 | + |
| 2265 | + assertThat(deserializedMetadata.getProject(ProjectId.DEFAULT).templates(), hasKey("template")); |
| 2266 | + assertThat(deserializedMetadata.getProject(ProjectId.DEFAULT).indices(), hasKey("index")); |
| 2267 | + } |
| 2268 | + |
| 2269 | + public void testCombinedReservedMetadataSerialization() throws IOException { |
| 2270 | + ClusterState clusterState = ClusterState.builder(buildClusterState()) |
| 2271 | + .putCustom( |
| 2272 | + ProjectStateRegistry.TYPE, |
| 2273 | + ProjectStateRegistry.builder() |
| 2274 | + .putReservedStateMetadata(ProjectId.DEFAULT, ReservedStateMetadata.builder("file_settings").putHandler( |
| 2275 | + new ReservedStateHandlerMetadata("settings", Set.of(PROJECT_SETTING.getKey(), PROJECT_SETTING2.getKey())) |
| 2276 | + ).build()) |
| 2277 | + .build() |
| 2278 | + ) |
| 2279 | + .build(); |
| 2280 | + |
| 2281 | + TransportVersion oldVersion = TransportVersionUtils.getPreviousVersion(TransportVersions.MULTI_PROJECT); |
| 2282 | + |
| 2283 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 2284 | + out.setTransportVersion(oldVersion); |
| 2285 | + clusterState.writeTo(out); |
| 2286 | + |
| 2287 | + // check it deserializes ok |
| 2288 | + NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables()); |
| 2289 | + NamedWriteableAwareStreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry); |
| 2290 | + in.setTransportVersion(oldVersion); |
| 2291 | + ClusterState deserialisedClusterState = ClusterState.readFrom(in, null); |
| 2292 | + |
| 2293 | + // check it matches the original object |
| 2294 | + Metadata deserializedMetadata = deserialisedClusterState.metadata(); |
| 2295 | + assertThat(deserializedMetadata.projects(), aMapWithSize(1)); |
| 2296 | + assertThat(deserializedMetadata.projects(), hasKey(ProjectId.DEFAULT)); |
| 2297 | + |
| 2298 | + assertThat(deserializedMetadata.getProject(ProjectId.DEFAULT).templates(), hasKey("template")); |
| 2299 | + assertThat(deserializedMetadata.getProject(ProjectId.DEFAULT).indices(), hasKey("index")); |
| 2300 | + |
| 2301 | + assertThat(deserializedMetadata.reservedStateMetadata(), hasKey("file_settings")); |
| 2302 | + ReservedStateMetadata fileSettings = deserializedMetadata.reservedStateMetadata().get("file_settings"); |
| 2303 | + assertThat(fileSettings.handlers(), aMapWithSize(1)); |
| 2304 | + assertThat(fileSettings.handlers(), hasKey("settings")); |
| 2305 | + ReservedStateHandlerMetadata settingsHandlerMetadata = fileSettings.handlers().get("settings"); |
| 2306 | + assertThat(settingsHandlerMetadata.keys(), equalTo(Set.of(PROJECT_SETTING.getKey(), PROJECT_SETTING2.getKey()))); |
| 2307 | + } |
2244 | 2308 | } |
0 commit comments