forked from apache/pulsar
/
ManagedCursor.java
713 lines (639 loc) · 23.3 KB
/
ManagedCursor.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/**
* 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;
import com.google.common.base.Predicate;
import com.google.common.collect.Range;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.bookkeeper.common.annotation.InterfaceAudience;
import org.apache.bookkeeper.common.annotation.InterfaceStability;
import org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.FindEntryCallback;
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.impl.PositionImpl;
/**
* A ManagedCursor is a persisted cursor inside a ManagedLedger.
*
* <p/>The ManagedCursor is used to read from the ManagedLedger and to signal when the consumer is done with the
* messages that it has read before.
*/
@InterfaceAudience.LimitedPrivate
@InterfaceStability.Stable
public interface ManagedCursor {
@SuppressWarnings("checkstyle:javadoctype")
enum FindPositionConstraint {
SearchActiveEntries, SearchAllAvailableEntries
}
@SuppressWarnings("checkstyle:javadoctype")
enum IndividualDeletedEntries {
Include, Exclude
}
/**
* Get the unique cursor name.
*
* @return the cursor name
*/
String getName();
/**
* Get the last active time of the cursor.
*
* @return the last active time of the cursor
*/
long getLastActive();
/**
* Update the last active time of the cursor
*
*/
void updateLastActive();
/**
* Return any properties that were associated with the last stored position.
*/
Map<String, Long> getProperties();
/**
* Add a property associated with the last stored position.
*/
boolean putProperty(String key, Long value);
/**
* Remove a property associated with the last stored position.
*/
boolean removeProperty(String key);
/**
* Read entries from the ManagedLedger, up to the specified number. The returned list can be smaller.
*
* @param numberOfEntriesToRead
* maximum number of entries to return
* @return the list of entries
* @throws ManagedLedgerException
*/
List<Entry> readEntries(int numberOfEntriesToRead) throws InterruptedException, ManagedLedgerException;
/**
* Asynchronously read entries from the ManagedLedger.
*
* @see #readEntries(int)
* @param numberOfEntriesToRead
* maximum number of entries to return
* @param callback
* callback object
* @param ctx
* opaque context
* @param maxPosition
* max position can read
*/
void asyncReadEntries(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
PositionImpl maxPosition);
/**
* Asynchronously read entries from the ManagedLedger.
*
* @param numberOfEntriesToRead maximum number of entries to return
* @param maxSizeBytes max size in bytes of the entries to return
* @param callback callback object
* @param ctx opaque context
* @param maxPosition max position can read
*/
void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes, ReadEntriesCallback callback,
Object ctx, PositionImpl maxPosition);
/**
* Get 'N'th entry from the mark delete position in the cursor without updating any cursor positions.
*
* @param n
* entry position
* @param deletedEntries
* skip individual deleted entries
*
* @return the entry
*
* @throws InterruptedException
* @throws ManagedLedgerException
*/
Entry getNthEntry(int n, IndividualDeletedEntries deletedEntries)
throws InterruptedException, ManagedLedgerException;
/**
* Asynchronously get 'N'th entry from the mark delete position in the cursor without updating any cursor positions.
*
* @param n
* entry position
* @param deletedEntries
* skip individual deleted entries
* @param callback
* @param ctx
*/
void asyncGetNthEntry(int n, IndividualDeletedEntries deletedEntries, ReadEntryCallback callback,
Object ctx);
/**
* Read entries from the ManagedLedger, up to the specified number. The returned list can be smaller.
*
* <p/>If no entries are available, the method will block until at least a new message will be persisted.
*
* @param numberOfEntriesToRead
* maximum number of entries to return
* @return the list of entries
* @throws ManagedLedgerException
*/
List<Entry> readEntriesOrWait(int numberOfEntriesToRead) throws InterruptedException, ManagedLedgerException;
/**
* Read entries from the ManagedLedger, up to the specified number and size.
*
* <p/>
* If no entries are available, the method will block until at least a new message will be persisted.
*
* @param maxEntries
* maximum number of entries to return
* @param maxSizeBytes
* max size in bytes of the entries to return
* @return the list of entries
* @throws ManagedLedgerException
*/
List<Entry> readEntriesOrWait(int maxEntries, long maxSizeBytes)
throws InterruptedException, ManagedLedgerException;
/**
* Asynchronously read entries from the ManagedLedger.
*
* <p/>If no entries are available, the callback will not be triggered. Instead it will be registered to wait until
* a new message will be persisted into the managed ledger
*
* @see #readEntriesOrWait(int)
* @param numberOfEntriesToRead
* maximum number of entries to return
* @param callback
* callback object
* @param ctx
* opaque context
* @param maxPosition
* max position can read
*/
void asyncReadEntriesOrWait(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx,
PositionImpl maxPosition);
/**
* Asynchronously read entries from the ManagedLedger, up to the specified number and size.
*
* <p/>If no entries are available, the callback will not be triggered. Instead it will be registered to wait until
* a new message will be persisted into the managed ledger
*
* @see #readEntriesOrWait(int, long)
* @param maxEntries
* maximum number of entries to return
* @param maxSizeBytes
* max size in bytes of the entries to return
* @param callback
* callback object
* @param ctx
* opaque context
* @param maxPosition
* max position can read
*/
void asyncReadEntriesOrWait(int maxEntries, long maxSizeBytes, ReadEntriesCallback callback, Object ctx,
PositionImpl maxPosition);
/**
* Cancel a previously scheduled asyncReadEntriesOrWait operation.
*
* @see #asyncReadEntriesOrWait(int, ReadEntriesCallback, Object, PositionImpl)
* @return true if the read operation was canceled or false if there was no pending operation
*/
boolean cancelPendingReadRequest();
/**
* Tells whether this cursor has already consumed all the available entries.
*
* <p/>This method is not blocking.
*
* @return true if there are pending entries to read, false otherwise
*/
boolean hasMoreEntries();
/**
* Return the number of messages that this cursor still has to read.
*
* <p/>This method has linear time complexity on the number of ledgers included in the managed ledger.
*
* @return the number of entries
*/
long getNumberOfEntries();
/**
* Return the number of non-deleted messages on this cursor.
*
* <p/>This will also include messages that have already been read from the cursor but not deleted or mark-deleted
* yet.
*
* <p/>This method has linear time complexity on the number of ledgers included in the managed ledger.
*
* @param isPrecise set to true to get precise backlog count
* @return the number of entries
*/
long getNumberOfEntriesInBacklog(boolean isPrecise);
/**
* This signals that the reader is done with all the entries up to "position" (included). This can potentially
* trigger a ledger deletion, if all the other cursors are done too with the underlying ledger.
*
* @param position
* the last position that have been successfully consumed
*
* @throws ManagedLedgerException
*/
void markDelete(Position position) throws InterruptedException, ManagedLedgerException;
/**
* This signals that the reader is done with all the entries up to "position" (included). This can potentially
* trigger a ledger deletion, if all the other cursors are done too with the underlying ledger.
*
* @param position
* the last position that have been successfully consumed
* @param properties
* additional user-defined properties that can be associated with a particular cursor position
*
* @throws ManagedLedgerException
*/
void markDelete(Position position, Map<String, Long> properties)
throws InterruptedException, ManagedLedgerException;
/**
* Asynchronous mark delete.
*
* @see #markDelete(Position)
* @param position
* the last position that have been successfully consumed
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncMarkDelete(Position position, MarkDeleteCallback callback, Object ctx);
/**
* Asynchronous mark delete.
*
* @see #markDelete(Position)
* @param position
* the last position that have been successfully consumed
* @param properties
* additional user-defined properties that can be associated with a particular cursor position
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncMarkDelete(Position position, Map<String, Long> properties, MarkDeleteCallback callback, Object ctx);
/**
* Delete a single message.
*
* <p/>Mark a single message for deletion. When all the previous messages are all deleted, then markDelete() will be
* called internally to advance the persistent acknowledged position.
*
* <p/>The deletion of the message is not persisted into the durable storage and cannot be recovered upon the
* reopening of the ManagedLedger
*
* @param position
* the position of the message to be deleted
*/
void delete(Position position) throws InterruptedException, ManagedLedgerException;
/**
* Delete a single message asynchronously
*
* <p/>Mark a single message for deletion. When all the previous messages are all deleted, then markDelete() will be
* called internally to advance the persistent acknowledged position.
*
* <p/>The deletion of the message is not persisted into the durable storage and cannot be recovered upon the
* reopening of the ManagedLedger
*
* @param position
* the position of the message to be deleted
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncDelete(Position position, DeleteCallback callback, Object ctx);
/**
* Delete a group of entries.
*
* <p/>
* Mark multiple single messages for deletion. When all the previous messages are all deleted, then markDelete()
* will be called internally to advance the persistent acknowledged position.
*
* <p/>
* The deletion of the message is not persisted into the durable storage and cannot be recovered upon the reopening
* of the ManagedLedger
*
* @param positions
* positions of the messages to be deleted
*/
void delete(Iterable<Position> positions) throws InterruptedException, ManagedLedgerException;
/**
* Delete a group of messages asynchronously
*
* <p/>
* Mark a group of messages for deletion. When all the previous messages are all deleted, then markDelete() will be
* called internally to advance the persistent acknowledged position.
*
* <p/>
* The deletion of the messages is not persisted into the durable storage and cannot be recovered upon the reopening
* of the ManagedLedger
*
* @param position
* the positions of the messages to be deleted
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncDelete(Iterable<Position> position, DeleteCallback callback, Object ctx);
/**
* Get the read position. This points to the next message to be read from the cursor.
*
* @return the read position
*/
Position getReadPosition();
/**
* Get the newest mark deleted position on this cursor.
*
* @return the mark deleted position
*/
Position getMarkDeletedPosition();
/**
* Get the persistent newest mark deleted position on this cursor.
*
* @return the persistent mark deleted position
*/
Position getPersistentMarkDeletedPosition();
/**
* Rewind the cursor to the mark deleted position to replay all the already read but not yet mark deleted messages.
*
* <p/>The next message to be read is the one after the current mark deleted message.
*/
void rewind();
/**
* Move the cursor to a different read position.
*
* <p/>If the new position happens to be before the already mark deleted position, it will be set to the mark
* deleted position instead.
*
* @param newReadPosition
* the position where to move the cursor
*/
default void seek(Position newReadPosition) {
seek(newReadPosition, false);
}
void seek(Position newReadPosition, boolean force);
/**
* Clear the cursor backlog.
*
* <p/>Consume all the entries for this cursor.
*/
void clearBacklog() throws InterruptedException, ManagedLedgerException;
/**
* Clear the cursor backlog.
*
* <p/>Consume all the entries for this cursor.
*
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncClearBacklog(ClearBacklogCallback callback, Object ctx);
/**
* Skip n entries from the read position of this cursor.
*
* @param numEntriesToSkip
* number of entries to skip
* @param deletedEntries
* skip individual deleted entries
* @throws InterruptedException
* @throws ManagedLedgerException
*/
void skipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries)
throws InterruptedException, ManagedLedgerException;
/**
* Skip n entries from the read position of this cursor.
*
* @param numEntriesToSkip
* number of entries to skip
* @param deletedEntries
* skip individual deleted entries
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries,
final SkipEntriesCallback callback, Object ctx);
/**
* Find the newest entry that matches the given predicate. Will only search among active entries
*
* @param condition
* predicate that reads an entry an applies a condition
* @return Position of the newest entry that matches the given predicate
* @throws InterruptedException
* @throws ManagedLedgerException
*/
Position findNewestMatching(Predicate<Entry> condition) throws InterruptedException, ManagedLedgerException;
/**
* Find the newest entry that matches the given predicate.
*
* @param constraint
* search only active entries or all entries
* @param condition
* predicate that reads an entry an applies a condition
* @return Position of the newest entry that matches the given predicate
* @throws InterruptedException
* @throws ManagedLedgerException
*/
Position findNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition) throws InterruptedException, ManagedLedgerException;
/**
* Find the newest entry that matches the given predicate.
*
* @param constraint
* search only active entries or all entries
* @param condition
* predicate that reads an entry an applies a condition
* @param callback
* callback object returning the resultant position
* @param ctx
* opaque context
*/
void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition,
FindEntryCallback callback, Object ctx);
/**
* reset the cursor to specified position to enable replay of messages.
*
* @param position
* position to move the cursor to
* @throws InterruptedException
* @throws ManagedLedgerException
*/
void resetCursor(final Position position) throws InterruptedException, ManagedLedgerException;
/**
* reset the cursor to specified position to enable replay of messages.
*
* @param position
* position to move the cursor to
* @param callback
* callback object
*/
void asyncResetCursor(final Position position, AsyncCallbacks.ResetCursorCallback callback);
/**
* Read the specified set of positions from ManagedLedger.
*
* @param positions
* set of positions to read
* @return the list of entries
* @throws InterruptedException
* @throws ManagedLedgerException
*/
List<Entry> replayEntries(Set<? extends Position> positions)
throws InterruptedException, ManagedLedgerException;
/**
* Read the specified set of positions from ManagedLedger without ordering.
*
* @param positions
* set of positions to read
* @param callback
* callback object returning the list of entries
* @param ctx
* opaque context
* @return skipped positions
* set of positions which are already deleted/acknowledged and skipped while replaying them
*/
Set<? extends Position> asyncReplayEntries(
Set<? extends Position> positions, ReadEntriesCallback callback, Object ctx);
/**
* Read the specified set of positions from ManagedLedger.
*
* @param positions
* set of positions to read
* @param callback
* callback object returning the list of entries
* @param ctx
* opaque context
* @param sortEntries
* callback with sorted entry list.
* @return skipped positions
* set of positions which are already deleted/acknowledged and skipped while replaying them
*/
Set<? extends Position> asyncReplayEntries(
Set<? extends Position> positions, ReadEntriesCallback callback, Object ctx, boolean sortEntries);
/**
* Close the cursor and releases the associated resources.
*
* @throws InterruptedException
* @throws ManagedLedgerException
*/
void close() throws InterruptedException, ManagedLedgerException;
/**
* Close the cursor asynchronously and release the associated resources.
*
* @param callback
* callback object
* @param ctx
* opaque context
*/
void asyncClose(AsyncCallbacks.CloseCallback callback, Object ctx);
/**
* Get the first position.
*
* @return the first position
*/
Position getFirstPosition();
/**
* Activate cursor: EntryCacheManager caches entries only for activated-cursors.
*
*/
void setActive();
/**
* Deactivate cursor.
*
*/
void setInactive();
/**
* A cursor that is set as always-inactive will never trigger the caching of
* entries.
*/
void setAlwaysInactive();
/**
* Checks if cursor is active or not.
*
* @return
*/
boolean isActive();
/**
* Tells whether the cursor is durable or just kept in memory.
*/
boolean isDurable();
/**
* Returns total number of entries from the first not-acked message to current dispatching position.
*
* @return
*/
long getNumberOfEntriesSinceFirstNotAckedMessage();
/**
* Returns number of mark-Delete range.
*
* @return
*/
int getTotalNonContiguousDeletedMessagesRange();
/**
* Returns the serialized size of mark-Delete ranges.
*/
int getNonContiguousDeletedMessagesRangeSerializedSize();
/**
* Returns the estimated size of the unacknowledged backlog for this cursor
*
* @return the estimated size from the mark delete position of the cursor
*/
long getEstimatedSizeSinceMarkDeletePosition();
/**
* Returns cursor throttle mark-delete rate.
*
* @return
*/
double getThrottleMarkDelete();
/**
* Update throttle mark delete rate.
*
*/
void setThrottleMarkDelete(double throttleMarkDelete);
/**
* Get {@link ManagedLedger} attached with cursor
*
* @return ManagedLedger
*/
ManagedLedger getManagedLedger();
/**
* Get last individual deleted range
* @return range
*/
Range<PositionImpl> getLastIndividualDeletedRange();
/**
* Trim delete entries for the given entries
*/
void trimDeletedEntries(List<Entry> entries);
/**
* Get deleted batch indexes list for a batch message.
*/
long[] getDeletedBatchIndexesAsLongArray(PositionImpl position);
/**
* @return the managed cursor stats MBean
*/
ManagedCursorMXBean getStats();
/**
* Checks if read position changed since this method was called last time.
*
* @return if read position changed
*/
boolean checkAndUpdateReadPositionChanged();
}