|  | 
|  | 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 | 
|  | 19 | + | 
|  | 20 | +/** | 
|  | 21 | + * Low-level status reporting APIs for monitoring job and stage progress. | 
|  | 22 | + * | 
|  | 23 | + * These APIs intentionally provide very weak consistency semantics; consumers of these APIs should | 
|  | 24 | + * be prepared to handle empty / missing information.  For example, a job's stage ids may be known | 
|  | 25 | + * but the status API may not have any information about the details of those stages, so | 
|  | 26 | + * `getStageInfo` could potentially return `None` for a valid stage id. | 
|  | 27 | + * | 
|  | 28 | + * To limit memory usage, these APIs only provide information on recent jobs / stages.  These APIs | 
|  | 29 | + * will provide information for the last `spark.ui.retainedStages` stages and | 
|  | 30 | + * `spark.ui.retainedJobs` jobs. | 
|  | 31 | + * | 
|  | 32 | + * NOTE: this class's constructor should be considered private and may be subject to change. | 
|  | 33 | + */ | 
|  | 34 | +class SparkStatusTracker private[spark] (sc: SparkContext) { | 
|  | 35 | + | 
|  | 36 | +  private val jobProgressListener = sc.jobProgressListener | 
|  | 37 | + | 
|  | 38 | +  /** | 
|  | 39 | +   * Return a list of all known jobs in a particular job group.  If `jobGroup` is `null`, then | 
|  | 40 | +   * returns all known jobs that are not associated with a job group. | 
|  | 41 | +   * | 
|  | 42 | +   * The returned list may contain running, failed, and completed jobs, and may vary across | 
|  | 43 | +   * invocations of this method.  This method does not guarantee the order of the elements in | 
|  | 44 | +   * its result. | 
|  | 45 | +   */ | 
|  | 46 | +  def getJobIdsForGroup(jobGroup: String): Array[Int] = { | 
|  | 47 | +    jobProgressListener.synchronized { | 
|  | 48 | +      val jobData = jobProgressListener.jobIdToData.valuesIterator | 
|  | 49 | +      jobData.filter(_.jobGroup.orNull == jobGroup).map(_.jobId).toArray | 
|  | 50 | +    } | 
|  | 51 | +  } | 
|  | 52 | + | 
|  | 53 | +  /** | 
|  | 54 | +   * Returns an array containing the ids of all active stages. | 
|  | 55 | +   * | 
|  | 56 | +   * This method does not guarantee the order of the elements in its result. | 
|  | 57 | +   */ | 
|  | 58 | +  def getActiveStageIds(): Array[Int] = { | 
|  | 59 | +    jobProgressListener.synchronized { | 
|  | 60 | +      jobProgressListener.activeStages.values.map(_.stageId).toArray | 
|  | 61 | +    } | 
|  | 62 | +  } | 
|  | 63 | + | 
|  | 64 | +  /** | 
|  | 65 | +   * Returns an array containing the ids of all active jobs. | 
|  | 66 | +   * | 
|  | 67 | +   * This method does not guarantee the order of the elements in its result. | 
|  | 68 | +   */ | 
|  | 69 | +  def getActiveJobIds(): Array[Int] = { | 
|  | 70 | +    jobProgressListener.synchronized { | 
|  | 71 | +      jobProgressListener.activeJobs.values.map(_.jobId).toArray | 
|  | 72 | +    } | 
|  | 73 | +  } | 
|  | 74 | + | 
|  | 75 | +  /** | 
|  | 76 | +   * Returns job information, or `None` if the job info could not be found or was garbage collected. | 
|  | 77 | +   */ | 
|  | 78 | +  def getJobInfo(jobId: Int): Option[SparkJobInfo] = { | 
|  | 79 | +    jobProgressListener.synchronized { | 
|  | 80 | +      jobProgressListener.jobIdToData.get(jobId).map { data => | 
|  | 81 | +        new SparkJobInfoImpl(jobId, data.stageIds.toArray, data.status) | 
|  | 82 | +      } | 
|  | 83 | +    } | 
|  | 84 | +  } | 
|  | 85 | + | 
|  | 86 | +  /** | 
|  | 87 | +   * Returns stage information, or `None` if the stage info could not be found or was | 
|  | 88 | +   * garbage collected. | 
|  | 89 | +   */ | 
|  | 90 | +  def getStageInfo(stageId: Int): Option[SparkStageInfo] = { | 
|  | 91 | +    jobProgressListener.synchronized { | 
|  | 92 | +      for ( | 
|  | 93 | +        info <- jobProgressListener.stageIdToInfo.get(stageId); | 
|  | 94 | +        data <- jobProgressListener.stageIdToData.get((stageId, info.attemptId)) | 
|  | 95 | +      ) yield { | 
|  | 96 | +        new SparkStageInfoImpl( | 
|  | 97 | +          stageId, | 
|  | 98 | +          info.attemptId, | 
|  | 99 | +          info.name, | 
|  | 100 | +          info.numTasks, | 
|  | 101 | +          data.numActiveTasks, | 
|  | 102 | +          data.numCompleteTasks, | 
|  | 103 | +          data.numFailedTasks) | 
|  | 104 | +      } | 
|  | 105 | +    } | 
|  | 106 | +  } | 
|  | 107 | +} | 
0 commit comments