question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KafkaJS and java producer partitioning returning different results

See original GitHub issue

Producing message with the same key gets assigned to different partitions depending if app is using java or nodejs client.

This creates problems when using kafka streams for joining those data - meeting the co-partitioning requirement fails even when message keys are the same.

I created an isolated test code which confirmed it. Simple java and nodejs app with partitioning using murmur2 code extracted from the latest kafkajs and java producer. The code looks the same, but results differ. Even checked that input bytes are equal. ¯_(ツ)_/¯

Test runner result matches the partition number I’m seeing in Kafka broker…

Any idea why and can it be fixed?

Our current workaround is to repartition all kafkajs based messages with java producer…

let key = '100:48069';
let numPartitions = 12;

node bytes {"type":"Buffer","data":[49,48,48,58,52,56,48,54,57]}
node murmur 1713501762
node hash 1713501762
node partition 6

java bytes 49 48 48 58 52 56 48 54 57 
java murmur 1009543857
java hash 1009543857
java partition 9

partitioning test code.zip

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pimpelsangcommented, May 11, 2019

Tried it out. Works! Thank you very much for so fast fix

1reaction
Nevoncommented, May 8, 2019

I couldn’t stop myself, because this is fascinating. I found where the issue is, though I don’t understand it yet.

https://github.com/tulios/kafkajs/blob/c5ab8c229369ae4966ffd3ce22e2b0c00191943b/src/producer/partitioners/default/murmur2.js#L28

I ran some interactive debugging of both the Java and JS implementations, executing them side-by side and comparing the values. On this line, the values diverge between the JS and Java implementations. Given the key you provided, on the first iteration of the loop, k is 976236593 and m is 1540483477 in both JS and Java, but after doing k *= M, the value of k is 2095687045 in Java and 1503876341159274000 in JS.

Essentially:

int k = 976236593
int m = 1540483477

k * m == 2095687045
const k = 976236593
const m = 1540483477

k * m === 1503876341159274000

I’m assuming this is because we’re using the native Number type in Javascript, which has different limits from Java’s int.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Producing Messages - KafkaJS
To publish messages to Kafka you have to create a producer. Simply call the producer function of the client to create it: const...
Read more >
Solving My Weird Kafka Rebalancing Problems & Explaining ...
While Kafka is rebalancing, all involved consumers' processing is blocked (Incremental rebalancing aims to revoke only partitions that need to ...
Read more >
How could I properly pause/resume considering the ...
I have a topic with 10 partitions. Producer. One producer publishes messages to the Kafka cluster, distributing them among partitions according ...
Read more >
Top 5 Things Every Apache Kafka Developer Should Know
In other words, these in-sync replicas have the same content for a given topic ... In this case, the producer always uses this...
Read more >
Documentation - Apache Kafka
We provide a java client for Kafka, but clients are available in many ... The producer is able to choose which message to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found