转自:http://www.aboutyun.com/thread-21115-1-1.html
问题导读:
01
02
03
04
05
06
07
08
09
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
|
EventLoopGroup createEventLoop(IOMode mode, int numThreads, String threadPrefix) {
switch (mode) {
case
NIO
:
return
new
NioEventLoopGroup(numThreads, threadFactory);
case
EPOLL
:
return
new
EpollEventLoopGroup(numThreads, threadFactory);
}
}
Class
extends
Channel> getClientChannelClass(IOMode mode) {
switch (mode) {
case
NIO
:
return
NioSocketChannel.
class
;
case
EPOLL
:
return
EpollSocketChannel.
class
;
}
}
Class
extends
ServerChannel> getServerChannelClass(IOMode mode) {
switch (mode) {
case
NIO
:
return
NioServerSocketChannel.
class
;
case
EPOLL
:
return
EpollServerSocketChannel.
class
;
}
}
//构造pipelet
responseHandler
=
new
TransportResponseHandler(channel);
TransportClient client
=
new
TransportClient(channel, responseHandler);
requestHandler
=
new
TransportRequestHandler(channel, client,rpcHandler);
channelHandler
=
new
TransportChannelHandler(client, responseHandler, requestHandler,
conf.connectionTimeoutMs(), closeIdleConnections);
channel.pipeline()
.addLast(
"encoder"
, encoder)
.addLast(TransportFrameDecoder.HANDLER
_
NAME, NettyUtils.createFrameDecoder())
.addLast(
"decoder"
, decoder)
.addLast(
"idleStateHandler"
,
new
IdleStateHandler())
.addLast(
"handler"
, channelHandler);
|
1
2
3
4
5
6
7
|
public void channelRead
0
(ChannelHandlerContext ctx, Message request) throws Exception {
if
(request instanceof RequestMessage) {
requestHandler.handle((RequestMessage) request);
}
else
{
responseHandler.handle((ResponseMessage) request);
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//参考TransportRequestHandler的构造函数
public TransportRequestHandler(RpcHandler rpcHandler) {
this
.rpcHandler
=
rpcHandler;
//****注入功能****
this
.streamManager
=
rpcHandler.getStreamManager();
//****注入streamManager****
}
//实现ChunkFecth的功能
private
void processFetchRequest(
final
ChunkFetchRequest req) {
buf
=
streamManager.getChunk(req.streamId, req.chunkIndex);
respond(
new
ChunkFetchSuccess(req.streamChunkId, buf));
}
//实现Stream的功能
private
void processStreamRequest(
final
StreamRequest req) {
buf
=
streamManager.openStream(req.streamId);
respond(
new
StreamResponse(req.streamId, buf.size(), buf));
}
//实现RPC的功能
private
void processRpcRequest(
final
RpcRequest req) {
rpcHandler.receive(reverseClient, req.body().nioByteBuffer(),
new
RpcResponseCallback() {
public void onSuccess(ByteBuffer response) {
respond(
new
RpcResponse(req.requestId,
new
NioManagedBuffer(response)));
}
});
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
//以TransportResponseHandler中处理ChunkFetchSuccess响应包的处理逻辑
public void handle(ResponseMessage message) throws Exception {
String remoteAddress
=
NettyUtils.getRemoteAddress(channel);
if
(message instanceof ChunkFetchSuccess) {
resp
=
(ChunkFetchSuccess) message;
listener
=
outstandingFetches.get(resp.streamChunkId);
if
(listener
==
null
) {
//没有监听的回调函数
}
else
{
outstandingFetches.remove(resp.streamChunkId);
//回调函数,并把resp.body()对应的chunk数据返回给listener
listener.onSuccess(resp.streamChunkId.chunkIndex, resp.body());
resp.body().release();
}
}
}
//ChunkFetchFailure/RpcResponse/RpcFailure/StreamResponse/StreamFailure处理的方法是一致的
|
1
2
3
4
5
6
|
val
blockTransferService
=
new
NettyBlockTransferService(conf, securityManager, hostname, numUsableCores)
val
envInstance
=
new
SparkEnv(executorId,rpcEnv,serializer, closureSerializer,
blockTransferService,
//为SparkEnv的一个组成
....,conf)
|
1
2
3
4
5
|
//case openBlocks: OpenBlocks =>
val
blocks
:
Seq[ManagedBuffer]
=
openBlocks.blockIds.map(BlockId.apply).map(blockManager.getBlockData)
val
streamId
=
streamManager.registerStream(appId, blocks.iterator.asJava)
responseContext.onSuccess(
new
StreamHandle(streamId, blocks.size).toByteBuffer)
|
01
02
03
04
05
06
07
08
09
10
11
|
//发出openMessage请求
client.sendRpc(openMessage.toByteBuffer(),
new
RpcResponseCallback() {
@
Override
public void onSuccess(ByteBuffer response) {
streamHandle
=
(StreamHandle)response;
//获取streamId
//针对streamid发出一组fetchChunk
for
(int i
=
0
; i < streamHandle.numChunks; i++) {
client.fetchChunk(streamHandle.streamId, i, chunkCallback);
}
}
});
|
1
2
3
4
5
6
7
8
|
class
HelloActor
extends
Actor {
def
receive
=
{
case
"hello"
=
> println(
"world"
)
case
_
=
> println(
"huh?"
)
}
}
//
Receive
=
PartialFunction[Any, Unit]
|
1
2
3
4
5
6
7
8
9
|
private
[spark]
trait
RpcEndpoint {
def
receive
:
PartialFunction[Any, Unit]
=
{
case
_
=
>
throw
new
SparkException()
}
def
receiveAndReply(context
:
RpcCallContext)
:
PartialFunction[Any, Unit]
=
{
case
_
=
> context.sendFailure(
new
SparkException())
}
//onStart(),onStop()
}
|