/
FakeSequentialFileFactory.java
683 lines (529 loc) · 18.1 KB
/
FakeSequentialFileFactory.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
* 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.fakes;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.activemq.artemis.ArtemisConstants;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.ActiveMQBuffers;
import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
import org.apache.activemq.artemis.core.io.IOCallback;
import org.apache.activemq.artemis.core.io.SequentialFile;
import org.apache.activemq.artemis.core.io.SequentialFileFactory;
import org.apache.activemq.artemis.core.io.buffer.TimedBuffer;
import org.apache.activemq.artemis.core.journal.EncodingSupport;
import org.apache.activemq.artemis.core.journal.impl.SimpleWaitIOCallback;
public class FakeSequentialFileFactory implements SequentialFileFactory {
private final Map<String, FakeSequentialFile> fileMap = new ConcurrentHashMap<>();
private volatile int alignment;
private final boolean supportsCallback;
private Runnable writeDirectCallback;
private volatile boolean holdCallbacks;
private ListenerHoldCallback holdCallbackListener;
private volatile boolean generateErrors;
private final List<CallbackRunnable> callbacksInHold;
public FakeSequentialFileFactory(final int alignment, final boolean supportsCallback) {
this.alignment = alignment;
this.supportsCallback = supportsCallback;
callbacksInHold = new ArrayList<>();
}
public FakeSequentialFileFactory() {
this(1, false);
}
@Override
public SequentialFileFactory setDatasync(boolean enabled) {
return null;
}
@Override
public boolean isDatasync() {
return false;
}
@Override
public long getBufferSize() {
return ArtemisConstants.DEFAULT_JOURNAL_BUFFER_SIZE_AIO;
}
@Override
public int getMaxIO() {
return 1;
}
public void setWriteDirectCallback(Runnable writeDirectCallback) {
this.writeDirectCallback = writeDirectCallback;
}
@Override
public SequentialFile createSequentialFile(final String fileName) {
FakeSequentialFile sf = fileMap.get(fileName);
if (sf == null || sf.data == null) {
sf = newSequentialFile(fileName);
fileMap.put(fileName, sf);
} else {
sf.getData().position(0);
// log.debug("positioning data to 0");
}
return sf;
}
@Override
public void clearBuffer(final ByteBuffer buffer) {
final int limit = buffer.limit();
buffer.rewind();
for (int i = 0; i < limit; i++) {
buffer.put((byte) 0);
}
buffer.rewind();
}
@Override
public List<String> listFiles(final String extension) {
List<String> files = new ArrayList<>();
for (String s : fileMap.keySet()) {
if (s.endsWith("." + extension)) {
files.add(s);
}
}
return files;
}
public Map<String, FakeSequentialFile> getFileMap() {
return fileMap;
}
public void clear() {
fileMap.clear();
}
@Override
public boolean isSupportsCallbacks() {
return supportsCallback;
}
@Override
public ByteBuffer newBuffer(int size) {
if (size % alignment != 0) {
size = (size / alignment + 1) * alignment;
}
return ByteBuffer.allocate(size);
}
@Override
public int calculateBlockSize(final int position) {
int alignment = getAlignment();
int pos = (position / alignment + (position % alignment != 0 ? 1 : 0)) * alignment;
return pos;
}
@Override
public ByteBuffer wrapBuffer(final byte[] bytes) {
return ByteBuffer.wrap(bytes);
}
public synchronized boolean isHoldCallbacks() {
return holdCallbacks;
}
public synchronized void setHoldCallbacks(final boolean holdCallbacks,
final ListenerHoldCallback holdCallbackListener) {
this.holdCallbacks = holdCallbacks;
this.holdCallbackListener = holdCallbackListener;
}
public boolean isGenerateErrors() {
return generateErrors;
}
public void setGenerateErrors(final boolean generateErrors) {
this.generateErrors = generateErrors;
}
public synchronized void flushAllCallbacks() {
for (Runnable action : callbacksInHold) {
action.run();
}
callbacksInHold.clear();
}
public synchronized void flushCallback(final int position) {
Runnable run = callbacksInHold.get(position);
run.run();
callbacksInHold.remove(run);
}
public synchronized void setCallbackAsError(final int position) {
callbacksInHold.get(position).setSendError(true);
}
public synchronized int getNumberOfCallbacks() {
return callbacksInHold.size();
}
@Override
public int getAlignment() {
return alignment;
}
@Override
public FakeSequentialFileFactory setAlignment(int alignment) {
this.alignment = alignment;
return this;
}
protected FakeSequentialFile newSequentialFile(final String fileName) {
return new FakeSequentialFile(fileName);
}
/**
* This listener will return a message to the test with each callback added
*/
public interface ListenerHoldCallback {
void callbackAdded(ByteBuffer bytes);
}
private class CallbackRunnable implements Runnable {
final FakeSequentialFile file;
final ByteBuffer bytes;
final IOCallback callback;
volatile boolean sendError;
CallbackRunnable(final FakeSequentialFile file, final ByteBuffer bytes, final IOCallback callback) {
this.file = file;
this.bytes = bytes;
this.callback = callback;
}
@Override
public void run() {
if (sendError) {
callback.onError(ActiveMQExceptionType.UNSUPPORTED_PACKET.getCode(), "Fake aio error");
} else {
try {
file.data.put(bytes);
if (callback != null) {
callback.done();
}
} catch (Throwable e) {
e.printStackTrace();
callback.onError(ActiveMQExceptionType.GENERIC_EXCEPTION.getCode(), e.getMessage());
}
}
}
public void setSendError(final boolean sendError) {
this.sendError = sendError;
}
}
public class FakeSequentialFile implements SequentialFile {
private volatile boolean open;
private String fileName;
private ByteBuffer data;
public ByteBuffer getData() {
return data;
}
@Override
public boolean isOpen() {
return open;
}
public void flush() {
}
public FakeSequentialFile(final String fileName) {
this.fileName = fileName;
}
@Override
public synchronized void close() {
open = false;
if (data != null) {
data.position(0);
}
notifyAll();
}
@Override
public ByteBuffer map(int position, long size) throws IOException {
return null;
}
@Override
public void delete() {
fileMap.remove(fileName);
if (open) {
close();
}
}
@Override
public String getFileName() {
return fileName;
}
@Override
public void open() throws Exception {
open(1, true);
}
@Override
public synchronized void open(final int currentMaxIO, final boolean useExecutor) throws Exception {
open = true;
checkAndResize(0);
}
@Override
public void fill(final int size) throws Exception {
if (!open) {
throw new IllegalStateException("Is closed");
}
checkAndResize(size);
// log.debug("size is " + size + " pos is " + pos);
for (int i = 0; i < size; i++) {
byte[] array = data.array();
array[i] = 0;
// log.debug("Filling " + pos + " with char " + fillCharacter);
}
}
@Override
public int read(final ByteBuffer bytes) throws Exception {
return read(bytes, null);
}
@Override
public int read(final ByteBuffer bytes, final IOCallback callback) throws Exception {
if (!open) {
throw new IllegalStateException("Is closed");
}
byte[] bytesRead = new byte[Math.min(bytes.remaining(), data.remaining())];
data.get(bytesRead);
bytes.put(bytesRead);
bytes.rewind();
if (callback != null) {
callback.done();
}
return bytesRead.length;
}
@Override
public void position(final long pos) {
if (!open) {
throw new IllegalStateException("Is closed");
}
checkAlignment(pos);
data.position((int) pos);
}
@Override
public long position() {
return data.position();
}
@Override
public synchronized void writeDirect(final ByteBuffer bytes, final boolean sync, final IOCallback callback) {
if (!open) {
throw new IllegalStateException("Is closed");
}
final int position = data == null ? 0 : data.position();
// checkAlignment(position);
// checkAlignment(bytes.limit());
checkAndResize(bytes.limit() + position);
CallbackRunnable action = new CallbackRunnable(this, bytes, callback);
if (generateErrors) {
action.setSendError(true);
}
if (holdCallbacks) {
addCallback(bytes, action);
} else {
action.run();
}
if (writeDirectCallback != null) {
writeDirectCallback.run();
}
}
@Override
public synchronized void blockingWriteDirect(ByteBuffer bytes,
boolean sync,
boolean releaseBuffer) throws Exception {
SimpleWaitIOCallback callback = new SimpleWaitIOCallback();
try {
writeDirect(bytes, sync, callback);
} finally {
callback.waitCompletion();
}
}
@Override
public void sync() throws IOException {
if (supportsCallback) {
throw new IllegalStateException("sync is not supported when supportsCallback=true");
}
}
@Override
public long size() throws Exception {
if (data == null) {
return 0;
} else {
return data.limit();
}
}
@Override
public void writeDirect(final ByteBuffer bytes, final boolean sync) throws Exception {
writeDirect(bytes, sync, null);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#writeInternal(java.nio.ByteBuffer)
*/
public void writeInternal(ByteBuffer bytes) throws Exception {
writeDirect(bytes, true);
}
private void checkAndResize(final int size) {
int oldpos = data == null ? 0 : data.position();
if (data == null || data.array().length < size) {
byte[] newBytes = new byte[size];
if (data != null) {
System.arraycopy(data.array(), 0, newBytes, 0, data.array().length);
}
data = ByteBuffer.wrap(newBytes);
data.position(oldpos);
}
}
/**
* @param bytes
* @param action
*/
private void addCallback(final ByteBuffer bytes, final CallbackRunnable action) {
synchronized (FakeSequentialFileFactory.this) {
callbacksInHold.add(action);
if (holdCallbackListener != null) {
holdCallbackListener.callbackAdded(bytes);
}
}
}
@Override
public int calculateBlockStart(final int position) throws Exception {
int pos = (position / alignment + (position % alignment != 0 ? 1 : 0)) * alignment;
return pos;
}
@Override
public String toString() {
return "FakeSequentialFile:" + fileName;
}
private void checkAlignment(final long position) {
if (position % alignment != 0) {
throw new IllegalStateException("Position is not aligned to " + alignment);
}
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#renameTo(org.apache.activemq.artemis.core.io.SequentialFile)
*/
@Override
public void renameTo(final String newFileName) throws Exception {
fileMap.remove(fileName);
fileName = newFileName;
fileMap.put(newFileName, this);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#fits(int)
*/
@Override
public boolean fits(final int size) {
return data.position() + size <= data.limit();
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#setBuffering(boolean)
*/
public void setBuffering(final boolean buffering) {
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#lockBuffer()
*/
public void disableAutoFlush() {
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#unlockBuffer()
*/
public void enableAutoFlush() {
}
@Override
public SequentialFile cloneFile() {
return null; // To change body of implemented methods use File | Settings | File Templates.
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#write(org.apache.activemq.artemis.spi.core.remoting.ActiveMQBuffer, boolean, org.apache.activemq.artemis.core.journal.IOCallback)
*/
@Override
public void write(final ActiveMQBuffer bytes, final boolean sync, final IOCallback callback) throws Exception {
bytes.writerIndex(bytes.capacity());
bytes.readerIndex(0);
writeDirect(bytes.toByteBuffer(), sync, callback);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#write(org.apache.activemq.artemis.spi.core.remoting.ActiveMQBuffer, boolean)
*/
@Override
public void write(final ActiveMQBuffer bytes, final boolean sync) throws Exception {
bytes.writerIndex(bytes.capacity());
bytes.readerIndex(0);
writeDirect(bytes.toByteBuffer(), sync);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#write(org.apache.activemq.artemis.core.journal.EncodingSupport, boolean, org.apache.activemq.artemis.core.journal.IOCompletion)
*/
@Override
public void write(final EncodingSupport bytes, final boolean sync, final IOCallback callback) throws Exception {
ByteBuffer buffer = newBuffer(bytes.getEncodeSize());
ActiveMQBuffer outbuffer = ActiveMQBuffers.wrappedBuffer(buffer);
bytes.encode(outbuffer);
write(outbuffer, sync, callback);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#write(org.apache.activemq.artemis.core.journal.EncodingSupport, boolean)
*/
@Override
public void write(final EncodingSupport bytes, final boolean sync) throws Exception {
ByteBuffer buffer = newBuffer(bytes.getEncodeSize());
ActiveMQBuffer outbuffer = ActiveMQBuffers.wrappedBuffer(buffer);
bytes.encode(outbuffer);
write(outbuffer, sync);
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#exists()
*/
@Override
public boolean exists() {
FakeSequentialFile file = fileMap.get(fileName);
return file != null && file.data != null && file.data.capacity() > 0;
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#setTimedBuffer(org.apache.activemq.artemis.core.io.buffer.TimedBuffer)
*/
@Override
public void setTimedBuffer(final TimedBuffer buffer) {
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.core.io.SequentialFile#copyTo(org.apache.activemq.artemis.core.io.SequentialFile)
*/
@Override
public void copyTo(SequentialFile newFileName) {
// TODO Auto-generated method stub
}
@Override
public File getJavaFile() {
throw new UnsupportedOperationException();
}
}
@Override
public void createDirs() throws Exception {
// nothing to be done on the fake Sequential file
}
@Override
public void releaseBuffer(final ByteBuffer buffer) {
}
@Override
public void stop() {
}
@Override
public void activateBuffer(final SequentialFile file) {
}
@Override
public void start() {
}
@Override
public void deactivateBuffer() {
}
@Override
public void flush() {
}
@Override
public void onIOError(Exception exception, String message, SequentialFile file) {
}
@Override
public ByteBuffer allocateDirectBuffer(int size) {
return ByteBuffer.allocateDirect(size);
}
@Override
public void releaseDirectBuffer(ByteBuffer buffer) {
}
@Override
public File getDirectory() {
// TODO Auto-generated method stub
return null;
}
}