Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
236 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
addSbtPlugin("org.hammerlab" % "sbt-parent" % "3.1.1") | ||
addSbtPlugin("org.hammerlab" % "sbt-parent" % "3.3.0") |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/scala/org/hammerlab/spark/AtomicLongAccumulator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.hammerlab.spark.test | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
import org.apache.spark.util.AccumulatorV2 | ||
|
||
/** | ||
* Thread-safe version of [[org.apache.spark.util.LongAccumulator]], suitable for use as a static value in tests. | ||
* | ||
* See https://issues.apache.org/jira/browse/SPARK-21425. | ||
*/ | ||
case class AtomicLongAccumulator(initialValue: Long = 0) | ||
extends AccumulatorV2[Long, Long] { | ||
private var _value = new AtomicLong(initialValue) | ||
override def value: Long = _value.get | ||
override def isZero: Boolean = value == 0 | ||
override def copy(): AccumulatorV2[Long, Long] = AtomicLongAccumulator(value) | ||
override def reset(): Unit = _value = new AtomicLong(0) | ||
override def add(v: Long): Unit = _value.addAndGet(v) | ||
override def merge(other: AccumulatorV2[Long, Long]): Unit = add(other.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/scala/org/hammerlab/spark/test/AtomicLongAccumulatorTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.hammerlab.spark.test | ||
|
||
import org.hammerlab.spark.test.suite.SparkSuite | ||
|
||
class AtomicLongAccumulatorTest | ||
extends SparkSuite { | ||
|
||
test("instance") { | ||
val acc = AtomicLongAccumulator() | ||
sc.register(acc, "acc") | ||
sc | ||
.parallelize( | ||
1 to 10, | ||
numSlices = 4 | ||
) | ||
.map { | ||
n ⇒ | ||
acc.add(n) | ||
n.toString | ||
} | ||
.collect should be(1 to 10 map(_.toString)) | ||
|
||
acc.value should be(55) | ||
} | ||
|
||
test("static") { | ||
import AtomicLongAccumulatorTest.acc | ||
sc.register(acc, "acc") | ||
sc | ||
.parallelize( | ||
1 to 10, | ||
numSlices = 4 | ||
) | ||
.map { | ||
n ⇒ | ||
acc.add(n) | ||
n.toString | ||
} | ||
.collect should be(1 to 10 map(_.toString)) | ||
|
||
acc.value should be(55) | ||
} | ||
} | ||
|
||
object AtomicLongAccumulatorTest { | ||
val acc = AtomicLongAccumulator() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.