Kafka 使用 math.abs(key.hashCode) % numPartitions 来计算要发送的分区。
如果 hashCode 是 Integer.MIN_VALUE 怎么办?
由于 math.abs(Integer.MIN_VALUE) 是一个负数,kafka 将发送到负分区。这是如何处理的,我应该关心这个吗?
最佳答案
实际上,当我在 kafka 代码中搜索它时,它并没有使用 math.abs() 将负数转换为正数。
它使用:
public static int toPositive(int number) {
return number & 0x7fffffff;
}
所以它可以解决你担心的问题即使是2147483648也会被转换为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.
关于java - Kafka的分区选择算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873728/