@@ -43,13 +43,13 @@ class Partitioner {
43
43
44
44
async init ( hashFunction : ( ( value : object ) => number ) , topicName : string , producer : Kafka . Producer ) {
45
45
this . hashFunction = hashFunction ;
46
- this . partitionCount = await this . findPartitionCount ( topicName , producer ) ;
46
+ this . partitionCount = await this . # findPartitionCount( topicName , producer ) ;
47
47
if ( this . partitionCount <= 0 ) {
48
48
throw new Error ( 'Partition count should be > 0' ) ;
49
49
}
50
50
}
51
51
52
- async findPartitionCount ( topicName : string , producer : Kafka . Producer ) {
52
+ async # findPartitionCount( topicName : string , producer : Kafka . Producer ) {
53
53
// We depend on the producer already being connected
54
54
logger . info ( `Finding partition count for topic ${ topicName } ` ) ;
55
55
const metadata : Kafka . Metadata = await new Promise ( ( resolve , reject ) => {
@@ -106,7 +106,7 @@ export class Exporter {
106
106
private readonly zookeeperClient : ZookeeperClientAsync ;
107
107
private partitioner : Partitioner | null ;
108
108
109
- constructor ( exporter_name : string , transactional : boolean , topicName : string ) {
109
+ constructor ( exporter_name : string , transactional : boolean , topicName : string , disableStickyPartition : boolean = false ) {
110
110
this . exporter_name = exporter_name ;
111
111
112
112
const producer_settings : ProducerGlobalConfig = {
@@ -133,6 +133,10 @@ export class Exporter {
133
133
producer_settings [ 'enable.idempotence' ] = true ;
134
134
}
135
135
136
+ if ( disableStickyPartition ) {
137
+ producer_settings [ 'sticky.partitioning.linger.ms' ] = 0 ;
138
+ }
139
+
136
140
this . producer = new Kafka . Producer ( producer_settings ) ;
137
141
138
142
this . producer . on ( 'event.log' , function ( log ) {
@@ -296,42 +300,24 @@ export class Exporter {
296
300
}
297
301
}
298
302
299
- async sendData ( events : Array < any > ) {
300
- if ( events . constructor !== Array ) {
301
- events = [ events ] ;
302
- }
303
-
304
- events = events . map (
305
- event => ( typeof event === 'object' ? JSON . stringify ( event ) : event )
306
- ) ;
307
- events . forEach ( event => {
308
- this . producer . produce ( this . topicName , null , Buffer . from ( event ) ) ;
309
- } ) ;
310
-
311
- return new Promise < void > ( ( resolve , reject ) =>
312
- this . producer . flush ( KAFKA_FLUSH_TIMEOUT , ( err : LibrdKafkaError ) => {
313
- if ( err ) return reject ( err ) ;
314
- resolve ( ) ;
315
- } )
316
- ) ;
317
- }
318
-
319
- async sendDataWithKey ( events : object | Array < object > , keyField : string , signalRecordData : object | null ) {
303
+ async sendData ( events : object | Array < object > , keyField : string | null , signalRecordData : object | null ) {
320
304
const arrayEvents : Array < object > = ( events . constructor !== Array ) ? [ events ] : events
321
305
322
306
if ( signalRecordData !== null && this . partitioner === null ) {
323
307
throw new Error ( 'Signal record logic needs partitioner' ) ;
324
308
}
325
309
326
310
arrayEvents . forEach ( ( event : any ) => {
327
- const partitionNumberPayload = this . partitioner ? this . partitioner . getPartitionNumber ( event ) : null ;
311
+ const key = keyField !== null ? event [ keyField ] : null
312
+
313
+ const partitionNumberOfPayload = this . partitioner ? this . partitioner . getPartitionNumber ( event ) : null ;
328
314
const eventString = typeof event === 'object' ? JSON . stringify ( event ) : event ;
329
- this . producer . produce ( this . topicName , partitionNumberPayload , Buffer . from ( eventString ) , event [ keyField ] ) ;
315
+ this . producer . produce ( this . topicName , partitionNumberOfPayload , Buffer . from ( eventString ) , key ) ;
330
316
if ( signalRecordData !== null && this . partitioner !== null ) {
331
317
const signalRecordString = typeof signalRecordData === 'object' ? JSON . stringify ( signalRecordData ) : signalRecordData ;
332
318
for ( let partitionNumber = 0 ; partitionNumber < this . partitioner . getPartitionCount ( ) ; ++ partitionNumber ) {
333
- if ( partitionNumber !== partitionNumberPayload ) {
334
- this . producer . produce ( this . topicName , partitionNumber , Buffer . from ( signalRecordString ) , event [ keyField ] ) ;
319
+ if ( partitionNumber !== partitionNumberOfPayload ) {
320
+ this . producer . produce ( this . topicName , partitionNumber , Buffer . from ( signalRecordString ) , key ) ;
335
321
}
336
322
}
337
323
}
@@ -382,11 +368,12 @@ export class Exporter {
382
368
const signalRecord : object | null = writeSignalRecordsKafka ? { 'santiment_signal_record' : true } : null
383
369
try {
384
370
if ( BLOCKCHAIN === 'utxo' ) {
385
- await this . sendDataWithKey ( events , 'height' , signalRecord ) ;
386
- } else if ( BLOCKCHAIN === 'receipts' ) {
387
- await this . sendDataWithKey ( events , 'transactionHash' , signalRecord ) ;
388
- } else {
389
- await this . sendDataWithKey ( events , 'primaryKey' , signalRecord ) ;
371
+ await this . sendData ( events , 'height' , signalRecord ) ;
372
+ } else if ( BLOCKCHAIN === 'receipts' || BLOCKCHAIN === 'eth' ) {
373
+ await this . sendData ( events , 'transactionHash' , signalRecord ) ;
374
+ }
375
+ else {
376
+ await this . sendData ( events , 'primaryKey' , signalRecord ) ;
390
377
}
391
378
await this . commitTransaction ( ) ;
392
379
} catch ( exception ) {
0 commit comments