|
| 1 | +package org.apache.spark.rdd |
| 2 | + |
| 3 | +import scala.concurrent.{Await, Future} |
| 4 | +import scala.collection.mutable.ArrayBuffer |
| 5 | +import scala.concurrent.duration.Duration |
| 6 | +import scala.annotation.tailrec |
| 7 | +import scala.collection.mutable |
| 8 | +import org.apache.spark.rdd.RDDiterator._ |
| 9 | +import org.apache.spark.FutureAction |
| 10 | + |
| 11 | +/** |
| 12 | + * Iterable whose iterator iterates over all elements of an RDD without fetching all partitions |
| 13 | + * to the driver process |
| 14 | + * |
| 15 | + * @param rdd RDD to iterate |
| 16 | + * @param prefetchPartitions The number of partitions to prefetch. |
| 17 | + * If <1 will not prefetch. |
| 18 | + * partitions prefetched = min(prefetchPartitions, partitionBatchSize) |
| 19 | + * @param partitionBatchSize How many partitions to fetch per job |
| 20 | + * @param timeOut How long to wait for each partition before failing. |
| 21 | + */ |
| 22 | +class RDDiterator[T: ClassManifest](rdd: RDD[T], prefetchPartitions: Int, partitionBatchSize: Int, |
| 23 | + timeOut: Duration) |
| 24 | + extends Iterator[T] { |
| 25 | + |
| 26 | + val batchSize = math.max(1,partitionBatchSize) |
| 27 | + var partitionsBatches: Iterator[Seq[Int]] = Range(0, rdd.partitions.size).grouped(batchSize) |
| 28 | + var pendingFetchesQueue = mutable.Queue.empty[Future[Seq[Seq[T]]]] |
| 29 | + //add prefetchPartitions prefetch |
| 30 | + 0.until(math.max(0, prefetchPartitions / batchSize)).foreach(x=>enqueueDataFetch()) |
| 31 | + |
| 32 | + var currentIterator: Iterator[T] = Iterator.empty |
| 33 | + @tailrec |
| 34 | + final def hasNext = { |
| 35 | + if (currentIterator.hasNext) { |
| 36 | + //Still values in the current partition |
| 37 | + true |
| 38 | + } else { |
| 39 | + //Move on to the next partition |
| 40 | + //Queue new prefetch of a partition |
| 41 | + enqueueDataFetch() |
| 42 | + if (pendingFetchesQueue.isEmpty) { |
| 43 | + //No more partitions |
| 44 | + currentIterator = Iterator.empty |
| 45 | + false |
| 46 | + } else { |
| 47 | + val future = pendingFetchesQueue.dequeue() |
| 48 | + currentIterator = Await.result(future, timeOut).flatMap(x => x).iterator |
| 49 | + //Next partition might be empty so check again. |
| 50 | + this.hasNext |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + def next() = { |
| 55 | + hasNext |
| 56 | + currentIterator.next() |
| 57 | + } |
| 58 | + |
| 59 | + def enqueueDataFetch() ={ |
| 60 | + if (partitionsBatches.hasNext) { |
| 61 | + pendingFetchesQueue.enqueue(fetchData(partitionsBatches.next(), rdd)) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +object RDDiterator { |
| 67 | + private def fetchData[T: ClassManifest](partitionIndexes: Seq[Int], |
| 68 | + rdd: RDD[T]): FutureAction[Seq[Seq[T]]] = { |
| 69 | + val results = new ArrayBuffer[Seq[T]]() |
| 70 | + rdd.context.submitJob[T, Array[T], Seq[Seq[T]]](rdd, |
| 71 | + x => x.toArray, |
| 72 | + partitionIndexes, |
| 73 | + (inx: Int, res: Array[T]) => results.append(res), |
| 74 | + results.toSeq) |
| 75 | + } |
| 76 | +} |
0 commit comments