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 ());
@@ -149,7 +161,7 @@ public CompletableFuture<Pair<ManagedCursor, Long>> removeCursorFuture(long offs
149
161
150
162
if (log .isDebugEnabled ()) {
151
163
log .debug ("[{}] Get cursor for offset: {} in cache. cache size: {}" ,
152
- requestHandler . ctx . channel () , offset , cursors .size ());
164
+ description , offset , cursors .size ());
153
165
}
154
166
return cursorFuture ;
155
167
}
@@ -183,7 +195,7 @@ public void add(long offset, Pair<ManagedCursor, Long> pair) {
183
195
184
196
if (log .isDebugEnabled ()) {
185
197
log .debug ("[{}] Add cursor back {} for offset: {}" ,
186
- requestHandler . ctx . channel () , pair .getLeft ().getName (), offset );
198
+ description , pair .getLeft ().getName (), offset );
187
199
}
188
200
}
189
201
@@ -195,7 +207,7 @@ public void close() {
195
207
}
196
208
if (log .isDebugEnabled ()) {
197
209
log .debug ("[{}] Close TCM for topic {}." ,
198
- requestHandler . ctx . channel () , topic .getName ());
210
+ description , topic .getName ());
199
211
}
200
212
final List <CompletableFuture <Pair <ManagedCursor , Long >>> cursorFuturesToClose = new ArrayList <>();
201
213
cursors .forEach ((ignored , cursorFuture ) -> cursorFuturesToClose .add (cursorFuture ));
@@ -225,7 +237,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
225
237
if (((ManagedLedgerImpl ) ledger ).getState () == ManagedLedgerImpl .State .Closed ) {
226
238
log .error ("[{}] Async get cursor for offset {} for topic {} failed, "
227
239
+ "because current managedLedger has been closed" ,
228
- requestHandler . ctx . channel () , offset , topic .getName ());
240
+ description , offset , topic .getName ());
229
241
CompletableFuture <Pair <ManagedCursor , Long >> future = new CompletableFuture <>();
230
242
future .completeExceptionally (new Exception ("Current managedLedger for "
231
243
+ topic .getName () + " has been closed." ));
@@ -244,7 +256,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
244
256
final PositionImpl previous = ((ManagedLedgerImpl ) ledger ).getPreviousPosition ((PositionImpl ) position );
245
257
if (log .isDebugEnabled ()) {
246
258
log .debug ("[{}] Create cursor {} for offset: {}. position: {}, previousPosition: {}" ,
247
- requestHandler . ctx . channel () , cursorName , offset , position , previous );
259
+ description , cursorName , offset , position , previous );
248
260
}
249
261
try {
250
262
final ManagedCursor newCursor = ledger .newNonDurableCursor (previous , cursorName );
@@ -253,7 +265,7 @@ private CompletableFuture<Pair<ManagedCursor, Long>> asyncGetCursorByOffset(long
253
265
return Pair .of (newCursor , offset );
254
266
} catch (ManagedLedgerException e ) {
255
267
log .error ("[{}] Error new cursor for topic {} at offset {} - {}. will cause fetch data error." ,
256
- requestHandler . ctx . channel () , topic .getName (), offset , previous , e );
268
+ description , topic .getName (), offset , previous , e );
257
269
return null ;
258
270
}
259
271
});
0 commit comments