/
JournalAsyncTest.java
351 lines (252 loc) · 9.56 KB
/
JournalAsyncTest.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
/*
* 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.unit.core.journal.impl;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.core.journal.IOCompletion;
import org.apache.activemq.artemis.core.journal.PreparedTransactionInfo;
import org.apache.activemq.artemis.core.journal.RecordInfo;
import org.apache.activemq.artemis.core.journal.impl.JournalImpl;
import org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory;
import org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class JournalAsyncTest extends ActiveMQTestBase {
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
private FakeSequentialFileFactory factory;
private JournalImpl journalImpl = null;
private ArrayList<RecordInfo> records = null;
private ArrayList<PreparedTransactionInfo> transactions = null;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testAsynchronousCommit() throws Exception {
doAsynchronousTest(true);
}
@Test
public void testAsynchronousRollback() throws Exception {
doAsynchronousTest(false);
}
public void doAsynchronousTest(final boolean isCommit) throws Exception {
final int JOURNAL_SIZE = 20000;
setupJournal(JOURNAL_SIZE, 100, 5);
factory.setHoldCallbacks(true, null);
final CountDownLatch latch = new CountDownLatch(1);
class LocalThread extends Thread {
Exception e;
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
journalImpl.appendAddRecordTransactional(1L, i, (byte) 1, new SimpleEncoding(1, (byte) 0));
}
journalImpl.debugWait();
latch.countDown();
factory.setHoldCallbacks(false, null);
if (isCommit) {
journalImpl.appendCommitRecord(1L, true);
} else {
journalImpl.appendRollbackRecord(1L, true);
}
} catch (Exception e) {
e.printStackTrace();
this.e = e;
}
}
}
LocalThread t = new LocalThread();
t.start();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Thread.yield();
Thread.sleep(100);
Assert.assertTrue(t.isAlive());
factory.flushAllCallbacks();
t.join();
if (t.e != null) {
throw t.e;
}
}
@Test
public void testAsyncAppendRecord1() throws Exception {
final int JOURNAL_SIZE = 20000;
setupJournal(JOURNAL_SIZE, 100, 5);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latchHoldDirect = new CountDownLatch(1);
try {
factory.setWriteDirectCallback(() -> {
try {
latchHoldDirect.await(10, TimeUnit.MINUTES);
} catch (Throwable ignored) {
}
});
class LocalThread extends Thread {
Exception e;
@Override
public void run() {
try {
journalImpl.appendAddRecord(1, (byte) 1, new SimpleEncoding(1, (byte) 0), true, null);
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
this.e = e;
}
}
}
LocalThread t = new LocalThread();
t.start();
Assert.assertFalse("journal.append with sync true should hold until the write is done", latch.await(100, TimeUnit.MILLISECONDS));
Thread.yield();
Assert.assertTrue(t.isAlive());
latchHoldDirect.countDown();
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
t.join();
Assert.assertFalse(t.isAlive());
if (t.e != null) {
throw t.e;
}
} finally {
latchHoldDirect.countDown();
}
}
@Test
public void testAsyncAppendRecord2() throws Exception {
final int JOURNAL_SIZE = 20000;
setupJournal(JOURNAL_SIZE, 100, 5);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latchHoldDirect = new CountDownLatch(1);
try {
factory.setWriteDirectCallback(() -> {
try {
latchHoldDirect.await(10, TimeUnit.MINUTES);
} catch (Throwable ignored) {
}
});
class LocalThread extends Thread {
Exception e;
@Override
public void run() {
try {
journalImpl.appendAddRecord(1, (byte) 1, new SimpleEncoding(1, (byte) 0), true, new IOCompletion() {
@Override
public void storeLineUp() {
}
@Override
public void done() {
}
@Override
public void onError(int errorCode, String errorMessage) {
}
});
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
this.e = e;
}
}
}
LocalThread t = new LocalThread();
t.start();
Assert.assertTrue("journal.append with sync true and IOContext should not hold thread", latch.await(10, TimeUnit.SECONDS));
latchHoldDirect.countDown();
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
t.join();
Assert.assertFalse(t.isAlive());
if (t.e != null) {
throw t.e;
}
} finally {
latchHoldDirect.countDown();
}
}
// If a callback error already arrived, we should just throw the exception right away
@Test
public void testPreviousError() throws Exception {
final int JOURNAL_SIZE = 20000;
setupJournal(JOURNAL_SIZE, 100, 5);
factory.setHoldCallbacks(true, null);
factory.setGenerateErrors(true);
journalImpl.appendAddRecordTransactional(1L, 1, (byte) 1, new SimpleEncoding(1, (byte) 0));
journalImpl.debugWait();
factory.flushAllCallbacks();
factory.setGenerateErrors(false);
factory.setHoldCallbacks(false, null);
try {
journalImpl.appendAddRecordTransactional(1L, 2, (byte) 1, new SimpleEncoding(1, (byte) 0));
journalImpl.appendCommitRecord(1L, true);
Assert.fail("Exception expected");
// An exception already happened in one of the elements on this transaction.
// We can't accept any more elements on the transaction
} catch (Exception ignored) {
}
}
@Test
public void testSyncNonTransaction() throws Exception {
final int JOURNAL_SIZE = 20000;
setupJournal(JOURNAL_SIZE, 100, 5);
factory.setGenerateErrors(true);
try {
journalImpl.appendAddRecord(1L, (byte) 0, new SimpleEncoding(1, (byte) 0), true);
Assert.fail("Exception expected");
} catch (Exception ignored) {
}
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
@Override
@Before
public void setUp() throws Exception {
super.setUp();
records = new ArrayList<>();
transactions = new ArrayList<>();
factory = null;
journalImpl = null;
}
@Override
@After
public void tearDown() throws Exception {
if (journalImpl != null) {
try {
journalImpl.stop();
} catch (Throwable ignored) {
}
}
super.tearDown();
}
// Private -------------------------------------------------------
private void setupJournal(final int journalSize,
final int alignment,
final int numberOfMinimalFiles) throws Exception {
if (factory == null) {
factory = new FakeSequentialFileFactory(alignment, true);
}
if (journalImpl != null) {
journalImpl.stop();
}
journalImpl = new JournalImpl(journalSize, numberOfMinimalFiles, numberOfMinimalFiles, 0, 0, factory, "tt", "tt", 1000);
journalImpl.start();
records.clear();
transactions.clear();
journalImpl.load(records, transactions, null);
}
// Inner classes -------------------------------------------------
}