-
Notifications
You must be signed in to change notification settings - Fork 6
/
scope.go
216 lines (172 loc) · 4.76 KB
/
scope.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/hagen1778/chproxy/config"
"github.com/hagen1778/chproxy/log"
)
func (s *scope) String() string {
return fmt.Sprintf("[ Id: %d; User %q(%d) proxying as %q(%d) to %q(%d) ]",
s.id,
s.user.name, s.user.runningQueries(),
s.clusterUser.name, s.clusterUser.runningQueries(),
s.host.addr.Host, s.host.runningQueries())
}
type scope struct {
id uint32
host *host
cluster *cluster
user *user
clusterUser *clusterUser
}
var scopeId = uint32(time.Now().UnixNano())
func newScope(u *user, cu *clusterUser, c *cluster) *scope {
return &scope{
id: atomic.AddUint32(&scopeId, 1),
host: c.getHost(),
cluster: c,
user: u,
clusterUser: cu,
}
}
func (s *scope) inc() error {
uq := s.user.inc()
cq := s.clusterUser.inc()
s.host.inc()
var err error
if s.user.maxConcurrentQueries > 0 && uq > s.user.maxConcurrentQueries {
err = fmt.Errorf("limits for user %q are exceeded: maxConcurrentQueries limit: %d", s.user.name, s.user.maxConcurrentQueries)
}
if s.clusterUser.maxConcurrentQueries > 0 && cq > s.clusterUser.maxConcurrentQueries {
err = fmt.Errorf("limits for cluster user %q are exceeded: maxConcurrentQueries limit: %d", s.clusterUser.name, s.clusterUser.maxConcurrentQueries)
}
if err != nil {
s.dec()
return err
}
return nil
}
func (s *scope) dec() {
s.host.dec()
s.user.dec()
s.clusterUser.dec()
}
func (s *scope) killQuery() error {
if len(s.cluster.killQueryUserName) == 0 {
return nil
}
query := fmt.Sprintf("KILL QUERY WHERE query_id = '%d'", s.id)
log.Debugf("ExecutionTime exceeded. Going to call query %q", query)
r := strings.NewReader(query)
addr := s.host.addr.String()
req, err := http.NewRequest("POST", addr, r)
if err != nil {
return fmt.Errorf("error while creating kill query request to %s: %s", addr, err)
}
// send request as kill_query_user
req.SetBasicAuth(s.cluster.killQueryUserName, s.cluster.killQueryUserPassword)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error while executing clickhouse query %q at %q: %s", query, addr, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBody, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code returned from query %q at %q: %d. Response body: %q",
query, addr, resp.StatusCode, responseBody)
}
log.Debugf("Query with id=%d successfully killed", s.id)
return nil
}
func (s *scope) decorateRequest(req *http.Request) *http.Request {
params := req.URL.Query()
// delete possible credentials from query since they
// aren't longer needed
params.Del("user")
params.Del("password")
// set query_id as scope_id to have possibility kill query if needed
params.Set("query_id", strconv.Itoa(int(s.id)))
req.URL.RawQuery = params.Encode()
// rewrite possible previous Basic Auth
// and send request as cluster user
req.SetBasicAuth(s.clusterUser.name, s.clusterUser.password)
// send request to chosen host from cluster
req.URL.Scheme = s.host.addr.Scheme
req.URL.Host = s.host.addr.Host
// extend ua with additional info
ua := fmt.Sprintf("IP: %s; CHProxy-User: %s; CHProxy-ClusterUser: %s; %s",
req.RemoteAddr, s.user.name, s.clusterUser.name, req.UserAgent())
req.Header.Set("User-Agent", ua)
return req
}
type user struct {
toUser string
toCluster string
allowedNetworks config.Networks
name, password string
maxExecutionTime time.Duration
maxConcurrentQueries uint32
queryCounter
}
type clusterUser struct {
name, password string
maxExecutionTime time.Duration
maxConcurrentQueries uint32
queryCounter
}
type host struct {
addr *url.URL
queryCounter
}
type cluster struct {
nextIdx uint32
hosts []*host
users map[string]*clusterUser
killQueryUserName string
killQueryUserPassword string
}
var client = &http.Client{
Timeout: time.Second * 60,
}
// get least loaded + round-robin host from cluster
func (c *cluster) getHost() *host {
idx := atomic.AddUint32(&c.nextIdx, 1)
l := uint32(len(c.hosts))
idx = idx % l
idle := c.hosts[idx]
idleN := idle.runningQueries()
if idleN == 0 {
return idle
}
// round hosts checking
// until the least loaded is found
for i := (idx + 1) % l; i != idx; i = (i + 1) % l {
h := c.hosts[i]
n := h.runningQueries()
if n == 0 {
return h
}
if n < idleN {
idle, idleN = h, n
}
}
return idle
}
type queryCounter struct {
value uint32
}
func (qc *queryCounter) runningQueries() uint32 {
return atomic.LoadUint32(&qc.value)
}
func (qc *queryCounter) inc() uint32 {
return atomic.AddUint32(&qc.value, 1)
}
func (qc *queryCounter) dec() {
atomic.AddUint32(&qc.value, ^uint32(0))
}