-
Notifications
You must be signed in to change notification settings - Fork 52
/
server.go
282 lines (249 loc) · 5.66 KB
/
server.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package ch
import (
"context"
"io"
"net"
"sync"
"time"
"github.com/go-faster/errors"
"go.uber.org/atomic"
"go.uber.org/zap"
"github.com/ClickHouse/ch-go/compress"
"github.com/ClickHouse/ch-go/proto"
)
// Server is basic ClickHouse server.
type Server struct {
lg *zap.Logger
tz *time.Location
conn atomic.Uint64
ver int
onErr func(err error)
}
// ServerOptions wraps possible Server configuration.
type ServerOptions struct {
Logger *zap.Logger
Timezone *time.Location
OnError func(err error)
}
// NewServer returns new ClickHouse Server.
func NewServer(opt ServerOptions) *Server {
if opt.Logger == nil {
opt.Logger = zap.NewNop()
}
if opt.Timezone == nil {
opt.Timezone = time.UTC
}
if opt.OnError == nil {
opt.OnError = func(err error) {}
}
return &Server{
lg: opt.Logger,
tz: opt.Timezone,
ver: proto.Version,
onErr: opt.OnError,
}
}
// ServerConn wraps Server connection.
type ServerConn struct {
lg *zap.Logger
tz *time.Location
conn net.Conn
buf *proto.Buffer
reader *proto.Reader
client proto.ClientHello
info proto.ServerHello
ver int
// compressor performs block compression,
// see encodeBlock.
compressor *compress.Writer
settings []Setting
}
func (c *ServerConn) packet() (proto.ClientCode, error) {
n, err := c.reader.UVarInt()
if err != nil {
return 0, errors.Wrap(err, "uvarint")
}
code := proto.ClientCode(n)
if ce := c.lg.Check(zap.DebugLevel, "Packet"); ce != nil {
ce.Write(
zap.Uint64("packet_code_raw", n),
zap.Stringer("packet_code", code),
)
}
if !code.IsAClientCode() {
return 0, errors.Errorf("bad client packet type %d", n)
}
return code, nil
}
func (c *ServerConn) handshake() error {
p, err := c.packet()
if err != nil {
return errors.Wrap(err, "packet")
}
if p != proto.ClientCodeHello {
return errors.Errorf("unexpected packet %q", p)
}
if err := c.client.Decode(c.reader); err != nil {
return errors.Wrap(err, "decode hello")
}
c.ver = c.client.ProtocolVersion
c.info.EncodeAware(c.buf, c.ver)
if err := c.flush(); err != nil {
return errors.Wrap(err, "flush")
}
_ = c.compressor // hack
_ = c.settings // hack
return nil
}
func (c *ServerConn) flush() error {
n, err := c.conn.Write(c.buf.Buf)
if err != nil {
return errors.Wrap(err, "write")
}
if n != len(c.buf.Buf) {
return errors.Wrap(io.ErrShortWrite, "wrote less than expected")
}
if ce := c.lg.Check(zap.DebugLevel, "Flush"); ce != nil {
ce.Write(zap.Int("bytes", n))
}
c.buf.Reset()
return nil
}
func (c *ServerConn) handlePacket(p proto.ClientCode) error {
switch p {
case proto.ClientCodePing:
return c.handlePing()
case proto.ClientCodeQuery:
return c.handleQuery()
default:
return errors.Errorf("%q not implemented", p)
}
}
func (c *ServerConn) handlePing() error {
proto.ServerCodePong.Encode(c.buf)
return c.flush()
}
func (c *ServerConn) handleClientData(ctx context.Context, q proto.Query) error {
var data proto.ClientData
if err := data.DecodeAware(c.reader, c.ver); err != nil {
return errors.Wrap(err, "decode")
}
var block proto.Block
if err := block.DecodeBlock(c.reader, c.ver, nil); err != nil {
return errors.Wrap(err, "decode block")
}
if block.Rows > 0 || block.Columns > 0 {
return errors.New("input not implemented")
}
_ = ctx
_ = q
return nil
}
func (c *ServerConn) handleQuery() error {
c.lg.Debug("Decoding query", zap.Int("v", c.ver))
deadline := time.Now().Add(time.Second * 1)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
go func() {
ticker := time.NewTicker(time.Millisecond * 100)
defer ticker.Stop()
defer func() {
_ = c.conn.SetReadDeadline(time.Time{})
}()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
_ = c.conn.SetReadDeadline(deadline)
}
}
}()
var q proto.Query
if err := q.DecodeAware(c.reader, c.ver); err != nil {
return errors.Wrap(err, "decode")
}
lg := c.lg.With(zap.String("query_id", q.ID))
Ingest:
for {
lg.Debug("Reading packet")
p, err := c.packet()
if err != nil {
return errors.Wrap(err, "packet")
}
switch p {
case proto.ClientCodeData:
if err := c.handleClientData(ctx, q); err != nil {
return errors.Wrap(err, "client data")
}
break Ingest
default:
return errors.Errorf("unexpected packet %q", p)
}
}
proto.ServerCodeEndOfStream.Encode(c.buf)
if err := c.flush(); err != nil {
return errors.Wrap(err, "flush")
}
return nil
}
// Handle connection.
func (c *ServerConn) Handle() error {
if err := c.handshake(); err != nil {
return errors.Wrap(err, "handshake")
}
for {
p, err := c.packet()
if err != nil {
return errors.Wrap(err, "packet")
}
if err := c.handlePacket(p); err != nil {
return errors.Wrapf(err, "handle %q", p)
}
}
}
func (s *Server) handle(conn net.Conn) error {
lg := s.lg.With(
zap.Uint64("conn", s.conn.Inc()),
)
lg.Info("Connected",
zap.String("addr", conn.RemoteAddr().String()),
)
sConn := &ServerConn{
lg: lg,
conn: conn,
ver: s.ver,
buf: new(proto.Buffer),
reader: proto.NewReader(conn),
client: proto.ClientHello{},
info: proto.ServerHello{
Name: "CH",
Revision: s.ver,
},
tz: time.UTC,
compressor: compress.NewWriter(),
}
return sConn.Handle()
}
// Serve connections on net.Listener.
func (s *Server) Serve(ln net.Listener) error {
var wg sync.WaitGroup
defer wg.Wait()
for {
c, err := ln.Accept()
if err != nil {
return errors.Wrap(err, "accept")
}
wg.Add(1)
go func() {
defer func() {
_ = c.Close()
}()
defer wg.Done()
if err := s.handle(c); err != nil && !errors.Is(err, io.EOF) {
s.lg.Error("Handle", zap.Error(err))
s.onErr(err)
}
}()
}
}