| 1 |
package rpc |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
|
| 7 |
hadoop "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_common" |
| 8 |
"google.golang.org/protobuf/proto" |
| 9 |
) |
| 10 |
|
| 11 |
var errUnexpectedSequenceNumber = errors.New("unexpected sequence number") |
| 12 |
|
| 13 |
type transport interface { |
| 14 |
writeRequest(w io.Writer, method string, requestID int32, req proto.Message) error |
| 15 |
readResponse(r io.Reader, method string, requestID int32, resp proto.Message) error |
| 16 |
} |
| 17 |
|
| 18 |
// basicTransport implements plain RPC. |
| 19 |
type basicTransport struct { |
| 20 |
// clientID is the client ID of this writer. |
| 21 |
clientID []byte |
| 22 |
} |
| 23 |
|
| 24 |
// writeRequest writes an RPC message. |
| 25 |
// |
| 26 |
// A request packet: |
| 27 |
// +-----------------------------------------------------------+ |
| 28 |
// | uint32 length of the next three parts | |
| 29 |
// +-----------------------------------------------------------+ |
| 30 |
// | varint length + RpcRequestHeaderProto | |
| 31 |
// +-----------------------------------------------------------+ |
| 32 |
// | varint length + RequestHeaderProto | |
| 33 |
// +-----------------------------------------------------------+ |
| 34 |
// | varint length + Request | |
| 35 |
// +-----------------------------------------------------------+ |
| 36 |
func (t *basicTransport) writeRequest(w io.Writer, method string, requestID int32, req proto.Message) error { |
| 37 |
rrh := newRPCRequestHeader(requestID, t.clientID) |
| 38 |
rh := newRequestHeader(method) |
| 39 |
|
| 40 |
reqBytes, err := makeRPCPacket(rrh, rh, req) |
| 41 |
if err != nil { |
| 42 |
return err |
| 43 |
} |
| 44 |
|
| 45 |
_, err = w.Write(reqBytes) |
| 46 |
return err |
| 47 |
} |
| 48 |
|
| 49 |
// ReadResponse reads a response message. |
| 50 |
// |
| 51 |
// A response from the namenode: |
| 52 |
// +-----------------------------------------------------------+ |
| 53 |
// | uint32 length of the next two parts | |
| 54 |
// +-----------------------------------------------------------+ |
| 55 |
// | varint length + RpcResponseHeaderProto | |
| 56 |
// +-----------------------------------------------------------+ |
| 57 |
// | varint length + Response | |
| 58 |
// +-----------------------------------------------------------+ |
| 59 |
func (t *basicTransport) readResponse(r io.Reader, method string, requestID int32, resp proto.Message) error { |
| 60 |
rrh := &hadoop.RpcResponseHeaderProto{} |
| 61 |
err := readRPCPacket(r, rrh, resp) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} else if int32(rrh.GetCallId()) != requestID { |
| 65 |
return errUnexpectedSequenceNumber |
| 66 |
} else if rrh.GetStatus() != hadoop.RpcResponseHeaderProto_SUCCESS { |
| 67 |
return &NamenodeError{ |
| 68 |
method: method, |
| 69 |
message: rrh.GetErrorMsg(), |
| 70 |
code: int(rrh.GetErrorDetail()), |
| 71 |
exception: rrh.GetExceptionClassName(), |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return nil |
| 76 |
} |