Skip to content

Commit

Permalink
[SPARK-14219][GRAPHX] Fix pickRandomVertex not to fall into infinit…
Browse files Browse the repository at this point in the history
…e loops for graphs with one vertex

## What changes were proposed in this pull request?

Currently, `GraphOps.pickRandomVertex()` falls into infinite loops for graphs having only one vertex. This PR fixes it by modifying the following termination-checking condition.
```scala
-      if (selectedVertices.count > 1) {
+      if (selectedVertices.count > 0) {
```

## How was this patch tested?

Pass the Jenkins tests (including new test case).

Author: Dongjoon Hyun <dongjoon@apache.org>

Closes #12018 from dongjoon-hyun/SPARK-14219.
  • Loading branch information
dongjoon-hyun authored and rxin committed Mar 29, 2016
1 parent 2bc7c96 commit 289257c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class GraphOps[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED]) extends Seriali
if (Random.nextDouble() < probability) { Some(vidVvals._1) }
else { None }
}
if (selectedVertices.count > 1) {
if (selectedVertices.count > 0) {
found = true
val collectedVertices = selectedVertices.collect()
retVal = collectedVertices(Random.nextInt(collectedVertices.length))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,13 @@ class GraphSuite extends SparkFunSuite with LocalSparkContext {
assert(sc.getPersistentRDDs.isEmpty)
}
}

test("SPARK-14219: pickRandomVertex") {
withSpark { sc =>
val vert = sc.parallelize(List((1L, "a")), 1)
val edges = sc.parallelize(List(Edge[Long](1L, 1L)), 1)
val g0 = Graph(vert, edges)
assert(g0.pickRandomVertex() === 1L)
}
}
}

0 comments on commit 289257c

Please sign in to comment.