Skip to content

Commit aaaeaf9

Browse files
committed
[SPARK-4264] Completion iterator should only invoke callback once
Author: Aaron Davidson <[email protected]> Closes #3128 from aarondav/compiter and squashes the following commits: 698e4be [Aaron Davidson] [SPARK-4264] Completion iterator should only invoke callback once (cherry picked from commit 23eaf0e) Signed-off-by: Aaron Davidson <[email protected]>
1 parent 0148445 commit aaaeaf9

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

core/src/main/scala/org/apache/spark/util/CompletionIterator.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ private[spark]
2525
// scalastyle:off
2626
abstract class CompletionIterator[ +A, +I <: Iterator[A]](sub: I) extends Iterator[A] {
2727
// scalastyle:on
28+
29+
private[this] var completed = false
2830
def next() = sub.next()
2931
def hasNext = {
3032
val r = sub.hasNext
31-
if (!r) {
33+
if (!r && !completed) {
34+
completed = true
3235
completion()
3336
}
3437
r
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.util
19+
20+
import org.scalatest.FunSuite
21+
22+
class CompletionIteratorSuite extends FunSuite {
23+
test("basic test") {
24+
var numTimesCompleted = 0
25+
val iter = List(1, 2, 3).iterator
26+
val completionIter = CompletionIterator[Int, Iterator[Int]](iter, { numTimesCompleted += 1 })
27+
28+
assert(completionIter.hasNext)
29+
assert(completionIter.next() === 1)
30+
assert(numTimesCompleted === 0)
31+
32+
assert(completionIter.hasNext)
33+
assert(completionIter.next() === 2)
34+
assert(numTimesCompleted === 0)
35+
36+
assert(completionIter.hasNext)
37+
assert(completionIter.next() === 3)
38+
assert(numTimesCompleted === 0)
39+
40+
assert(!completionIter.hasNext)
41+
assert(numTimesCompleted === 1)
42+
43+
// SPARK-4264: Calling hasNext should not trigger the completion callback again.
44+
assert(!completionIter.hasNext)
45+
assert(numTimesCompleted === 1)
46+
}
47+
}

0 commit comments

Comments
 (0)