23
23
import com .arpnetworking .metrics .common .parsers .Parser ;
24
24
import com .arpnetworking .metrics .common .parsers .exceptions .ParsingException ;
25
25
import com .arpnetworking .metrics .incubator .PeriodicMetrics ;
26
+ import com .arpnetworking .metrics .mad .model .Record ;
26
27
import com .arpnetworking .steno .LogValueMapFactory ;
27
28
import com .arpnetworking .steno .Logger ;
28
29
import com .arpnetworking .steno .LoggerFactory ;
37
38
import org .apache .kafka .common .KafkaException ;
38
39
39
40
import java .time .Duration ;
41
+ import java .util .List ;
40
42
import java .util .Optional ;
41
43
import java .util .concurrent .ArrayBlockingQueue ;
42
44
import java .util .concurrent .BlockingQueue ;
49
51
* Produce instances of {@link com.arpnetworking.metrics.mad.model.Record} from the values of entries
50
52
* from a Kafka topic. The key from the entries gets discarded
51
53
*
52
- * @param <T> the type of data created by the source
53
54
* @param <V> the type of data of value in kafka {@code ConsumerRecords}
54
55
*
55
56
* @author Joey Jackson (jjackson at dropbox dot com)
56
57
*/
57
- public final class KafkaSource <T , V > extends BaseSource {
58
+ public final class KafkaSource <V > extends BaseSource {
58
59
private static final Logger LOGGER = LoggerFactory .getLogger (KafkaSource .class );
59
60
60
61
private final Consumer <?, V > _consumer ;
61
62
private final RunnableConsumer _runnableConsumer ;
62
63
private final ExecutorService _consumerExecutor ;
63
64
private final ExecutorService _parserExecutor ;
64
- private final Parser <T , V > _parser ;
65
+ private final Parser <List < Record > , V > _parser ;
65
66
private final Logger _logger ;
66
67
private final Duration _shutdownAwaitTime ;
67
68
private final Duration _backoffTime ;
@@ -135,21 +136,21 @@ public String toString() {
135
136
}
136
137
137
138
@ SuppressWarnings ("unused" )
138
- private KafkaSource (final Builder <T , V > builder ) {
139
+ private KafkaSource (final Builder <V > builder ) {
139
140
this (builder , LOGGER , new ArrayBlockingQueue <>(builder ._bufferSize ));
140
141
}
141
142
142
143
// NOTE: Package private for testing
143
- /* package private */ KafkaSource (final Builder <T , V > builder , final Logger logger ) {
144
+ /* package private */ KafkaSource (final Builder <V > builder , final Logger logger ) {
144
145
this (builder , logger , new ArrayBlockingQueue <>(builder ._bufferSize ));
145
146
}
146
147
147
148
// NOTE: Package private for testing
148
- /* package private */ KafkaSource (final Builder <T , V > builder , final BlockingQueue <V > buffer ) {
149
+ /* package private */ KafkaSource (final Builder <V > builder , final BlockingQueue <V > buffer ) {
149
150
this (builder , LOGGER , buffer );
150
151
}
151
152
152
- private KafkaSource (final Builder <T , V > builder , final Logger logger , final BlockingQueue <V > buffer ) {
153
+ private KafkaSource (final Builder <V > builder , final Logger logger , final BlockingQueue <V > buffer ) {
153
154
super (builder );
154
155
_consumer = builder ._consumer ;
155
156
_parser = builder ._parser ;
@@ -183,10 +184,10 @@ public void run() {
183
184
final V value = _buffer .poll ();
184
185
_periodicMetrics .recordGauge (_queueSizeGaugeMetricName , _buffer .size ());
185
186
if (value != null ) {
186
- final T record ;
187
+ final List < Record > records ;
187
188
try {
188
189
final Stopwatch parsingTimer = Stopwatch .createStarted ();
189
- record = _parser .parse (value );
190
+ records = _parser .parse (value );
190
191
parsingTimer .stop ();
191
192
_periodicMetrics .recordTimer (_parsingTimeMetricName ,
192
193
parsingTimer .elapsed (TimeUnit .NANOSECONDS ), Optional .of (Units .NANOSECOND ));
@@ -198,8 +199,10 @@ record = _parser.parse(value);
198
199
.log ();
199
200
continue ;
200
201
}
201
- KafkaSource .this .notify (record );
202
- _currentRecordsProcessedCount .getAndIncrement ();
202
+ for (final Record record : records ) {
203
+ KafkaSource .this .notify (record );
204
+ _currentRecordsProcessedCount .getAndIncrement ();
205
+ }
203
206
} else {
204
207
// Queue is empty
205
208
try {
@@ -289,12 +292,11 @@ private void backoff(final Throwable throwable) {
289
292
/**
290
293
* Builder pattern class for {@link KafkaSource}.
291
294
*
292
- * @param <T> the type of data created by the source
293
295
* @param <V> the type of data of value in kafka {@code ConsumerRecords}
294
296
*
295
297
* @author Joey Jackson (jjackson at dropbox dot com)
296
298
*/
297
- public static final class Builder <T , V > extends BaseSource .Builder <Builder <T , V >, KafkaSource <T , V >> {
299
+ public static final class Builder <V > extends BaseSource .Builder <Builder <V >, KafkaSource <V >> {
298
300
299
301
/**
300
302
* Public constructor.
@@ -309,7 +311,7 @@ public Builder() {
309
311
* @param consumer The {@code Consumer}.
310
312
* @return This instance of {@link KafkaSource.Builder}.
311
313
*/
312
- public Builder <T , V > setConsumer (final Consumer <?, V > consumer ) {
314
+ public Builder <V > setConsumer (final Consumer <?, V > consumer ) {
313
315
_consumer = consumer ;
314
316
return this ;
315
317
}
@@ -320,7 +322,7 @@ public Builder<T, V> setConsumer(final Consumer<?, V> consumer) {
320
322
* @param value The {@link Parser}.
321
323
* @return This instance of {@link KafkaSource.Builder}.
322
324
*/
323
- public Builder <T , V > setParser (final Parser <T , V > value ) {
325
+ public Builder <V > setParser (final Parser <List < Record > , V > value ) {
324
326
_parser = value ;
325
327
return this ;
326
328
}
@@ -331,7 +333,7 @@ public Builder<T, V> setParser(final Parser<T, V> value) {
331
333
* @param pollTime The {@code Duration} of each poll call made by the {@code KafkaConsumer}.
332
334
* @return This instance of {@link KafkaSource.Builder}.
333
335
*/
334
- public Builder <T , V > setPollTime (final Duration pollTime ) {
336
+ public Builder <V > setPollTime (final Duration pollTime ) {
335
337
_pollTime = pollTime ;
336
338
return this ;
337
339
}
@@ -344,7 +346,7 @@ public Builder<T, V> setPollTime(final Duration pollTime) {
344
346
* the {@link RunnableConsumer} thread.
345
347
* @return This instance of {@link KafkaSource.Builder}.
346
348
*/
347
- public Builder <T , V > setShutdownAwaitTime (final Duration shutdownAwaitTime ) {
349
+ public Builder <V > setShutdownAwaitTime (final Duration shutdownAwaitTime ) {
348
350
_shutdownAwaitTime = shutdownAwaitTime ;
349
351
return this ;
350
352
}
@@ -357,7 +359,7 @@ public Builder<T, V> setShutdownAwaitTime(final Duration shutdownAwaitTime) {
357
359
* an operation on exception.
358
360
* @return This instance of {@link KafkaSource.Builder}.
359
361
*/
360
- public Builder <T , V > setBackoffTime (final Duration backoffTime ) {
362
+ public Builder <V > setBackoffTime (final Duration backoffTime ) {
361
363
_backoffTime = backoffTime ;
362
364
return this ;
363
365
}
@@ -369,7 +371,7 @@ public Builder<T, V> setBackoffTime(final Duration backoffTime) {
369
371
* @param numWorkerThreads The number of parsing worker threads.
370
372
* @return This instance of {@link KafkaSource.Builder}.
371
373
*/
372
- public Builder <T , V > setNumWorkerThreads (final Integer numWorkerThreads ) {
374
+ public Builder <V > setNumWorkerThreads (final Integer numWorkerThreads ) {
373
375
_numWorkerThreads = numWorkerThreads ;
374
376
return this ;
375
377
}
@@ -381,7 +383,7 @@ public Builder<T, V> setNumWorkerThreads(final Integer numWorkerThreads) {
381
383
* @param bufferSize The size of the buffer.
382
384
* @return This instance of {@link KafkaSource.Builder}.
383
385
*/
384
- public Builder <T , V > setBufferSize (final Integer bufferSize ) {
386
+ public Builder <V > setBufferSize (final Integer bufferSize ) {
385
387
_bufferSize = bufferSize ;
386
388
return this ;
387
389
}
@@ -392,20 +394,20 @@ public Builder<T, V> setBufferSize(final Integer bufferSize) {
392
394
* @param periodicMetrics The {@code PeriodicMetrics} for the {@link KafkaSource}.
393
395
* @return This instance of {@link KafkaSource.Builder}.
394
396
*/
395
- public Builder <T , V > setPeriodicMetrics (final PeriodicMetrics periodicMetrics ) {
397
+ public Builder <V > setPeriodicMetrics (final PeriodicMetrics periodicMetrics ) {
396
398
_periodicMetrics = periodicMetrics ;
397
399
return this ;
398
400
}
399
401
400
402
@ Override
401
- protected Builder <T , V > self () {
403
+ protected Builder <V > self () {
402
404
return this ;
403
405
}
404
406
405
407
@ NotNull
406
408
private Consumer <?, V > _consumer ;
407
409
@ NotNull
408
- private Parser <T , V > _parser ;
410
+ private Parser <List < Record > , V > _parser ;
409
411
@ NotNull
410
412
@ CheckWith (value = PositiveDuration .class , message = "Poll duration must be positive." )
411
413
private Duration _pollTime ;
0 commit comments