HeartSaVioR commented on a change in pull request #22138: [SPARK-25151][SS] 
Apply Apache Commons Pool to KafkaDataConsumer
URL: https://github.com/apache/spark/pull/22138#discussion_r317342035
 
 

 ##########
 File path: 
external/kafka-0-10-sql/src/test/scala/org/apache/spark/sql/kafka010/InternalKafkaConsumerPoolSuite.scala
 ##########
 @@ -0,0 +1,316 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.kafka010
+
+import java.{util => ju}
+
+import scala.collection.JavaConverters._
+
+import org.apache.kafka.clients.consumer.ConsumerConfig._
+import org.apache.kafka.common.TopicPartition
+import org.apache.kafka.common.serialization.ByteArrayDeserializer
+
+import org.apache.spark.SparkEnv
+import org.apache.spark.sql.kafka010.KafkaDataConsumer.CacheKey
+import org.apache.spark.sql.test.SharedSQLContext
+
+class InternalKafkaConsumerPoolSuite extends SharedSQLContext {
+  import org.apache.spark.sql.kafka010.InternalKafkaConsumerPool.PoolConfig._
+
+  test("basic multiple borrows and returns for single key") {
+    val pool = InternalKafkaConsumerPool.build
+
+    val topic = "topic"
+    val partitionId = 0
+    val topicPartition = new TopicPartition(topic, partitionId)
+
+    val kafkaParams: ju.Map[String, Object] = getTestKafkaParams
+
+    val key = new CacheKey(topicPartition, kafkaParams)
+
+    val pooledObjects = (0 to 2).map { _ =>
+      val pooledObject = pool.borrowObject(key, kafkaParams)
+      assertPooledObject(pooledObject, topicPartition, kafkaParams)
+      pooledObject
+    }
+
+    assertPoolStateForKey(pool, key, numIdle = 0, numActive = 3, numTotal = 3)
+    assertPoolState(pool, numIdle = 0, numActive = 3, numTotal = 3)
+
+    val pooledObject2 = pool.borrowObject(key, kafkaParams)
+
+    assertPooledObject(pooledObject2, topicPartition, kafkaParams)
+    assertPoolStateForKey(pool, key, numIdle = 0, numActive = 4, numTotal = 4)
+    assertPoolState(pool, numIdle = 0, numActive = 4, numTotal = 4)
+
+    pooledObjects.foreach(pool.returnObject)
+
+    assertPoolStateForKey(pool, key, numIdle = 3, numActive = 1, numTotal = 4)
+    assertPoolState(pool, numIdle = 3, numActive = 1, numTotal = 4)
+
+    pool.returnObject(pooledObject2)
+
+    // we only allow three idle objects per key
+    assertPoolStateForKey(pool, key, numIdle = 3, numActive = 0, numTotal = 3)
+    assertPoolState(pool, numIdle = 3, numActive = 0, numTotal = 3)
+
+    pool.close()
+  }
+
+  test("basic borrow and return for multiple keys") {
+    val pool = InternalKafkaConsumerPool.build
+
+    val kafkaParams = getTestKafkaParams
+    val topicPartitions: List[TopicPartition] = for (
+      topic <- List("topic", "topic2");
+      partitionId <- 0 to 5
+    ) yield new TopicPartition(topic, partitionId)
+
+    val keys: List[CacheKey] = topicPartitions.map { part =>
+      new CacheKey(part, kafkaParams)
+    }
+
+    // while in loop pool doesn't still exceed total pool size
+    val keyToPooledObjectPairs = borrowObjectsPerKey(pool, kafkaParams, keys)
+
+    assertPoolState(pool, numIdle = 0, numActive = 
keyToPooledObjectPairs.length,
+      numTotal = keyToPooledObjectPairs.length)
+
+    returnObjects(pool, keyToPooledObjectPairs)
+
+    assertPoolState(pool, numIdle = keyToPooledObjectPairs.length, numActive = 
0,
+      numTotal = keyToPooledObjectPairs.length)
+
+    pool.close()
+  }
+
+  test("borrow more than soft max capacity from pool which is neither free 
space nor idle object") {
 
 Review comment:
   Addressed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to