@@ -213,18 +213,17 @@ class Consumer {
213
213
*/
214
214
#running = false ;
215
215
216
- /**
217
- * Whether the consumer is in KafkaJS compatibility mode.
218
- * @type {boolean }
219
- */
220
- #kafkaJSCompatibilityMode = false ;
221
-
222
216
/**
223
217
* The message cache for KafkaJS compatibility mode.
224
218
* @type {MessageCache|null }
225
219
*/
226
220
#messageCache = null ;
227
221
222
+ /**
223
+ * Whether the user has enabled manual offset management (stores).
224
+ */
225
+ #userManagedStores = false ;
226
+
228
227
/**
229
228
* @constructor
230
229
* @param {import("../../types/kafkajs").ConsumerConfig } kJSConfig
@@ -343,12 +342,13 @@ class Consumer {
343
342
try {
344
343
if ( err . code === LibrdKafkaError . codes . ERR__ASSIGN_PARTITIONS ) {
345
344
346
- if ( this . #checkPendingSeeks && this . #kafkaJSCompatibilityMode)
345
+ const checkPendingSeeks = this . #pendingSeeks. size !== 0 ;
346
+ if ( checkPendingSeeks )
347
347
assignment = this . #assignAsPerSeekedOffsets( assignment ) ;
348
348
349
349
this . #internalClient. assign ( assignment ) ;
350
350
351
- if ( this . # checkPendingSeeks) {
351
+ if ( checkPendingSeeks ) {
352
352
const offsetsToCommit = assignment
353
353
. filter ( ( topicPartition ) => topicPartition . offset !== undefined )
354
354
. map ( ( topicPartition ) => ( {
@@ -491,11 +491,6 @@ class Consumer {
491
491
throw new error . KafkaJSError ( CompatibilityErrorMessages . runOptionsAutoCommitThreshold ( ) , { code : error . ErrorCodes . ERR__NOT_IMPLEMENTED } ) ;
492
492
}
493
493
494
- /* Offset storage management is manual in kafkaJS compability mode if auto-commit is turned on (ie by default). */
495
- if ( rdKafkaConfig [ 'enable.auto.commit' ] ) {
496
- rdKafkaConfig [ 'enable.auto.offset.store' ] = false ;
497
- }
498
-
499
494
/* Set the logger */
500
495
if ( Object . hasOwn ( kjsConfig , 'logger' ) ) {
501
496
this . #logger = kjsConfig . logger ;
@@ -517,8 +512,6 @@ class Consumer {
517
512
* log level, as librdkafka will control the granularity. */
518
513
if ( ! compatibleConfig || Object . keys ( compatibleConfig ) . length === 0 ) {
519
514
this . #logger. setLogLevel ( logLevel . DEBUG ) ;
520
- } else {
521
- this . #kafkaJSCompatibilityMode = true ;
522
515
}
523
516
524
517
/* Even if we are in compability mode, setting a 'debug' in the main config must override the logger's level. */
@@ -542,6 +535,21 @@ class Consumer {
542
535
}
543
536
rdKafkaConfig [ 'rebalance_cb' ] = this . #rebalanceCallback. bind ( this ) ;
544
537
538
+ /* Offset management is different from case to case.
539
+ * Case 1: User has changed value of enable.auto.offset.store. In this case, we respect that.
540
+ * Case 2: automatic committing is on. In this case, we turn off auto.offset.store and store offsets manually.
541
+ * this is necessary for cache invalidation and management, as we want to put things into the store
542
+ * after eachMessage is called, and not on consume itself.
543
+ * Case 3: automatic committing is off. In this case, we turn off auto.offset.store too. Since the user might
544
+ * call an empty commit() and expect things to work properly (ie. the right offsets be stored).
545
+ * All this works out a singular, simple condition.
546
+ */
547
+ if ( ! Object . hasOwn ( this . #userConfig, 'enable.auto.offset.store' ) ) {
548
+ rdKafkaConfig [ 'enable.auto.offset.store' ] = false ;
549
+ } else {
550
+ this . #userManagedStores = ! rdKafkaConfig [ 'enable.auto.offset.store' ] ;
551
+ }
552
+
545
553
return rdKafkaConfig ;
546
554
}
547
555
@@ -699,10 +707,7 @@ class Consumer {
699
707
}
700
708
701
709
const rdKafkaConfig = this . #config( ) ;
702
-
703
- if ( this . #kafkaJSCompatibilityMode) {
704
- this . #messageCache = new MessageCache ( Math . floor ( rdKafkaConfig [ 'max.poll.interval.ms' ] * 0.8 ) ) ;
705
- }
710
+ this . #messageCache = new MessageCache ( Math . floor ( rdKafkaConfig [ 'max.poll.interval.ms' ] * 0.8 ) ) ;
706
711
707
712
this . #state = ConsumerState . CONNECTING ;
708
713
this . #internalClient = new RdKafka . KafkaConsumer ( rdKafkaConfig ) ;
@@ -804,10 +809,6 @@ class Consumer {
804
809
throw new error . KafkaJSError ( CompatibilityErrorMessages . runOptionsPartitionsConsumedConcurrently ( ) , { code : error . ErrorCodes . ERR__NOT_IMPLEMENTED } ) ;
805
810
}
806
811
807
- if ( ! this . #kafkaJSCompatibilityMode) {
808
- throw new error . KafkaJSError ( 'run() can only be used in KafkaJS compatibility mode.' , { code : error . ErrorCodes . ERR__NOT_IMPLEMENTED } ) ;
809
- }
810
-
811
812
if ( this . #running) {
812
813
throw new error . KafkaJSError ( 'Consumer is already running.' , { code : error . ErrorCodes . ERR__STATE } ) ;
813
814
}
@@ -829,7 +830,7 @@ class Consumer {
829
830
/* Invalidate the message cache if needed. */
830
831
if ( this . #messageCache. isStale ( ) ) {
831
832
await this . #clearCacheAndResetPositions( true ) ;
832
- this . #lock. release ( ) ;
833
+ await this . #lock. release ( ) ;
833
834
continue ;
834
835
}
835
836
@@ -841,7 +842,7 @@ class Consumer {
841
842
} ) ;
842
843
843
844
if ( ! m ) {
844
- this . #lock. release ( ) ;
845
+ await this . #lock. release ( ) ;
845
846
continue ;
846
847
}
847
848
@@ -876,7 +877,7 @@ class Consumer {
876
877
* This is especially true since the pattern of pause() followed by throwing an error
877
878
* is encouraged. To meet the API contract, we seek one offset backward at this point (which
878
879
* means seeking to the message offset). */
879
- this . seek ( {
880
+ await this . seek ( {
880
881
topic : m . topic ,
881
882
partition : m . partition ,
882
883
offset : m . offset ,
@@ -886,7 +887,7 @@ class Consumer {
886
887
/* Store the offsets we need to store, or at least record them for cache invalidation reasons. */
887
888
if ( eachMessageProcessed ) {
888
889
try {
889
- if ( this . #internalConfig [ 'enable.auto.commit' ] ) {
890
+ if ( ! this . #userManagedStores ) {
890
891
this . #internalClient. offsetsStore ( [ { topic : m . topic , partition : m . partition , offset : Number ( m . offset ) + 1 } ] ) ;
891
892
}
892
893
this . #lastConsumedOffsets. set ( `${ m . topic } |${ m . partition } ` , Number ( m . offset ) + 1 ) ;
@@ -916,14 +917,12 @@ class Consumer {
916
917
917
918
/**
918
919
* Consumes a single message from the consumer within the given timeout.
920
+ * THIS METHOD IS NOT IMPLEMENTED.
919
921
* @note This method cannot be used with run(). Either that, or this must be used.
920
922
*
921
923
* @param {any } args
922
924
* @param {number } args.timeout - the timeout in milliseconds, defaults to 1000.
923
925
* @returns {import("../..").Message|null } a message, or null if the timeout was reached.
924
- *
925
- * @note This API is currently in an experimental stage and subject to change.
926
- * This should not be used in KafkaJS compatibility mode (ie with kafkaJS blocks in the config).
927
926
*/
928
927
async consume ( { timeout } = { timeout : 1000 } ) {
929
928
if ( this . #state !== ConsumerState . CONNECTED ) {
@@ -934,10 +933,6 @@ class Consumer {
934
933
throw new error . KafkaJSError ( 'consume() and run() cannot be used together.' , { code : error . ErrorCodes . ERR__CONFLICT } ) ;
935
934
}
936
935
937
- if ( this . #kafkaJSCompatibilityMode) {
938
- throw new error . KafkaJSError ( 'consume() cannot be used in KafkaJS compatibility mode.' , { code : error . ErrorCodes . ERR__NOT_IMPLEMENTED } ) ;
939
- }
940
-
941
936
this . #internalClient. setDefaultConsumeTimeout ( timeout ) ;
942
937
let m = null ;
943
938
@@ -948,7 +943,8 @@ class Consumer {
948
943
this . #internalClient. setDefaultConsumeTimeout ( undefined ) ;
949
944
}
950
945
951
- return m ?? null ;
946
+ throw new error . KafkaJSError ( 'consume() is not implemented.' + m , { code : error . ErrorCodes . ERR__NOT_IMPLEMENTED } ) ;
947
+ // return m ?? null;
952
948
}
953
949
954
950
async #commitOffsetsUntilNoStateErr( offsetsToCommit ) {
@@ -1043,8 +1039,7 @@ class Consumer {
1043
1039
/* We need a complete reset of the cache if we're seeking to a different offset even for one partition.
1044
1040
* At a later point, this may be improved at the cost of added complexity of maintaining message generation,
1045
1041
* or else purging the cache of just those partitions which are seeked. */
1046
- if ( this . #kafkaJSCompatibilityMode)
1047
- await this . #clearCacheAndResetPositions( true ) ;
1042
+ await this . #clearCacheAndResetPositions( true ) ;
1048
1043
1049
1044
/* It's assumed that topicPartition is already assigned, and thus can be seeked to and committed to.
1050
1045
* Errors are logged to detect bugs in the internal code. */
@@ -1064,7 +1059,7 @@ class Consumer {
1064
1059
}
1065
1060
1066
1061
/* Offsets are committed on seek only when in compatibility mode. */
1067
- if ( offsetsToCommit . length !== 0 && this . #internalConfig[ 'enable.auto.commit' ] && this . #kafkaJSCompatibilityMode ) {
1062
+ if ( offsetsToCommit . length !== 0 && this . #internalConfig[ 'enable.auto.commit' ] ) {
1068
1063
await this . #commitOffsetsUntilNoStateErr( offsetsToCommit ) ;
1069
1064
}
1070
1065
@@ -1078,7 +1073,7 @@ class Consumer {
1078
1073
* If at any time, the consumer is assigned the partition, the seek will be performed.
1079
1074
* Depending on the value of the librdkafka property 'enable.auto.commit', the consumer will commit the offset seeked to.
1080
1075
* @param {import("../../types/kafkajs").TopicPartitionOffset } topicPartitionOffset
1081
- * @returns {Promise<void>|null } a promise that resolves when the seek has been performed (only when not in compatibility mode), or null (when in compatibility mode)
1076
+ * @returns {Promise<void>|null } a promise that resolves when the seek has been performed.
1082
1077
*/
1083
1078
seek ( topicPartitionOffset ) {
1084
1079
if ( this . #state !== ConsumerState . CONNECTED ) {
@@ -1098,12 +1093,6 @@ class Consumer {
1098
1093
1099
1094
this . #checkPendingSeeks = true ;
1100
1095
this . #pendingSeeks. set ( `${ rdKafkaTopicPartitionOffset . topic } |${ rdKafkaTopicPartitionOffset . partition } ` , rdKafkaTopicPartitionOffset . offset ) ;
1101
-
1102
- /* Immediately realize the seek if we're not in compatibility mode. And clear pending seeks.
1103
- * We don't need them for rebalance. */
1104
- if ( ! this . #kafkaJSCompatibilityMode) {
1105
- return this . #seekInternal( ) . then ( ( ) => this . #pendingSeeks. clear ( ) ) ;
1106
- }
1107
1096
}
1108
1097
1109
1098
async describeGroup ( ) {
@@ -1160,8 +1149,7 @@ class Consumer {
1160
1149
return ;
1161
1150
}
1162
1151
this . #internalClient. pause ( topics ) ;
1163
- if ( this . #kafkaJSCompatibilityMode)
1164
- this . #messageCache. stale = true ;
1152
+ this . #messageCache. stale = true ;
1165
1153
1166
1154
topics . map ( JSON . stringify ) . forEach ( topicPartition => this . #pausedPartitions. add ( topicPartition ) ) ;
1167
1155
0 commit comments