aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/spark/network/netty/ShuffleCopier.scala
blob: a91f5a886d732234b2640b6305e0ecd54baec1c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package spark.network.netty

import java.util.concurrent.Executors

import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.util.CharsetUtil

import spark.Logging
import spark.network.ConnectionManagerId


private[spark] class ShuffleCopier extends Logging {

  def getBlock(cmId: ConnectionManagerId, blockId: String,
      resultCollectCallback: (String, Long, ByteBuf) => Unit) {

    val handler = new ShuffleCopier.ShuffleClientHandler(resultCollectCallback)
    val fc = new FileClient(handler)
    fc.init()
    fc.connect(cmId.host, cmId.port)
    fc.sendRequest(blockId)
    fc.waitForClose()
    fc.close()
  }

  def getBlocks(cmId: ConnectionManagerId,
    blocks: Seq[(String, Long)],
    resultCollectCallback: (String, Long, ByteBuf) => Unit) {

    for ((blockId, size) <- blocks) {
      getBlock(cmId, blockId, resultCollectCallback)
    }
  }
}


private[spark] object ShuffleCopier extends Logging {

  private class ShuffleClientHandler(resultCollectCallBack: (String, Long, ByteBuf) => Unit)
    extends FileClientHandler with Logging {

    override def handle(ctx: ChannelHandlerContext, in: ByteBuf, header: FileHeader) {
      logDebug("Received Block: " + header.blockId + " (" + header.fileLen + "B)");
      resultCollectCallBack(header.blockId, header.fileLen.toLong, in.readBytes(header.fileLen))
    }
  }

  def echoResultCollectCallBack(blockId: String, size: Long, content: ByteBuf) {
    logInfo("File: " + blockId + " content is : \" " + content.toString(CharsetUtil.UTF_8) + "\"")
  }

  def runGetBlock(host:String, port:Int, file:String){
    val handler = new ShuffleClientHandler(echoResultCollectCallBack)
    val fc = new FileClient(handler)
    fc.init();
    fc.connect(host, port)
    fc.sendRequest(file)
    fc.waitForClose();
    fc.close()
  }

  def main(args: Array[String]) {
    if (args.length < 3) {
      System.err.println("Usage: ShuffleCopier <host> <port> <shuffle_block_id> <threads>")
      System.exit(1)
    }
    val host = args(0)
    val port = args(1).toInt
    val file = args(2)
    val threads = if (args.length > 3) args(3).toInt else 10

    val copiers = Executors.newFixedThreadPool(80)
    for (i <- Range(0, threads)) {
      val runnable = new Runnable() {
        def run() {
          runGetBlock(host, port, file)
        }
      }
      copiers.execute(runnable)
    }
    copiers.shutdown
  }
}