-
Notifications
You must be signed in to change notification settings - Fork 683
/
ClusterCommunicationsDUnitTest.java
529 lines (474 loc) · 21 KB
/
ClusterCommunicationsDUnitTest.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
518
519
520
521
522
523
524
525
526
527
528
529
/*
* 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.geode;
import static org.apache.geode.distributed.ConfigurationProperties.CONSERVE_SOCKETS;
import static org.apache.geode.distributed.ConfigurationProperties.DISABLE_TCP;
import static org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION;
import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
import static org.apache.geode.distributed.ConfigurationProperties.NAME;
import static org.apache.geode.distributed.ConfigurationProperties.SOCKET_BUFFER_SIZE;
import static org.apache.geode.distributed.ConfigurationProperties.SOCKET_LEASE_TIME;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_ENABLED_COMPONENTS;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_KEYSTORE;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_KEYSTORE_PASSWORD;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_PROTOCOLS;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_REQUIRE_AUTHENTICATION;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_TRUSTSTORE;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_TRUSTSTORE_PASSWORD;
import static org.apache.geode.distributed.ConfigurationProperties.USE_CLUSTER_CONFIGURATION;
import static org.apache.geode.distributed.internal.OperationExecutors.SERIAL_EXECUTOR;
import static org.apache.geode.internal.serialization.DataSerializableFixedID.SERIAL_ACKED_MESSAGE;
import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.apache.geode.test.awaitility.GeodeAwaitility.getTimeout;
import static org.apache.geode.test.dunit.IgnoredException.addIgnoredException;
import static org.apache.geode.test.dunit.Invoke.invokeInEveryVM;
import static org.apache.geode.test.dunit.VM.getVM;
import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.Scope;
import org.apache.geode.distributed.DistributedLockService;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.Locator;
import org.apache.geode.distributed.internal.ClusterDistributionManager;
import org.apache.geode.distributed.internal.DMStats;
import org.apache.geode.distributed.internal.DirectReplyProcessor;
import org.apache.geode.distributed.internal.DistributionMessage;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.distributed.internal.MessageWithReply;
import org.apache.geode.distributed.internal.ReplyException;
import org.apache.geode.distributed.internal.ReplyMessage;
import org.apache.geode.distributed.internal.SerialAckedMessage;
import org.apache.geode.distributed.internal.membership.gms.membership.GMSJoinLeave;
import org.apache.geode.internal.AvailablePortHelper;
import org.apache.geode.internal.InternalDataSerializer;
import org.apache.geode.internal.cache.DirectReplyMessage;
import org.apache.geode.internal.serialization.DeserializationContext;
import org.apache.geode.internal.serialization.SerializationContext;
import org.apache.geode.test.dunit.AsyncInvocation;
import org.apache.geode.test.dunit.DUnitBlackboard;
import org.apache.geode.test.dunit.DistributedTestUtils;
import org.apache.geode.test.dunit.Host;
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.rules.DistributedRestoreSystemProperties;
import org.apache.geode.test.dunit.rules.DistributedRule;
import org.apache.geode.test.junit.categories.BackwardCompatibilityTest;
import org.apache.geode.test.junit.categories.MembershipTest;
import org.apache.geode.test.junit.rules.serializable.SerializableTestName;
import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory;
import org.apache.geode.test.version.VersionManager;
/**
* This class tests cluster tcp/ip communications both with and without SSL enabled
*/
@Category({MembershipTest.class, BackwardCompatibilityTest.class})
@RunWith(Parameterized.class)
@UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
public class ClusterCommunicationsDUnitTest implements Serializable {
private static final int NUM_SERVERS = 2;
private static final int SMALL_BUFFER_SIZE = 8000;
private static final long serialVersionUID = -3438183140385150550L;
private static Cache cache;
private final String regionName = "clusterTestRegion";
private final boolean disableTcp;
private boolean useDAck;
private boolean conserveSockets;
private boolean useSSL;
@Parameters(name = "{0}")
public static Collection<RunConfiguration> data() {
return Arrays.asList(RunConfiguration.values());
}
@Rule
public DistributedRule distributedRule =
DistributedRule.builder().withVMCount(NUM_SERVERS + 1).build();
@Rule
public DistributedRestoreSystemProperties restoreSystemProperties =
new DistributedRestoreSystemProperties();
@Rule
public SerializableTestName testName = new SerializableTestName();
public ClusterCommunicationsDUnitTest(RunConfiguration runConfiguration) {
useSSL = runConfiguration.useSSL;
conserveSockets = runConfiguration.conserveSockets;
disableTcp = runConfiguration.disableTcp;
useDAck = true;
}
@Before
public void setUp() throws Exception {
addIgnoredException("Socket Closed");
addIgnoredException("Remote host closed connection during handshake");
}
@Test
public void applicationUseOfDLockWithNoAckCacheOps() throws Exception {
useDAck = false; // use no-ack scope
DUnitBlackboard dUnitBlackboard = new DUnitBlackboard();
int locatorPort = createLocator(getVM(0));
for (int i = 1; i <= NUM_SERVERS; i++) {
createCacheAndRegion(getVM(i), locatorPort);
}
performCreate(getVM(1));
getVM(1).invoke("initialize dlock service", () -> {
DistributedLockService distLockService =
DistributedLockService.create("testLockService", cache.getDistributedSystem());
distLockService.lock("myLock", 50000, 50000);
});
AsyncInvocation async = waitForTheLockAsync(dUnitBlackboard, true);
for (int i = 0; i < 5; i++) {
getVM(1).invoke("update cache and release lock in vm1", () -> {
DistributedLockService distLockService =
DistributedLockService.getServiceNamed("testLockService");
try {
DistributedSystem.setThreadsSocketPolicy(false);
cache.getRegion(regionName).put("myKey", "myValue");
} finally {
distLockService.unlock("myLock");
DistributedSystem.releaseThreadsSockets();
}
});
async.get(30, TimeUnit.SECONDS);
getVM(2).invoke("release the lock in vm2 to try again", () -> {
DistributedLockService distLockService =
DistributedLockService.getServiceNamed("testLockService");
distLockService.unlock("myLock");
});
getVM(1).invoke("grab the lock in vm1", () -> {
DistributedLockService distLockService =
DistributedLockService.getServiceNamed("testLockService");
distLockService.lock("myLock", 50000, 50000);
});
async = waitForTheLockAsync(dUnitBlackboard, false);
}
}
@NotNull
private AsyncInvocation waitForTheLockAsync(DUnitBlackboard dUnitBlackboard, boolean initialWait)
throws Exception {
dUnitBlackboard.clearGate("waitingForLock");
AsyncInvocation async = getVM(2).invokeAsync("wait for the lock", () -> {
DistributedLockService distLockService = initialWait
? DistributedLockService.create("testLockService", cache.getDistributedSystem())
: DistributedLockService.getServiceNamed("testLockService");
final DUnitBlackboard myBlackboard = new DUnitBlackboard();
myBlackboard.signalGate("waitingForLock");
distLockService.lock("myLock", 50000, 50000);
});
dUnitBlackboard.waitForGate("waitingForLock", 30, TimeUnit.SECONDS);
Thread.sleep(2000);
if (async.isDone()) {
throw new Exception("async thread did not wait for the lock");
}
return async;
}
@Test
public void createEntryAndVerifyUpdate() {
int locatorPort = createLocator(getVM(0));
for (int i = 1; i <= NUM_SERVERS; i++) {
createCacheAndRegion(getVM(i), locatorPort);
}
performCreate(getVM(1));
for (int i = 1; i <= NUM_SERVERS; i++) {
verifyCreatedEntry(getVM(i));
}
for (int iteration = 1; iteration < 6; iteration++) {
performUpdate(getVM(1));
}
for (int i = 1; i <= NUM_SERVERS; i++) {
verifyUpdatedEntry(getVM(i));
}
}
@Test
public void createEntryWithBigMessage() {
int locatorPort = createLocator(getVM(0));
for (int i = 1; i <= NUM_SERVERS; i++) {
createCacheAndRegion(getVM(i), locatorPort);
}
performCreateWithLargeValue(getVM(1));
// fault the value into an empty cache - forces use of message chunking
for (int i = 1; i <= NUM_SERVERS - 1; i++) {
verifyCreatedEntry(getVM(i));
}
}
@Test
public void receiveBigResponse() {
invokeInEveryVM(
() -> InternalDataSerializer.getDSFIDSerializer().registerDSFID(SERIAL_ACKED_MESSAGE,
SerialAckedMessageWithBigReply.class));
try {
int locatorPort = createLocator(getVM(0));
for (int i = 1; i <= NUM_SERVERS; i++) {
createCacheAndRegion(getVM(i), locatorPort);
}
DistributedMember vm2ID =
getVM(2).invoke(() -> cache.getDistributedSystem().getDistributedMember());
getVM(1).invoke("receive a large direct-reply message", () -> {
SerialAckedMessageWithBigReply messageWithBigReply = new SerialAckedMessageWithBigReply();
await().until(() -> {
messageWithBigReply.send(Collections.singleton(vm2ID));
return true;
});
});
} finally {
invokeInEveryVM(
() -> InternalDataSerializer.getDSFIDSerializer().registerDSFID(SERIAL_ACKED_MESSAGE,
SerialAckedMessage.class));
}
}
@Test
public void performARollingUpgrade() {
List<String> testVersions = VersionManager.getInstance().getVersionsWithoutCurrent();
String testVersion = testVersions.get(testVersions.size() - 1);
// create a cluster with the previous version of Geode
VM locatorVM = Host.getHost(0).getVM(testVersion, 0);
VM server1VM = Host.getHost(0).getVM(testVersion, 1);
int locatorPort = createLocator(locatorVM, true);
createCacheAndRegion(server1VM, locatorPort);
performCreate(getVM(1));
// roll the locator to the current version
locatorVM.invoke("stop locator", () -> Locator.getLocator().stop());
locatorVM = Host.getHost(0).getVM(VersionManager.CURRENT_VERSION, 0);
locatorVM.invoke("roll locator to current version", () -> {
// if you need to debug SSL communications use this property:
// System.setProperty("javax.net.debug", "all");
Properties props = getDistributedSystemProperties();
// locator must restart with the same port so that it reconnects to the server
await().atMost(getTimeout().getSeconds(), TimeUnit.SECONDS)
.until(() -> Locator.startLocatorAndDS(locatorPort, new File(""), props) != null);
assertThat(Locator.getLocator().getDistributedSystem().getAllOtherMembers().size())
.isGreaterThan(0);
});
// start server2 with current version
VM server2VM = Host.getHost(0).getVM(VersionManager.CURRENT_VERSION, 2);
createCacheAndRegion(server2VM, locatorPort);
// roll server1 to the current version
server1VM.invoke("stop server1", () -> cache.close());
server1VM = Host.getHost(0).getVM(VersionManager.CURRENT_VERSION, 1);
createCacheAndRegion(server1VM, locatorPort);
verifyCreatedEntry(server1VM);
verifyCreatedEntry(server2VM);
}
private void createCacheAndRegion(VM memberVM, int locatorPort) {
memberVM.invoke("start cache and create region", () -> {
cache = createCache(locatorPort);
cache.createRegionFactory(RegionShortcut.REPLICATE)
.setScope(useDAck ? Scope.DISTRIBUTED_ACK : Scope.DISTRIBUTED_NO_ACK)
.create(regionName);
});
}
private void performCreate(VM memberVM) {
memberVM.invoke("perform create", () -> cache
.getRegion(regionName).put("testKey", "testValue"));
}
private void performUpdate(VM memberVM) {
memberVM.invoke("perform update", () -> {
DMStats stats = ((InternalDistributedSystem) cache.getDistributedSystem())
.getDistributionManager().getStats();
long reconnectAttempts = stats.getReconnectAttempts();
cache.getRegion(regionName).put("testKey", "updatedTestValue");
assertThat(stats.getReconnectAttempts()).isEqualTo(reconnectAttempts);
});
}
private void performCreateWithLargeValue(VM memberVM) {
memberVM.invoke("perform create", () -> {
byte[] value = new byte[SMALL_BUFFER_SIZE * 20];
Arrays.fill(value, (byte) 1);
cache.getRegion(regionName).put("testKey", value);
});
}
private void verifyCreatedEntry(VM memberVM) {
memberVM.invoke("verify entry created", () -> Assert.assertTrue(cache
.getRegion(regionName).containsKey("testKey")));
}
private void verifyUpdatedEntry(VM memberVM) {
memberVM.invoke("verify entry updated", () -> Assert.assertTrue(cache
.getRegion(regionName).containsValue("updatedTestValue")));
}
private int createLocator(VM memberVM) {
return createLocator(memberVM, false);
}
private int createLocator(VM memberVM, boolean usingOldVersion) {
return memberVM.invoke("create locator", () -> {
// if you need to debug SSL communications use this property:
// System.setProperty("javax.net.debug", "all");
System.setProperty(GMSJoinLeave.BYPASS_DISCOVERY_PROPERTY, "true");
Properties dsProperties = getDistributedSystemProperties();
try {
int port = 0;
// for stress-tests make sure that an older-version locator doesn't try
// to read state persisted by another run's newer-version locator
if (usingOldVersion) {
port = AvailablePortHelper.getRandomAvailableTCPPort();
DistributedTestUtils.deleteLocatorStateFile(port);
}
return Locator.startLocatorAndDS(port, new File(""), getDistributedSystemProperties())
.getPort();
} finally {
System.clearProperty(GMSJoinLeave.BYPASS_DISCOVERY_PROPERTY);
}
});
}
private Cache createCache(int locatorPort) {
// if you need to debug SSL communications use this property:
// System.setProperty("javax.net.debug", "all");
Properties properties = getDistributedSystemProperties();
properties.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
return new CacheFactory(properties).create();
}
public Properties getDistributedSystemProperties() {
Properties properties = new Properties();
properties.setProperty(ENABLE_CLUSTER_CONFIGURATION, "false");
properties.setProperty(USE_CLUSTER_CONFIGURATION, "false");
properties.setProperty(NAME, "vm" + VM.getCurrentVMNum());
properties.setProperty(CONSERVE_SOCKETS, "" + conserveSockets);
properties.setProperty(DISABLE_TCP, "" + disableTcp);
properties.setProperty(SOCKET_LEASE_TIME, "10000");
properties.setProperty(SOCKET_BUFFER_SIZE, "" + SMALL_BUFFER_SIZE);
if (useSSL) {
properties.setProperty(SSL_ENABLED_COMPONENTS, "cluster,locator");
properties
.setProperty(SSL_KEYSTORE, createTempFileFromResource(getClass(), "server.keystore")
.getAbsolutePath());
properties.setProperty(SSL_TRUSTSTORE,
createTempFileFromResource(getClass(), "server.keystore")
.getAbsolutePath());
properties.setProperty(SSL_PROTOCOLS, "TLSv1.2");
properties.setProperty(SSL_KEYSTORE_PASSWORD, "password");
properties.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
properties.setProperty(SSL_REQUIRE_AUTHENTICATION, "true");
}
return properties;
}
enum RunConfiguration {
SHARED_CONNECTIONS(true, false, false),
SHARED_CONNECTIONS_WITH_SSL(true, true, false),
UNSHARED_CONNECTIONS(false, false, false),
UNSHARED_CONNECTIONS_WITH_SSL(false, true, false),
UDP_CONNECTIONS(true, false, true);
boolean useSSL;
boolean conserveSockets;
boolean disableTcp;
RunConfiguration(boolean conserveSockets, boolean useSSL, boolean disableTcp) {
this.useSSL = useSSL;
this.conserveSockets = conserveSockets;
this.disableTcp = disableTcp;
}
}
/**
* SerialAckedMessageWithBigReply requires conserve-sockets=false and acts to send
* a large reply message to the sender. You must have already created a cache in the
* sender and receiver VMs and registered this class with the DataSerializableFixedID
* of SERIAL_ACKED_MESSAGE. Don't forget to reset the registration to
* SerialAckedMessage at the end of the test.
*/
private static class SerialAckedMessageWithBigReply extends DistributionMessage
implements MessageWithReply, DirectReplyMessage {
static final int DSFID = SERIAL_ACKED_MESSAGE;
private int processorId;
private ClusterDistributionManager originDm;
private DirectReplyProcessor replyProcessor;
public SerialAckedMessageWithBigReply() {
InternalDistributedSystem ds = InternalDistributedSystem.getAnyInstance();
// this constructor is used in serialization as well as when sending to others
if (ds != null) {
originDm = (ClusterDistributionManager) ds.getDistributionManager();
}
}
public void send(Set<DistributedMember> recipients)
throws InterruptedException, ReplyException {
// this message is only used by battery tests so we can log info level debug
// messages
replyProcessor = new DirectReplyProcessor(originDm, recipients);
processorId = replyProcessor.getProcessorId();
setRecipients(recipients);
Set failures = originDm.putOutgoing(this);
if (failures != null && !failures.isEmpty()) {
for (Object failure : failures) {
System.err.println("Unable to send serial acked message to " + failure);
}
}
replyProcessor.waitForReplies();
}
@Override
public void toData(DataOutput out, SerializationContext context) throws IOException {
super.toData(out, context);
out.writeInt(processorId);
}
@Override
public void fromData(DataInput in, DeserializationContext context)
throws IOException, ClassNotFoundException {
super.fromData(in, context);
processorId = in.readInt();
}
@Override
protected void process(ClusterDistributionManager dm) {
ReplyMessage reply = new ReplyMessage();
reply.setProcessorId(processorId);
reply.setRecipient(getSender());
byte[] returnValue = new byte[SMALL_BUFFER_SIZE * 6];
reply.setReturnValue(returnValue);
System.out.println("<" + Thread.currentThread().getName() +
"> sending reply with return value size "
+ returnValue.length + " using " + getReplySender(dm));
getReplySender(dm).putOutgoing(reply);
}
@Override
public int getProcessorId() {
return processorId;
}
@Override
public int getProcessorType() {
return SERIAL_EXECUTOR;
}
@Override
public int getDSFID() {
return DSFID;
}
@Override
public DirectReplyProcessor getDirectReplyProcessor() {
return replyProcessor;
}
@Override
public boolean supportsDirectAck() {
return processorId == 0;
}
@Override
public void registerProcessor() {
if (replyProcessor != null) {
processorId = replyProcessor.register();
}
}
}
}