forked from apache/spark
/
FetchedDataPoolSuite.scala
305 lines (220 loc) · 10.8 KB
/
FetchedDataPoolSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* 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.consumer
import java.{util => ju}
import java.util.concurrent.TimeUnit
import scala.collection.JavaConverters._
import scala.collection.mutable
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.TopicPartition
import org.jmock.lib.concurrent.DeterministicScheduler
import org.scalatest.PrivateMethodTester
import org.apache.spark.SparkConf
import org.apache.spark.sql.kafka010.{FETCHED_DATA_CACHE_EVICTOR_THREAD_RUN_INTERVAL, FETCHED_DATA_CACHE_TIMEOUT}
import org.apache.spark.sql.kafka010.consumer.KafkaDataConsumer.{AvailableOffsetRange, CacheKey}
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.util.ManualClock
class FetchedDataPoolSuite extends SharedSparkSession with PrivateMethodTester {
import FetchedDataPool._
type Record = ConsumerRecord[Array[Byte], Array[Byte]]
private val dummyBytes = "dummy".getBytes
// Helper private method accessors for FetchedDataPool
private type PoolCacheType = mutable.Map[CacheKey, CachedFetchedDataList]
private val _cache = PrivateMethod[PoolCacheType](Symbol("cache"))
def getCache(pool: FetchedDataPool): PoolCacheType = {
pool.invokePrivate(_cache())
}
test("acquire fetched data from multiple keys") {
val dataPool = new FetchedDataPool(new SparkConf())
val cacheKeys = (0 until 10).map { partId =>
CacheKey("testgroup", new TopicPartition("topic", partId))
}
assert(getCache(dataPool).size === 0)
cacheKeys.foreach { key => assert(getCache(dataPool).get(key).isEmpty) }
val dataList = cacheKeys.map(key => (key, dataPool.acquire(key, 0)))
assert(getCache(dataPool).size === cacheKeys.size)
cacheKeys.map { key =>
assert(getCache(dataPool)(key).size === 1)
assert(getCache(dataPool)(key).head.inUse)
}
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 10, expectedNumTotal = 10)
dataList.map { case (_, data) =>
data.withNewPoll(testRecords(0, 5).listIterator, 5, AvailableOffsetRange(0, 4))
}
dataList.foreach { case (key, data) =>
dataPool.release(key, data)
}
assert(getCache(dataPool).size === cacheKeys.size)
cacheKeys.map { key =>
assert(getCache(dataPool)(key).size === 1)
assert(!getCache(dataPool)(key).head.inUse)
}
dataPool.shutdown()
}
test("continuous use of fetched data from single key") {
val dataPool = new FetchedDataPool(new SparkConf())
val cacheKey = CacheKey("testgroup", new TopicPartition("topic", 0))
val data = dataPool.acquire(cacheKey, 0)
data.withNewPoll(testRecords(0, 5).listIterator, 5, AvailableOffsetRange(0, 4))
(0 to 3).foreach { _ => data.next() }
dataPool.release(cacheKey, data)
// suppose next batch
val data2 = dataPool.acquire(cacheKey, data.nextOffsetInFetchedData)
assert(data.eq(data2))
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 1, expectedNumTotal = 1)
assert(getCache(dataPool)(cacheKey).size === 1)
assert(getCache(dataPool)(cacheKey).head.inUse)
dataPool.release(cacheKey, data2)
assert(getCache(dataPool)(cacheKey).size === 1)
assert(!getCache(dataPool)(cacheKey).head.inUse)
dataPool.shutdown()
}
test("multiple tasks referring same key continuously using fetched data") {
val dataPool = new FetchedDataPool(new SparkConf())
val cacheKey = CacheKey("testgroup", new TopicPartition("topic", 0))
val dataFromTask1 = dataPool.acquire(cacheKey, 0)
val dataFromTask2 = dataPool.acquire(cacheKey, 0)
// it shouldn't give same object as dataFromTask1 though it asks same offset
// it definitely works when offsets are not overlapped: skip adding test for that
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 2, expectedNumTotal = 2)
assert(getCache(dataPool)(cacheKey).size === 2)
assert(getCache(dataPool)(cacheKey)(1).inUse)
// reading from task 1
dataFromTask1.withNewPoll(testRecords(0, 5).listIterator, 5, AvailableOffsetRange(0, 4))
(0 to 3).foreach { _ => dataFromTask1.next() }
dataPool.release(cacheKey, dataFromTask1)
// reading from task 2
dataFromTask2.withNewPoll(testRecords(0, 30).listIterator, 30, AvailableOffsetRange(0, 29))
(0 to 5).foreach { _ => dataFromTask2.next() }
dataPool.release(cacheKey, dataFromTask2)
// suppose next batch for task 1
val data2FromTask1 = dataPool.acquire(cacheKey, dataFromTask1.nextOffsetInFetchedData)
assert(data2FromTask1.eq(dataFromTask1))
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 2, expectedNumTotal = 2)
assert(getCache(dataPool)(cacheKey).head.inUse)
// suppose next batch for task 2
val data2FromTask2 = dataPool.acquire(cacheKey, dataFromTask2.nextOffsetInFetchedData)
assert(data2FromTask2.eq(dataFromTask2))
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 2, expectedNumTotal = 2)
assert(getCache(dataPool)(cacheKey)(1).inUse)
// release from task 2
dataPool.release(cacheKey, data2FromTask2)
assert(!getCache(dataPool)(cacheKey)(1).inUse)
// release from task 1
dataPool.release(cacheKey, data2FromTask1)
assert(!getCache(dataPool)(cacheKey).head.inUse)
dataPool.shutdown()
}
test("evict idle fetched data") {
val minEvictableIdleTimeMillis = 2000L
val evictorThreadRunIntervalMillis = 500L
val conf = new SparkConf()
conf.set(FETCHED_DATA_CACHE_TIMEOUT, minEvictableIdleTimeMillis)
conf.set(FETCHED_DATA_CACHE_EVICTOR_THREAD_RUN_INTERVAL, evictorThreadRunIntervalMillis)
val scheduler = new DeterministicScheduler()
val clock = new ManualClock()
val dataPool = new FetchedDataPool(scheduler, clock, conf)
val cacheKeys = (0 until 10).map { partId =>
CacheKey("testgroup", new TopicPartition("topic", partId))
}
val dataList = cacheKeys.map(key => (key, dataPool.acquire(key, 0)))
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 10, expectedNumTotal = 10)
dataList.map { case (_, data) =>
data.withNewPoll(testRecords(0, 5).listIterator, 5, AvailableOffsetRange(0, 4))
}
val dataToEvict = dataList.take(3)
// release key with around 500 ms delay, so that we can check eviction per key
dataToEvict.foreach { case (key, data) =>
dataPool.release(key, data)
clock.advance(500)
}
// time elapsed after releasing
// first key: 1500ms, second key: 1000 ms, third key: 500 ms
// advancing - first key: 2100ms, second key: 1600 ms, third key: 1100 ms
clock.advance(600)
scheduler.tick(minEvictableIdleTimeMillis + 100, TimeUnit.MILLISECONDS)
assert(getCache(dataPool)(dataToEvict(0)._1).isEmpty)
assert(getCache(dataPool)(dataToEvict(1)._1).nonEmpty)
assert(getCache(dataPool)(dataToEvict(2)._1).nonEmpty)
// advancing - second key: 2100 ms, third key: 1600 ms
clock.advance(500)
scheduler.tick(minEvictableIdleTimeMillis + 100, TimeUnit.MILLISECONDS)
assert(getCache(dataPool)(dataToEvict(0)._1).isEmpty)
assert(getCache(dataPool)(dataToEvict(1)._1).isEmpty)
assert(getCache(dataPool)(dataToEvict(2)._1).nonEmpty)
// advancing - third key: 2300 ms
clock.advance(500)
scheduler.tick(minEvictableIdleTimeMillis + 100, TimeUnit.MILLISECONDS)
assert(getCache(dataPool)(dataToEvict(0)._1).isEmpty)
assert(getCache(dataPool)(dataToEvict(1)._1).isEmpty)
assert(getCache(dataPool)(dataToEvict(2)._1).isEmpty)
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 10, expectedNumTotal = 7)
assert(getCache(dataPool).values.map(_.size).sum === dataList.size - dataToEvict.size)
dataList.takeRight(3).foreach { case (key, data) =>
dataPool.release(key, data)
}
// add objects to be candidates for eviction
clock.advance(minEvictableIdleTimeMillis + 100)
// ensure releasing more objects don't trigger eviction unless evictor runs
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 10, expectedNumTotal = 7)
assert(getCache(dataPool).values.map(_.size).sum === dataList.size - dataToEvict.size)
try {
dataPool.shutdown()
} catch {
// ignore as it's known issue, DeterministicScheduler doesn't support shutdown
case _: UnsupportedOperationException =>
}
}
test("invalidate key") {
val dataPool = new FetchedDataPool(new SparkConf())
val cacheKey = CacheKey("testgroup", new TopicPartition("topic", 0))
val dataFromTask1 = dataPool.acquire(cacheKey, 0)
val dataFromTask2 = dataPool.acquire(cacheKey, 0)
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 2, expectedNumTotal = 2)
// 1 idle, 1 active
dataPool.release(cacheKey, dataFromTask1)
val cacheKey2 = CacheKey("testgroup", new TopicPartition("topic", 1))
dataPool.acquire(cacheKey2, 0)
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 3, expectedNumTotal = 3)
assert(getCache(dataPool).size === 2)
assert(getCache(dataPool)(cacheKey).size === 2)
assert(getCache(dataPool)(cacheKey2).size === 1)
dataPool.invalidate(cacheKey)
assertFetchedDataPoolStatistic(dataPool, expectedNumCreated = 3, expectedNumTotal = 1)
assert(getCache(dataPool).size === 1)
assert(getCache(dataPool).get(cacheKey).isEmpty)
// it doesn't affect other keys
assert(getCache(dataPool)(cacheKey2).size === 1)
dataPool.release(cacheKey, dataFromTask2)
// it doesn't throw error on invalidated objects, but it doesn't cache them again
assert(getCache(dataPool).size === 1)
assert(getCache(dataPool).get(cacheKey).isEmpty)
dataPool.shutdown()
}
private def testRecords(startOffset: Long, count: Int): ju.List[Record] = {
(0 until count).map { offset =>
new Record("topic", 0, startOffset + offset, dummyBytes, dummyBytes)
}.toList.asJava
}
private def assertFetchedDataPoolStatistic(
fetchedDataPool: FetchedDataPool,
expectedNumCreated: Long,
expectedNumTotal: Long): Unit = {
assert(fetchedDataPool.numCreated === expectedNumCreated)
assert(fetchedDataPool.numTotal === expectedNumTotal)
}
}