Skip to content

Commit 37db20c

Browse files
Kenji Kikushimaankurdave
authored andcommitted
[SPARK-5064][GraphX] Add numEdges upperbound validation for R-MAT graph generator to prevent infinite loop
I looked into GraphGenerators#chooseCell, and found that chooseCell can't generate more edges than pow(2, (2 * (log2(numVertices)-1))) to make a Power-law graph. (Ex. numVertices:4 upperbound:4, numVertices:8 upperbound:16, numVertices:16 upperbound:64) If we request more edges over the upperbound, rmatGraph fall into infinite loop. So, how about adding an argument validation? Author: Kenji Kikushima <[email protected]> Closes #3950 from kj-ki/SPARK-5064 and squashes the following commits: 4ee18c7 [Ankur Dave] Reword error message and add unit test d760bc7 [Kenji Kikushima] Add numEdges upperbound validation for R-MAT graph generator to prevent infinite loop. (cherry picked from commit 3ee3ab5) Signed-off-by: Ankur Dave <[email protected]>
1 parent e90f6b5 commit 37db20c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

graphx/src/main/scala/org/apache/spark/graphx/util/GraphGenerators.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ object GraphGenerators {
133133
// This ensures that the 4 quadrants are the same size at all recursion levels
134134
val numVertices = math.round(
135135
math.pow(2.0, math.ceil(math.log(requestedNumVertices) / math.log(2.0)))).toInt
136+
val numEdgesUpperBound =
137+
math.pow(2.0, 2 * ((math.log(numVertices) / math.log(2.0)) - 1)).toInt
138+
if (numEdgesUpperBound < numEdges) {
139+
throw new IllegalArgumentException(
140+
s"numEdges must be <= $numEdgesUpperBound but was $numEdges")
141+
}
136142
var edges: Set[Edge[Int]] = Set()
137143
while (edges.size < numEdges) {
138144
if (edges.size % 100 == 0) {

graphx/src/test/scala/org/apache/spark/graphx/util/GraphGeneratorsSuite.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,14 @@ class GraphGeneratorsSuite extends FunSuite with LocalSparkContext {
110110
}
111111
}
112112

113+
test("SPARK-5064 GraphGenerators.rmatGraph numEdges upper bound") {
114+
withSpark { sc =>
115+
val g1 = GraphGenerators.rmatGraph(sc, 4, 4)
116+
assert(g1.edges.count() === 4)
117+
intercept[IllegalArgumentException] {
118+
val g2 = GraphGenerators.rmatGraph(sc, 4, 8)
119+
}
120+
}
121+
}
122+
113123
}

0 commit comments

Comments
 (0)