|
| 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.streaming.ui |
| 19 | + |
| 20 | +import org.scalatest.Matchers |
| 21 | + |
| 22 | +import org.apache.spark.streaming.dstream.DStream |
| 23 | +import org.apache.spark.streaming.scheduler._ |
| 24 | +import org.apache.spark.streaming.{Time, Milliseconds, TestSuiteBase} |
| 25 | + |
| 26 | +class StreamingJobProgressListenerSuite extends TestSuiteBase with Matchers { |
| 27 | + |
| 28 | + val input = (1 to 4).map(Seq(_)).toSeq |
| 29 | + val operation = (d: DStream[Int]) => d.map(x => x) |
| 30 | + |
| 31 | + override def batchDuration = Milliseconds(100) |
| 32 | + |
| 33 | + test("onBatchSubmitted, onBatchStarted, onBatchCompleted, " + |
| 34 | + "onReceiverStarted, onReceiverError, onReceiverStopped") { |
| 35 | + val ssc = setupStreams(input, operation) |
| 36 | + val listener = new StreamingJobProgressListener(ssc) |
| 37 | + |
| 38 | + val receivedBlockInfo = Map( |
| 39 | + 0 -> Array(ReceivedBlockInfo(0, 100, null), ReceivedBlockInfo(0, 200, null)), |
| 40 | + 1 -> Array(ReceivedBlockInfo(1, 300, null)) |
| 41 | + ) |
| 42 | + |
| 43 | + // onBatchSubmitted |
| 44 | + val batchInfoSubmitted = BatchInfo(Time(1000), receivedBlockInfo, 1000, None, None) |
| 45 | + listener.onBatchSubmitted(StreamingListenerBatchSubmitted(batchInfoSubmitted)) |
| 46 | + listener.waitingBatches should be (List(batchInfoSubmitted)) |
| 47 | + listener.runningBatches should be (Nil) |
| 48 | + listener.retainedCompletedBatches should be (Nil) |
| 49 | + listener.lastCompletedBatch should be (None) |
| 50 | + listener.numUnprocessedBatches should be (1) |
| 51 | + listener.numTotalCompletedBatches should be (0) |
| 52 | + listener.numTotalProcessedRecords should be (0) |
| 53 | + listener.numTotalReceivedRecords should be (0) |
| 54 | + |
| 55 | + // onBatchStarted |
| 56 | + val batchInfoStarted = BatchInfo(Time(1000), receivedBlockInfo, 1000, Some(2000), None) |
| 57 | + listener.onBatchStarted(StreamingListenerBatchStarted(batchInfoStarted)) |
| 58 | + listener.waitingBatches should be (Nil) |
| 59 | + listener.runningBatches should be (List(batchInfoStarted)) |
| 60 | + listener.retainedCompletedBatches should be (Nil) |
| 61 | + listener.lastCompletedBatch should be (None) |
| 62 | + listener.numUnprocessedBatches should be (1) |
| 63 | + listener.numTotalCompletedBatches should be (0) |
| 64 | + listener.numTotalProcessedRecords should be (0) |
| 65 | + listener.numTotalReceivedRecords should be (600) |
| 66 | + |
| 67 | + // onBatchCompleted |
| 68 | + val batchInfoCompleted = BatchInfo(Time(1000), receivedBlockInfo, 1000, Some(2000), None) |
| 69 | + listener.onBatchCompleted(StreamingListenerBatchCompleted(batchInfoCompleted)) |
| 70 | + listener.waitingBatches should be (Nil) |
| 71 | + listener.runningBatches should be (Nil) |
| 72 | + listener.retainedCompletedBatches should be (List(batchInfoCompleted)) |
| 73 | + listener.lastCompletedBatch should be (Some(batchInfoCompleted)) |
| 74 | + listener.numUnprocessedBatches should be (0) |
| 75 | + listener.numTotalCompletedBatches should be (1) |
| 76 | + listener.numTotalProcessedRecords should be (600) |
| 77 | + listener.numTotalReceivedRecords should be (600) |
| 78 | + |
| 79 | + // onReceiverStarted |
| 80 | + val receiverInfoStarted = ReceiverInfo(0, "test", null, true, "localhost") |
| 81 | + listener.onReceiverStarted(StreamingListenerReceiverStarted(receiverInfoStarted)) |
| 82 | + listener.receiverInfo(0) should be (Some(receiverInfoStarted)) |
| 83 | + listener.receiverInfo(1) should be (None) |
| 84 | + |
| 85 | + // onReceiverError |
| 86 | + val receiverInfoError = ReceiverInfo(1, "test", null, true, "localhost") |
| 87 | + listener.onReceiverError(StreamingListenerReceiverError(receiverInfoError)) |
| 88 | + listener.receiverInfo(0) should be (Some(receiverInfoStarted)) |
| 89 | + listener.receiverInfo(1) should be (Some(receiverInfoError)) |
| 90 | + listener.receiverInfo(2) should be (None) |
| 91 | + |
| 92 | + // onReceiverStopped |
| 93 | + val receiverInfoStopped = ReceiverInfo(2, "test", null, true, "localhost") |
| 94 | + listener.onReceiverStopped(StreamingListenerReceiverStopped(receiverInfoStopped)) |
| 95 | + listener.receiverInfo(0) should be (Some(receiverInfoStarted)) |
| 96 | + listener.receiverInfo(1) should be (Some(receiverInfoError)) |
| 97 | + listener.receiverInfo(2) should be (Some(receiverInfoStopped)) |
| 98 | + listener.receiverInfo(3) should be (None) |
| 99 | + } |
| 100 | + |
| 101 | + test("Remove the old completed batches when exceeding the limit") { |
| 102 | + val ssc = setupStreams(input, operation) |
| 103 | + val limit = ssc.conf.getInt("spark.streaming.ui.retainedBatches", 100) |
| 104 | + val listener = new StreamingJobProgressListener(ssc) |
| 105 | + |
| 106 | + val receivedBlockInfo = Map( |
| 107 | + 0 -> Array(ReceivedBlockInfo(0, 100, null), ReceivedBlockInfo(0, 200, null)), |
| 108 | + 1 -> Array(ReceivedBlockInfo(1, 300, null)) |
| 109 | + ) |
| 110 | + val batchInfoCompleted = BatchInfo(Time(1000), receivedBlockInfo, 1000, Some(2000), None) |
| 111 | + |
| 112 | + for(_ <- 0 until (limit + 10)) { |
| 113 | + listener.onBatchCompleted(StreamingListenerBatchCompleted(batchInfoCompleted)) |
| 114 | + } |
| 115 | + |
| 116 | + listener.retainedCompletedBatches.size should be (limit) |
| 117 | + listener.numTotalCompletedBatches should be(limit + 10) |
| 118 | + } |
| 119 | +} |
0 commit comments