9
9
10
10
import org .elasticsearch .ResourceNotFoundException ;
11
11
import org .elasticsearch .Version ;
12
- import org .elasticsearch .action .ActionListener ;
13
12
import org .elasticsearch .action .support .ActionFilters ;
13
+ import org .elasticsearch .action .support .PlainActionFuture ;
14
14
import org .elasticsearch .cluster .ClusterInfo ;
15
15
import org .elasticsearch .cluster .ClusterInfoService ;
16
16
import org .elasticsearch .cluster .ClusterName ;
40
40
import org .elasticsearch .index .shard .ShardId ;
41
41
import org .elasticsearch .rest .RestStatus ;
42
42
import org .elasticsearch .tasks .Task ;
43
+ import org .elasticsearch .tasks .TaskId ;
44
+ import org .elasticsearch .test .AbstractChunkedSerializingTestCase ;
43
45
import org .elasticsearch .threadpool .ThreadPool ;
44
46
import org .elasticsearch .transport .TransportService ;
45
- import org .mockito .ArgumentCaptor ;
46
47
47
48
import java .util .HashMap ;
48
49
import java .util .List ;
49
50
import java .util .Map ;
50
51
import java .util .Optional ;
51
52
import java .util .Set ;
53
+ import java .util .concurrent .TimeUnit ;
52
54
import java .util .stream .Collectors ;
53
55
54
56
import static org .elasticsearch .cluster .ClusterModule .BALANCED_ALLOCATOR ;
57
59
import static org .hamcrest .Matchers .equalTo ;
58
60
import static org .hamcrest .Matchers .notNullValue ;
59
61
import static org .mockito .Mockito .mock ;
60
- import static org .mockito .Mockito .verify ;
61
62
import static org .mockito .Mockito .when ;
62
63
63
64
public class TransportGetDesiredBalanceActionTests extends ESAllocationTestCase {
@@ -74,13 +75,28 @@ public class TransportGetDesiredBalanceActionTests extends ESAllocationTestCase
74
75
clusterInfoService ,
75
76
TEST_WRITE_LOAD_FORECASTER
76
77
);
77
- @ SuppressWarnings ("unchecked" )
78
- private final ActionListener <DesiredBalanceResponse > listener = mock (ActionListener .class );
78
+
79
+ private static DesiredBalanceResponse execute (TransportGetDesiredBalanceAction action , ClusterState clusterState ) throws Exception {
80
+ return PlainActionFuture .get (
81
+ future -> action .masterOperation (
82
+ new Task (1 , "test" , GetDesiredBalanceAction .NAME , "" , TaskId .EMPTY_TASK_ID , Map .of ()),
83
+ new DesiredBalanceRequest (),
84
+ clusterState ,
85
+ future
86
+ ),
87
+ 10 ,
88
+ TimeUnit .SECONDS
89
+ );
90
+ }
91
+
92
+ private DesiredBalanceResponse executeAction (ClusterState clusterState ) throws Exception {
93
+ return execute (transportGetDesiredBalanceAction , clusterState );
94
+ }
79
95
80
96
public void testReturnsErrorIfAllocatorIsNotDesiredBalanced () throws Exception {
81
97
var clusterState = ClusterState .builder (ClusterName .DEFAULT ).metadata (metadataWithConfiguredAllocator (BALANCED_ALLOCATOR )).build ();
82
98
83
- new TransportGetDesiredBalanceAction (
99
+ final var action = new TransportGetDesiredBalanceAction (
84
100
mock (TransportService .class ),
85
101
mock (ClusterService .class ),
86
102
mock (ThreadPool .class ),
@@ -89,12 +105,9 @@ public void testReturnsErrorIfAllocatorIsNotDesiredBalanced() throws Exception {
89
105
mock (ShardsAllocator .class ),
90
106
mock (ClusterInfoService .class ),
91
107
mock (WriteLoadForecaster .class )
92
- ).masterOperation (mock (Task .class ), mock (DesiredBalanceRequest .class ), clusterState , listener );
93
-
94
- ArgumentCaptor <ResourceNotFoundException > exceptionArgumentCaptor = ArgumentCaptor .forClass (ResourceNotFoundException .class );
95
- verify (listener ).onFailure (exceptionArgumentCaptor .capture ());
108
+ );
96
109
97
- final var exception = exceptionArgumentCaptor . getValue ( );
110
+ final var exception = expectThrows ( ResourceNotFoundException . class , () -> execute ( action , clusterState ) );
98
111
assertEquals ("Desired balance allocator is not in use, no desired balance found" , exception .getMessage ());
99
112
assertThat (exception .status (), equalTo (RestStatus .NOT_FOUND ));
100
113
}
@@ -104,12 +117,10 @@ public void testReturnsErrorIfDesiredBalanceIsNotAvailable() throws Exception {
104
117
.metadata (metadataWithConfiguredAllocator (DESIRED_BALANCE_ALLOCATOR ))
105
118
.build ();
106
119
107
- transportGetDesiredBalanceAction .masterOperation (mock (Task .class ), mock (DesiredBalanceRequest .class ), clusterState , listener );
108
-
109
- ArgumentCaptor <ResourceNotFoundException > exceptionArgumentCaptor = ArgumentCaptor .forClass (ResourceNotFoundException .class );
110
- verify (listener ).onFailure (exceptionArgumentCaptor .capture ());
111
-
112
- assertEquals ("Desired balance is not computed yet" , exceptionArgumentCaptor .getValue ().getMessage ());
120
+ assertEquals (
121
+ "Desired balance is not computed yet" ,
122
+ expectThrows (ResourceNotFoundException .class , () -> executeAction (clusterState )).getMessage ()
123
+ );
113
124
}
114
125
115
126
public void testGetDesiredBalance () throws Exception {
@@ -220,15 +231,15 @@ public void testGetDesiredBalance() throws Exception {
220
231
.routingTable (routingTable )
221
232
.build ();
222
233
223
- transportGetDesiredBalanceAction .masterOperation (mock (Task .class ), mock (DesiredBalanceRequest .class ), clusterState , listener );
224
-
225
- ArgumentCaptor <DesiredBalanceResponse > desiredBalanceResponseCaptor = ArgumentCaptor .forClass (DesiredBalanceResponse .class );
226
- verify (listener ).onResponse (desiredBalanceResponseCaptor .capture ());
227
- DesiredBalanceResponse desiredBalanceResponse = desiredBalanceResponseCaptor .getValue ();
234
+ final var desiredBalanceResponse = executeAction (clusterState );
228
235
assertThat (desiredBalanceResponse .getStats (), equalTo (desiredBalanceStats ));
229
236
assertThat (desiredBalanceResponse .getClusterBalanceStats (), notNullValue ());
230
237
assertThat (desiredBalanceResponse .getClusterInfo (), equalTo (clusterInfo ));
231
238
assertEquals (indexShards .keySet (), desiredBalanceResponse .getRoutingTable ().keySet ());
239
+
240
+ assertEquals (desiredBalanceResponse , copyWriteable (desiredBalanceResponse , writableRegistry (), DesiredBalanceResponse ::from ));
241
+ AbstractChunkedSerializingTestCase .assertChunkCount (desiredBalanceResponse , r -> 2 + r .getRoutingTable ().size ());
242
+
232
243
for (var e : desiredBalanceResponse .getRoutingTable ().entrySet ()) {
233
244
String index = e .getKey ();
234
245
Map <Integer , DesiredBalanceResponse .DesiredShards > shardsMap = e .getValue ();
@@ -267,14 +278,14 @@ public void testGetDesiredBalance() throws Exception {
267
278
);
268
279
assertEquals (indexMetadata .getTierPreference (), shardView .tierPreference ());
269
280
}
270
- Optional <ShardAssignment > shardAssignment = Optional .ofNullable (shardAssignments .get (indexShardRoutingTable .shardId ()));
271
- if (shardAssignment .isPresent ()) {
272
- assertEquals (shardAssignment .get ().nodeIds (), desiredShard .desired ().nodeIds ());
273
- assertEquals (shardAssignment .get ().total (), desiredShard .desired ().total ());
274
- assertEquals (shardAssignment .get ().unassigned (), desiredShard .desired ().unassigned ());
275
- assertEquals (shardAssignment .get ().ignored (), desiredShard .desired ().ignored ());
281
+ final var shardAssignment = shardAssignments .get (indexShardRoutingTable .shardId ());
282
+ if (shardAssignment == null ) {
283
+ assertSame (desiredShard .desired (), DesiredBalanceResponse .ShardAssignmentView .EMPTY );
276
284
} else {
277
- assertNull (desiredShard .desired ());
285
+ assertEquals (shardAssignment .nodeIds (), desiredShard .desired ().nodeIds ());
286
+ assertEquals (shardAssignment .total (), desiredShard .desired ().total ());
287
+ assertEquals (shardAssignment .unassigned (), desiredShard .desired ().unassigned ());
288
+ assertEquals (shardAssignment .ignored (), desiredShard .desired ().ignored ());
278
289
}
279
290
}
280
291
}
0 commit comments