Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-29779][CORE] Compact old event log files and cleanup #26416

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c65d6ea
WIP [SPARK-29779][CORE] Compact old event log files and cleanup - no …
HeartSaVioR Nov 2, 2019
bb485c7
Reflect review comments
HeartSaVioR Nov 10, 2019
87404de
Address FIXMEs
HeartSaVioR Nov 11, 2019
2944416
Add UTs for BasicEventFilter/BasicEventFilterBuilder
HeartSaVioR Nov 11, 2019
916a458
Address UTs for SQL event filter (builder)
HeartSaVioR Nov 13, 2019
0729a41
Address some more tests - leave new TODO/FIXME for tests
HeartSaVioR Nov 13, 2019
316bc17
Address review comments
HeartSaVioR Nov 13, 2019
b8a4bc1
Review comment & fix the test
HeartSaVioR Nov 13, 2019
1106bcb
Add more tests - TODO for tests addressed
HeartSaVioR Nov 14, 2019
5bd4274
Fix UT (silly mistake)
HeartSaVioR Nov 14, 2019
45e828e
Address scaladoc
HeartSaVioR Nov 14, 2019
f64388a
Address documentation
HeartSaVioR Nov 14, 2019
4559c0d
Clean up codebase a bit
HeartSaVioR Nov 14, 2019
a3b7b1f
Reflect review comments
HeartSaVioR Nov 15, 2019
a3a0c4a
More comments reflection
HeartSaVioR Nov 15, 2019
7054b2c
Fix indentation
HeartSaVioR Nov 17, 2019
5a54af9
Introduce the rate of filter to determine whether the event log for a…
HeartSaVioR Nov 25, 2019
82e268a
Clean a bit
HeartSaVioR Nov 27, 2019
e5d9250
Remove invalid comments, strengthen doc
HeartSaVioR Nov 28, 2019
b53aaf2
Address review comments
HeartSaVioR Dec 9, 2019
ef9f331
Apply PartialFunction to EventFilter
HeartSaVioR Dec 9, 2019
ed2b1e2
Refine comments
HeartSaVioR Dec 9, 2019
7db7e78
Address review comments
HeartSaVioR Dec 10, 2019
3286f9d
Reflect review comments partially
HeartSaVioR Dec 13, 2019
ab5d233
Add missed one
HeartSaVioR Dec 13, 2019
27244f3
Inline FilterRateCalculator into EventLogFileCompactor, reflect chang…
HeartSaVioR Dec 16, 2019
872ffbc
Merge branch 'master' into SPARK-29779-merge-master
HeartSaVioR Dec 17, 2019
761833b
Address incremental load on constructing EventFilterBuilder
HeartSaVioR Dec 23, 2019
e1a6e42
Reset EventFilterBuildersLoader for any exceptions
HeartSaVioR Dec 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.spark.deploy.history.BasicEventFilterBuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* 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.FilterStatistic
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, Seq[Int]]
private val _stageToTasks = new mutable.HashMap[Int, mutable.Set[Long]]
private val _stageToRDDs = new mutable.HashMap[Int, Seq[Int]]
private val _liveExecutors = new mutable.HashSet[String]

private var totalJobs: Long = 0L
private var totalStages: Long = 0L
private var totalTasks: Long = 0L

def liveJobToStages: Map[Int, Seq[Int]] = _liveJobToStages.toMap
def stageToTasks: Map[Int, Set[Long]] = _stageToTasks.mapValues(_.toSet).toMap
def stageToRDDs: Map[Int, Seq[Int]] = _stageToRDDs.toMap
def liveExecutors: Set[String] = _liveExecutors.toSet

override def onJobStart(jobStart: SparkListenerJobStart): Unit = {
totalJobs += 1
totalStages += jobStart.stageIds.length
_liveJobToStages += jobStart.jobId -> jobStart.stageIds
}

override def onJobEnd(jobEnd: SparkListenerJobEnd): Unit = {
val stages = _liveJobToStages.getOrElse(jobEnd.jobId, Seq.empty[Int])
_liveJobToStages -= jobEnd.jobId
_stageToTasks --= stages
_stageToRDDs --= stages
}

override def onStageSubmitted(stageSubmitted: SparkListenerStageSubmitted): Unit = {
_stageToRDDs.getOrElseUpdate(stageSubmitted.stageInfo.stageId,
stageSubmitted.stageInfo.rddInfos.map(_.id))
}

override def onTaskStart(taskStart: SparkListenerTaskStart): Unit = {
totalTasks += 1
val curTasks = _stageToTasks.getOrElseUpdate(taskStart.stageId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that task end events can arrive after a stage end; but just in case the start event also can, maybe it's safer to check if the stage is really active (by checking _liveJobToStages?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I'm not sure how much the events can be out of order. The code assumes job start event will be placed earlier than any stage/task events in the job, and job end event will be placed later than any stage/task events in the job. If that's not true, may need to revisit the whole builder logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

job end event will be placed later than any stage/task events

I'm sure that's not true. You can see in AppStatusListener that onTaskEnd has logic to clean up stuff if the event arrives after a job / stage end.

I'm actually going further here, and saying you should double check that the stage is active when a start event arrives, or maybe change the way you're tracking things here if being exact is important.

mutable.HashSet[Long]())
curTasks += taskStart.taskInfo.taskId
}

override def onExecutorAdded(executorAdded: SparkListenerExecutorAdded): Unit = {
_liveExecutors += executorAdded.executorId
}

override def onExecutorRemoved(executorRemoved: SparkListenerExecutorRemoved): Unit = {
_liveExecutors -= executorRemoved.executorId
}

override def createFilter(): EventFilter = new BasicEventFilter(this)

def statistic(): FilterStatistic = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statistics (also in the type name)

FilterStatistic(totalJobs, liveJobToStages.size, totalStages,
liveJobToStages.map(_._2.size).sum, totalTasks, _stageToTasks.map(_._2.size).sum)
}
}

/**
* This class provides the functionality to reject events which are related to the finished
* jobs based on the given information. This class only deals with job related events, and provides
* a PartialFunction which returns false for rejected events for finished jobs, returns true
* otherwise.
*/
private[spark] abstract class JobEventFilter(
stats: Option[FilterStatistic],
jobToStages: Map[Int, Seq[Int]],
stageToTasks: Map[Int, Set[Long]],
stageToRDDs: Map[Int, Seq[Int]]) extends EventFilter with Logging {

private val liveTasks: Set[Long] = stageToTasks.values.flatten.toSet
private val liveRDDs: Set[Int] = stageToRDDs.values.flatten.toSet

logDebug(s"jobs : ${jobToStages.keySet}")
logDebug(s"stages in jobs : ${jobToStages.values.flatten}")
logDebug(s"stages : ${stageToTasks.keySet}")
logDebug(s"tasks in stages : ${stageToTasks.values.flatten}")
logDebug(s"RDDs in stages : ${stageToRDDs.values.flatten}")

override def statistic(): Option[FilterStatistic] = stats

protected val acceptFnForJobEvents: PartialFunction[SparkListenerEvent, Boolean] = {
case e: SparkListenerStageCompleted =>
stageToTasks.contains(e.stageInfo.stageId)

case e: SparkListenerStageSubmitted =>
stageToTasks.contains(e.stageInfo.stageId)

case e: SparkListenerTaskStart =>
liveTasks.contains(e.taskInfo.taskId)

case e: SparkListenerTaskGettingResult =>
liveTasks.contains(e.taskInfo.taskId)

case e: SparkListenerTaskEnd =>
liveTasks.contains(e.taskInfo.taskId)

case e: SparkListenerJobStart =>
jobToStages.contains(e.jobId)

case e: SparkListenerJobEnd =>
jobToStages.contains(e.jobId)

case e: SparkListenerUnpersistRDD =>
liveRDDs.contains(e.rddId)

case e: SparkListenerExecutorMetricsUpdate =>
e.accumUpdates.exists { case (_, stageId, _, _) =>
stageToTasks.contains(stageId)
}

case e: SparkListenerSpeculativeTaskSubmitted =>
stageToTasks.contains(e.stageId)
}
}

/**
* This class rejects events which are related to the finished jobs or dead executors,
* based on the given information. The events which are not related to the job and executor
* will be considered as "Don't mind".
*/
private[spark] class BasicEventFilter(
_stats: FilterStatistic,
_liveJobToStages: Map[Int, Seq[Int]],
_stageToTasks: Map[Int, Set[Long]],
_stageToRDDs: Map[Int, Seq[Int]],
liveExecutors: Set[String])
extends JobEventFilter(Some(_stats), _liveJobToStages, _stageToTasks, _stageToRDDs) with Logging {

def this(builder: BasicEventFilterBuilder) = {
this(builder.statistic(), builder.liveJobToStages, builder.stageToTasks, builder.stageToRDDs,
builder.liveExecutors)
}

logDebug(s"live executors : $liveExecutors")

private val _acceptFn: PartialFunction[SparkListenerEvent, Boolean] = {
case e: SparkListenerExecutorAdded => liveExecutors.contains(e.executorId)
case e: SparkListenerExecutorRemoved => liveExecutors.contains(e.executorId)
case e: SparkListenerExecutorBlacklisted => liveExecutors.contains(e.executorId)
case e: SparkListenerExecutorUnblacklisted => liveExecutors.contains(e.executorId)
case e: SparkListenerStageExecutorMetrics => liveExecutors.contains(e.execId)
}

override def acceptFn(): PartialFunction[SparkListenerEvent, Boolean] = {
_acceptFn.orElse(acceptFnForJobEvents)
}
}
111 changes: 111 additions & 0 deletions core/src/main/scala/org/apache/spark/deploy/history/EventFilter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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 java.util.ServiceLoader

import scala.io.{Codec, Source}
import scala.util.control.NonFatal

import org.apache.hadoop.fs.{FileSystem, Path}
import org.json4s.jackson.JsonMethods.parse

import org.apache.spark.deploy.history.EventFilter.FilterStatistic
import org.apache.spark.internal.Logging
import org.apache.spark.scheduler._
import org.apache.spark.util.{JsonProtocol, Utils}

/**
* EventFilterBuilder provides the interface to gather the information from events being received
* by [[SparkListenerInterface]], and create a new [[EventFilter]] instance which leverages
* information gathered to decide whether the event should be accepted or not.
*/
private[spark] trait EventFilterBuilder extends SparkListenerInterface {
def createFilter(): EventFilter
}

/** [[EventFilter]] decides whether the given event should be accepted or rejected. */
private[spark] trait EventFilter {
/**
* Provide statistic information of event filter, which would be used for measuring the score
* of compaction.
*
* To simplify the condition, currently the fields of statistic are static, since major kinds of
* events compaction would filter out are job related event types. If the filter doesn't track
* with job related events, return None instead.
*/
def statistic(): Option[FilterStatistic]

/**
* Classify whether the event is accepted or rejected by this filter.
*
* The method should return the partial function which matches the events where the filter can
* decide whether the event should be accepted or rejected. Otherwise it should leave the events
* be unmatched.
*/
def acceptFn(): PartialFunction[SparkListenerEvent, Boolean]
}

object EventFilter extends Logging {
case class FilterStatistic(
totalJobs: Long,
liveJobs: Long,
totalStages: Long,
liveStages: Long,
totalTasks: Long,
liveTasks: Long)

def applyFilterToFile(
fs: FileSystem,
filters: Seq[EventFilter],
path: Path,
onAccepted: (String, SparkListenerEvent) => Unit,
onRejected: (String, SparkListenerEvent) => Unit,
onUnidentified: String => Unit): Unit = {
Utils.tryWithResource(EventLogFileReader.openEventLog(path, fs)) { in =>
val lines = Source.fromInputStream(in)(Codec.UTF8).getLines()

lines.zipWithIndex.foreach { case (line, lineNum) =>
try {
val event = try {
Some(JsonProtocol.sparkEventFromJson(parse(line)))
} catch {
// ignore any exception occurred from unidentified json
case NonFatal(_) =>
onUnidentified(line)
None
}

event.foreach { e =>
val results = filters.flatMap(_.acceptFn().lift.apply(e))
if (results.isEmpty || !results.contains(false)) {
onAccepted(line, e)
} else {
onRejected(line, e)
}
}
} catch {
case e: Exception =>
logError(s"Exception parsing Spark event log: ${path.getName}", e)
logError(s"Malformed line #$lineNum: $line\n")
throw e
}
}
}
}
}
Loading