| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #ifndef _HDFS_LIBHDFS3_CLIENT_REMOTEBLOCKREADER_H_ |
| 29 | #define _HDFS_LIBHDFS3_CLIENT_REMOTEBLOCKREADER_H_ |
| 30 | |
| 31 | #include "BlockReader.h" |
| 32 | #include "Checksum.h" |
| 33 | #include "DataTransferProtocol.h" |
| 34 | #include "Memory.h" |
| 35 | #include "network/BufferedSocketReader.h" |
| 36 | #include "network/TcpSocket.h" |
| 37 | #include "PacketHeader.h" |
| 38 | #include "PeerCache.h" |
| 39 | #include "server/DatanodeInfo.h" |
| 40 | #include "server/LocatedBlocks.h" |
| 41 | #include "SessionConfig.h" |
| 42 | |
| 43 | namespace Hdfs { |
| 44 | namespace Internal { |
| 45 | |
| 46 | class RemoteBlockReader: public BlockReader { |
| 47 | public: |
| 48 | RemoteBlockReader(const ExtendedBlock& eb, DatanodeInfo& datanode, |
| 49 | PeerCache& peerCache, int64_t start, int64_t len, |
| 50 | const Token& token, const char* clientName, bool verify, |
| 51 | SessionConfig& conf); |
| 52 | |
| 53 | ~RemoteBlockReader(); |
| 54 | |
| 55 | /** |
| 56 | * Get how many bytes can be read without blocking. |
| 57 | * @return The number of bytes can be read without blocking. |
| 58 | */ |
| 59 | virtual int64_t available(); |
| 60 | |
| 61 | /** |
| 62 | * To read data from block. |
| 63 | * @param buf the buffer used to filled. |
| 64 | * @param size the number of bytes to be read. |
| 65 | * @return return the number of bytes filled in the buffer, |
| 66 | * it may less than size. Return 0 if reach the end of block. |
| 67 | */ |
| 68 | virtual int32_t read(char * buf, int32_t len); |
| 69 | |
| 70 | /** |
| 71 | * Move the cursor forward len bytes. |
| 72 | * @param len The number of bytes to skip. |
| 73 | */ |
| 74 | virtual void skip(int64_t len); |
| 75 | |
| 76 | private: |
| 77 | bool readTrailingEmptyPacket(); |
| 78 | shared_ptr<PacketHeader> (); |
| 79 | shared_ptr<Socket> getNextPeer(const DatanodeInfo& dn); |
| 80 | void checkResponse(); |
| 81 | void readNextPacket(); |
| 82 | void sendStatus(); |
| 83 | void verifyChecksum(int chunks); |
| 84 | |
| 85 | private: |
| 86 | bool sentStatus; |
| 87 | bool verify; //verify checksum or not. |
| 88 | const ExtendedBlock & binfo; |
| 89 | DatanodeInfo & datanode; |
| 90 | int checksumSize; |
| 91 | int chunkSize; |
| 92 | int connTimeout; |
| 93 | int position; //point in buffer. |
| 94 | int readTimeout; |
| 95 | int size; //data size in buffer. |
| 96 | int writeTimeout; |
| 97 | int64_t cursor; //point in block. |
| 98 | int64_t endOffset; //offset in block requested to read to. |
| 99 | int64_t lastSeqNo; //segno of the last chunk received |
| 100 | PeerCache& peerCache; |
| 101 | shared_ptr<BufferedSocketReader> in; |
| 102 | shared_ptr<Checksum> checksum; |
| 103 | shared_ptr<DataTransferProtocol> sender; |
| 104 | shared_ptr<PacketHeader> ; |
| 105 | shared_ptr<Socket> sock; |
| 106 | std::vector<char> buffer; |
| 107 | }; |
| 108 | |
| 109 | } |
| 110 | } |
| 111 | #endif /* _HDFS_LIBHDFS3_CLIENT_REMOTEBLOCKREADER_H_ */ |
| 112 | |