-
Notifications
You must be signed in to change notification settings - Fork 912
/
PrimaryToPrimaryFailoverTest.java
386 lines (297 loc) · 14.2 KB
/
PrimaryToPrimaryFailoverTest.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
/*
* 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.activemq.artemis.tests.integration.cluster.failover;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal;
import org.apache.activemq.artemis.core.config.ScaleDownConfiguration;
import org.apache.activemq.artemis.core.config.ha.ColocatedPolicyConfiguration;
import org.apache.activemq.artemis.core.config.ha.SharedStorePrimaryPolicyConfiguration;
import org.apache.activemq.artemis.core.config.ha.SharedStoreBackupPolicyConfiguration;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.impl.InVMNodeManager;
import org.apache.activemq.artemis.tests.util.TransportConfigurationUtils;
import org.apache.activemq.artemis.tests.util.Wait;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class PrimaryToPrimaryFailoverTest extends FailoverTest {
private InVMNodeManager nodeManager0;
private InVMNodeManager nodeManager1;
private ClientSessionFactoryInternal sf2;
@Override
public void setUp() throws Exception {
super.setUp();
}
@Override
protected void createConfigs() throws Exception {
nodeManager0 = new InVMNodeManager(false);
nodeManager1 = new InVMNodeManager(false);
TransportConfiguration primaryConnector0 = getConnectorTransportConfiguration(true, 0);
TransportConfiguration primaryConnector1 = getConnectorTransportConfiguration(true, 1);
backupConfig = super.createDefaultInVMConfig(1).clearAcceptorConfigurations().addAcceptorConfiguration(getAcceptorTransportConfiguration(true, 1)).setHAPolicyConfiguration(new ColocatedPolicyConfiguration().setRequestBackup(true).setPrimaryConfig(new SharedStorePrimaryPolicyConfiguration()).setBackupConfig(new SharedStoreBackupPolicyConfiguration().setScaleDownConfiguration(new ScaleDownConfiguration().addConnector(primaryConnector1.getName())))).addConnectorConfiguration(primaryConnector0.getName(), primaryConnector0).addConnectorConfiguration(primaryConnector1.getName(), primaryConnector1).addClusterConfiguration(basicClusterConnectionConfig(primaryConnector1.getName(), primaryConnector0.getName()));
backupServer = createColocatedTestableServer(backupConfig, nodeManager1, nodeManager0, 1);
primaryConfig = super.createDefaultInVMConfig(0).clearAcceptorConfigurations().addAcceptorConfiguration(getAcceptorTransportConfiguration(true, 0)).setHAPolicyConfiguration(new ColocatedPolicyConfiguration().setRequestBackup(true).setBackupRequestRetryInterval(1000).setPrimaryConfig(new SharedStorePrimaryPolicyConfiguration()).setBackupConfig(new SharedStoreBackupPolicyConfiguration().setScaleDownConfiguration(new ScaleDownConfiguration()))).addConnectorConfiguration(primaryConnector0.getName(), primaryConnector0).addConnectorConfiguration(primaryConnector1.getName(), primaryConnector1).addClusterConfiguration(basicClusterConnectionConfig(primaryConnector0.getName(), primaryConnector1.getName()));
primaryServer = createColocatedTestableServer(primaryConfig, nodeManager0, nodeManager1, 0);
}
@Override
protected void setPrimaryIdentity() {
primaryServer.setIdentity(this.getClass().getSimpleName() + "/liveServer0");
}
@Override
protected void setBackupIdentity() {
backupServer.setIdentity(this.getClass().getSimpleName() + "/liveServer1");
}
@Override
protected void waitForBackup() {
Map<String, ActiveMQServer> backupServers0 = primaryServer.getServer().getClusterManager().getHAManager().getBackupServers();
Map<String, ActiveMQServer> backupServers1 = backupServer.getServer().getClusterManager().getHAManager().getBackupServers();
final long toWait = 10000;
final long time = System.currentTimeMillis();
while (true) {
if (backupServers0.size() == 1 && backupServers1.size() == 1) {
break;
}
if (System.currentTimeMillis() > (time + toWait)) {
fail("backup started? ( primary server0 backups = " + backupServers0.size() + " primary server1 backups = " + backupServers1.size() + ")");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
waitForRemoteBackupSynchronization(backupServers0.values().iterator().next());
waitForRemoteBackupSynchronization(backupServers1.values().iterator().next());
}
@Override
protected final ClientSessionFactoryInternal createSessionFactoryAndWaitForTopology(ServerLocator locator,
int topologyMembers) throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(topologyMembers * 2);
locator.addClusterTopologyListener(new LatchClusterTopologyListener(countDownLatch));
ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) locator.createSessionFactory();
addSessionFactory(sf);
assertTrue("topology members expected " + topologyMembers, countDownLatch.await(5, TimeUnit.SECONDS));
closeSessionFactory(sf);
sf = (ClientSessionFactoryInternal) locator.createSessionFactory(primaryServer.getServer().getNodeID().toString());
addSessionFactory(sf);
if (sf2 == null) {
sf2 = (ClientSessionFactoryInternal) locator.createSessionFactory(backupServer.getServer().getNodeID().toString());
ClientSession session2 = createSession(sf2, false, false);
session2.createQueue(new QueueConfiguration(ADDRESS));
addSessionFactory(sf2);
}
return sf;
}
protected final ClientSessionFactoryInternal createSessionFactoryAndWaitForTopology(ServerLocator locator,
TransportConfiguration transportConfiguration,
int topologyMembers) throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(topologyMembers * 2);
locator.addClusterTopologyListener(new LatchClusterTopologyListener(countDownLatch));
ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) locator.createSessionFactory(transportConfiguration);
addSessionFactory(sf);
assertTrue("topology members expected " + topologyMembers, countDownLatch.await(5, TimeUnit.SECONDS));
closeSessionFactory(sf);
sf = (ClientSessionFactoryInternal) locator.createSessionFactory(primaryServer.getServer().getNodeID().toString());
addSessionFactory(sf);
if (sf2 == null) {
sf2 = (ClientSessionFactoryInternal) locator.createSessionFactory(backupServer.getServer().getNodeID().toString());
ClientSession session2 = createSession(sf2, false, false);
session2.createQueue(new QueueConfiguration(ADDRESS));
addSessionFactory(sf2);
}
return sf;
}
@Override
protected void createClientSessionFactory() throws Exception {
if (primaryServer.getServer().isStarted()) {
sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
sf = (ClientSessionFactoryInternal) locator.createSessionFactory(primaryServer.getServer().getNodeID().toString());
} else {
sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
}
}
@Override
protected void createSessionFactory() throws Exception {
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setRetryInterval(100);
sf = createSessionFactoryAndWaitForTopology(locator, getConnectorTransportConfiguration(true, 0), 2);
if (sf2 == null) {
sf2 = (ClientSessionFactoryInternal) locator.createSessionFactory(backupServer.getServer().getNodeID().toString());
addSessionFactory(sf2);
ClientSession session2 = createSession(sf2, false, false);
session2.createQueue(new QueueConfiguration(ADDRESS));
}
}
private TransportConfiguration getConnectorTransportConfiguration(boolean primary, int server) {
return TransportConfigurationUtils.getInVMConnector(primary, server);
}
private TransportConfiguration getAcceptorTransportConfiguration(boolean primary, int server) {
return TransportConfigurationUtils.getInVMAcceptor(primary, server);
}
/**
* TODO: https://issues.apache.org/jira/browse/ARTEMIS-2709
* this test has been intermittently failing since its day one.
* Ignoring the test for now until we can fix it.
* @throws Exception
*/
@Test
public void scaleDownDelay() throws Exception {
createSessionFactory();
ClientSession session = createSession(sf, true, true);
session.createQueue(new QueueConfiguration(ADDRESS));
ClientProducer producer = session.createProducer(ADDRESS);
//send enough messages to ensure that when the client fails over scaledown hasn't complete
sendMessages(session, producer, 1000);
crash(session);
// Wait for failover to happen
Queue serverQueue = backupServer.getServer().locateQueue(ADDRESS);
Wait.assertEquals(1000, serverQueue::getMessageCount);
ClientConsumer consumer = session.createConsumer(ADDRESS);
session.start();
receiveDurableMessages(consumer);
session.close();
sf.close();
Assert.assertEquals(0, sf.numSessions());
Assert.assertEquals(0, sf.numConnections());
}
// https://jira.jboss.org/jira/browse/HORNETQ-285
@Override
@Test
public void testFailoverOnInitialConnection() throws Exception {
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setRetryInterval(100);
sf = createSessionFactoryAndWaitForTopology(locator, 2);
// Crash primary server
crash();
ClientSession session = createSession(sf);
//session.createQueue(new QueueConfiguration(FailoverTestBase.ADDRESS));
ClientProducer producer = session.createProducer(ADDRESS);
sendMessages(session, producer, NUM_MESSAGES);
ClientConsumer consumer = session.createConsumer(ADDRESS);
session.start();
receiveMessages(consumer);
session.close();
}
@Override
@Test
public void testCreateNewFactoryAfterFailover() throws Exception {
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
sf = createSessionFactoryAndWaitForTopology(locator, 2);
ClientSession session = sendAndConsume(sf, true);
crash(true, session);
session.close();
long timeout;
timeout = System.currentTimeMillis() + 5000;
while (timeout > System.currentTimeMillis()) {
try {
createClientSessionFactory();
break;
} catch (Exception e) {
// retrying
Thread.sleep(100);
}
}
session = sendAndConsume(sf, false);
}
@Override
public void testTimeoutOnFailoverTransactionCommitTimeoutCommunication() throws Exception {
}
@Override
@Ignore
public void testFailBothRestartPrimary() throws Exception {
}
//invalid tests for primary to primary failover
//all the timeout ones aren't as we don't migrate timeouts, any failback or server restart
//or replicating tests aren't either
@Override
@Ignore
public void testPrimaryAndBackupBackupComesBackNewFactory() throws Exception {
}
@Override
@Ignore
public void testPrimaryAndBackupPrimaryComesBackNewFactory() {
}
@Override
@Ignore
public void testTimeoutOnFailoverConsumeBlocked() throws Exception {
}
@Override
@Ignore
public void testFailoverMultipleSessionsWithConsumers() throws Exception {
//
}
@Override
@Ignore
public void testTimeoutOnFailover() throws Exception {
}
@Override
@Ignore
public void testTimeoutOnFailoverTransactionRollback() throws Exception {
}
@Override
@Ignore
public void testTimeoutOnFailoverConsume() throws Exception {
}
@Override
@Ignore
public void testTimeoutOnFailoverTransactionCommit() throws Exception {
}
@Override
@Ignore
public void testFailBack() throws Exception {
}
@Override
@Ignore
public void testFailBackPrimaryRestartsBackupIsGone() throws Exception {
}
@Override
@Ignore
public void testPrimaryAndBackupPrimaryComesBack() throws Exception {
}
@Override
@Ignore
public void testSimpleFailover() throws Exception {
}
@Override
@Ignore
public void testFailThenReceiveMoreMessagesAfterFailover2() throws Exception {
}
@Override
@Ignore
public void testWithoutUsingTheBackup() throws Exception {
}
//todo check to see which failing tests are valid,
@Override
@Ignore
public void testSimpleSendAfterFailoverDurableNonTemporary() throws Exception {
}
@Override
@Ignore
public void testCommitOccurredUnblockedAndResendNoDuplicates() throws Exception {
}
@Override
@Ignore
public void testFailPrimaryTooSoon() throws Exception {
}
}