/
checksum_reader.go
141 lines (118 loc) · 3.76 KB
/
checksum_reader.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package rpc
import (
"context"
"errors"
"io"
"net"
"time"
hdfs "github.com/keepv1/hdfs/internal/protocol/hadoop_hdfs"
)
// ChecksumReader provides an interface for reading the "MD5CRC32" checksums of
// individual blocks. It abstracts over reading from multiple datanodes, in
// order to be robust to failures.
type ChecksumReader struct {
// Block is the block location provided by the namenode.
Block *hdfs.LocatedBlockProto
// UseDatanodeHostname specifies whether the datanodes should be connected to
// via their hostnames (if true) or IP addresses (if false).
UseDatanodeHostname bool
// DialFunc is used to connect to the datanodes. If nil, then
// (&net.Dialer{}).DialContext is used.
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
deadline time.Time
datanodes *datanodeFailover
}
// SetDeadline sets the deadline for future ReadChecksum calls. A zero value
// for t means Read will not time out.
func (cr *ChecksumReader) SetDeadline(t time.Time) error {
cr.deadline = t
// Return the error at connection time.
return nil
}
// ReadChecksum returns the checksum of the block.
func (cr *ChecksumReader) ReadChecksum() ([]byte, error) {
if cr.datanodes == nil {
locs := cr.Block.GetLocs()
datanodes := make([]string, len(locs))
for i, loc := range locs {
dn := loc.GetId()
datanodes[i] = getDatanodeAddress(dn, cr.UseDatanodeHostname)
}
cr.datanodes = newDatanodeFailover(datanodes)
}
for cr.datanodes.numRemaining() > 0 {
address := cr.datanodes.next()
checksum, err := cr.readChecksum(address)
if err != nil {
cr.datanodes.recordFailure(err)
continue
}
return checksum, nil
}
err := cr.datanodes.lastError()
if err != nil {
err = errors.New("No available datanodes for block.")
}
return nil, err
}
func (cr *ChecksumReader) readChecksum(address string) ([]byte, error) {
if cr.DialFunc == nil {
cr.DialFunc = (&net.Dialer{}).DialContext
}
conn, err := cr.DialFunc(context.Background(), "tcp", address)
if err != nil {
return nil, err
}
err = conn.SetDeadline(cr.deadline)
if err != nil {
return nil, err
}
err = cr.writeBlockChecksumRequest(conn)
if err != nil {
return nil, err
}
resp, err := cr.readBlockChecksumResponse(conn)
if err != nil {
return nil, err
}
return resp.GetChecksumResponse().GetMd5(), nil
}
// A checksum request to a datanode:
// +-----------------------------------------------------------+
// | Data Transfer Protocol Version, int16 |
// +-----------------------------------------------------------+
// | Op code, 1 byte (CHECKSUM_BLOCK = 0x55) |
// +-----------------------------------------------------------+
// | varint length + OpReadBlockProto |
// +-----------------------------------------------------------+
func (cr *ChecksumReader) writeBlockChecksumRequest(w io.Writer) error {
header := []byte{0x00, dataTransferVersion, checksumBlockOp}
op := newChecksumBlockOp(cr.Block)
opBytes, err := makePrefixedMessage(op)
if err != nil {
return err
}
req := append(header, opBytes...)
_, err = w.Write(req)
if err != nil {
return err
}
return nil
}
// The response from the datanode:
// +-----------------------------------------------------------+
// | varint length + BlockOpResponseProto |
// +-----------------------------------------------------------+
func (cr *ChecksumReader) readBlockChecksumResponse(r io.Reader) (*hdfs.BlockOpResponseProto, error) {
resp := &hdfs.BlockOpResponseProto{}
err := readPrefixedMessage(r, resp)
return resp, err
}
func newChecksumBlockOp(block *hdfs.LocatedBlockProto) *hdfs.OpBlockChecksumProto {
return &hdfs.OpBlockChecksumProto{
Header: &hdfs.BaseHeaderProto{
Block: block.GetB(),
Token: block.GetBlockToken(),
},
}
}