-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NettyRemoteBlockReader.java
110 lines (95 loc) · 4.22 KB
/
NettyRemoteBlockReader.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Licensed to the University of California, Berkeley under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package tachyon.client.netty;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import tachyon.Constants;
import tachyon.client.RemoteBlockReader;
import tachyon.network.protocol.RPCBlockReadRequest;
import tachyon.network.protocol.RPCBlockReadResponse;
import tachyon.network.protocol.RPCErrorResponse;
import tachyon.network.protocol.RPCMessage;
import tachyon.network.protocol.RPCResponse;
/**
* Read data from remote data server using Netty.
*/
public final class NettyRemoteBlockReader implements RemoteBlockReader {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
private final Bootstrap mClientBootstrap;
private final ClientHandler mHandler;
/** A reference to read response so we can explicitly release the resource after reading.*/
private RPCBlockReadResponse mReadResponse = null;
// TODO: Creating a new remote block reader may be expensive, so consider a connection pool.
public NettyRemoteBlockReader() {
mHandler = new ClientHandler();
mClientBootstrap = NettyClient.createClientBootstrap(mHandler);
}
@Override
public ByteBuffer readRemoteBlock(String host, int port, long blockId, long offset, long length)
throws IOException {
InetSocketAddress address = new InetSocketAddress(host, port);
try {
ChannelFuture f = mClientBootstrap.connect(address).sync();
LOG.info("Connected to remote machine " + address);
Channel channel = f.channel();
SingleResponseListener listener = new SingleResponseListener();
mHandler.addListener(listener);
channel.writeAndFlush(new RPCBlockReadRequest(blockId, offset, length));
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
channel.close().sync();
switch (response.getType()) {
case RPC_BLOCK_READ_RESPONSE:
RPCBlockReadResponse blockResponse = (RPCBlockReadResponse) response;
LOG.info("Data " + blockId + " from remote machine " + address + " received");
RPCResponse.Status status = blockResponse.getStatus();
if (status == RPCResponse.Status.SUCCESS) {
// always clear the previous response before reading another one
clearReadResponse();
mReadResponse = blockResponse;
return blockResponse.getPayloadDataBuffer().getReadOnlyByteBuffer();
}
throw new IOException(status.getMessage() + " response: " + blockResponse);
case RPC_ERROR_RESPONSE:
RPCErrorResponse error = (RPCErrorResponse) response;
throw new IOException(error.getStatus().getMessage());
default:
throw new IOException("Unexpected response message type: " + response.getType()
+ " (expected: " + RPCMessage.Type.RPC_BLOCK_READ_RESPONSE + ")");
}
} catch (Exception e) {
throw new IOException(e);
}
}
/**
* Clear the previous read response, release the resource the response references.
*
* @return true if the response is cleared, or there is nothing needs to be cleared.
*/
public boolean clearReadResponse() {
boolean res = true;
if (mReadResponse != null) {
res = mReadResponse.releaseBuffer();
mReadResponse = null;
}
return res;
}
}