18
18
*/
19
19
package org .elasticsearch .action .admin .cluster .bootstrap ;
20
20
21
- import org .elasticsearch .ElasticsearchException ;
22
21
import org .elasticsearch .Version ;
23
- import org .elasticsearch .action .ActionListener ;
24
22
import org .elasticsearch .action .admin .cluster .bootstrap .BootstrapConfiguration .NodeDescription ;
25
23
import org .elasticsearch .action .support .ActionFilters ;
26
24
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
33
31
import org .elasticsearch .cluster .node .DiscoveryNode ;
34
32
import org .elasticsearch .cluster .node .DiscoveryNode .Role ;
35
33
import org .elasticsearch .cluster .service .MasterService ;
34
+ import org .elasticsearch .common .io .stream .StreamInput ;
36
35
import org .elasticsearch .common .settings .ClusterSettings ;
37
36
import org .elasticsearch .common .settings .Settings ;
38
37
import org .elasticsearch .discovery .Discovery ;
39
- import org .elasticsearch .tasks .Task ;
40
38
import org .elasticsearch .test .ESTestCase ;
41
39
import org .elasticsearch .test .transport .MockTransport ;
42
40
import org .elasticsearch .threadpool .TestThreadPool ;
43
41
import org .elasticsearch .threadpool .ThreadPool ;
42
+ import org .elasticsearch .threadpool .ThreadPool .Names ;
43
+ import org .elasticsearch .transport .TransportException ;
44
+ import org .elasticsearch .transport .TransportResponseHandler ;
44
45
import org .elasticsearch .transport .TransportService ;
45
46
46
- import java .util .concurrent .atomic .AtomicBoolean ;
47
+ import java .io .IOException ;
48
+ import java .util .Random ;
49
+ import java .util .concurrent .CountDownLatch ;
50
+ import java .util .concurrent .TimeUnit ;
47
51
48
52
import static java .util .Collections .emptyList ;
49
53
import static java .util .Collections .emptyMap ;
55
59
import static org .mockito .Mockito .verifyZeroInteractions ;
56
60
57
61
public class TransportBootstrapClusterActionTests extends ESTestCase {
62
+
63
+ private final ActionFilters EMPTY_FILTERS = new ActionFilters (emptySet ());
64
+
58
65
private static BootstrapClusterRequest exampleRequest () {
59
66
return new BootstrapClusterRequest (new BootstrapConfiguration (singletonList (new NodeDescription ("id" , "name" ))));
60
67
}
61
68
62
- public void testHandlesNonstandardDiscoveryImplementation () {
69
+ public void testHandlesNonstandardDiscoveryImplementation () throws InterruptedException {
63
70
final MockTransport transport = new MockTransport ();
64
71
final ThreadPool threadPool = new TestThreadPool ("test" , Settings .EMPTY );
65
72
final DiscoveryNode discoveryNode = new DiscoveryNode ("local" , buildNewFakeTransportAddress (), Version .CURRENT );
@@ -69,133 +76,148 @@ public void testHandlesNonstandardDiscoveryImplementation() {
69
76
final Discovery discovery = mock (Discovery .class );
70
77
verifyZeroInteractions (discovery );
71
78
72
- final TransportBootstrapClusterAction transportBootstrapClusterAction
73
- = new TransportBootstrapClusterAction (Settings .EMPTY , mock (ActionFilters .class ), transportService , discovery );
79
+ new TransportBootstrapClusterAction (Settings .EMPTY , EMPTY_FILTERS , transportService , discovery ); // registers action
80
+ transportService .start ();
81
+ transportService .acceptIncomingRequests ();
74
82
75
- final ActionListener <AcknowledgedResponse > listener = new ActionListener <AcknowledgedResponse >() {
83
+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
84
+ transportService .sendRequest (discoveryNode , BootstrapClusterAction .NAME , exampleRequest (), new ResponseHandler () {
76
85
@ Override
77
- public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
86
+ public void handleResponse (AcknowledgedResponse response ) {
78
87
throw new AssertionError ("should not be called" );
79
88
}
80
89
81
90
@ Override
82
- public void onFailure (Exception e ) {
83
- throw new AssertionError ("should not be called" );
91
+ public void handleException (TransportException exp ) {
92
+ assertThat (exp .getRootCause ().getMessage (), equalTo ("cluster bootstrapping is not supported by this discovery type" ));
93
+ countDownLatch .countDown ();
84
94
}
85
- };
86
-
87
- assertThat (expectThrows (IllegalStateException .class ,
88
- () -> transportBootstrapClusterAction .doExecute (mock (Task .class ), exampleRequest (), listener ))
89
- .getMessage (), equalTo ("cluster bootstrapping is not supported by this discovery type" ));
95
+ });
90
96
97
+ assertTrue (countDownLatch .await (10 , TimeUnit .SECONDS ));
91
98
threadPool .shutdown ();
92
99
}
93
100
94
- public void testFailsOnNonMasterEligibleNodes () {
101
+ public void testFailsOnNonMasterEligibleNodes () throws InterruptedException {
95
102
final DiscoveryNode discoveryNode
96
103
= new DiscoveryNode ("local" , buildNewFakeTransportAddress (), emptyMap (), emptySet (), Version .CURRENT );
97
104
98
105
final MockTransport transport = new MockTransport ();
99
106
final ThreadPool threadPool = new TestThreadPool ("test" , Settings .EMPTY );
100
107
final TransportService transportService = transport .createTransportService (Settings .EMPTY , threadPool ,
101
108
TransportService .NOOP_TRANSPORT_INTERCEPTOR , boundTransportAddress -> discoveryNode , null , emptySet ());
102
- transportService .start ();
103
- transportService .acceptIncomingRequests ();
104
109
105
110
final ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
106
111
final ClusterState state = ClusterState .builder (new ClusterName ("cluster" )).build ();
107
112
final Coordinator coordinator = new Coordinator ("local" , Settings .EMPTY , clusterSettings , transportService ,
108
113
ESAllocationTestCase .createAllocationService (Settings .EMPTY ),
109
114
new MasterService ("local" , Settings .EMPTY , threadPool ),
110
115
() -> new InMemoryPersistedState (0 , state ), r -> emptyList (),
111
- new NoOpClusterApplier (), random ());
112
- coordinator .start ();
116
+ new NoOpClusterApplier (), new Random (random ().nextLong ()));
113
117
114
- final TransportBootstrapClusterAction transportBootstrapClusterAction
115
- = new TransportBootstrapClusterAction (Settings .EMPTY , mock (ActionFilters .class ), transportService , coordinator );
118
+ new TransportBootstrapClusterAction (Settings .EMPTY , EMPTY_FILTERS , transportService , coordinator ); // registers action
119
+ transportService .start ();
120
+ transportService .acceptIncomingRequests ();
121
+ coordinator .start ();
116
122
117
- final ActionListener <AcknowledgedResponse > listener = new ActionListener <AcknowledgedResponse >() {
123
+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
124
+ transportService .sendRequest (discoveryNode , BootstrapClusterAction .NAME , exampleRequest (), new ResponseHandler () {
118
125
@ Override
119
- public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
126
+ public void handleResponse (AcknowledgedResponse response ) {
120
127
throw new AssertionError ("should not be called" );
121
128
}
122
129
123
130
@ Override
124
- public void onFailure (Exception e ) {
125
- throw new AssertionError ("should not be called" );
131
+ public void handleException (TransportException exp ) {
132
+ assertThat (exp .getRootCause ().getMessage (), equalTo ("this node is not master-eligible" ));
133
+ countDownLatch .countDown ();
126
134
}
127
- };
128
-
129
- assertThat (expectThrows (ElasticsearchException .class ,
130
- () -> transportBootstrapClusterAction .doExecute (mock (Task .class ), exampleRequest (), listener )).getMessage (),
131
- equalTo ("this node is not master-eligible" ));
135
+ });
132
136
137
+ assertTrue (countDownLatch .await (10 , TimeUnit .SECONDS ));
133
138
threadPool .shutdown ();
134
139
}
135
140
136
- public void testSetsInitialConfiguration () {
141
+ public void testSetsInitialConfiguration () throws InterruptedException {
137
142
final DiscoveryNode discoveryNode
138
143
= new DiscoveryNode ("local" , buildNewFakeTransportAddress (), emptyMap (), singleton (Role .MASTER ), Version .CURRENT );
139
144
140
145
final MockTransport transport = new MockTransport ();
141
146
final ThreadPool threadPool = new TestThreadPool ("test" , Settings .EMPTY );
142
147
final TransportService transportService = transport .createTransportService (Settings .EMPTY , threadPool ,
143
148
TransportService .NOOP_TRANSPORT_INTERCEPTOR , boundTransportAddress -> discoveryNode , null , emptySet ());
144
- transportService .start ();
145
- transportService .acceptIncomingRequests ();
146
149
147
150
final ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
148
151
final ClusterState state = ClusterState .builder (new ClusterName ("cluster" )).build ();
149
152
final Coordinator coordinator = new Coordinator ("local" , Settings .EMPTY , clusterSettings , transportService ,
150
153
ESAllocationTestCase .createAllocationService (Settings .EMPTY ),
151
154
new MasterService ("local" , Settings .EMPTY , threadPool ),
152
155
() -> new InMemoryPersistedState (0 , state ), r -> emptyList (),
153
- new NoOpClusterApplier (), random ());
156
+ new NoOpClusterApplier (), new Random (random ().nextLong ()));
157
+
158
+ new TransportBootstrapClusterAction (Settings .EMPTY , EMPTY_FILTERS , transportService , coordinator ); // registers action
159
+ transportService .start ();
160
+ transportService .acceptIncomingRequests ();
154
161
coordinator .start ();
155
162
coordinator .startInitialJoin ();
156
163
157
- final TransportBootstrapClusterAction transportBootstrapClusterAction
158
- = new TransportBootstrapClusterAction (Settings .EMPTY , mock (ActionFilters .class ), transportService , coordinator );
159
-
160
- final AtomicBoolean responseReceived = new AtomicBoolean ();
161
-
162
164
assertFalse (coordinator .isInitialConfigurationSet ());
163
165
164
166
final BootstrapClusterRequest request
165
167
= new BootstrapClusterRequest (new BootstrapConfiguration (singletonList (new NodeDescription (discoveryNode ))));
166
168
167
- transportBootstrapClusterAction .doExecute (mock (Task .class ), request ,
168
- new ActionListener <AcknowledgedResponse >() {
169
+ {
170
+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
171
+ transportService .sendRequest (discoveryNode , BootstrapClusterAction .NAME , request , new ResponseHandler () {
169
172
@ Override
170
- public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
171
- assertTrue (acknowledgedResponse .isAcknowledged ());
172
- responseReceived . set ( true );
173
+ public void handleResponse (AcknowledgedResponse response ) {
174
+ assertTrue (response .isAcknowledged ());
175
+ countDownLatch . countDown ( );
173
176
}
174
177
175
178
@ Override
176
- public void onFailure ( Exception e ) {
177
- throw new AssertionError ("should not be called" );
179
+ public void handleException ( TransportException exp ) {
180
+ throw new AssertionError ("should not be called" , exp );
178
181
}
179
182
});
180
- assertTrue (responseReceived .get ());
183
+
184
+ assertTrue (countDownLatch .await (10 , TimeUnit .SECONDS ));
185
+ }
186
+
181
187
assertTrue (coordinator .isInitialConfigurationSet ());
182
188
183
- responseReceived . set ( false );
184
- transportBootstrapClusterAction . doExecute ( mock ( Task . class ), request ,
185
- new ActionListener < AcknowledgedResponse > () {
189
+ {
190
+ final CountDownLatch countDownLatch = new CountDownLatch ( 1 );
191
+ transportService . sendRequest ( discoveryNode , BootstrapClusterAction . NAME , request , new ResponseHandler () {
186
192
@ Override
187
- public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
188
- assertFalse (acknowledgedResponse .isAcknowledged ());
189
- responseReceived . set ( true );
193
+ public void handleResponse (AcknowledgedResponse response ) {
194
+ assertFalse (response .isAcknowledged ());
195
+ countDownLatch . countDown ( );
190
196
}
191
197
192
198
@ Override
193
- public void onFailure ( Exception e ) {
194
- throw new AssertionError ("should not be called" );
199
+ public void handleException ( TransportException exp ) {
200
+ throw new AssertionError ("should not be called" , exp );
195
201
}
196
202
});
197
- assertTrue (responseReceived .get ());
203
+
204
+ assertTrue (countDownLatch .await (10 , TimeUnit .SECONDS ));
205
+ }
198
206
199
207
threadPool .shutdown ();
200
208
}
209
+
210
+ private abstract class ResponseHandler implements TransportResponseHandler <AcknowledgedResponse > {
211
+ @ Override
212
+ public String executor () {
213
+ return Names .SAME ;
214
+ }
215
+
216
+ @ Override
217
+ public AcknowledgedResponse read (StreamInput in ) throws IOException {
218
+ AcknowledgedResponse acknowledgedResponse = new AcknowledgedResponse ();
219
+ acknowledgedResponse .readFrom (in );
220
+ return acknowledgedResponse ;
221
+ }
222
+ }
201
223
}
0 commit comments