/
AbstractClusterInvokerTest.java
517 lines (452 loc) · 19.9 KB
/
AbstractClusterInvokerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.support;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
import org.apache.dubbo.rpc.cluster.filter.DemoService;
import org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* AbstractClusterInvokerTest
*
*/
@SuppressWarnings("rawtypes")
public class AbstractClusterInvokerTest {
List<Invoker<IHelloService>> invokers = new ArrayList<Invoker<IHelloService>>();
List<Invoker<IHelloService>> selectedInvokers = new ArrayList<Invoker<IHelloService>>();
AbstractClusterInvoker<IHelloService> cluster;
AbstractClusterInvoker<IHelloService> cluster_nocheck;
Directory<IHelloService> dic;
RpcInvocation invocation = new RpcInvocation();
URL url = URL.valueOf("registry://localhost:9090");
Invoker<IHelloService> invoker1;
Invoker<IHelloService> invoker2;
Invoker<IHelloService> invoker3;
Invoker<IHelloService> invoker4;
Invoker<IHelloService> invoker5;
Invoker<IHelloService> mockedInvoker1;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@SuppressWarnings({"unchecked"})
@Before
public void setUp() throws Exception {
invocation.setMethodName("sayHello");
invoker1 = mock(Invoker.class);
invoker2 = mock(Invoker.class);
invoker3 = mock(Invoker.class);
invoker4 = mock(Invoker.class);
invoker5 = mock(Invoker.class);
mockedInvoker1 = mock(Invoker.class);
URL turl = URL.valueOf("test://test:11/test");
given(invoker1.isAvailable()).willReturn(false);
given(invoker1.getInterface()).willReturn(IHelloService.class);
given(invoker1.getUrl()).willReturn(turl.addParameter("name", "invoker1"));
given(invoker2.isAvailable()).willReturn(true);
given(invoker2.getInterface()).willReturn(IHelloService.class);
given(invoker2.getUrl()).willReturn(turl.addParameter("name", "invoker2"));
given(invoker3.isAvailable()).willReturn(false);
given(invoker3.getInterface()).willReturn(IHelloService.class);
given(invoker3.getUrl()).willReturn(turl.addParameter("name", "invoker3"));
given(invoker4.isAvailable()).willReturn(true);
given(invoker4.getInterface()).willReturn(IHelloService.class);
given(invoker4.getUrl()).willReturn(turl.addParameter("name", "invoker4"));
given(invoker5.isAvailable()).willReturn(false);
given(invoker5.getInterface()).willReturn(IHelloService.class);
given(invoker5.getUrl()).willReturn(turl.addParameter("name", "invoker5"));
given(mockedInvoker1.isAvailable()).willReturn(false);
given(mockedInvoker1.getInterface()).willReturn(IHelloService.class);
given(mockedInvoker1.getUrl()).willReturn(turl.setProtocol("mock"));
invokers.add(invoker1);
dic = new StaticDirectory<IHelloService>(url, invokers, null);
cluster = new AbstractClusterInvoker(dic) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
throws RpcException {
return null;
}
};
cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(Constants.CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
throws RpcException {
return null;
}
};
}
@Test
public void testBindingAttachment() {
final String attachKey = "attach";
final String attachValue = "value";
// setup attachment
RpcContext.getContext().setAttachment(attachKey, attachValue);
Map<String, String> attachments = RpcContext.getContext().getAttachments();
Assert.assertTrue("set attachment failed!", attachments != null && attachments.size() == 1);
cluster = new AbstractClusterInvoker(dic) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance)
throws RpcException {
// attachment will be bind to invocation
String value = invocation.getAttachment(attachKey);
Assert.assertTrue("binding attachment failed!", value != null && value.equals(attachValue));
return null;
}
};
// invoke
cluster.invoke(invocation);
}
@Test
public void testSelect_Invokersize0() throws Exception {
{
Invoker invoker = cluster.select(null, null, null, null);
Assert.assertEquals(null, invoker);
}
{
invokers.clear();
selectedInvokers.clear();
Invoker invoker = cluster.select(null, null, invokers, null);
Assert.assertEquals(null, invoker);
}
}
@Test
public void testSelect_Invokersize1() throws Exception {
invokers.clear();
invokers.add(invoker1);
Invoker invoker = cluster.select(null, null, invokers, null);
Assert.assertEquals(invoker1, invoker);
}
@Test
public void testSelect_Invokersize2AndselectNotNull() throws Exception {
invokers.clear();
invokers.add(invoker2);
invokers.add(invoker4);
{
selectedInvokers.clear();
selectedInvokers.add(invoker4);
Invoker invoker = cluster.select(null, invocation, invokers, selectedInvokers);
Assert.assertEquals(invoker2, invoker);
}
{
selectedInvokers.clear();
selectedInvokers.add(invoker2);
Invoker invoker = cluster.select(null, invocation, invokers, selectedInvokers);
Assert.assertEquals(invoker4, invoker);
}
}
@Test
public void testSelect_multiInvokers() throws Exception {
testSelect_multiInvokers(RoundRobinLoadBalance.NAME);
testSelect_multiInvokers(LeastActiveLoadBalance.NAME);
testSelect_multiInvokers(RandomLoadBalance.NAME);
}
@Test
public void testCloseAvailablecheck() {
LoadBalance lb = mock(LoadBalance.class);
given(lb.select(invokers, url, invocation)).willReturn(invoker1);
initlistsize5();
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(false, sinvoker.isAvailable());
Assert.assertEquals(invoker1, sinvoker);
}
@Test
public void testDonotSelectAgainAndNoCheckAvailable() {
LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
initlistsize5();
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertSame(invoker1, sinvoker);
}
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertSame(invoker2, sinvoker);
}
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertSame(invoker3, sinvoker);
}
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker4);
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertSame(invoker5, sinvoker);
}
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(invokers.contains(sinvoker));
}
}
@Test
public void testSelectAgainAndCheckAvailable() {
LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
initlistsize5();
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(sinvoker == invoker4);
}
{
//Boundary condition test .
selectedInvokers.clear();
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
}
{
//Boundary condition test .
for (int i = 0; i < 100; i++) {
selectedInvokers.clear();
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
}
}
{
//Boundary condition test .
for (int i = 0; i < 100; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
}
}
{
//Boundary condition test .
for (int i = 0; i < 100; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker4);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
}
}
}
public void testSelect_multiInvokers(String lbname) throws Exception {
int min = 1000, max = 5000;
Double d = (Math.random() * (max - min + 1) + min);
int runs = d.intValue();
Assert.assertTrue(runs > min);
LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(lbname);
initlistsize5();
for (int i = 0; i < runs; i++) {
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker2);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker4);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker3);
selectedInvokers.add(invoker5);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
selectedInvokers.add(invoker1);
selectedInvokers.add(invoker2);
selectedInvokers.add(invoker3);
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
Assert.assertEquals(true, sinvoker.isAvailable());
Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
}
}
/**
* Test balance.
*/
@Test
public void testSelectBalance() {
LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
initlistsize5();
Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
for (Invoker invoker : invokers) {
counter.put(invoker, new AtomicLong(0));
}
int runs = 1000;
for (int i = 0; i < runs; i++) {
selectedInvokers.clear();
Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
counter.get(sinvoker).incrementAndGet();
}
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
// System.out.println(count);
if (minvoker.isAvailable())
Assert.assertTrue("count should > avg", count > runs / invokers.size());
}
Assert.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get());
;
}
private void initlistsize5() {
invokers.clear();
selectedInvokers.clear();//Clear first, previous test case will make sure that the right invoker2 will be used.
invokers.add(invoker1);
invokers.add(invoker2);
invokers.add(invoker3);
invokers.add(invoker4);
invokers.add(invoker5);
}
@Test()
public void testTimeoutExceptionCode() {
List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
invokers.add(new Invoker<DemoService>() {
@Override
public Class<DemoService> getInterface() {
return DemoService.class;
}
public URL getUrl() {
return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
}
@Override
public boolean isAvailable() {
return false;
}
public Result invoke(Invocation invocation) throws RpcException {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
}
@Override
public void destroy() {
}
});
Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
try {
failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
try {
forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
try {
failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
Assert.fail();
} catch (RpcException e) {
Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
}
/**
* Test mock invoker selector works as expected
*/
@Test
public void testMockedInvokerSelect() {
initlistsize5();
invokers.add(mockedInvoker1);
RpcInvocation mockedInvocation = new RpcInvocation();
mockedInvocation.setMethodName("sayHello");
mockedInvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
List<Invoker<IHelloService>> mockedInvokers = dic.list(mockedInvocation);
Assert.assertEquals(1, mockedInvokers.size());
List<Invoker<IHelloService>> invokers = dic.list(invocation);
Assert.assertEquals(5, invokers.size());
}
public static interface IHelloService {
}
}