|
| 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.broadcast |
| 19 | + |
| 20 | +import java.io._ |
| 21 | + |
| 22 | +import scala.math |
| 23 | +import scala.util.Random |
| 24 | + |
| 25 | +import org.apache.spark._ |
| 26 | +import org.apache.spark.storage.{BroadcastBlockId, BroadcastHelperBlockId, StorageLevel} |
| 27 | +import org.apache.spark.util.Utils |
| 28 | + |
| 29 | + |
| 30 | +private[spark] class TorrentBroadcast[T](@transient var value_ : T, isLocal: Boolean, id: Long) |
| 31 | +extends Broadcast[T](id) with Logging with Serializable { |
| 32 | + |
| 33 | + def value = value_ |
| 34 | + |
| 35 | + def broadcastId = BroadcastBlockId(id) |
| 36 | + |
| 37 | + TorrentBroadcast.synchronized { |
| 38 | + SparkEnv.get.blockManager.putSingle(broadcastId, value_, StorageLevel.MEMORY_AND_DISK, false) |
| 39 | + } |
| 40 | + |
| 41 | + @transient var arrayOfBlocks: Array[TorrentBlock] = null |
| 42 | + @transient var totalBlocks = -1 |
| 43 | + @transient var totalBytes = -1 |
| 44 | + @transient var hasBlocks = 0 |
| 45 | + |
| 46 | + if (!isLocal) { |
| 47 | + sendBroadcast() |
| 48 | + } |
| 49 | + |
| 50 | + def sendBroadcast() { |
| 51 | + var tInfo = TorrentBroadcast.blockifyObject(value_) |
| 52 | + |
| 53 | + totalBlocks = tInfo.totalBlocks |
| 54 | + totalBytes = tInfo.totalBytes |
| 55 | + hasBlocks = tInfo.totalBlocks |
| 56 | + |
| 57 | + // Store meta-info |
| 58 | + val metaId = BroadcastHelperBlockId(broadcastId, "meta") |
| 59 | + val metaInfo = TorrentInfo(null, totalBlocks, totalBytes) |
| 60 | + TorrentBroadcast.synchronized { |
| 61 | + SparkEnv.get.blockManager.putSingle( |
| 62 | + metaId, metaInfo, StorageLevel.MEMORY_AND_DISK, true) |
| 63 | + } |
| 64 | + |
| 65 | + // Store individual pieces |
| 66 | + for (i <- 0 until totalBlocks) { |
| 67 | + val pieceId = BroadcastHelperBlockId(broadcastId, "piece" + i) |
| 68 | + TorrentBroadcast.synchronized { |
| 69 | + SparkEnv.get.blockManager.putSingle( |
| 70 | + pieceId, tInfo.arrayOfBlocks(i), StorageLevel.MEMORY_AND_DISK, true) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Called by JVM when deserializing an object |
| 76 | + private def readObject(in: ObjectInputStream) { |
| 77 | + in.defaultReadObject() |
| 78 | + TorrentBroadcast.synchronized { |
| 79 | + SparkEnv.get.blockManager.getSingle(broadcastId) match { |
| 80 | + case Some(x) => |
| 81 | + value_ = x.asInstanceOf[T] |
| 82 | + |
| 83 | + case None => |
| 84 | + val start = System.nanoTime |
| 85 | + logInfo("Started reading broadcast variable " + id) |
| 86 | + |
| 87 | + // Initialize @transient variables that will receive garbage values from the master. |
| 88 | + resetWorkerVariables() |
| 89 | + |
| 90 | + if (receiveBroadcast(id)) { |
| 91 | + value_ = TorrentBroadcast.unBlockifyObject[T](arrayOfBlocks, totalBytes, totalBlocks) |
| 92 | + |
| 93 | + // Store the merged copy in cache so that the next worker doesn't need to rebuild it. |
| 94 | + // This creates a tradeoff between memory usage and latency. |
| 95 | + // Storing copy doubles the memory footprint; not storing doubles deserialization cost. |
| 96 | + SparkEnv.get.blockManager.putSingle( |
| 97 | + broadcastId, value_, StorageLevel.MEMORY_AND_DISK, false) |
| 98 | + |
| 99 | + // Remove arrayOfBlocks from memory once value_ is on local cache |
| 100 | + resetWorkerVariables() |
| 101 | + } else { |
| 102 | + logError("Reading broadcast variable " + id + " failed") |
| 103 | + } |
| 104 | + |
| 105 | + val time = (System.nanoTime - start) / 1e9 |
| 106 | + logInfo("Reading broadcast variable " + id + " took " + time + " s") |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private def resetWorkerVariables() { |
| 112 | + arrayOfBlocks = null |
| 113 | + totalBytes = -1 |
| 114 | + totalBlocks = -1 |
| 115 | + hasBlocks = 0 |
| 116 | + } |
| 117 | + |
| 118 | + def receiveBroadcast(variableID: Long): Boolean = { |
| 119 | + // Receive meta-info |
| 120 | + val metaId = BroadcastHelperBlockId(broadcastId, "meta") |
| 121 | + var attemptId = 10 |
| 122 | + while (attemptId > 0 && totalBlocks == -1) { |
| 123 | + TorrentBroadcast.synchronized { |
| 124 | + SparkEnv.get.blockManager.getSingle(metaId) match { |
| 125 | + case Some(x) => |
| 126 | + val tInfo = x.asInstanceOf[TorrentInfo] |
| 127 | + totalBlocks = tInfo.totalBlocks |
| 128 | + totalBytes = tInfo.totalBytes |
| 129 | + arrayOfBlocks = new Array[TorrentBlock](totalBlocks) |
| 130 | + hasBlocks = 0 |
| 131 | + |
| 132 | + case None => |
| 133 | + Thread.sleep(500) |
| 134 | + } |
| 135 | + } |
| 136 | + attemptId -= 1 |
| 137 | + } |
| 138 | + if (totalBlocks == -1) { |
| 139 | + return false |
| 140 | + } |
| 141 | + |
| 142 | + // Receive actual blocks |
| 143 | + val recvOrder = new Random().shuffle(Array.iterate(0, totalBlocks)(_ + 1).toList) |
| 144 | + for (pid <- recvOrder) { |
| 145 | + val pieceId = BroadcastHelperBlockId(broadcastId, "piece" + pid) |
| 146 | + TorrentBroadcast.synchronized { |
| 147 | + SparkEnv.get.blockManager.getSingle(pieceId) match { |
| 148 | + case Some(x) => |
| 149 | + arrayOfBlocks(pid) = x.asInstanceOf[TorrentBlock] |
| 150 | + hasBlocks += 1 |
| 151 | + SparkEnv.get.blockManager.putSingle( |
| 152 | + pieceId, arrayOfBlocks(pid), StorageLevel.MEMORY_AND_DISK, true) |
| 153 | + |
| 154 | + case None => |
| 155 | + throw new SparkException("Failed to get " + pieceId + " of " + broadcastId) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + (hasBlocks == totalBlocks) |
| 161 | + } |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +private object TorrentBroadcast |
| 166 | +extends Logging { |
| 167 | + |
| 168 | + private var initialized = false |
| 169 | + |
| 170 | + def initialize(_isDriver: Boolean) { |
| 171 | + synchronized { |
| 172 | + if (!initialized) { |
| 173 | + initialized = true |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + def stop() { |
| 179 | + initialized = false |
| 180 | + } |
| 181 | + |
| 182 | + val BLOCK_SIZE = System.getProperty("spark.broadcast.blockSize", "4096").toInt * 1024 |
| 183 | + |
| 184 | + def blockifyObject[T](obj: T): TorrentInfo = { |
| 185 | + val byteArray = Utils.serialize[T](obj) |
| 186 | + val bais = new ByteArrayInputStream(byteArray) |
| 187 | + |
| 188 | + var blockNum = (byteArray.length / BLOCK_SIZE) |
| 189 | + if (byteArray.length % BLOCK_SIZE != 0) |
| 190 | + blockNum += 1 |
| 191 | + |
| 192 | + var retVal = new Array[TorrentBlock](blockNum) |
| 193 | + var blockID = 0 |
| 194 | + |
| 195 | + for (i <- 0 until (byteArray.length, BLOCK_SIZE)) { |
| 196 | + val thisBlockSize = math.min(BLOCK_SIZE, byteArray.length - i) |
| 197 | + var tempByteArray = new Array[Byte](thisBlockSize) |
| 198 | + val hasRead = bais.read(tempByteArray, 0, thisBlockSize) |
| 199 | + |
| 200 | + retVal(blockID) = new TorrentBlock(blockID, tempByteArray) |
| 201 | + blockID += 1 |
| 202 | + } |
| 203 | + bais.close() |
| 204 | + |
| 205 | + var tInfo = TorrentInfo(retVal, blockNum, byteArray.length) |
| 206 | + tInfo.hasBlocks = blockNum |
| 207 | + |
| 208 | + return tInfo |
| 209 | + } |
| 210 | + |
| 211 | + def unBlockifyObject[T](arrayOfBlocks: Array[TorrentBlock], |
| 212 | + totalBytes: Int, |
| 213 | + totalBlocks: Int): T = { |
| 214 | + var retByteArray = new Array[Byte](totalBytes) |
| 215 | + for (i <- 0 until totalBlocks) { |
| 216 | + System.arraycopy(arrayOfBlocks(i).byteArray, 0, retByteArray, |
| 217 | + i * BLOCK_SIZE, arrayOfBlocks(i).byteArray.length) |
| 218 | + } |
| 219 | + Utils.deserialize[T](retByteArray, Thread.currentThread.getContextClassLoader) |
| 220 | + } |
| 221 | + |
| 222 | +} |
| 223 | + |
| 224 | +private[spark] case class TorrentBlock( |
| 225 | + blockID: Int, |
| 226 | + byteArray: Array[Byte]) |
| 227 | + extends Serializable |
| 228 | + |
| 229 | +private[spark] case class TorrentInfo( |
| 230 | + @transient arrayOfBlocks : Array[TorrentBlock], |
| 231 | + totalBlocks: Int, |
| 232 | + totalBytes: Int) |
| 233 | + extends Serializable { |
| 234 | + |
| 235 | + @transient var hasBlocks = 0 |
| 236 | +} |
| 237 | + |
| 238 | +private[spark] class TorrentBroadcastFactory |
| 239 | + extends BroadcastFactory { |
| 240 | + |
| 241 | + def initialize(isDriver: Boolean) { TorrentBroadcast.initialize(isDriver) } |
| 242 | + |
| 243 | + def newBroadcast[T](value_ : T, isLocal: Boolean, id: Long) = |
| 244 | + new TorrentBroadcast[T](value_, isLocal, id) |
| 245 | + |
| 246 | + def stop() { TorrentBroadcast.stop() } |
| 247 | +} |
0 commit comments