You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ch...@apache.org on 2024/10/17 06:16:50 UTC
(kafka) branch trunk updated: KAFKA-17677 Remove atomicGetOrUpdate (#17484)
This is an automated email from the ASF dual-hosted git repository.
chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new 863b728c403 KAFKA-17677 Remove atomicGetOrUpdate (#17484)
863b728c403 is described below
commit 863b728c403238738ca8f25d42b2a2577ebe0b6a
Author: TengYao Chi <ki...@gmail.com>
AuthorDate: Thu Oct 17 14:16:44 2024 +0800
KAFKA-17677 Remove atomicGetOrUpdate (#17484)
Since we are removing support for Scala 2.12, CoreUtils#atomicGetOrUpdate should also be removed, as it is a workaround for the different behavior between ConcurrentMap.getOrElse and JConcurrentMapWrapper.
JMH results show no significant performance regression when using `getOrElseUpdate`:
Benchmark (mapSize) (writePercentage) Mode Cnt Score Error Units
ConcurrentMapBenchmark.testConcurrentHashMapComputeIfAbsentReadOnly 100 0.1 avgt 5 4.182 ± 0.006 ns/op
ConcurrentMapBenchmark.testConcurrentHashMapGetReadOnly 100 0.1 avgt 5 3.372 ± 0.024 ns/op
JMH benchmarks done
Reviewers: Mickael Maison <mi...@gmail.com>, Chia-Ping Tsai <ch...@gmail.com>
---
.../TransactionMarkerChannelManager.scala | 6 ++---
core/src/main/scala/kafka/utils/CoreUtils.scala | 18 --------------
.../scala/unit/kafka/utils/CoreUtilsTest.scala | 29 ----------------------
3 files changed, 3 insertions(+), 50 deletions(-)
diff --git a/core/src/main/scala/kafka/coordinator/transaction/TransactionMarkerChannelManager.scala b/core/src/main/scala/kafka/coordinator/transaction/TransactionMarkerChannelManager.scala
index afd645877d1..9eaf19e6e09 100644
--- a/core/src/main/scala/kafka/coordinator/transaction/TransactionMarkerChannelManager.scala
+++ b/core/src/main/scala/kafka/coordinator/transaction/TransactionMarkerChannelManager.scala
@@ -22,7 +22,7 @@ import kafka.coordinator.transaction.TransactionMarkerChannelManager.{LogAppendR
import java.util
import java.util.concurrent.{BlockingQueue, ConcurrentHashMap, LinkedBlockingQueue}
import kafka.server.{KafkaConfig, MetadataCache}
-import kafka.utils.{CoreUtils, Logging}
+import kafka.utils.Logging
import org.apache.kafka.clients._
import org.apache.kafka.common.metrics.Metrics
import org.apache.kafka.common.network._
@@ -120,7 +120,7 @@ class TxnMarkerQueue(@volatile var destination: Node) extends Logging {
}
def addMarkers(txnTopicPartition: Int, pendingCompleteTxnAndMarker: PendingCompleteTxnAndMarkerEntry): Unit = {
- val queue = CoreUtils.atomicGetOrUpdate(markersPerTxnTopicPartition, txnTopicPartition, {
+ val queue = markersPerTxnTopicPartition.getOrElseUpdate(txnTopicPartition, {
// Note that this may get called more than once if threads have a close race while adding new queue.
info(s"Creating new marker queue for txn partition $txnTopicPartition to destination broker ${destination.id}")
new LinkedBlockingQueue[PendingCompleteTxnAndMarkerEntry]()
@@ -208,7 +208,7 @@ class TransactionMarkerChannelManager(
// we do not synchronize on the update of the broker node with the enqueuing,
// since even if there is a race condition we will just retry
- val brokerRequestQueue = CoreUtils.atomicGetOrUpdate(markersQueuePerBroker, brokerId, {
+ val brokerRequestQueue = markersQueuePerBroker.getOrElseUpdate(brokerId, {
// Note that this may get called more than once if threads have a close race while adding new queue.
info(s"Creating new marker queue map to destination broker $brokerId")
new TxnMarkerQueue(broker)
diff --git a/core/src/main/scala/kafka/utils/CoreUtils.scala b/core/src/main/scala/kafka/utils/CoreUtils.scala
index 29c89c29898..1355643d91d 100755
--- a/core/src/main/scala/kafka/utils/CoreUtils.scala
+++ b/core/src/main/scala/kafka/utils/CoreUtils.scala
@@ -241,24 +241,6 @@ object CoreUtils {
properties
}
- /**
- * Atomic `getOrElseUpdate` for concurrent maps. This is optimized for the case where
- * keys often exist in the map, avoiding the need to create a new value. `createValue`
- * may be invoked more than once if multiple threads attempt to insert a key at the same
- * time, but the same inserted value will be returned to all threads.
- *
- * In Scala 2.12, `ConcurrentMap.getOrElse` has the same behaviour as this method, but JConcurrentMapWrapper that
- * wraps Java maps does not.
- */
- def atomicGetOrUpdate[K, V](map: concurrent.Map[K, V], key: K, createValue: => V): V = {
- map.get(key) match {
- case Some(value) => value
- case None =>
- val value = createValue
- map.putIfAbsent(key, value).getOrElse(value)
- }
- }
-
def replicaToBrokerAssignmentAsScala(map: util.Map[Integer, util.List[Integer]]): Map[Int, Seq[Int]] = {
map.asScala.map(e => (e._1.asInstanceOf[Int], e._2.asScala.map(_.asInstanceOf[Int])))
}
diff --git a/core/src/test/scala/unit/kafka/utils/CoreUtilsTest.scala b/core/src/test/scala/unit/kafka/utils/CoreUtilsTest.scala
index 8acaa958610..5f703c7be33 100755
--- a/core/src/test/scala/unit/kafka/utils/CoreUtilsTest.scala
+++ b/core/src/test/scala/unit/kafka/utils/CoreUtilsTest.scala
@@ -19,8 +19,6 @@ package kafka.utils
import java.util
import java.util.{Base64, UUID}
-import java.util.concurrent.{ConcurrentHashMap, Executors, TimeUnit}
-import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.locks.ReentrantLock
import java.nio.ByteBuffer
import java.util.regex.Pattern
@@ -31,9 +29,6 @@ import org.apache.kafka.common.KafkaException
import org.apache.kafka.common.utils.Utils
import org.slf4j.event.Level
-import scala.jdk.CollectionConverters._
-import scala.concurrent.duration.Duration
-import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutorService, Future}
class CoreUtilsTest extends Logging {
@@ -124,28 +119,4 @@ class CoreUtilsTest extends Logging {
assertEquals(clusterId.length, 22)
assertTrue(clusterIdPattern.matcher(clusterId).matches())
}
-
- @Test
- def testAtomicGetOrUpdate(): Unit = {
- val count = 1000
- val nThreads = 5
- val createdCount = new AtomicInteger
- val map = new ConcurrentHashMap[Int, AtomicInteger]().asScala
- implicit val executionContext: ExecutionContextExecutorService = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(nThreads))
- try {
- Await.result(Future.traverse(1 to count) { _ =>
- Future {
- CoreUtils.atomicGetOrUpdate(map, 0, {
- createdCount.incrementAndGet
- new AtomicInteger
- }).incrementAndGet()
- }
- }, Duration(1, TimeUnit.MINUTES))
- assertEquals(count, map(0).get)
- val created = createdCount.get
- assertTrue(created > 0 && created <= nThreads, s"Too many creations $created")
- } finally {
- executionContext.shutdownNow()
- }
- }
}