Skip to content
Snippets Groups Projects
Commit 289257c4 authored by Dongjoon Hyun's avatar Dongjoon Hyun Committed by Reynold Xin
Browse files

[SPARK-14219][GRAPHX] Fix `pickRandomVertex` not to fall into infinite loops...

[SPARK-14219][GRAPHX] Fix `pickRandomVertex` not to fall into infinite 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.
parent 2bc7c96d
No related branches found
No related tags found
No related merge requests found
...@@ -276,7 +276,7 @@ class GraphOps[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED]) extends Seriali ...@@ -276,7 +276,7 @@ class GraphOps[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED]) extends Seriali
if (Random.nextDouble() < probability) { Some(vidVvals._1) } if (Random.nextDouble() < probability) { Some(vidVvals._1) }
else { None } else { None }
} }
if (selectedVertices.count > 1) { if (selectedVertices.count > 0) {
found = true found = true
val collectedVertices = selectedVertices.collect() val collectedVertices = selectedVertices.collect()
retVal = collectedVertices(Random.nextInt(collectedVertices.length)) retVal = collectedVertices(Random.nextInt(collectedVertices.length))
......
...@@ -404,4 +404,13 @@ class GraphSuite extends SparkFunSuite with LocalSparkContext { ...@@ -404,4 +404,13 @@ class GraphSuite extends SparkFunSuite with LocalSparkContext {
assert(sc.getPersistentRDDs.isEmpty) 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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment