Github user squito commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11105#discussion_r55426319
  
    --- Diff: 
core/src/test/scala/org/apache/spark/ConsistentAccumulatorsSuite.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * 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
    +
    +import scala.ref.WeakReference
    +
    +import org.scalatest.Matchers
    +
    +import org.apache.spark.scheduler._
    +
    +
    +class ConsistentAccumulatorSuite extends SparkFunSuite with Matchers with 
LocalSparkContext {
    +
    +  test("single partition") {
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 20, 1)
    +    val b = a.map{x => acc += x; x}
    +    b.cache()
    +    b.count()
    +    acc.value should be (210)
    +  }
    +
    +  test("map + cache + first + count") {
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 20, 10)
    +    val b = a.map{x => acc += x; x}
    +    b.cache()
    +    b.first()
    +    acc.value should be > (0)
    +    b.collect()
    +    acc.value should be (210)
    +  }
    +
    +  test ("basic accumulation"){
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val d = sc.parallelize(1 to 20)
    +    d.map{x => acc += x}.count()
    +    acc.value should be (210)
    +
    +    val longAcc = sc.accumulator(0L, consistent = true)
    +    val maxInt = Integer.MAX_VALUE.toLong
    +    d.map{x => longAcc += maxInt + x; x}.count()
    +    longAcc.value should be (210L + maxInt * 20)
    +  }
    +
    +  test ("basic accumulation flatMap"){
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val d = sc.parallelize(1 to 20)
    +    d.map{x => acc += x}.count()
    +    acc.value should be (210)
    +
    +    val longAcc = sc.accumulator(0L, consistent = true)
    +    val maxInt = Integer.MAX_VALUE.toLong
    +    val c = d.flatMap{x =>
    +      longAcc += maxInt + x
    +      if (x % 2 == 0) {
    +        Some(x)
    +      } else {
    +        None
    +      }
    +    }.count()
    +    longAcc.value should be (210L + maxInt * 20)
    +    c should be (10)
    +  }
    +
    +  test("map + map + count") {
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 20, 10)
    +    val b = a.map{x => acc += x; x}
    +    val c = b.map{x => acc += x; x}
    +    c.count()
    +    acc.value should be (420)
    +  }
    +
    +  test("first + count") {
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 20, 10)
    +    val b = a.map{x => acc += x; x}
    +    b.first()
    +    b.count()
    +    acc.value should be (210)
    +  }
    +
    +  test("map + count + count + map + count") {
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 20, 10)
    +    val b = a.map{x => acc += x; x}
    +    b.count()
    +    acc.value should be (210)
    +    b.count()
    +    acc.value should be (210)
    +    val c = b.map{x => acc += x; x}
    +    c.count()
    +    acc.value should be (420)
    +  }
    +
    +  test ("map + toLocalIterator + count"){
    +    sc = new SparkContext("local", "test")
    +    val acc : Accumulator[Int] = sc.accumulator(0, consistent = true)
    +
    +    val a = sc.parallelize(1 to 100, 10)
    +    val b = a.map{x => acc += x; x}
    +    // This depends on toLocalIterators per-partition fetch behaviour
    +    b.toLocalIterator.take(2).toList
    +    acc.value should be > (0)
    +    b.count()
    +    acc.value should be (5050)
    +    b.count()
    +    acc.value should be (5050)
    +
    +    val c = b.map{x => acc += x; x}
    +    c.cache()
    +    c.toLocalIterator.take(2).toList
    +    acc.value should be > (5050)
    +    c.count()
    +    acc.value should be (10100)
    +  }
    --- End diff --
    
    these tests are great, some cases in here I hadn't thought of, but I think 
we should add some more:
    
    1) using `rdd.filter`(I know its the same code path right now, but it 
should be there as a regression just in case it changes)
    2) something with `rdd.coalesce`, so there are a different number of tasks 
from partitions
    3) Some dag with more forks in it, eg.
    
    ```
    val data = sc.parallelize(1 to 1e4.toInt, 20)
    val x = sc.accumulator(0, consistent = true)
    val y = sc.accumulator(0, consistent = true)
    
    // this rdd gets computed multiple times, sometimes with a coalesce, 
sometimes not, still only applies updates from x once
    val a = data.filter { i => if (i % 10 == 0) { x += 1; true} else false }
    
    val b = a.coalese(10)
    val c = b.map { i => y += i; i + 1 }
    
    assert(c.take(10).length == 10)
    
    // these updates to x also get counted
    val d = a.map { i => x += 10; i + 1 }
    
    val e = c.map { i => (i -> i) }.cogroup( d.map { i => y += i; (i -> i) } )
    e.count()
    assert(x.value == 11e3.toInt)
    assert(y.value == 1001000) // s = (1e3 * (1 + 1e3)) / 2; s * 2 * 10 + 1000
    ```
    (something a bit simpler would work too.)
    
    4) concurrent jobs on a shared RDD w/ a consistent accumulator.  Perhaps it 
actually merits a special test on the internals to cover some interleavings?
    
    Also can we use `local[2]` for the context?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to