|
| 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.scheduler |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | + |
| 22 | +class LoadBalanceReceiverSchedulerImplSuite extends SparkFunSuite { |
| 23 | + |
| 24 | + val receiverScheduler = new LoadBalanceReceiverSchedulerImpl |
| 25 | + |
| 26 | + test("empty executors") { |
| 27 | + val scheduledLocations = |
| 28 | + receiverScheduler.scheduleReceiver(0, None, Map.empty, executors = Seq.empty) |
| 29 | + assert(scheduledLocations === Seq.empty) |
| 30 | + } |
| 31 | + |
| 32 | + test("receiver preferredLocation") { |
| 33 | + val receiverTrackingInfoMap = Map( |
| 34 | + 0 -> ReceiverTrackingInfo(0, ReceiverState.INACTIVE, None, None)) |
| 35 | + val scheduledLocations = receiverScheduler.scheduleReceiver( |
| 36 | + 0, Some("host1"), receiverTrackingInfoMap, executors = Seq("host2")) |
| 37 | + assert(scheduledLocations.toSet === Set("host1", "host2")) |
| 38 | + } |
| 39 | + |
| 40 | + test("choose the idle executor") { |
| 41 | + val executors = Seq("host1", "host2", "host3") |
| 42 | + // host3 is idle |
| 43 | + val receiverTrackingInfoMap = Map( |
| 44 | + 0 -> ReceiverTrackingInfo(0, ReceiverState.ACTIVE, None, Some("host1")), |
| 45 | + 1 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host2")), None)) |
| 46 | + val scheduledLocations = receiverScheduler.scheduleReceiver( |
| 47 | + 2, None, receiverTrackingInfoMap, executors) |
| 48 | + assert(scheduledLocations.toSet === Set("host3")) |
| 49 | + } |
| 50 | + |
| 51 | + test("all executors are busy") { |
| 52 | + val executors = Seq("host1", "host2", "host3") |
| 53 | + // Weights: host1 = 1.5, host2 = 0.5, host3 = 1.0 |
| 54 | + val receiverTrackingInfoMap = Map( |
| 55 | + 0 -> ReceiverTrackingInfo(0, ReceiverState.ACTIVE, None, Some("host1")), |
| 56 | + 1 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host2", "host3")), None), |
| 57 | + 2 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host1", "host3")), None)) |
| 58 | + val scheduledLocations = receiverScheduler.scheduleReceiver( |
| 59 | + 3, None, receiverTrackingInfoMap, executors) |
| 60 | + assert(scheduledLocations.toSet === Set("host2")) |
| 61 | + } |
| 62 | + |
| 63 | + test("ignore the receiver's info") { |
| 64 | + val executors = Seq("host1", "host2", "host3") |
| 65 | + // Weights: host1 = 1.0, host2 = 1.5, host3 = 1.5 |
| 66 | + // But since we are scheduling the receiver 1, we should ignore |
| 67 | + // receiver 1's ReceiverTrackingInfo |
| 68 | + // So the new weights are host1 = 1.0, host2 = 0.5, host3 = 1.5 |
| 69 | + // Then the scheduled location should be "host2" |
| 70 | + val receiverTrackingInfoMap = Map( |
| 71 | + 0 -> ReceiverTrackingInfo(0, ReceiverState.ACTIVE, None, Some("host1")), |
| 72 | + 1 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host2")), None), |
| 73 | + 2 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host3")), None), |
| 74 | + 3 -> ReceiverTrackingInfo(1, ReceiverState.SCHEDULED, Some(Seq("host2", "host3")), None)) |
| 75 | + val scheduledLocations = receiverScheduler.scheduleReceiver( |
| 76 | + 1, None, receiverTrackingInfoMap, executors) |
| 77 | + assert(scheduledLocations.toSet === Set("host2")) |
| 78 | + } |
| 79 | +} |
0 commit comments