20
20
package org .elasticsearch .indices .state ;
21
21
22
22
import com .google .common .collect .ImmutableMap ;
23
-
23
+ import org . elasticsearch . Version ;
24
24
import org .elasticsearch .action .ActionListener ;
25
25
import org .elasticsearch .action .admin .indices .mapping .put .PutMappingResponse ;
26
26
import org .elasticsearch .action .index .IndexResponse ;
27
- import org .elasticsearch .cluster .ClusterInfo ;
28
- import org .elasticsearch .cluster .ClusterState ;
29
- import org .elasticsearch .cluster .DiskUsage ;
27
+ import org .elasticsearch .cluster .*;
28
+ import org .elasticsearch .cluster .block .ClusterBlocks ;
30
29
import org .elasticsearch .cluster .metadata .IndexMetaData ;
31
30
import org .elasticsearch .cluster .metadata .MappingMetaData ;
31
+ import org .elasticsearch .cluster .metadata .MetaData ;
32
+ import org .elasticsearch .cluster .node .DiscoveryNode ;
32
33
import org .elasticsearch .cluster .node .DiscoveryNodes ;
33
34
import org .elasticsearch .cluster .routing .RoutingNodes ;
34
35
import org .elasticsearch .cluster .routing .RoutingTable ;
35
36
import org .elasticsearch .cluster .routing .ShardRouting ;
37
+ import org .elasticsearch .cluster .routing .allocation .AllocationService ;
36
38
import org .elasticsearch .cluster .routing .allocation .RoutingAllocation ;
37
39
import org .elasticsearch .cluster .routing .allocation .decider .AllocationDecider ;
38
40
import org .elasticsearch .cluster .routing .allocation .decider .AllocationDeciders ;
39
41
import org .elasticsearch .common .collect .ImmutableOpenMap ;
40
42
import org .elasticsearch .common .settings .Settings ;
43
+ import org .elasticsearch .common .transport .DummyTransportAddress ;
41
44
import org .elasticsearch .common .unit .TimeValue ;
42
45
import org .elasticsearch .discovery .DiscoveryModule ;
43
46
import org .elasticsearch .discovery .DiscoverySettings ;
52
55
import org .junit .Test ;
53
56
54
57
import java .io .IOException ;
55
- import java .util .Arrays ;
56
- import java .util .HashSet ;
57
- import java .util .List ;
58
- import java .util .Map ;
59
- import java .util .Set ;
58
+ import java .util .*;
60
59
import java .util .concurrent .atomic .AtomicReference ;
61
60
62
61
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
63
62
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertHitCount ;
64
- import static org .hamcrest .Matchers .equalTo ;
65
- import static org .hamcrest .Matchers .hasItem ;
66
- import static org .hamcrest .Matchers .hasSize ;
67
- import static org .hamcrest .Matchers .instanceOf ;
63
+ import static org .hamcrest .Matchers .*;
68
64
69
65
/**
70
66
*/
@@ -102,6 +98,72 @@ public void testUnassignedShardAndEmptyNodesInRoutingTable() throws Exception {
102
98
allocator .allocateUnassigned (routingAllocation );
103
99
}
104
100
101
+ @ Test
102
+ @ TestLogging ("gateway:TRACE" )
103
+ public void testAssignmentWithJustAddedNodes () throws Exception {
104
+ internalCluster ().startNode ();
105
+ final String index = "index" ;
106
+ prepareCreate (index ).setSettings (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 , IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).get ();
107
+ ensureGreen (index );
108
+
109
+ // close to have some unassigned started shards shards..
110
+ client ().admin ().indices ().prepareClose (index ).get ();
111
+
112
+
113
+ final String masterName = internalCluster ().getMasterName ();
114
+ final ClusterService clusterService = internalCluster ().clusterService (masterName );
115
+ final AllocationService allocationService = internalCluster ().getInstance (AllocationService .class , masterName );
116
+ clusterService .submitStateUpdateTask ("test-inject-node-and-reroute" , new ClusterStateUpdateTask () {
117
+ @ Override
118
+ public ClusterState execute (ClusterState currentState ) throws Exception {
119
+ // inject a node
120
+ ClusterState .Builder builder = ClusterState .builder (currentState );
121
+ builder .nodes (DiscoveryNodes .builder (currentState .nodes ()).put (new DiscoveryNode ("_non_existent" , DummyTransportAddress .INSTANCE , Version .CURRENT )));
122
+
123
+ // open index
124
+ final IndexMetaData indexMetaData = IndexMetaData .builder (currentState .metaData ().index (index )).state (IndexMetaData .State .OPEN ).build ();
125
+
126
+ builder .metaData (MetaData .builder (currentState .metaData ()).put (indexMetaData , true ));
127
+ builder .blocks (ClusterBlocks .builder ().blocks (currentState .blocks ()).removeIndexBlocks (index ));
128
+ ClusterState updatedState = builder .build ();
129
+
130
+ RoutingTable .Builder routingTable = RoutingTable .builder (updatedState .routingTable ());
131
+ routingTable .addAsRecovery (updatedState .metaData ().index (index ));
132
+ updatedState = ClusterState .builder (updatedState ).routingTable (routingTable ).build ();
133
+
134
+ RoutingAllocation .Result result = allocationService .reroute (updatedState );
135
+ return ClusterState .builder (updatedState ).routingResult (result ).build ();
136
+
137
+ }
138
+
139
+ @ Override
140
+ public void onFailure (String source , Throwable t ) {
141
+
142
+ }
143
+ });
144
+ ensureGreen (index );
145
+ // remove the extra node
146
+ clusterService .submitStateUpdateTask ("test-remove-injected-node" , new ClusterStateUpdateTask () {
147
+ @ Override
148
+ public ClusterState execute (ClusterState currentState ) throws Exception {
149
+ // inject a node
150
+ ClusterState .Builder builder = ClusterState .builder (currentState );
151
+ builder .nodes (DiscoveryNodes .builder (currentState .nodes ()).remove ("_non_existent" ));
152
+
153
+ currentState = builder .build ();
154
+ RoutingAllocation .Result result = allocationService .reroute (currentState );
155
+ return ClusterState .builder (currentState ).routingResult (result ).build ();
156
+
157
+ }
158
+
159
+ @ Override
160
+ public void onFailure (String source , Throwable t ) {
161
+
162
+ }
163
+ });
164
+ }
165
+
166
+
105
167
@ Test
106
168
@ TestLogging (value = "cluster.service:TRACE" )
107
169
public void testDeleteCreateInOneBulk () throws Exception {
@@ -190,6 +252,7 @@ public void testDelayedMappingPropagationOnPrimary() throws Exception {
190
252
public void onResponse (PutMappingResponse response ) {
191
253
putMappingResponse .set (response );
192
254
}
255
+
193
256
@ Override
194
257
public void onFailure (Throwable e ) {
195
258
putMappingResponse .set (e );
@@ -221,6 +284,7 @@ public void run() {
221
284
public void onResponse (IndexResponse response ) {
222
285
docIndexResponse .set (response );
223
286
}
287
+
224
288
@ Override
225
289
public void onFailure (Throwable e ) {
226
290
docIndexResponse .set (e );
@@ -304,6 +368,7 @@ public void testDelayedMappingPropagationOnReplica() throws Exception {
304
368
public void onResponse (PutMappingResponse response ) {
305
369
putMappingResponse .set (response );
306
370
}
371
+
307
372
@ Override
308
373
public void onFailure (Throwable e ) {
309
374
putMappingResponse .set (e );
@@ -329,6 +394,7 @@ public void run() {
329
394
public void onResponse (IndexResponse response ) {
330
395
docIndexResponse .set (response );
331
396
}
397
+
332
398
@ Override
333
399
public void onFailure (Throwable e ) {
334
400
docIndexResponse .set (e );
0 commit comments