This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
connection.go
195 lines (146 loc) · 3.49 KB
/
connection.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package hbase
import (
pb "github.com/golang/protobuf/proto"
"github.com/lazyshot/go-hbase/proto"
"fmt"
"net"
)
type connection struct {
connstr string
id int
name string
socket net.Conn
in *inputStream
calls map[int]*call
callId *atomicCounter
isMaster bool
}
var connectionIds *atomicCounter = newAtomicCounter()
func newConnection(connstr string, isMaster bool) (*connection, error) {
id := connectionIds.IncrAndGet()
log.Debug("Connecting to server[id=%d] [%s]", id, connstr)
socket, err := net.Dial("tcp", connstr)
if err != nil {
return nil, err
}
c := &connection{
connstr: connstr,
id: id,
name: fmt.Sprintf("connection(%s) id: %d", connstr, id),
socket: socket,
in: newInputStream(socket),
calls: make(map[int]*call),
callId: newAtomicCounter(),
isMaster: isMaster,
}
err = c.init()
if err != nil {
return nil, err
}
log.Debug("Initiated connection [id=%d] [%s]", id, connstr)
return c, nil
}
func (c *connection) init() error {
err := c.writeHead()
if err != nil {
return err
}
err = c.writeConnectionHeader()
if err != nil {
return err
}
go c.processMessages()
return nil
}
func (c *connection) writeHead() error {
buf := newOutputBuffer()
buf.Write(hbase_header_bytes)
buf.WriteByte(0)
buf.WriteByte(80)
n, err := c.socket.Write(buf.Bytes())
log.Debug("Outgoing Head [n=%d]", n)
return err
}
func (c *connection) writeConnectionHeader() error {
buf := newOutputBuffer()
service := pb.String("ClientService")
if c.isMaster {
service = pb.String("MasterService")
}
err := buf.WritePBMessage(&proto.ConnectionHeader{
UserInfo: &proto.UserInformation{
EffectiveUser: pb.String("bryan"),
},
ServiceName: service,
})
if err != nil {
return err
}
err = buf.PrependSize()
if err != nil {
return err
}
n, err := c.socket.Write(buf.Bytes())
if err != nil {
return err
}
log.Debug("Outgoing ConnHeader [n=%d]", n)
return nil
}
func (c *connection) call(request *call) error {
id := c.callId.IncrAndGet()
rh := &proto.RequestHeader{
CallId: pb.Uint32(uint32(id)),
MethodName: pb.String(request.methodName),
RequestParam: pb.Bool(true),
}
request.setid(uint32(id))
bfrh := newOutputBuffer()
err := bfrh.WritePBMessage(rh)
if err != nil {
panic(err)
}
bfr := newOutputBuffer()
err = bfr.WritePBMessage(request.request)
if err != nil {
panic(err)
}
buf := newOutputBuffer()
buf.writeDelimitedBuffers(bfrh, bfr)
c.calls[id] = request
n, err := c.socket.Write(buf.Bytes())
if err != nil {
return err
}
log.Debug("Sent bytes to server [callId=%d] [n=%d] [connection=%s]", id, n, c.name)
if n != len(buf.Bytes()) {
return fmt.Errorf("Sent bytes not match number bytes [n=%d] [actual_n=%d]", n, len(buf.Bytes()))
}
return nil
}
func (c *connection) processMessages() {
for {
msgs := c.in.processData()
if msgs == nil || len(msgs) == 0 || len(msgs[0]) == 0 {
continue
}
var rh proto.ResponseHeader
err := pb.Unmarshal(msgs[0], &rh)
if err != nil {
panic(err)
}
log.Debug("Responseheader received [id=%d] [conn=%s]", rh.GetCallId(), c.name)
callId := rh.GetCallId()
call, ok := c.calls[int(callId)]
if !ok {
panic(fmt.Errorf("Invalid call id: %d", callId))
}
delete(c.calls, int(callId))
exception := rh.GetException()
if exception != nil {
call.complete(fmt.Errorf("Exception returned: %s\n%s", exception.GetExceptionClassName(), exception.GetStackTrace()), nil)
} else if len(msgs) == 2 {
call.complete(nil, msgs[1])
}
}
}