13
13
import org .elasticsearch .Version ;
14
14
import org .elasticsearch .action .ActionListener ;
15
15
import org .elasticsearch .cluster .ClusterInfo ;
16
+ import org .elasticsearch .cluster .ClusterName ;
16
17
import org .elasticsearch .cluster .ClusterState ;
17
18
import org .elasticsearch .cluster .ESAllocationTestCase ;
18
19
import org .elasticsearch .cluster .TestShardRoutingRoleStrategies ;
19
20
import org .elasticsearch .cluster .metadata .IndexMetadata ;
20
21
import org .elasticsearch .cluster .metadata .Metadata ;
21
22
import org .elasticsearch .cluster .node .DiscoveryNodes ;
22
- import org .elasticsearch .cluster .routing .IndexShardRoutingTable ;
23
23
import org .elasticsearch .cluster .routing .RoutingNode ;
24
24
import org .elasticsearch .cluster .routing .RoutingNodes ;
25
25
import org .elasticsearch .cluster .routing .RoutingTable ;
34
34
import static org .elasticsearch .cluster .routing .ShardRoutingState .UNASSIGNED ;
35
35
import static org .hamcrest .Matchers .equalTo ;
36
36
import static org .hamcrest .Matchers .nullValue ;
37
+ import static org .hamcrest .Matchers .oneOf ;
37
38
38
39
public class RebalanceAfterActiveTests extends ESAllocationTestCase {
39
40
private final Logger logger = LogManager .getLogger (RebalanceAfterActiveTests .class );
40
41
41
- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/94086" )
42
42
public void testRebalanceOnlyAfterAllShardsAreActive () {
43
43
final long [] sizes = new long [5 ];
44
44
for (int i = 0 ; i < sizes .length ; i ++) {
@@ -63,18 +63,13 @@ public Long getShardSize(ShardRouting shardRouting) {
63
63
);
64
64
logger .info ("Building initial routing table" );
65
65
66
- Metadata metadata = Metadata .builder ()
67
- .put (IndexMetadata .builder ("test" ).settings (settings (Version .CURRENT )).numberOfShards (5 ).numberOfReplicas (1 ))
68
- .build ();
66
+ var indexMetadata = IndexMetadata .builder ("test" ).settings (settings (Version .CURRENT )).numberOfShards (5 ).numberOfReplicas (1 ).build ();
69
67
70
- RoutingTable initialRoutingTable = RoutingTable .builder (TestShardRoutingRoleStrategies .DEFAULT_ROLE_ONLY )
71
- .addAsNew (metadata .index ("test" ))
68
+ ClusterState clusterState = ClusterState .builder (ClusterName .DEFAULT )
69
+ .metadata (Metadata .builder ().put (indexMetadata , false ))
70
+ .routingTable (RoutingTable .builder (TestShardRoutingRoleStrategies .DEFAULT_ROLE_ONLY ).addAsNew (indexMetadata ))
72
71
.build ();
73
72
74
- ClusterState clusterState = ClusterState .builder (
75
- org .elasticsearch .cluster .ClusterName .CLUSTER_NAME_SETTING .getDefault (Settings .EMPTY )
76
- ).metadata (metadata ).routingTable (initialRoutingTable ).build ();
77
-
78
73
assertThat (clusterState .routingTable ().index ("test" ).size (), equalTo (5 ));
79
74
for (int i = 0 ; i < clusterState .routingTable ().index ("test" ).size (); i ++) {
80
75
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).size (), equalTo (2 ));
@@ -93,6 +88,7 @@ public Long getShardSize(ShardRouting shardRouting) {
93
88
for (int i = 0 ; i < clusterState .routingTable ().index ("test" ).size (); i ++) {
94
89
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).size (), equalTo (2 ));
95
90
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).primaryShard ().state (), equalTo (INITIALIZING ));
91
+ assertThat (clusterState .routingTable ().index ("test" ).shard (i ).primaryShard ().getExpectedShardSize (), equalTo (sizes [i ]));
96
92
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).state (), equalTo (UNASSIGNED ));
97
93
}
98
94
@@ -103,7 +99,7 @@ public Long getShardSize(ShardRouting shardRouting) {
103
99
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).size (), equalTo (2 ));
104
100
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).primaryShard ().state (), equalTo (STARTED ));
105
101
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).state (), equalTo (INITIALIZING ));
106
- assertEquals (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).getExpectedShardSize (), sizes [i ]);
102
+ assertThat (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).getExpectedShardSize (), equalTo ( sizes [i ]) );
107
103
}
108
104
109
105
logger .info ("now, start 8 more nodes, and check that no rebalancing/relocation have happened" );
@@ -126,44 +122,20 @@ public Long getShardSize(ShardRouting shardRouting) {
126
122
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).size (), equalTo (2 ));
127
123
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).primaryShard ().state (), equalTo (STARTED ));
128
124
assertThat (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).state (), equalTo (INITIALIZING ));
129
- assertEquals (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).getExpectedShardSize (), sizes [i ]);
130
-
125
+ assertThat (clusterState .routingTable ().index ("test" ).shard (i ).replicaShards ().get (0 ).getExpectedShardSize (), equalTo (sizes [i ]));
131
126
}
132
127
133
128
logger .info ("start the replica shards, rebalancing should start" );
134
129
clusterState = startInitializingShardsAndReroute (strategy , clusterState );
135
130
136
- // we only allow one relocation at a time
137
- assertThat (shardsWithState (clusterState .getRoutingNodes (), STARTED ).size (), equalTo (5 ));
138
- assertThat (shardsWithState (clusterState .getRoutingNodes (), RELOCATING ).size (), equalTo (5 ));
139
- for (int shardId = 0 ; shardId < clusterState .routingTable ().index ("test" ).size (); shardId ++) {
140
- int num = 0 ;
141
- final IndexShardRoutingTable shardRoutingTable = clusterState .routingTable ().index ("test" ).shard (shardId );
142
- for (int copy = 0 ; copy < shardRoutingTable .size (); copy ++) {
143
- ShardRouting routing = shardRoutingTable .shard (copy );
144
- if (routing .state () == RELOCATING || routing .state () == INITIALIZING ) {
145
- assertEquals (routing .getExpectedShardSize (), sizes [shardId ]);
146
- num ++;
147
- }
148
- }
149
- assertTrue (num > 0 );
150
- }
151
-
152
- logger .info ("complete relocation, other half of relocation should happen" );
153
- clusterState = startInitializingShardsAndReroute (strategy , clusterState );
131
+ // both primary and replica should not be rebalanced at once so 5 replicas should start moving
132
+ // unless we computed the balance where one of the indices already have both primary and replica on desired nodes
133
+ // in such case only 4 shards are immediately relocating
134
+ assertThat (shardsWithState (clusterState .getRoutingNodes (), STARTED ).size (), oneOf (5 , 6 ));
135
+ assertThat (shardsWithState (clusterState .getRoutingNodes (), RELOCATING ).size (), oneOf (4 , 5 ));
154
136
155
- // we now only relocate 3, since 2 remain where they are!
156
- assertThat (shardsWithState (clusterState .getRoutingNodes (), STARTED ).size (), equalTo (7 ));
157
- assertThat (shardsWithState (clusterState .getRoutingNodes (), RELOCATING ).size (), equalTo (3 ));
158
- for (int i = 0 ; i < clusterState .routingTable ().index ("test" ).size (); i ++) {
159
- final IndexShardRoutingTable shardRoutingTable = clusterState .routingTable ().index ("test" ).shard (i );
160
- for (int j = 0 ; j < shardRoutingTable .size (); j ++) {
161
- ShardRouting routing = shardRoutingTable .shard (j );
162
- if (routing .state () == RELOCATING || routing .state () == INITIALIZING ) {
163
- assertEquals (routing .getExpectedShardSize (), sizes [i ]);
164
- }
165
- }
166
- }
137
+ logger .info ("complete all relocations" );
138
+ clusterState = applyStartedShardsUntilNoChange (clusterState , strategy );
167
139
168
140
logger .info ("complete relocation, that's it!" );
169
141
clusterState = startInitializingShardsAndReroute (strategy , clusterState );
0 commit comments