48
48
public class KafkaTopicConsumerManager implements Closeable {
49
49
50
50
private final PersistentTopic topic ;
51
- private final KafkaRequestHandler requestHandler ;
52
51
53
52
private final AtomicBoolean closed = new AtomicBoolean (false );
54
53
@@ -67,13 +66,21 @@ public class KafkaTopicConsumerManager implements Closeable {
67
66
68
67
private final boolean skipMessagesWithoutIndex ;
69
68
69
+ private final String description ;
70
+
70
71
KafkaTopicConsumerManager (KafkaRequestHandler requestHandler , PersistentTopic topic ) {
72
+ this (requestHandler .ctx .channel () + "" ,
73
+ requestHandler .isSkipMessagesWithoutIndex (),
74
+ topic );
75
+ }
76
+
77
+ public KafkaTopicConsumerManager (String description , boolean skipMessagesWithoutIndex , PersistentTopic topic ) {
71
78
this .topic = topic ;
72
79
this .cursors = new ConcurrentHashMap <>();
73
80
this .createdCursors = new ConcurrentHashMap <>();
74
81
this .lastAccessTimes = new ConcurrentHashMap <>();
75
- this .requestHandler = requestHandler ;
76
- this .skipMessagesWithoutIndex = requestHandler . isSkipMessagesWithoutIndex () ;
82
+ this .description = description ;
83
+ this .skipMessagesWithoutIndex = skipMessagesWithoutIndex ;
77
84
}
78
85
79
86
// delete expired cursors, so backlog can be cleared.
@@ -96,7 +103,7 @@ void deleteOneExpiredCursor(long offset) {
96
103
if (cursorFuture != null ) {
97
104
if (log .isDebugEnabled ()) {
98
105
log .debug ("[{}] Cursor timed out for offset: {}, cursors cache size: {}" ,
99
- requestHandler . ctx . channel () , offset , cursors .size ());
106
+ description , offset , cursors .size ());
100
107
}
101
108
102
109
// TODO: Should we just cancel this future?
@@ -118,14 +125,19 @@ public void deleteOneCursorAsync(ManagedCursor cursor, String reason) {
118
125
public void deleteCursorComplete (Object ctx ) {
119
126
if (log .isDebugEnabled ()) {
120
127
log .debug ("[{}] Cursor {} for topic {} deleted successfully for reason: {}." ,
121
- requestHandler . ctx . channel () , cursor .getName (), topic .getName (), reason );
128
+ description , cursor .getName (), topic .getName (), reason );
122
129
}
123
130
}
124
131
125
132
@ Override
126
133
public void deleteCursorFailed (ManagedLedgerException exception , Object ctx ) {
127
- log .warn ("[{}] Error deleting cursor {} for topic {} for reason: {}." ,
128
- requestHandler .ctx .channel (), cursor .getName (), topic .getName (), reason , exception );
134
+ if (exception instanceof ManagedLedgerException .CursorNotFoundException ) {
135
+ log .debug ("[{}] Cursor already deleted {} for topic {} for reason: {} - {}." ,
136
+ description , cursor .getName (), topic .getName (), reason , exception .toString ());
137
+ } else {
138
+ log .warn ("[{}] Error deleting cursor {} for topic {} for reason: {}." ,
139
+ description , cursor .getName (), topic .getName (), reason , exception );
140
+ }
129
141
}
130
142
}, null );
131
143
createdCursors .remove (cursor .getName ());
@@ -148,7 +160,7 @@ public CompletableFuture<Pair<ManagedCursor, Long>> removeCursorFuture(long offs
148
160
149
161
if (log .isDebugEnabled ()) {
150
162
log .debug ("[{}] Get cursor for offset: {} in cache. cache size: {}" ,
151
- requestHandler . ctx . channel () , offset , cursors .size ());
163
+ description , offset , cursors .size ());
152
164
}
153
165
return cursorFuture ;
154
166
}
@@ -182,7 +194,7 @@ public void add(long offset, Pair<ManagedCursor, Long> pair) {
182
194
183
195
if (log .isDebugEnabled ()) {
184
196
log .debug ("[{}] Add cursor back {} for offset: {}" ,
185
- requestHandler . ctx . channel () , pair .getLeft ().getName (), offset );
197
+ description , pair .getLeft ().getName (), offset );
186
198
}
187
199
}
188
200
@@ -194,7 +206,7 @@ public void close() {
194
206
}
195
207
if (log .isDebugEnabled ()) {
196
208
log .debug ("[{}] Close TCM for topic {}." ,
197
- requestHandler . ctx . channel () , topic .getName ());
209
+ description , topic .getName ());
198
210
}
199
211
final List <CompletableFuture <Pair <ManagedCursor , Long >>> cursorFuturesToClose = new ArrayList <>();
200
212
cursors .forEach ((ignored , cursorFuture ) -> cursorFuturesToClose .add (cursorFuture ));
@@ -224,7 +236,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
224
236
if (((ManagedLedgerImpl ) ledger ).getState () == ManagedLedgerImpl .State .Closed ) {
225
237
log .error ("[{}] Async get cursor for offset {} for topic {} failed, "
226
238
+ "because current managedLedger has been closed" ,
227
- requestHandler . ctx . channel () , offset , topic .getName ());
239
+ description , offset , topic .getName ());
228
240
CompletableFuture <Pair <ManagedCursor , Long >> future = new CompletableFuture <>();
229
241
future .completeExceptionally (new Exception ("Current managedLedger for "
230
242
+ topic .getName () + " has been closed." ));
@@ -243,7 +255,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
243
255
final PositionImpl previous = ((ManagedLedgerImpl ) ledger ).getPreviousPosition ((PositionImpl ) position );
244
256
if (log .isDebugEnabled ()) {
245
257
log .debug ("[{}] Create cursor {} for offset: {}. position: {}, previousPosition: {}" ,
246
- requestHandler . ctx . channel () , cursorName , offset , position , previous );
258
+ description , cursorName , offset , position , previous );
247
259
}
248
260
try {
249
261
final ManagedCursor newCursor = ledger .newNonDurableCursor (previous , cursorName );
@@ -252,7 +264,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
252
264
return Pair .of (newCursor , offset );
253
265
} catch (ManagedLedgerException e ) {
254
266
log .error ("[{}] Error new cursor for topic {} at offset {} - {}. will cause fetch data error." ,
255
- requestHandler . ctx . channel () , topic .getName (), offset , previous , e );
267
+ description , topic .getName (), offset , previous , e );
256
268
return null ;
257
269
}
258
270
});
0 commit comments