/
MegaCleanerPagingTest.java
357 lines (290 loc) · 14.3 KB
/
MegaCleanerPagingTest.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
/*
* 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.soak.paging;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
import org.apache.activemq.artemis.core.settings.impl.AddressFullMessagePolicy;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.logs.AssertionLoggerHandler;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.tests.util.CFUtil;
import org.apache.activemq.artemis.utils.SpawnedVMSupport;
import org.apache.activemq.artemis.utils.Wait;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* PageCleanup should still be able to perform it well.
* */
// supressing because the helper methods need to be public as they are called from a spawned java
@SuppressWarnings("JUnit4TestNotRun")
public class MegaCleanerPagingTest extends ActiveMQTestBase {
// set this to true to have the test to be called directly
// useful for debugging, keeping it false
private static final boolean DIRECT_CALL = false;
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final int OK = 35; // Abitrary code the spawn must return if ok.
@Test
public void testCleanup() throws Throwable {
if (DIRECT_CALL) {
internalTestRegular();
} else {
remoteCall("internalTestRegular");
}
}
@Test
public void testCleanupMidstream() throws Throwable {
if (DIRECT_CALL) {
internalTestMidstream();
} else {
remoteCall("internalTestMidstream");
}
}
@Test
public void testRestart() throws Throwable {
remoteCall("populate");
logger.debug("Resuming...");
remoteCall("resume");
logger.debug("....done");
}
private void remoteCall(String methodName) throws Exception {
// Using a spawn to limit memory consumption to the test
Process process = SpawnedVMSupport.spawnVM(MegaCleanerPagingTest.class.getName(), new String[]{"-Xmx512M"}, getTestDir(), methodName);
logger.debug("process PID {}", process.pid());
Assert.assertTrue(process.waitFor(10, TimeUnit.MINUTES));
Assert.assertEquals(OK, process.exitValue());
}
// I am using a separate VM to limit memory..
// and the test will pass the parameters needed for the JUnit Class to be able to proceed
public static void main(String[] arg) {
try {
MegaCleanerPagingTest megaCleanerPagingTest = new MegaCleanerPagingTest();
megaCleanerPagingTest.setTestDir(arg[0]);
String methodName = arg[1];
Method method = megaCleanerPagingTest.getClass().getMethod(methodName);
method.invoke(megaCleanerPagingTest);
System.exit(OK);
} catch (Throwable e) {
e.printStackTrace();
logger.warn(e.getMessage(), e);
System.exit(-1);
}
}
public void internalTestMidstream() throws Throwable {
internalTest(true);
}
public void internalTestRegular() throws Throwable {
internalTest(false);
}
public void populate() throws Throwable {
ActiveMQServer server = createServer(true, true);
server.getConfiguration().clearAddressSettings();
server.getConfiguration().addAddressSetting("#", new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxSizeMessages(1000).setPageSizeBytes(10 * 1024 * 1024));
server.start();
// if I didn't limit the memory on this test, the NUMBER_OF_MESSAGES would have to be something huge such as 500_000, and that could be a moving target
// if more memory is set to the JUNIT Runner.
// This is the main reason we limit memory on this test
int NUMBER_OF_MESSAGES = 100_000;
String queueName = "testPageAndDepage";
server.addAddressInfo(new AddressInfo(queueName).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server.createQueue(new QueueConfiguration(queueName).setRoutingType(RoutingType.ANYCAST));
ConnectionFactory factory = CFUtil.createConnectionFactory("core", "tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
org.apache.activemq.artemis.core.server.Queue serverQueue = server.locateQueue(queueName);
Assert.assertNotNull(serverQueue);
serverQueue.getPagingStore().startPaging();
ConnectionFactory cf = CFUtil.createConnectionFactory("core", "tcp://localhost:61616?consumerWindowSize=0");
Assert.assertEquals(0, ((ActiveMQConnectionFactory)cf).getServerLocator().getConsumerWindowSize());
final int SIZE = 10 * 1024;
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
producer.send(session.createTextMessage(createBuffer(i, SIZE)));
if (i % 1000 == 0) {
logger.debug("sent {} messages", i);
session.commit();
}
}
session.commit();
PagingStoreImpl store = (PagingStoreImpl) server.getPagingManager().getPageStore(SimpleString.toSimpleString(queueName));
store.disableCleanup();
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
for (int i = 0; i < NUMBER_OF_MESSAGES / 2; i++) {
TextMessage message = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals(createBuffer(i, SIZE), message.getText());
if (i % 1000 == 0) {
logger.debug("received {} messages", i);
session.commit();
}
}
session.commit();
connection.close();
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
server.stop();
Assert.assertFalse(loggerHandler.findText("AMQ222023")); // error associated with OME
Assert.assertFalse(loggerHandler.findText("AMQ222010")); // critical IO Error
}
}
public void resume() throws Throwable {
ActiveMQServer server = createServer(true, true);
server.getConfiguration().clearAddressSettings();
server.getConfiguration().addAddressSetting("#", new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxSizeMessages(1000).setPageSizeBytes(10 * 1024 * 1024));
server.start();
// if I didn't limit the memory on this test, the NUMBER_OF_MESSAGES would have to be something huge such as 500_000, and that could be a moving target
// if more memory is set to the JUNIT Runner.
// This is the main reason we limit memory on this test
int NUMBER_OF_MESSAGES = 100_000;
String queueName = "testPageAndDepage";
ConnectionFactory factory = CFUtil.createConnectionFactory("core", "tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue(queueName);
org.apache.activemq.artemis.core.server.Queue serverQueue = server.locateQueue(queueName);
Assert.assertNotNull(serverQueue);
serverQueue.getPagingStore().startPaging();
ConnectionFactory cf = CFUtil.createConnectionFactory("core", "tcp://localhost:61616?consumerWindowSize=0");
Assert.assertEquals(0, ((ActiveMQConnectionFactory)cf).getServerLocator().getConsumerWindowSize());
final int SIZE = 10 * 1024;
session.commit();
PagingStoreImpl store = (PagingStoreImpl) server.getPagingManager().getPageStore(SimpleString.toSimpleString(queueName));
store.disableCleanup();
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
for (int i = NUMBER_OF_MESSAGES / 2; i < NUMBER_OF_MESSAGES; i++) {
TextMessage message = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals(createBuffer(i, SIZE), message.getText());
if (i % 1000 == 0) {
logger.debug("received {} messages", i);
session.commit();
}
}
session.commit();
Assert.assertNull(consumer.receiveNoWait());
connection.close();
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
store.getCursorProvider().resumeCleanup();
server.stop();
Assert.assertFalse(loggerHandler.findText("AMQ222023")); // error associated with OME
Assert.assertFalse(loggerHandler.findText("AMQ222010")); // critical IO Error
}
}
public void internalTest(boolean midstream) throws Throwable {
ActiveMQServer server = createServer(true, true);
server.getConfiguration().clearAddressSettings();
server.getConfiguration().addAddressSetting("#", new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxSizeMessages(1000).setPageSizeBytes(10 * 1024 * 1024));
server.start();
// if I didn't limit the memory on this test, the NUMBER_OF_MESSAGES would have to be something huge such as 500_000, and that could be a moving target
// if more memory is set to the JUNIT Runner.
// This is the main reason we limit memory on this test
int NUMBER_OF_MESSAGES = 50_000;
String queueName = "testPageAndDepage";
server.addAddressInfo(new AddressInfo(queueName).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server.createQueue(new QueueConfiguration(queueName).setRoutingType(RoutingType.ANYCAST));
ConnectionFactory factory = CFUtil.createConnectionFactory("core", "tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
org.apache.activemq.artemis.core.server.Queue serverQueue = server.locateQueue(queueName);
Assert.assertNotNull(serverQueue);
serverQueue.getPagingStore().startPaging();
ConnectionFactory cf = CFUtil.createConnectionFactory("core", "tcp://localhost:61616?consumerWindowSize=0");
Assert.assertEquals(0, ((ActiveMQConnectionFactory)cf).getServerLocator().getConsumerWindowSize());
Connection slowConnection = cf.createConnection();
Session slowSession = slowConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Queue slowQueue = slowSession.createQueue(queueName);
MessageProducer slowProducer = slowSession.createProducer(slowQueue);
if (midstream) {
slowProducer.send(session.createTextMessage("slow"));
}
slowConnection.start();
MessageConsumer slowConsumer = slowSession.createConsumer(slowQueue);
TextMessage slowMessage;
if (midstream) {
slowMessage = (TextMessage) slowConsumer.receive(5000);
Assert.assertNotNull(slowMessage);
Assert.assertEquals("slow", slowMessage.getText());
} else {
slowMessage = (TextMessage) slowConsumer.receiveNoWait();
Assert.assertNull(slowMessage);
}
final int SIZE = 10 * 1024;
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
producer.send(session.createTextMessage(createBuffer(i, SIZE)));
if (i % 1000 == 0) {
logger.debug("sent {} messages", i);
session.commit();
}
}
session.commit();
PagingStoreImpl store = (PagingStoreImpl) server.getPagingManager().getPageStore(SimpleString.toSimpleString(queueName));
store.disableCleanup();
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
TextMessage message = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals(createBuffer(i, SIZE), message.getText());
if (i % 1000 == 0) {
logger.debug("received {} messages", i);
session.commit();
}
}
session.commit();
Assert.assertNull(consumer.receiveNoWait());
connection.close();
slowConnection.close();
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
store.enableCleanup();
store.getCursorProvider().scheduleCleanup();
if (!midstream) {
Wait.assertFalse(store::isPaging);
}
server.stop();
Assert.assertFalse(loggerHandler.findText("AMQ222023")); // error associated with OME
Assert.assertFalse(loggerHandler.findText("AMQ222010")); // critical IO Error
}
}
String createBuffer(int msgNumber, int size) {
StringBuffer buffer = new StringBuffer();
buffer.append("message " + msgNumber + " ");
while (buffer.length() < size) {
buffer.append(" Lorem Ipsum Whatever it's saying in there... ");
}
return buffer.toString();
}
}