Dmitry Dzhus Dmitry Dzhus - 8 months ago 60
Scala Question

subtractByKey modifes values in the source RDD

I'm experiencing a problem with

using Spark 2.0.2 with Scala 2.11.x (it also reproduces with Spark 1.6.2 and Scala 2.10):

Relevant code:

object Types {
type ContentId = Int
type ContentKey = Tuple2[Int, ContentId]
type InternalContentId = Int

val inverseItemIDMap: RDD[(InternalContentId, ContentKey)] ="Built an inverse map of ${inverseItemIDMap.count()} item IDs")"I->E ", "\nI->E ", ""))

val superfluousItems: RDD[(InternalContentId, Int)] = .. .cache()"SI ", "\nSI ", ""))

val filteredInverseItemIDMap: RDD[(InternalContentId, ContentKey)] =
inverseItemIDMap.subtractByKey(superfluousItems).cache() // <<===!!!"${filteredInverseItemIDMap.count()} items in the filtered inverse ID mapping")"F I->E ", "\nF I->E ", ""))

The operation in question is
. Both RDDs involved are cached and forced via
prior to calling
, so I would expect the result to be unaffected by how exactly
is built.

I added debugging output and filtered the resulting logs by relevant
values (829911, 830071). Output:

Built an inverse map of 827354 item IDs
I->E (829911,(2,1135081))
I->E (830071,(1,2295102))
748190 items in the training set had less than 28 ratings
SI (829911,3)
79164 items in the filtered inverse ID mapping
F I->E (830071,(2,1135081))

There's no element with key 830071 in superfluousItems (SI), so it's not removed from the source RDD. However, its value is for some reason replaced with the one from key 829911. How could this be? I cannot reproduce it locally - only when running on a multi-machine cluster. Is this a bug or I'm missing something?

Answer Source

The problem was that I assumed that caching and forcing an RDD guarantees that it will never be re-evaluated. inverseItemIDMap is built using a non-determenistic operation, and multiple uses of it also give different results:

val itemIDMap: RDD[(ContentKey, InternalContentId)] =
  .map(u => (u._1, u._2.toInt))
  .cache()"Built a map of ${itemIDMap.count()} item IDs")
val inverseItemIDMap: RDD[(InternalContentId, ContentKey)] =

I made the operation stable by adding .sortBy(c => c) before .zipWithUniqueId() and this solved the issue.