-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultPartitioner.js
56 lines (46 loc) · 1.61 KB
/
defaultPartitioner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const murmurhash = require('murmurhash');
/**
* Seed value. Same as the java side implementation
* @type {number}
*/
const seed = 0x9747b28c;
let topicPreviousPartition = {};
/**
* Core partitioning logic. Uses murmurhash
* @param topicName
* @param messageKey
* @param totalNumberOfPartitions
* @returns {*} partitionNumber
*/
function partition(topicName, messageKey, totalNumberOfPartitions) {
if(topicName == null)
return null;
// Total number of partitions SHOULD be same as what it has been configured, else you'll get "Unknown partition" error.
totalNumberOfPartitions = isNaN(totalNumberOfPartitions) ? Number(totalNumberOfPartitions) : totalNumberOfPartitions;
// If total number of partition in null or equals 1, then set the partition number as 0.
if(totalNumberOfPartitions <= 1)
return 0;
// If partitionKey is empty/null, then do a round robin on the topic considering it's total number of partitions.
if(messageKey == null) {
if(!topicPreviousPartition[topicName])
topicPreviousPartition[topicName] = 0;
else {
if(topicPreviousPartition[topicName] >= totalNumberOfPartitions)
topicPreviousPartition[topicName] = 0;
}
return topicPreviousPartition[topicName]++;
}
// All good, calculate the partition number.
return toPositive(murmurhash.v2(messageKey, seed)) % totalNumberOfPartitions;
}
/**
* Convert the given number to *a* positive value.
* @param number
* @returns {number}
*/
function toPositive(number) {
return number & 0x7fffffff;
}
module.exports = {
partition
};