Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit 068233b

Browse files
committed
reusing same array instead of creating new one
1 parent 06dc83c commit 068233b

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

streaming-pubsub/src/main/scala/org/apache/spark/streaming/pubsub/PubsubInputDStream.scala

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class PubsubReceiver(
277277

278278
var buffer: ArrayBuffer[ReceivedMessage] = createBufferArray()
279279

280-
var latestStorePushTime: Long = -1
280+
var latestAttemptToPushInStoreTime: Long = -1
281281

282282
lazy val rateLimiter: RateLimiter = RateLimiter.create(getInitialRateLimit.toDouble)
283283

@@ -325,7 +325,7 @@ class PubsubReceiver(
325325
var backoff = INIT_BACKOFF
326326

327327
// To avoid the edge case when buffer is not full and no message pushed to store
328-
latestStorePushTime = System.currentTimeMillis()
328+
latestAttemptToPushInStoreTime = System.currentTimeMillis()
329329

330330
while (!isStopped()) {
331331
try {
@@ -393,7 +393,7 @@ class PubsubReceiver(
393393
*/
394394
def push(): Unit = {
395395

396-
val diff = System.currentTimeMillis() - latestStorePushTime
396+
val diff = System.currentTimeMillis() - latestAttemptToPushInStoreTime
397397
if (buffer.length >= blockSize || (buffer.length < blockSize && diff >= blockIntervalMs)) {
398398

399399
// grouping messages into complete and partial blocks (if any)
@@ -404,18 +404,29 @@ class PubsubReceiver(
404404
// messages in buffer is less than blockSize. So will push partial block
405405
val iterator = if (completeBlocks.nonEmpty) completeBlocks else partialBlock
406406

407-
// Creating new buffer
408-
buffer = createBufferArray()
409-
410-
// Pushing partial block messages back to buffer if complete blocks formed
411-
if (completeBlocks.nonEmpty && partialBlock.hasNext) {
412-
buffer.appendAll(partialBlock.next())
413-
}
407+
// Will push partial block messages back to buffer if complete blocks formed
408+
val partial = if (completeBlocks.nonEmpty && partialBlock.nonEmpty) {
409+
partialBlock.next()
410+
} else null
414411

415412
while (iterator.hasNext) {
416-
pushToStoreAndAck(iterator.next().toList)
417-
latestStorePushTime = System.currentTimeMillis()
413+
try {
414+
pushToStoreAndAck(iterator.next().toList)
415+
} catch {
416+
case e: SparkException => reportError(
417+
"Failed to write messages into reliable store", e)
418+
case NonFatal(e) => reportError(
419+
"Failed to write messages in reliable store", e)
420+
} finally {
421+
latestAttemptToPushInStoreTime = System.currentTimeMillis()
422+
}
418423
}
424+
425+
// clear existing buffer messages
426+
buffer.clear()
427+
428+
// Pushing partial block messages back to buffer if complete blocks formed
429+
if (partial != null) buffer.appendAll(partial)
419430
}
420431
}
421432

streaming-pubsub/src/test/scala/org/apache/spark/streaming/pubsub/PubsubStreamSuite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PubsubStreamSuite extends ConditionalSparkFunSuite with Eventually with Be
3535

3636
val batchDuration = Seconds(1)
3737

38-
val blockSize = 10
38+
val blockSize = 15
3939

4040
private val master: String = "local[2]"
4141

@@ -79,6 +79,7 @@ class PubsubStreamSuite extends ConditionalSparkFunSuite with Eventually with Be
7979
conf.set("spark.streaming.receiver.maxRate", "100")
8080
conf.set("spark.streaming.backpressure.pid.minRate", "10")
8181
conf.set("spark.streaming.blockQueueSize", blockSize.toString)
82+
conf.set("spark.streaming.blockInterval", "1000ms")
8283
}
8384

8485

0 commit comments

Comments
 (0)