Reputation: 1264
Kafka uses math.abs(key.hashCode) % numPartitions to calculate the partition to send.
What if hashCode is Integer.MIN_VALUE?
As math.abs(Integer.MIN_VALUE) is a negative number, kafka would send to a negative partition. How is that handled, should I care about this?
Upvotes: 2
Views: 599
Reputation: 4677
Actually, when I search this in kafka code, It does not use the math.abs() to convert the negative to positive.
It use that:
public static int toPositive(int number) {
return number & 0x7fffffff;
}
So It can solve the problem you worried even if the number is 2147483648, it will be converted to 0
A cheap way to deterministically convert a number to a positive value. When the input is positive, the original value is returned. When the input number is negative, the returned positive value is the original value bit AND against 0x7fffffff which is not its absolutely value.
Upvotes: 1