|
| 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.ui.scope |
| 19 | + |
| 20 | +import org.scalatest.FunSuite |
| 21 | + |
| 22 | +import org.apache.spark.SparkConf |
| 23 | +import org.apache.spark.scheduler.{SparkListenerJobStart, SparkListenerStageSubmitted, StageInfo} |
| 24 | + |
| 25 | +class RDDOperationGraphListenerSuite extends FunSuite { |
| 26 | + private var jobIdCounter = 0 |
| 27 | + private var stageIdCounter = 0 |
| 28 | + |
| 29 | + /** Run a job with the specified number of stages. */ |
| 30 | + private def runOneJob(numStages: Int, listener: RDDOperationGraphListener): Unit = { |
| 31 | + assert(numStages > 0, "I will not run a job with 0 stages for you.") |
| 32 | + val stageInfos = (0 until numStages).map { _ => |
| 33 | + val stageInfo = new StageInfo(stageIdCounter, 0, "s", 0, Seq.empty, Seq.empty, "d") |
| 34 | + listener.onStageSubmitted(new SparkListenerStageSubmitted(stageInfo)) |
| 35 | + stageIdCounter += 1 |
| 36 | + stageInfo |
| 37 | + } |
| 38 | + listener.onJobStart(new SparkListenerJobStart(jobIdCounter, 0, stageInfos)) |
| 39 | + jobIdCounter += 1 |
| 40 | + } |
| 41 | + |
| 42 | + test("listener cleans up metadata") { |
| 43 | + |
| 44 | + val conf = new SparkConf() |
| 45 | + .set("spark.ui.retainedStages", "10") |
| 46 | + .set("spark.ui.retainedJobs", "10") |
| 47 | + |
| 48 | + val listener = new RDDOperationGraphListener(conf) |
| 49 | + assert(listener.jobIdToStageIds.isEmpty) |
| 50 | + assert(listener.stageIdToGraph.isEmpty) |
| 51 | + assert(listener.jobIds.isEmpty) |
| 52 | + assert(listener.stageIds.isEmpty) |
| 53 | + |
| 54 | + // Run a few jobs, but not enough for clean up yet |
| 55 | + runOneJob(1, listener) |
| 56 | + runOneJob(2, listener) |
| 57 | + runOneJob(3, listener) |
| 58 | + assert(listener.jobIdToStageIds.size === 3) |
| 59 | + assert(listener.stageIdToGraph.size === 6) |
| 60 | + assert(listener.jobIds.size === 3) |
| 61 | + assert(listener.stageIds.size === 6) |
| 62 | + |
| 63 | + // Run a few more, but this time the stages should be cleaned up, but not the jobs |
| 64 | + runOneJob(5, listener) |
| 65 | + runOneJob(100, listener) |
| 66 | + assert(listener.jobIdToStageIds.size === 5) |
| 67 | + assert(listener.stageIdToGraph.size === 9) |
| 68 | + assert(listener.jobIds.size === 5) |
| 69 | + assert(listener.stageIds.size === 9) |
| 70 | + |
| 71 | + // Run a few more, but this time both jobs and stages should be cleaned up |
| 72 | + (1 to 100).foreach { _ => |
| 73 | + runOneJob(1, listener) |
| 74 | + } |
| 75 | + assert(listener.jobIdToStageIds.size === 9) |
| 76 | + assert(listener.stageIdToGraph.size === 9) |
| 77 | + assert(listener.jobIds.size === 9) |
| 78 | + assert(listener.stageIds.size === 9) |
| 79 | + |
| 80 | + // Ensure we clean up old jobs and stages, not arbitrary ones |
| 81 | + assert(!listener.jobIdToStageIds.contains(0)) |
| 82 | + assert(!listener.stageIdToGraph.contains(0)) |
| 83 | + assert(!listener.stageIds.contains(0)) |
| 84 | + assert(!listener.jobIds.contains(0)) |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments