aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/spark/ui/UIWorkloadGenerator.scala
blob: 8bbc6ce88ea8c29ab9c3ea9794772230e3e2123d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package spark.ui

import scala.util.Random

import spark.SparkContext
import spark.SparkContext._

/**
 * Continuously generates jobs that expose various features of the WebUI (internal testing tool).
 *
 * Usage: ./run spark.ui.UIWorkloadGenerator [master]
 */
private[spark] object UIWorkloadGenerator {
  val NUM_PARTITIONS = 100
  val INTER_JOB_WAIT_MS = 500

  def main(args: Array[String]) {
    val master = args(0)
    val appName = "Spark UI Tester"
    val sc = new SparkContext(master, appName)

    // NOTE: Right now there is no easy way for us to show spark.job.annotation for a given phase,
    //       but we pass it here anyways since it will be useful once we do.
    def setName(s: String) = {
      sc.addLocalProperties("spark.job.annotation", s)
    }
    val baseData = sc.makeRDD(1 to NUM_PARTITIONS * 10, NUM_PARTITIONS)
    def nextFloat() = (new Random()).nextFloat()

    val jobs = Seq[(String, () => Long)](
      ("Count", baseData.count),
      ("Cache and Count", baseData.map(x => x).cache.count),
      ("Single Shuffle", baseData.map(x => (x % 10, x)).reduceByKey(_ + _).count),
      ("Entirely failed phase", baseData.map(x => throw new Exception).count),
      ("Partially failed phase", {
        baseData.map{x =>
          val probFailure = (4.0 / NUM_PARTITIONS)
          if (nextFloat() < probFailure) {
            throw new Exception("This is a task failure")
          }
          1
        }.count
      }),
      ("Partially failed phase (longer tasks)", {
        baseData.map{x =>
          val probFailure = (4.0 / NUM_PARTITIONS)
          if (nextFloat() < probFailure) {
            Thread.sleep(100)
            throw new Exception("This is a task failure")
          }
          1
        }.count
      }),
      ("Job with delays", baseData.map(x => Thread.sleep(100)).count)
    )

    while (true) {
      for ((desc, job) <- jobs) {
        try {
          setName(desc)
          job()
          println("Job funished: " + desc)
        } catch {
          case e: Exception =>
            println("Job Failed: " + desc)
        }
        Thread.sleep(INTER_JOB_WAIT_MS)
      }
    }
  }
}