31
31
import org .elasticsearch .common .settings .Settings ;
32
32
import org .elasticsearch .index .IndexNotFoundException ;
33
33
import org .elasticsearch .test .ESAllocationTestCase ;
34
- import org .junit .Before ;
35
34
import org .junit .Test ;
36
35
37
36
import java .util .*;
@@ -97,9 +96,8 @@ private void initPrimaries() {
97
96
}
98
97
this .clusterState = ClusterState .builder (clusterState ).nodes (discoBuilder ).build ();
99
98
RoutingAllocation .Result rerouteResult = ALLOCATION_SERVICE .reroute (clusterState );
100
- this .testRoutingTable = rerouteResult .routingTable ();
101
99
assertThat (rerouteResult .changed (), is (true ));
102
- this . clusterState = ClusterState . builder ( clusterState ). routingResult ( rerouteResult ). build ( );
100
+ applyRerouteResult ( rerouteResult );
103
101
versionsPerIndex .keySet ().forEach (this ::incrementVersion );
104
102
primaryTermsPerIndex .keySet ().forEach (this ::incrementPrimaryTerm );
105
103
}
@@ -130,11 +128,25 @@ private void startInitializingShards(String index) {
130
128
this .clusterState = ClusterState .builder (clusterState ).routingTable (this .testRoutingTable ).build ();
131
129
logger .info ("start primary shards for index " + index );
132
130
RoutingAllocation .Result rerouteResult = ALLOCATION_SERVICE .applyStartedShards (this .clusterState , this .clusterState .getRoutingNodes ().shardsWithState (index , INITIALIZING ));
133
- this . clusterState = ClusterState . builder ( clusterState ). routingTable ( rerouteResult . routingTable ()). build ();
134
- this . testRoutingTable = rerouteResult . routingTable ( );
131
+ // TODO: this simulate the code in InternalClusterState.UpdateTask.run() we should unify this.
132
+ applyRerouteResult ( rerouteResult );
135
133
incrementVersion (index );
136
134
}
137
135
136
+ private void applyRerouteResult (RoutingAllocation .Result rerouteResult ) {
137
+ ClusterState previousClusterState = this .clusterState ;
138
+ ClusterState newClusterState = ClusterState .builder (previousClusterState ).routingResult (rerouteResult ).build ();
139
+ ClusterState .Builder builder = ClusterState .builder (newClusterState ).incrementVersion ();
140
+ if (previousClusterState .routingTable () != newClusterState .routingTable ()) {
141
+ builder .routingTable (RoutingTable .builder (newClusterState .routingTable ()).version (newClusterState .routingTable ().version () + 1 ).build ());
142
+ }
143
+ if (previousClusterState .metaData () != newClusterState .metaData ()) {
144
+ builder .metaData (MetaData .builder (newClusterState .metaData ()).version (newClusterState .metaData ().version () + 1 ));
145
+ }
146
+ this .clusterState = builder .build ();
147
+ this .testRoutingTable = rerouteResult .routingTable ();
148
+ }
149
+
138
150
private void failSomePrimaries (String index ) {
139
151
this .clusterState = ClusterState .builder (clusterState ).routingTable (this .testRoutingTable ).build ();
140
152
final IndexRoutingTable indexShardRoutingTable = testRoutingTable .index (index );
@@ -151,10 +163,7 @@ private void failSomePrimaries(String index) {
151
163
incrementVersion (index , shard ); // and another time when the primary flag is set to false
152
164
}
153
165
RoutingAllocation .Result rerouteResult = ALLOCATION_SERVICE .applyFailedShards (this .clusterState , failedShards );
154
- assertThat (rerouteResult .routingTable ().version (), greaterThan (clusterState .routingTable ().version ()));
155
- assertThat (rerouteResult .metaData ().version (), greaterThan (clusterState .metaData ().version ()));
156
- this .clusterState = ClusterState .builder (clusterState ).routingResult (rerouteResult ).build ();
157
- this .testRoutingTable = rerouteResult .routingTable ();
166
+ applyRerouteResult (rerouteResult );
158
167
}
159
168
160
169
private IndexMetaData .Builder createIndexMetaData (String indexName ) {
0 commit comments