20
20
package org .elasticsearch .cluster ;
21
21
22
22
import org .elasticsearch .action .UnavailableShardsException ;
23
+ import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
23
24
import org .elasticsearch .action .index .IndexResponse ;
24
25
import org .elasticsearch .action .support .ActiveShardCount ;
25
26
import org .elasticsearch .client .Requests ;
27
+ import org .elasticsearch .cluster .health .ClusterHealthStatus ;
28
+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
26
29
import org .elasticsearch .common .Priority ;
27
30
import org .elasticsearch .common .settings .Settings ;
28
31
import org .elasticsearch .common .xcontent .XContentType ;
35
38
import static org .elasticsearch .common .unit .TimeValue .timeValueSeconds ;
36
39
import static org .hamcrest .Matchers .equalTo ;
37
40
38
- @ ClusterScope (scope = Scope .TEST , numDataNodes =0 )
41
+ @ ClusterScope (scope = Scope .TEST , numDataNodes = 0 )
39
42
public class SimpleDataNodesIT extends ESIntegTestCase {
40
- public void testDataNodes () throws Exception {
43
+
44
+ private static final String SOURCE = "{\" type1\" :{\" id\" :\" 1\" ,\" name\" :\" test\" }}" ;
45
+
46
+ public void testIndexingBeforeAndAfterDataNodesStart () {
41
47
internalCluster ().startNode (Settings .builder ().put (Node .NODE_DATA_SETTING .getKey (), false ).build ());
42
48
client ().admin ().indices ().create (createIndexRequest ("test" ).waitForActiveShards (ActiveShardCount .NONE )).actionGet ();
43
49
try {
44
- client ().index (Requests .indexRequest ("test" ).type ( "type1" ). id ("1" ).source (source ( "1" , "test" ) , XContentType .JSON )
50
+ client ().index (Requests .indexRequest ("test" ).id ("1" ).source (SOURCE , XContentType .JSON )
45
51
.timeout (timeValueSeconds (1 ))).actionGet ();
46
52
fail ("no allocation should happen" );
47
53
} catch (UnavailableShardsException e ) {
@@ -54,7 +60,7 @@ public void testDataNodes() throws Exception {
54
60
55
61
// still no shard should be allocated
56
62
try {
57
- client ().index (Requests .indexRequest ("test" ).type ( "type1" ). id ("1" ).source (source ( "1" , "test" ) , XContentType .JSON )
63
+ client ().index (Requests .indexRequest ("test" ).id ("1" ).source (SOURCE , XContentType .JSON )
58
64
.timeout (timeValueSeconds (1 ))).actionGet ();
59
65
fail ("no allocation should happen" );
60
66
} catch (UnavailableShardsException e ) {
@@ -66,13 +72,43 @@ public void testDataNodes() throws Exception {
66
72
assertThat (client ().admin ().cluster ().prepareHealth ().setWaitForEvents (Priority .LANGUID ).setWaitForNodes ("3" )
67
73
.setLocal (true ).execute ().actionGet ().isTimedOut (), equalTo (false ));
68
74
69
- IndexResponse indexResponse = client ().index (Requests .indexRequest ("test" ).type ( "type1" ). id ("1" )
70
- .source (source ( "1" , "test" ) , XContentType .JSON )).actionGet ();
75
+ IndexResponse indexResponse = client ().index (Requests .indexRequest ("test" ).id ("1" )
76
+ .source (SOURCE , XContentType .JSON )).actionGet ();
71
77
assertThat (indexResponse .getId (), equalTo ("1" ));
72
- assertThat (indexResponse .getType (), equalTo ("type1" ));
73
78
}
74
79
75
- private String source (String id , String nameValue ) {
76
- return "{ \" type1\" : { \" id\" : \" " + id + "\" , \" name\" : \" " + nameValue + "\" } }" ;
80
+ public void testShardsAllocatedAfterDataNodesStart () {
81
+ internalCluster ().startNode (Settings .builder ().put (Node .NODE_DATA_SETTING .getKey (), false ).build ());
82
+ client ().admin ().indices ().create (createIndexRequest ("test" )
83
+ .settings (Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )).waitForActiveShards (ActiveShardCount .NONE ))
84
+ .actionGet ();
85
+ final ClusterHealthResponse healthResponse1 = client ().admin ().cluster ().prepareHealth ()
86
+ .setWaitForEvents (Priority .LANGUID ).execute ().actionGet ();
87
+ assertThat (healthResponse1 .isTimedOut (), equalTo (false ));
88
+ assertThat (healthResponse1 .getStatus (), equalTo (ClusterHealthStatus .YELLOW )); // TODO should be RED, see #41073
89
+ assertThat (healthResponse1 .getActiveShards (), equalTo (0 ));
90
+
91
+ internalCluster ().startNode (Settings .builder ().put (Node .NODE_DATA_SETTING .getKey (), true ).build ());
92
+
93
+ assertThat (client ().admin ().cluster ().prepareHealth ()
94
+ .setWaitForEvents (Priority .LANGUID ).setWaitForNodes ("2" ).setWaitForGreenStatus ().execute ().actionGet ().isTimedOut (),
95
+ equalTo (false ));
96
+ }
97
+
98
+ public void testAutoExpandReplicasAdjustedWhenDataNodeJoins () {
99
+ internalCluster ().startNode (Settings .builder ().put (Node .NODE_DATA_SETTING .getKey (), false ).build ());
100
+ client ().admin ().indices ().create (createIndexRequest ("test" )
101
+ .settings (Settings .builder ().put (IndexMetaData .SETTING_AUTO_EXPAND_REPLICAS , "0-all" ))
102
+ .waitForActiveShards (ActiveShardCount .NONE ))
103
+ .actionGet ();
104
+ final ClusterHealthResponse healthResponse1 = client ().admin ().cluster ().prepareHealth ()
105
+ .setWaitForEvents (Priority .LANGUID ).execute ().actionGet ();
106
+ assertThat (healthResponse1 .isTimedOut (), equalTo (false ));
107
+ assertThat (healthResponse1 .getStatus (), equalTo (ClusterHealthStatus .YELLOW )); // TODO should be RED, see #41073
108
+ assertThat (healthResponse1 .getActiveShards (), equalTo (0 ));
109
+
110
+ internalCluster ().startNode ();
111
+ internalCluster ().startNode ();
112
+ client ().admin ().cluster ().prepareReroute ().setRetryFailed (true ).get ();
77
113
}
78
114
}
0 commit comments