spark-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Mocanu <>
Subject RE: another updateStateByKey question - updated w possible Spark bug
Date Mon, 05 May 2014 16:29:55 GMT
Forgot to mention my batch interval is 1 second:
val ssc = new StreamingContext(conf, Seconds(1))
hence the Thread.sleep(1100)

From: Adrian Mocanu []
Sent: May-05-14 12:06 PM
Subject: RE: another updateStateByKey question - updated w possible Spark bug

I’ve encountered this issue again and am able to reproduce it about 10% of the time.

1. Here is the input:
RDD[ (a, 1262325660000, 1), (a, 1262325660000, 2) ]
RDD[ (a, 1262325660000, 1), (a, 1262325660000, 3) ]
RDD[ (a, 1262325660000, 3) ]
RDD[ (a, 1262325660000, 4) ]
RDD[ (a, 1262325660000, 2) ]
RDD[ (a, 1262325660000, 5), (a, 1262325660000, 5) ]

2. Here are the actual results (printed DStream – each line is a new RDD with RDD Id being
the last number on each line):
(((a,1262325660000),StateClass(3,2,ArrayBuffer(1.0, 2.0))),6)
(((a,1262325660000),StateClass(7,4,ArrayBuffer(1.0, 3.0))),13)
(((a,1262325660000),StateClass(10,5,ArrayBuffer())),26)   <-empty elements Seq[V]
(((a,1262325660000),StateClass(26,9,ArrayBuffer(5.0, 5.0))),47)
(((a,1262325660000),StateClass(26,9,ArrayBuffer())),53)  <-empty elements Seq[V]
(((a,1262325660000),StateClass(26,9,ArrayBuffer())),59)  <-empty elements Seq[V]

3. Here are the expected results: (all tuples from #2 except those with empty Seq[V] )
(((a,1262325660000),StateClass(3,2,ArrayBuffer(1.0, 2.0))),6)
(((a,1262325660000),StateClass(7,4,ArrayBuffer(1.0, 3.0))),13)
(((a,1262325660000),StateClass(26,9,ArrayBuffer(5.0, 5.0))),47)

4. Here is the code:
    case class StateClass(sum:Integer, count:Integer, elements:Seq[Double])

    val updateSumFunc = (values: Seq[(String, Long, Int)], state: Option[StateClass]) =>
//      if (values.isEmpty) {
//        // if RDD cannot find values for this key (which is from prev RDD,
//        // the tuple will not be shown in this RDD w values of 0
//        None
//      } else {
        val previousState = state.getOrElse(StateClass(0, 0, Seq()))
        val currentCount = values.size + previousState.count
        var currentSum=0
        for (newValue <- values) yield ({
          currentSum = currentSum + newValue._3
        currentSum= currentSum +previousState.sum
        val elements = for (newValues <- values) yield ({
        Some(StateClass(currentSum, currentCount, elements))
//      }

val partialResultSums=, Long, Int)) =>((x._1), (x._1, x._2,
x._3)))  //re map
.updateStateByKey[StateClass](updateSumFunc)  //update state
.transform(rdd=>>(t,   //add RDD ID to RDD tuples


Now this is how I generate the RDDs and I suspect the delay is why the issue surfaces:

    rddQueue += ssc.sparkContext.makeRDD(smallWindow1)  // smallWindow1 = List[(String, Long,
Int)]( (a, 1262325660000, 1), (a, 1262325660000, 2) )

    rddQueue += ssc.sparkContext.makeRDD(smallWindow2)   // smallWindow2= List[(String, Long,
Int)]((a, 1262325660000, 1), (a, 1262325660000, 3))

    rddQueue += ssc.sparkContext.makeRDD(smallWindow3)   // smallWindow3= List[(String, Long,
Int)]((a, 1262325660000, 3))

    rddQueue += ssc.sparkContext.makeRDD(smallWindow4)   // smallWindow4= List[(String, Long,
Int)]((a, 1262325660000, 4))

    rddQueue += ssc.sparkContext.makeRDD(smallWindow5)   // smallWindow5= List[(String, Long,
Int)]((a, 1262325660000, 2))

    rddQueue += ssc.sparkContext.makeRDD(smallWindow6)   // smallWindow6= List[(String, Long,
Int)]((a, 1262325660000, 5), (a, 1262325660000, 5))


In my use case when I detect an empty Seq[V] in updateStateByKey function I return None so
I can filter the tuples out. However, given that Spark calls updateStateByKey function with
empty Seq[V] when it should not, messes my logic up.
I wonder how to bypass this bug/feature of Spark.

From: Tathagata Das []
Sent: May-02-14 3:10 PM
Subject: Re: another updateStateByKey question

Could be a bug. Can you share a code with data that I can use to reproduce this?

On May 2, 2014 9:49 AM, "Adrian Mocanu" <<>>
Has anyone else noticed that sometimes the same tuple calls update state function twice?
I have 2 tuples with the same key in 1 RDD part of DStream: RDD[ (a,1), (a,2) ]
When the update function is called the first time Seq[V] has data: 1, 2 which is correct:
StateClass(3,2, ArrayBuffer(1, 2))
Then right away (in my output I see this) the same key is used and the function is called
again but this time Seq is empty: StateClass(3,2, ArrayBuffer( ))

In the update function I also save Seq[V] to state so I can see it in the RDD. I also show
a count and sum of the values.
StateClass(sum, count, Seq[V])

Why is the update function called with empty Seq[V] on the same key when all values for that
key have been already taken care of in a previous update?


View raw message