|
| 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.shuffle.unsafe |
| 19 | + |
| 20 | +import org.mockito.Mockito._ |
| 21 | +import org.mockito.invocation.InvocationOnMock |
| 22 | +import org.mockito.stubbing.Answer |
| 23 | +import org.scalatest.{FunSuite, Matchers} |
| 24 | + |
| 25 | +import org.apache.spark._ |
| 26 | +import org.apache.spark.serializer.{JavaSerializer, KryoSerializer, Serializer} |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for the fallback logic in UnsafeShuffleManager. Actual tests of shuffling data are |
| 30 | + * performed in other suites. |
| 31 | + */ |
| 32 | +class UnsafeShuffleManagerSuite extends FunSuite with Matchers { |
| 33 | + |
| 34 | + import UnsafeShuffleManager.canUseUnsafeShuffle |
| 35 | + |
| 36 | + private class RuntimeExceptionAnswer extends Answer[Object] { |
| 37 | + override def answer(invocation: InvocationOnMock): Object = { |
| 38 | + throw new RuntimeException("Called non-stubbed method, " + invocation.getMethod.getName) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private def shuffleDep( |
| 43 | + partitioner: Partitioner, |
| 44 | + serializer: Option[Serializer], |
| 45 | + keyOrdering: Option[Ordering[Any]], |
| 46 | + aggregator: Option[Aggregator[Any, Any, Any]], |
| 47 | + mapSideCombine: Boolean): ShuffleDependency[Any, Any, Any] = { |
| 48 | + val dep = mock(classOf[ShuffleDependency[Any, Any, Any]], new RuntimeExceptionAnswer()) |
| 49 | + doReturn(0).when(dep).shuffleId |
| 50 | + doReturn(partitioner).when(dep).partitioner |
| 51 | + doReturn(serializer).when(dep).serializer |
| 52 | + doReturn(keyOrdering).when(dep).keyOrdering |
| 53 | + doReturn(aggregator).when(dep).aggregator |
| 54 | + doReturn(mapSideCombine).when(dep).mapSideCombine |
| 55 | + dep |
| 56 | + } |
| 57 | + |
| 58 | + test("supported shuffle dependencies") { |
| 59 | + val kryo = Some(new KryoSerializer(new SparkConf())) |
| 60 | + |
| 61 | + assert(canUseUnsafeShuffle(shuffleDep( |
| 62 | + partitioner = new HashPartitioner(2), |
| 63 | + serializer = kryo, |
| 64 | + keyOrdering = None, |
| 65 | + aggregator = None, |
| 66 | + mapSideCombine = false |
| 67 | + ))) |
| 68 | + |
| 69 | + val rangePartitioner = mock(classOf[RangePartitioner[Any, Any]]) |
| 70 | + when(rangePartitioner.numPartitions).thenReturn(2) |
| 71 | + assert(canUseUnsafeShuffle(shuffleDep( |
| 72 | + partitioner = rangePartitioner, |
| 73 | + serializer = kryo, |
| 74 | + keyOrdering = None, |
| 75 | + aggregator = None, |
| 76 | + mapSideCombine = false |
| 77 | + ))) |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + test("unsupported shuffle dependencies") { |
| 82 | + val kryo = Some(new KryoSerializer(new SparkConf())) |
| 83 | + val java = Some(new JavaSerializer(new SparkConf())) |
| 84 | + |
| 85 | + // We only support serializers that support object relocation |
| 86 | + assert(!canUseUnsafeShuffle(shuffleDep( |
| 87 | + partitioner = new HashPartitioner(2), |
| 88 | + serializer = java, |
| 89 | + keyOrdering = None, |
| 90 | + aggregator = None, |
| 91 | + mapSideCombine = false |
| 92 | + ))) |
| 93 | + |
| 94 | + // We do not support shuffles with more than 16 million output partitions |
| 95 | + assert(!canUseUnsafeShuffle(shuffleDep( |
| 96 | + partitioner = new HashPartitioner(PackedRecordPointer.MAXIMUM_PARTITION_ID + 1), |
| 97 | + serializer = kryo, |
| 98 | + keyOrdering = None, |
| 99 | + aggregator = None, |
| 100 | + mapSideCombine = false |
| 101 | + ))) |
| 102 | + |
| 103 | + // We do not support shuffles that perform any kind of aggregation or sorting of keys |
| 104 | + assert(!canUseUnsafeShuffle(shuffleDep( |
| 105 | + partitioner = new HashPartitioner(2), |
| 106 | + serializer = kryo, |
| 107 | + keyOrdering = Some(mock(classOf[Ordering[Any]])), |
| 108 | + aggregator = None, |
| 109 | + mapSideCombine = false |
| 110 | + ))) |
| 111 | + assert(!canUseUnsafeShuffle(shuffleDep( |
| 112 | + partitioner = new HashPartitioner(2), |
| 113 | + serializer = kryo, |
| 114 | + keyOrdering = None, |
| 115 | + aggregator = Some(mock(classOf[Aggregator[Any, Any, Any]])), |
| 116 | + mapSideCombine = false |
| 117 | + ))) |
| 118 | + // We do not support shuffles that perform any kind of aggregation or sorting of keys |
| 119 | + assert(!canUseUnsafeShuffle(shuffleDep( |
| 120 | + partitioner = new HashPartitioner(2), |
| 121 | + serializer = kryo, |
| 122 | + keyOrdering = Some(mock(classOf[Ordering[Any]])), |
| 123 | + aggregator = Some(mock(classOf[Aggregator[Any, Any, Any]])), |
| 124 | + mapSideCombine = true |
| 125 | + ))) |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments