/
ManagedCursorContainerTest.java
666 lines (510 loc) · 23.1 KB
/
ManagedCursorContainerTest.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
/**
* 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.bookkeeper.mledger.impl;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.bookkeeper.mledger.AsyncCallbacks;
import org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.SkipEntriesCallback;
import org.apache.bookkeeper.mledger.Entry;
import org.apache.bookkeeper.mledger.ManagedCursor;
import org.apache.bookkeeper.mledger.ManagedCursorMXBean;
import org.apache.bookkeeper.mledger.ManagedLedger;
import org.apache.bookkeeper.mledger.ManagedLedgerException;
import org.apache.bookkeeper.mledger.Position;
import org.testng.annotations.Test;
public class ManagedCursorContainerTest {
private static class MockManagedCursor implements ManagedCursor {
ManagedCursorContainer container;
Position position;
String name;
public MockManagedCursor(ManagedCursorContainer container, String name, Position position) {
this.container = container;
this.name = name;
this.position = position;
}
@Override
public Map<String, Long> getProperties() {
return Collections.emptyMap();
}
@Override
public boolean putProperty(String key, Long value) {
return false;
}
@Override
public boolean removeProperty(String key) {
return false;
}
@Override
public boolean isDurable() {
return true;
}
@Override
public List<Entry> readEntries(int numberOfEntriesToRead) throws ManagedLedgerException {
return Lists.newArrayList();
}
@Override
public void asyncReadEntries(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
PositionImpl maxPosition) {
callback.readEntriesComplete(null, ctx);
}
@Override
public void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes, ReadEntriesCallback callback,
Object ctx, PositionImpl maxPosition) {
callback.readEntriesComplete(null, ctx);
}
@Override
public boolean hasMoreEntries() {
return true;
}
@Override
public long getNumberOfEntries() {
return 0;
}
@Override
public long getNumberOfEntriesInBacklog(boolean isPrecise) {
return 0;
}
@Override
public void markDelete(Position position) throws ManagedLedgerException {
markDelete(position, Collections.emptyMap());
}
@Override
public void markDelete(Position position, Map<String, Long> properties) throws ManagedLedgerException {
this.position = position;
container.cursorUpdated(this, (PositionImpl) position);
}
@Override
public void asyncMarkDelete(Position position, MarkDeleteCallback callback, Object ctx) {
fail();
}
@Override
public void asyncMarkDelete(Position position, Map<String, Long> properties, MarkDeleteCallback callback,
Object ctx) {
fail();
}
@Override
public Position getMarkDeletedPosition() {
return position;
}
@Override
public Position getPersistentMarkDeletedPosition() {
return position;
}
@Override
public String getName() {
return name;
}
@Override
public long getLastActive() {
return System.currentTimeMillis();
}
@Override
public void updateLastActive() {
// no-op
}
public String toString() {
return String.format("%s=%s", name, position);
}
@Override
public Position getReadPosition() {
return null;
}
@Override
public void rewind() {
}
@Override
public void seek(Position newReadPosition) {
}
@Override
public void close() {
}
@Override
public void asyncClose(AsyncCallbacks.CloseCallback callback, Object ctx) {
}
@Override
public void delete(Position position) throws InterruptedException, ManagedLedgerException {
}
@Override
public void asyncDelete(Position position, DeleteCallback callback, Object ctx) {
}
@Override
public void delete(Iterable<Position> positions) throws InterruptedException, ManagedLedgerException {
}
@Override
public void asyncDelete(Iterable<Position> position, DeleteCallback callback, Object ctx) {
}
@Override
public void clearBacklog() throws InterruptedException, ManagedLedgerException {
}
@Override
public void asyncClearBacklog(ClearBacklogCallback callback, Object ctx) {
}
@Override
public void skipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries)
throws InterruptedException, ManagedLedgerException {
}
@Override
public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries,
final SkipEntriesCallback callback, Object ctx) {
}
@Override
public Position findNewestMatching(Predicate<Entry> condition)
throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public Position findNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition) throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
AsyncCallbacks.FindEntryCallback callback, Object ctx) {
}
@Override
public void asyncResetCursor(final Position position, AsyncCallbacks.ResetCursorCallback callback) {
}
@Override
public void resetCursor(final Position position) throws ManagedLedgerException, InterruptedException {
}
@Override
public Position getFirstPosition() {
return null;
}
@Override
public void setAlwaysInactive() {
}
@Override
public List<Entry> replayEntries(Set<? extends Position> positions)
throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions, ReadEntriesCallback callback, Object ctx) {
return Sets.newConcurrentHashSet();
}
@Override
public Set<? extends Position> asyncReplayEntries(Set<? extends Position> positions, ReadEntriesCallback callback, Object ctx, boolean sortEntries) {
return Sets.newConcurrentHashSet();
}
@Override
public List<Entry> readEntriesOrWait(int numberOfEntriesToRead)
throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public void asyncReadEntriesOrWait(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
PositionImpl maxPosition) {
}
@Override
public void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes, ReadEntriesCallback callback,
Object ctx, PositionImpl maxPosition) {
}
@Override
public boolean cancelPendingReadRequest() {
return true;
}
@Override
public Entry getNthEntry(int N, IndividualDeletedEntries deletedEntries)
throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public void asyncGetNthEntry(int N, IndividualDeletedEntries deletedEntries, ReadEntryCallback callback,
Object ctx) {
}
@Override
public void setActive() {
}
@Override
public void setInactive() {
}
@Override
public boolean isActive() {
return true;
}
@Override
public long getNumberOfEntriesSinceFirstNotAckedMessage() {
return 0;
}
@Override
public int getTotalNonContiguousDeletedMessagesRange() {
return 0;
}
@Override
public int getNonContiguousDeletedMessagesRangeSerializedSize() {
return 0;
}
@Override
public long getEstimatedSizeSinceMarkDeletePosition() {
return 0L;
}
@Override
public void setThrottleMarkDelete(double throttleMarkDelete) {
}
@Override
public double getThrottleMarkDelete() {
return -1;
}
@Override
public ManagedLedger getManagedLedger() {
return null;
}
@Override
public Range<PositionImpl> getLastIndividualDeletedRange() {
return null;
}
@Override
public void trimDeletedEntries(List<Entry> entries) {
}
@Override
public long[] getDeletedBatchIndexesAsLongArray(PositionImpl position) {
return new long[0];
}
@Override
public ManagedCursorMXBean getStats() {
return null;
}
public void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes, ReadEntriesCallback callback,
Object ctx) {
}
@Override
public List<Entry> readEntriesOrWait(int maxEntries, long maxSizeBytes)
throws InterruptedException, ManagedLedgerException {
return null;
}
@Override
public boolean checkAndUpdateReadPositionChanged() {
return false;
}
}
@Test
public void simple() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
assertNull(container.getSlowestReaderPosition());
ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
container.add(cursor1);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
ManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(2, 2));
container.add(cursor2);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
ManagedCursor cursor3 = new MockManagedCursor(container, "test3", new PositionImpl(2, 0));
container.add(cursor3);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 0));
assertEquals(container.toString(), "[test1=5:5, test2=2:2, test3=2:0]");
ManagedCursor cursor4 = new MockManagedCursor(container, "test4", new PositionImpl(4, 0));
container.add(cursor4);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 0));
ManagedCursor cursor5 = new MockManagedCursor(container, "test5", new PositionImpl(3, 5));
container.add(cursor5);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 0));
cursor3.markDelete(new PositionImpl(3, 0));
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
cursor2.markDelete(new PositionImpl(10, 5));
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(3, 0));
container.removeCursor(cursor3.getName());
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(3, 5));
container.removeCursor(cursor2.getName());
container.removeCursor(cursor5.getName());
container.removeCursor(cursor1.getName());
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(4, 0));
assertTrue(container.hasDurableCursors());
container.removeCursor(cursor4.getName());
assertNull(container.getSlowestReaderPosition());
assertFalse(container.hasDurableCursors());
ManagedCursor cursor6 = new MockManagedCursor(container, "test6", new PositionImpl(6, 5));
container.add(cursor6);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(6, 5));
assertEquals(container.toString(), "[test6=6:5]");
}
@Test
public void updatingCursorOutsideContainer() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
container.add(cursor1);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(2, 2));
container.add(cursor2);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
cursor2.position = new PositionImpl(8, 8);
// Until we don't update the container, the ordering will not change
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
container.cursorUpdated(cursor2, cursor2.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
}
@Test
public void removingCursor() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
container.add(cursor1);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
assertEquals(container.get("test1"), cursor1);
MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(2, 2));
container.add(cursor2);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2));
assertEquals(container.get("test2"), cursor2);
MockManagedCursor cursor3 = new MockManagedCursor(container, "test3", new PositionImpl(1, 1));
container.add(cursor3);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(1, 1));
assertEquals(container.get("test3"), cursor3);
assertEquals(container, Lists.newArrayList(cursor1, cursor2, cursor3));
// Remove the cursor in the middle
container.removeCursor("test2");
assertEquals(container, Lists.newArrayList(cursor1, cursor3));
assertNull(container.get("test2"));
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(1, 1));
container.removeCursor("test3");
assertEquals(container, Lists.newArrayList(cursor1));
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
}
@Test
public void ordering() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
ManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(5, 1));
ManagedCursor cursor3 = new MockManagedCursor(container, "test3", new PositionImpl(7, 1));
ManagedCursor cursor4 = new MockManagedCursor(container, "test4", new PositionImpl(6, 4));
ManagedCursor cursor5 = new MockManagedCursor(container, "test5", new PositionImpl(7, 0));
container.add(cursor1);
container.add(cursor2);
container.add(cursor3);
container.add(cursor4);
container.add(cursor5);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
container.removeCursor("test2");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5));
container.removeCursor("test1");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(6, 4));
container.removeCursor("test4");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 0));
container.removeCursor("test5");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 1));
container.removeCursor("test3");
assertFalse(container.hasDurableCursors());
}
@Test
public void orderingWithUpdates() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
MockManagedCursor c1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
MockManagedCursor c2 = new MockManagedCursor(container, "test2", new PositionImpl(5, 1));
MockManagedCursor c3 = new MockManagedCursor(container, "test3", new PositionImpl(7, 1));
MockManagedCursor c4 = new MockManagedCursor(container, "test4", new PositionImpl(6, 4));
MockManagedCursor c5 = new MockManagedCursor(container, "test5", new PositionImpl(7, 0));
container.add(c1);
container.add(c2);
container.add(c3);
container.add(c4);
container.add(c5);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
c1.position = new PositionImpl(5, 8);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
c2.position = new PositionImpl(5, 6);
container.cursorUpdated(c2, c2.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c1.position = new PositionImpl(6, 8);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c3.position = new PositionImpl(8, 5);
container.cursorUpdated(c3, c3.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c1.position = new PositionImpl(8, 4);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c2.position = new PositionImpl(8, 4);
container.cursorUpdated(c2, c2.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(6, 4));
c4.position = new PositionImpl(7, 1);
container.cursorUpdated(c4, c4.position);
// ////
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 0));
container.removeCursor("test5");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 1));
container.removeCursor("test4");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(8, 4));
container.removeCursor("test1");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(8, 4));
container.removeCursor("test2");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(8, 5));
container.removeCursor("test3");
assertFalse(container.hasDurableCursors());
}
@Test
public void orderingWithUpdatesAndReset() throws Exception {
ManagedCursorContainer container = new ManagedCursorContainer();
MockManagedCursor c1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5));
MockManagedCursor c2 = new MockManagedCursor(container, "test2", new PositionImpl(5, 1));
MockManagedCursor c3 = new MockManagedCursor(container, "test3", new PositionImpl(7, 1));
MockManagedCursor c4 = new MockManagedCursor(container, "test4", new PositionImpl(6, 4));
MockManagedCursor c5 = new MockManagedCursor(container, "test5", new PositionImpl(7, 0));
container.add(c1);
container.add(c2);
container.add(c3);
container.add(c4);
container.add(c5);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
c1.position = new PositionImpl(5, 8);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
c1.position = new PositionImpl(5, 6);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 1));
c2.position = new PositionImpl(6, 8);
container.cursorUpdated(c2, c2.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c3.position = new PositionImpl(8, 5);
container.cursorUpdated(c3, c3.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 6));
c1.position = new PositionImpl(8, 4);
container.cursorUpdated(c1, c1.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(6, 4));
c2.position = new PositionImpl(4, 4);
container.cursorUpdated(c2, c2.position);
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(4, 4));
c4.position = new PositionImpl(7, 1);
container.cursorUpdated(c4, c4.position);
// ////
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(4, 4));
container.removeCursor("test2");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 0));
container.removeCursor("test5");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 1));
container.removeCursor("test1");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(7, 1));
container.removeCursor("test4");
assertEquals(container.getSlowestReaderPosition(), new PositionImpl(8, 5));
container.removeCursor("test3");
assertFalse(container.hasDurableCursors());
}
}