|
28 | 28 | import org.elasticsearch.cluster.RestoreInProgress;
|
29 | 29 | import org.elasticsearch.cluster.SnapshotDeletionsInProgress;
|
30 | 30 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
| 31 | +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; |
31 | 32 | import org.elasticsearch.cluster.metadata.MetaData;
|
32 | 33 | import org.elasticsearch.cluster.node.DiscoveryNodes;
|
33 | 34 | import org.elasticsearch.cluster.routing.RoutingTable;
|
|
43 | 44 | import org.elasticsearch.snapshots.SnapshotId;
|
44 | 45 | import org.elasticsearch.test.VersionUtils;
|
45 | 46 |
|
| 47 | +import java.io.IOException; |
| 48 | +import java.util.ArrayList; |
46 | 49 | import java.util.Collections;
|
47 | 50 |
|
48 | 51 | import static org.hamcrest.Matchers.equalTo;
|
@@ -154,4 +157,58 @@ public void testSnapshotDeletionsInProgressSerialization() throws Exception {
|
154 | 157 | assertThat(stateAfterDiffs.custom(SnapshotDeletionsInProgress.TYPE), notNullValue());
|
155 | 158 | }
|
156 | 159 |
|
| 160 | + private ClusterState updateUsingSerialisedDiff(ClusterState original, Diff<ClusterState> diff) throws IOException { |
| 161 | + BytesStreamOutput outStream = new BytesStreamOutput(); |
| 162 | + outStream.setVersion(Version.CURRENT); |
| 163 | + diff.writeTo(outStream); |
| 164 | + StreamInput inStream = new NamedWriteableAwareStreamInput(outStream.bytes().streamInput(), |
| 165 | + new NamedWriteableRegistry(ClusterModule.getNamedWriteables())); |
| 166 | + diff = ClusterState.readDiffFrom(inStream, newNode("node-name")); |
| 167 | + return diff.apply(original); |
| 168 | + } |
| 169 | + |
| 170 | + public void testObjectReuseWhenApplyingClusterStateDiff() throws Exception { |
| 171 | + IndexMetaData indexMetaData |
| 172 | + = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1).build(); |
| 173 | + IndexTemplateMetaData indexTemplateMetaData |
| 174 | + = IndexTemplateMetaData.builder("test-template").patterns(new ArrayList<>()).build(); |
| 175 | + MetaData metaData = MetaData.builder().put(indexMetaData, true).put(indexTemplateMetaData).build(); |
| 176 | + |
| 177 | + RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build(); |
| 178 | + |
| 179 | + ClusterState clusterState1 = ClusterState.builder(new ClusterName("clusterName1")) |
| 180 | + .metaData(metaData).routingTable(routingTable).build(); |
| 181 | + BytesStreamOutput outStream = new BytesStreamOutput(); |
| 182 | + outStream.setVersion(Version.CURRENT); |
| 183 | + clusterState1.writeTo(outStream); |
| 184 | + StreamInput inStream = new NamedWriteableAwareStreamInput(outStream.bytes().streamInput(), |
| 185 | + new NamedWriteableRegistry(ClusterModule.getNamedWriteables())); |
| 186 | + ClusterState serializedClusterState1 = ClusterState.readFrom(inStream, newNode("node4")); |
| 187 | + |
| 188 | + // Create a new, albeit equal, IndexMetadata object |
| 189 | + ClusterState clusterState2 = ClusterState.builder(clusterState1).incrementVersion() |
| 190 | + .metaData(MetaData.builder().put(IndexMetaData.builder(indexMetaData).numberOfReplicas(1).build(), true)).build(); |
| 191 | + assertNotSame("Should have created a new, equivalent, IndexMetaData object in clusterState2", |
| 192 | + clusterState1.metaData().index("test"), clusterState2.metaData().index("test")); |
| 193 | + |
| 194 | + ClusterState serializedClusterState2 = updateUsingSerialisedDiff(serializedClusterState1, clusterState2.diff(clusterState1)); |
| 195 | + assertSame("Unchanged metadata should not create new IndexMetaData objects", |
| 196 | + serializedClusterState1.metaData().index("test"), serializedClusterState2.metaData().index("test")); |
| 197 | + assertSame("Unchanged routing table should not create new IndexRoutingTable objects", |
| 198 | + serializedClusterState1.routingTable().index("test"), serializedClusterState2.routingTable().index("test")); |
| 199 | + |
| 200 | + // Create a new and different IndexMetadata object |
| 201 | + ClusterState clusterState3 = ClusterState.builder(clusterState1).incrementVersion() |
| 202 | + .metaData(MetaData.builder().put(IndexMetaData.builder(indexMetaData).numberOfReplicas(2).build(), true)).build(); |
| 203 | + ClusterState serializedClusterState3 = updateUsingSerialisedDiff(serializedClusterState2, clusterState3.diff(clusterState2)); |
| 204 | + assertNotEquals("Should have a new IndexMetaData object", |
| 205 | + serializedClusterState2.metaData().index("test"), serializedClusterState3.metaData().index("test")); |
| 206 | + assertSame("Unchanged routing table should not create new IndexRoutingTable objects", |
| 207 | + serializedClusterState2.routingTable().index("test"), serializedClusterState3.routingTable().index("test")); |
| 208 | + |
| 209 | + assertSame("nodes", serializedClusterState2.nodes(), serializedClusterState3.nodes()); |
| 210 | + assertSame("blocks", serializedClusterState2.blocks(), serializedClusterState3.blocks()); |
| 211 | + assertSame("template", serializedClusterState2.metaData().templates().get("test-template"), |
| 212 | + serializedClusterState3.metaData().templates().get("test-template")); |
| 213 | + } |
157 | 214 | }
|
0 commit comments