Github user pwendell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/290#discussion_r11545788
  
    --- Diff: 
streaming/src/main/scala/org/apache/spark/streaming/ui/StreamingJobProgressListener.scala
 ---
    @@ -0,0 +1,148 @@
    +/*
    + * 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.streaming.ui
    +
    +import org.apache.spark.streaming.{Time, StreamingContext}
    +import org.apache.spark.streaming.scheduler._
    +import scala.collection.mutable.{Queue, HashMap}
    +import 
org.apache.spark.streaming.scheduler.StreamingListenerReceiverStarted
    +import org.apache.spark.streaming.scheduler.StreamingListenerBatchStarted
    +import org.apache.spark.streaming.scheduler.BatchInfo
    +import org.apache.spark.streaming.scheduler.ReceiverInfo
    +import org.apache.spark.streaming.scheduler.StreamingListenerBatchSubmitted
    +import org.apache.spark.util.Distribution
    +
    +
    +private[ui] class StreamingJobProgressListener(ssc: StreamingContext) 
extends StreamingListener {
    +
    +  private val waitingBatchInfos = new HashMap[Time, BatchInfo]
    +  private val runningBatchInfos = new HashMap[Time, BatchInfo]
    +  private val completedaBatchInfos = new Queue[BatchInfo]
    +  private val batchInfoLimit = 
ssc.conf.getInt("spark.steaming.ui.maxBatches", 100)
    +  private var totalCompletedBatches = 0L
    +  private val receiverInfos = new HashMap[Int, ReceiverInfo]
    +
    +  val batchDuration = ssc.graph.batchDuration.milliseconds
    +
    +  override def onReceiverStarted(receiverStarted: 
StreamingListenerReceiverStarted) = {
    +    synchronized {
    +      receiverInfos.put(receiverStarted.receiverInfo.streamId, 
receiverStarted.receiverInfo)
    +    }
    +  }
    +
    +  override def onBatchSubmitted(batchSubmitted: 
StreamingListenerBatchSubmitted) = synchronized {
    +    runningBatchInfos(batchSubmitted.batchInfo.batchTime) = 
batchSubmitted.batchInfo
    +  }
    +
    +  override def onBatchStarted(batchStarted: StreamingListenerBatchStarted) 
= synchronized {
    +    runningBatchInfos(batchStarted.batchInfo.batchTime) = 
batchStarted.batchInfo
    +    waitingBatchInfos.remove(batchStarted.batchInfo.batchTime)
    +  }
    +
    +  override def onBatchCompleted(batchCompleted: 
StreamingListenerBatchCompleted) = synchronized {
    +    waitingBatchInfos.remove(batchCompleted.batchInfo.batchTime)
    +    runningBatchInfos.remove(batchCompleted.batchInfo.batchTime)
    +    completedaBatchInfos.enqueue(batchCompleted.batchInfo)
    +    if (completedaBatchInfos.size > batchInfoLimit) 
completedaBatchInfos.dequeue()
    +    totalCompletedBatches += 1L
    +  }
    +
    +  def numNetworkReceivers = synchronized {
    +    ssc.graph.getNetworkInputStreams().size
    +  }
    +
    +  def numTotalCompletedBatches: Long = synchronized {
    +    totalCompletedBatches
    +  }
    +
    +  def numUnprocessedBatches: Long = synchronized {
    +    waitingBatchInfos.size + runningBatchInfos.size
    +  }
    +
    +  def waitingBatches: Seq[BatchInfo] = synchronized {
    +    waitingBatchInfos.values.toSeq
    +  }
    +
    +  def runningBatches: Seq[BatchInfo] = synchronized {
    +    runningBatchInfos.values.toSeq
    +  }
    +
    +  def completedBatches: Seq[BatchInfo] = synchronized {
    +    completedaBatchInfos.toSeq
    +  }
    +
    +  def processingDelayDistribution: Option[Distribution] = synchronized {
    +    extractDistribution(_.processingDelay)
    +  }
    +
    +  def schedulingDelayDistribution: Option[Distribution] = synchronized {
    +    extractDistribution(_.schedulingDelay)
    +  }
    +
    +  def totalDelayDistribution: Option[Distribution] = synchronized {
    +    extractDistribution(_.totalDelay)
    +  }
    +
    +  def receivedRecordsDistributions: Map[Int, Option[Distribution]] = 
synchronized {
    +    val latestBatchInfos = allBatches.reverse.take(batchInfoLimit)
    +    val latestBlockInfos = latestBatchInfos.map(_.receivedBlockInfo)
    +    (0 until numNetworkReceivers).map { receiverId =>
    +      val blockInfoOfParticularReceiver = latestBlockInfos.map { batchInfo 
=>
    +        batchInfo.get(receiverId).getOrElse(Array.empty)
    +      }
    +      val recordsOfParticularReceiver = blockInfoOfParticularReceiver.map 
{ blockInfo =>
    +      // calculate records per second for each batch
    +        blockInfo.map(_.numRecords).sum.toDouble * 1000 / batchDuration
    +      }
    +      val distributionOption = Distribution(recordsOfParticularReceiver)
    +      (receiverId, distributionOption)
    +    }.toMap
    +  }
    +
    +  def lastReceivedBatchRecords: Map[Int, Long] = {
    +    val lastReceivedBlockInfoOption = 
lastReceivedBatch.map(_.receivedBlockInfo)
    +    lastReceivedBlockInfoOption.map { lastReceivedBlockInfo =>
    +      (0 until numNetworkReceivers).map { receiverId =>
    +        (receiverId, 
lastReceivedBlockInfo(receiverId).map(_.numRecords).sum)
    +      }.toMap
    +    }.getOrElse {
    +      (0 until numNetworkReceivers).map(receiverId => (receiverId, 
0L)).toMap
    +    }
    +  }
    +
    +  def receiverInfo(receiverId: Int): Option[ReceiverInfo] = {
    +    receiverInfos.get(receiverId)
    +  }
    +
    +  def lastCompletedBatch: Option[BatchInfo] = {
    +    completedaBatchInfos.sortBy(_.batchTime)(Time.ordering).lastOption
    +  }
    +
    +  def lastReceivedBatch: Option[BatchInfo] = {
    +    allBatches.lastOption
    +  }
    +
    +  private def allBatches: Seq[BatchInfo] = synchronized {
    --- End diff --
    
    maybe this should be called `retainedBatches`? In some sense this is not 
"all" of the batches because some have been evicted right?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to