diff --git a/common/src/main/scala/org/apache/celeborn/common/meta/WorkerInfo.scala b/common/src/main/scala/org/apache/celeborn/common/meta/WorkerInfo.scala index 42d7e9bfb00..4a67fbdeca7 100644 --- a/common/src/main/scala/org/apache/celeborn/common/meta/WorkerInfo.scala +++ b/common/src/main/scala/org/apache/celeborn/common/meta/WorkerInfo.scala @@ -49,6 +49,7 @@ class WorkerInfo( var endpoint: RpcEndpointRef = null // Cache the hash code for WorkerInfo private var hash = 0 + private var isZeroHash = false def this(host: String, rpcPort: Int, pushPort: Int, fetchPort: Int, replicatePort: Int) { this( @@ -235,14 +236,18 @@ class WorkerInfo( override def hashCode(): Int = { var h = hash - if (h == 0) { - val state = Seq(host, rpcPort, pushPort, fetchPort, replicatePort) + if (h == 0 || isZeroHash) { + val state = Array(host, rpcPort, pushPort, fetchPort, replicatePort) var i = 0 - while (i < state.size) { + while (i < state.length) { h = 31 * h + state(i).hashCode() i = i + 1 } - hash = h + if (h == 0) { + isZeroHash = true + } else { + hash = h + } } h } diff --git a/common/src/test/scala/org/apache/celeborn/common/ComputeIfAbsentBenchmark.scala b/common/src/test/scala/org/apache/celeborn/common/ComputeIfAbsentBenchmark.scala index eeee4d78a11..eb347e64f8e 100644 --- a/common/src/test/scala/org/apache/celeborn/common/ComputeIfAbsentBenchmark.scala +++ b/common/src/test/scala/org/apache/celeborn/common/ComputeIfAbsentBenchmark.scala @@ -30,9 +30,9 @@ import org.apache.celeborn.benchmark.{Benchmark, BenchmarkBase} * ComputeIfAbsent benchmark. * To run this benchmark: * {{{ - * 1. build/sbt "common/test:runMain " + * 1. build/sbt "celeborn-common/test:runMain " * 2. generate result: - * CELEBORN_GENERATE_BENCHMARK_FILES=1 build/sbt "common/test:runMain " + * CELEBORN_GENERATE_BENCHMARK_FILES=1 build/sbt "celeborn-common/test:runMain " * Results will be written to "benchmarks/ComputeIfAbsentBenchmark-results.txt". * }}} */ diff --git a/common/src/test/scala/org/apache/celeborn/common/meta/WorkerInfoSuite.scala b/common/src/test/scala/org/apache/celeborn/common/meta/WorkerInfoSuite.scala index 23b76646ae2..71dcdfb0622 100644 --- a/common/src/test/scala/org/apache/celeborn/common/meta/WorkerInfoSuite.scala +++ b/common/src/test/scala/org/apache/celeborn/common/meta/WorkerInfoSuite.scala @@ -314,10 +314,10 @@ class WorkerInfoSuite extends CelebornFunSuite { test("Test WorkerInfo hashcode") { val host = generateRandomIPv4Address - val rpcPort = Random.nextInt(65535) - val pushPort = Random.nextInt(65535) - val fetchPort = Random.nextInt(65535) - val replicatePort = Random.nextInt(65535) + val rpcPort = Random.nextInt(65536) + val pushPort = Random.nextInt(65536) + val fetchPort = Random.nextInt(65536) + val replicatePort = Random.nextInt(65536) val workerInfo = new WorkerInfo(host, rpcPort, pushPort, fetchPort, replicatePort) // origin hashCode() logic