vanzin commented on a change in pull request #27085: [SPARK-29779][CORE] 
Compact old event log files and cleanup
URL: https://github.com/apache/spark/pull/27085#discussion_r363424927
 
 

 ##########
 File path: 
core/src/main/scala/org/apache/spark/deploy/history/BasicEventFilterBuilder.scala
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.deploy.history
+
+import scala.collection.mutable
+
+import org.apache.spark.deploy.history.EventFilter.FilterStatistics
+import org.apache.spark.internal.Logging
+import org.apache.spark.scheduler._
+
+/**
+ * This class tracks both live jobs and live executors, and pass the list to 
the
+ * [[BasicEventFilter]] to help BasicEventFilter to reject finished jobs (+ 
stages/tasks/RDDs)
+ * and dead executors.
+ */
+private[spark] class BasicEventFilterBuilder extends SparkListener with 
EventFilterBuilder {
+  private val _liveJobToStages = new mutable.HashMap[Int, Set[Int]]
+  private val _stageToTasks = new mutable.HashMap[Int, mutable.Set[Long]]
+  private val _stageToRDDs = new mutable.HashMap[Int, Set[Int]]
+  private val _liveExecutors = new mutable.HashSet[String]
+
+  private var totalJobs: Long = 0L
+  private var totalStages: Long = 0L
+  private var totalTasks: Long = 0L
+
+  def liveJobs: Set[Int] = _liveJobToStages.keySet.toSet
+  def liveStages: Set[Int] = _stageToRDDs.keySet.toSet
+  def liveTasks: Set[Long] = _stageToTasks.values.flatten.toSet
+  def liveRDDs: Set[Int] = _stageToRDDs.values.flatten.toSet
+  def liveExecutors: Set[String] = _liveExecutors.toSet
+
+  override def onJobStart(jobStart: SparkListenerJobStart): Unit = {
+    totalJobs += 1
+    jobStart.stageIds.foreach { stageId =>
+      if (_stageToRDDs.get(stageId).isEmpty) {
+        // stage submit event is not received yet
+        totalStages += 1
+        _stageToRDDs.put(stageId, Set.empty[Int])
+      }
+    }
+    _liveJobToStages += jobStart.jobId -> jobStart.stageIds.toSet
+  }
+
+  override def onJobEnd(jobEnd: SparkListenerJobEnd): Unit = {
+    val stages = _liveJobToStages.getOrElse(jobEnd.jobId, Seq.empty[Int])
+    _liveJobToStages -= jobEnd.jobId
+    // This might leave some stages and tasks if job end event comes earlier 
than stage submitted
+    // or task start event; it's not accurate but safer than dropping wrong 
events which cannot be
+    // restored.
+    _stageToTasks --= stages
+    _stageToRDDs --= stages
+  }
+
+  override def onStageSubmitted(stageSubmitted: SparkListenerStageSubmitted): 
Unit = {
+    val stageId = stageSubmitted.stageInfo.stageId
+    if (_stageToRDDs.get(stageId).isEmpty) {
+      // job start event is not received yet
 
 Review comment:
   The order of job/stage/task events is defined by the DAGScheduler. Adding 
comments in `AppStatusListener` would just be trying to document the scheduler 
behavior, so if you want to track that, it would be better to document the 
scheduler itself. When you add other subsystems (like SQL), then you also add 
other things that may arrive out of order (see how the SQL listener needs to 
handle job / execution events).
   
   On the scheduler side, I think most of the out of order issues are related 
to tasks. I'm somewhat confident that job and stage events generally arrive in 
order, but tasks can be very off. e.g. see this comment in `handleBeginEvent`:
   
   ```
       // Note that there is a chance that this task is launched after the 
stage is cancelled.
       // In that case, we wouldn't have the stage anymore in stageIdToStage.
   ```
   
   I guess my reply here applies more to my `onTaskStart` comment below; here 
we can assume that the events are in order. But there, it would be better to 
ignore events if they arrive at the wrong time.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to