forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
272 lines (237 loc) · 6.91 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
// The MIT License (MIT)
//
// Copyright (c) 2014 wandoulabs
// Copyright (c) 2014 siddontang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"encoding/json"
"math/rand"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
// For pprof
_ "net/http/pprof"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/arena"
"github.com/pingcap/tidb/util/printer"
"github.com/prometheus/client_golang/prometheus"
)
var (
baseConnID uint32
)
var (
errUnknownFieldType = terror.ClassServer.New(codeUnknownFieldType, "unknown field type")
errInvalidPayloadLen = terror.ClassServer.New(codeInvalidPayloadLen, "invalid payload length")
errInvalidSequence = terror.ClassServer.New(codeInvalidSequence, "invalid sequence")
errInvalidType = terror.ClassServer.New(codeInvalidType, "invalid type")
errNotAllowedCommand = terror.ClassServer.New(codeNotAllowedCommand,
"the used command is not allowed with this TiDB version")
)
// Server is the MySQL protocol server
type Server struct {
cfg *Config
driver IDriver
listener net.Listener
rwlock *sync.RWMutex
concurrentLimiter *TokenLimiter
clients map[uint32]*clientConn
}
// ConnectionCount gets current connection count.
func (s *Server) ConnectionCount() int {
var cnt int
s.rwlock.RLock()
cnt = len(s.clients)
s.rwlock.RUnlock()
return cnt
}
func (s *Server) getToken() *Token {
return s.concurrentLimiter.Get()
}
func (s *Server) releaseToken(token *Token) {
s.concurrentLimiter.Put(token)
}
// Generate a random string using ASCII characters but avoid separator character.
// See https://github.com/mysql/mysql-server/blob/5.7/mysys_ssl/crypt_genhash_impl.cc#L435
func randomBuf(size int) []byte {
buf := make([]byte, size)
for i := 0; i < size; i++ {
buf[i] = byte(rand.Intn(127))
if buf[i] == 0 || buf[i] == byte('$') {
buf[i]++
}
}
return buf
}
// newConn creates a new *clientConn from a net.Conn.
// It allocates a connection ID and random salt data for authentication.
func (s *Server) newConn(conn net.Conn) *clientConn {
cc := &clientConn{
conn: conn,
pkt: newPacketIO(conn),
server: s,
connectionID: atomic.AddUint32(&baseConnID, 1),
collation: mysql.DefaultCollationID,
alloc: arena.NewAllocator(32 * 1024),
}
log.Infof("[%d] new connection %s", cc.connectionID, conn.RemoteAddr().String())
cc.salt = randomBuf(20)
return cc
}
func (s *Server) skipAuth() bool {
return s.cfg.SkipAuth
}
const tokenLimit = 1000
// NewServer creates a new Server.
func NewServer(cfg *Config, driver IDriver) (*Server, error) {
s := &Server{
cfg: cfg,
driver: driver,
concurrentLimiter: NewTokenLimiter(tokenLimit),
rwlock: &sync.RWMutex{},
clients: make(map[uint32]*clientConn),
}
var err error
if cfg.Socket != "" {
cfg.SkipAuth = true
s.listener, err = net.Listen("unix", cfg.Socket)
} else {
s.listener, err = net.Listen("tcp", s.cfg.Addr)
}
if err != nil {
return nil, errors.Trace(err)
}
// Init rand seed for randomBuf()
rand.Seed(time.Now().UTC().UnixNano())
log.Infof("Server run MySQL Protocol Listen at [%s]", s.cfg.Addr)
return s, nil
}
// Run runs the server.
func (s *Server) Run() error {
// Start http api to report tidb info such as tps.
if s.cfg.ReportStatus {
s.startStatusHTTP()
}
for {
conn, err := s.listener.Accept()
if err != nil {
if opErr, ok := err.(*net.OpError); ok {
if opErr.Err.Error() == "use of closed network connection" {
return nil
}
}
log.Errorf("accept error %s", err.Error())
return errors.Trace(err)
}
go s.onConn(conn)
}
}
// Close closes the server.
func (s *Server) Close() {
s.rwlock.Lock()
defer s.rwlock.Unlock()
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
}
// onConn runs in its own goroutine, handles queries from this connection.
func (s *Server) onConn(c net.Conn) {
conn := s.newConn(c)
defer func() {
log.Infof("[%d] close connection", conn.connectionID)
}()
if err := conn.handshake(); err != nil {
// Some keep alive services will send request to TiDB and disconnect immediately.
// So we use info log level.
log.Infof("handshake error %s", errors.ErrorStack(err))
c.Close()
return
}
s.rwlock.Lock()
s.clients[conn.connectionID] = conn
connections := len(s.clients)
s.rwlock.Unlock()
connGauge.Set(float64(connections))
conn.Run()
}
var once sync.Once
const defaultStatusAddr = ":10080"
func (s *Server) startStatusHTTP() {
once.Do(func() {
go func() {
http.HandleFunc("/status", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
s := status{
Connections: s.ConnectionCount(),
Version: mysql.ServerVersion,
GitHash: printer.TiDBGitHash,
}
js, err := json.Marshal(s)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Encode json error", err)
} else {
w.Write(js)
}
})
// HTTP path for prometheus.
http.Handle("/metrics", prometheus.Handler())
addr := s.cfg.StatusAddr
if len(addr) == 0 {
addr = defaultStatusAddr
}
log.Infof("Listening on %v for status and metrics report.", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}()
})
}
// TiDB status
type status struct {
Connections int `json:"connections"`
Version string `json:"version"`
GitHash string `json:"git_hash"`
}
// Server error codes.
const (
codeUnknownFieldType = 1
codeInvalidPayloadLen = 2
codeInvalidSequence = 3
codeInvalidType = 4
codeNotAllowedCommand = 1148
)
func init() {
serverMySQLErrCodes := map[terror.ErrCode]uint16{
codeNotAllowedCommand: mysql.ErrNotAllowedCommand,
}
terror.ErrClassToMySQLCodes[terror.ClassServer] = serverMySQLErrCodes
}