-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclickhouse_requests.go
251 lines (217 loc) · 6.29 KB
/
clickhouse_requests.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
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"time"
// "github.com/davecgh/go-spew/spew"
"github.com/satori/go.uuid" // generate sessionID and queryID
)
func get_id() string {
uu,err := uuid.NewV4()
if err == nil {
return uu.String()
}
uu,err = uuid.NewV1()
if err == nil {
return uu.String()
}
return fmt.Sprintf( "faultyuu %v", time.Now() )
}
var sessionID = get_id()
type progressInfo struct {
Elapsed float64
ReadRows uint64
ReadBytes uint64
TotalRowsApprox uint64
WrittenRows uint64
WrittenBytes uint64
MemoryUsage int64
}
type queryStats struct {
QueryDuration time.Duration
ReadRows uint64
ReadBytes uint64
WrittenRows uint64
WrittenBytes uint64
ResultRows uint64
ResultBytes uint64
MemoryUsage uint64
Exception string
StackTrace string
}
func getServerVersion() (version string, err error) {
data, err := serviceRequest("SELECT version()")
if err != nil {
return
}
version = data[0][0]
return
}
func getProgressInfo(queryID string) (pi progressInfo, err error) {
pi = progressInfo{}
query := fmt.Sprintf("select elapsed,read_rows,read_bytes,total_rows_approx,written_rows,written_bytes,memory_usage from system.processes where query_id='%s'", queryID)
data, err := serviceRequest(query)
if err != nil {
return
}
if len(data) != 1 || len(data[0]) != 7 {
err = errors.New("Bad response dimensions")
return
}
pi.Elapsed, _ = strconv.ParseFloat(data[0][0], 64)
pi.ReadRows, _ = strconv.ParseUint(data[0][1], 10, 64)
pi.ReadBytes, _ = strconv.ParseUint(data[0][2], 10, 64)
pi.TotalRowsApprox, _ = strconv.ParseUint(data[0][3], 10, 64)
pi.WrittenRows, _ = strconv.ParseUint(data[0][4], 10, 64)
pi.WrittenBytes, _ = strconv.ParseUint(data[0][5], 10, 64)
pi.MemoryUsage, _ = strconv.ParseInt(data[0][6], 10, 64)
// spew.Dump(pi)
return
}
func getQueryStats(queryID string) (qs queryStats, err error) {
query := fmt.Sprintf("select query_duration_ms,read_rows,read_bytes,written_rows,written_bytes,result_rows,result_bytes,memory_usage,exception,stack_trace,type from system.query_log where query_id='%s' and type>1", queryID)
data, err := serviceRequest(query)
if err != nil {
return
}
if len(data) != 1 || len(data[0]) != 7 {
err = errors.New("Bad response dimensions")
return
}
durationMs, _ := strconv.ParseUint(data[0][0], 10, 64)
qs.QueryDuration = time.Duration(durationMs) * time.Millisecond
qs.ReadRows, _ = strconv.ParseUint(data[0][1], 10, 64)
qs.ReadBytes, _ = strconv.ParseUint(data[0][2], 10, 64)
qs.WrittenRows, _ = strconv.ParseUint(data[0][3], 10, 64)
qs.WrittenBytes, _ = strconv.ParseUint(data[0][4], 10, 64)
qs.ResultRows, _ = strconv.ParseUint(data[0][5], 10, 64)
qs.ResultBytes, _ = strconv.ParseUint(data[0][6], 10, 64)
qs.MemoryUsage, _ = strconv.ParseUint(data[0][7], 10, 64)
qs.Exception = data[0][8]
qs.StackTrace = data[0][9]
return
}
func hasDataInStdin() bool {
fi, err := os.Stdin.Stat()
if err == nil {
if fi.Mode()&os.ModeNamedPipe != 0 {
return true
}
}
return false
}
var useCmdRegexp = regexp.MustCompile("^\\s*(?i)use\\s+(\"\\w+\"|\\w+|`\\w+`)\\s*$")
// it will not match SET GLOBAL as set global not affect current session, according to docs
var setCmdRegexp = regexp.MustCompile("^\\s*(?i)set\\s+(?:\"\\w+\"|\\w+|\\`\\w+\\`)\\s*=\\s*(?:'([^']+)'|[0-9]+|NULL)")
var settingsRegexp = regexp.MustCompile("\\s*(\"\\w+\"|\\w+|\\`\\w+\\`)\\s*=\\s*('[^']+'|[0-9]+|NULL)\\s*,?")
func fireQuery(sqlToExequte, format string, interactive bool) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)
cx, cancel := context.WithCancel(context.Background())
defer cancel()
queryFinished := make(chan bool)
go func() {
for {
select {
case <-signalCh:
cancel()
case <-queryFinished:
return
}
}
}()
if chcOutput.setupOutput(cancel) {
res := queryToStdout(cx, sqlToExequte, format, interactive)
if res == 200 {
useCmdMatches := useCmdRegexp.FindStringSubmatch(sqlToExequte)
if useCmdMatches != nil {
opts.Database = strings.Trim(useCmdMatches[1], "\"`")
chcOutput.printServiceMsg("Database changed to " + opts.Database + "\n")
}
if setCmdRegexp.MatchString(sqlToExequte) {
settings := sqlToExequte[4:]
settingsMatched := settingsRegexp.FindAllStringSubmatch(settings, -1)
for _, match := range settingsMatched {
clickhouseSetting[strings.Trim(match[1], "\"`")] = strings.Trim(match[2], "'")
}
// spew.Dump(clickhouseSetting)
}
}
queryFinished <- true
}
}
const (
dataPacket = iota
errPacket = iota
donePacket = iota
statusPacket = iota
progressPacket = iota
)
type queryExecution struct {
Err error
Data string
Progress progressInfo
Stats queryStats
StatusCode int
PacketType int
}
func queryToStdout(cx context.Context, query, format string, interactive bool) int {
queryID := get_id()
defer chcOutput.releaseOutput()
status := -1
initProgress()
queryExecutionChannel := makeQuery(cx, query, queryID, format, interactive)
Loop2:
for {
select {
case qe := <-queryExecutionChannel:
switch qe.PacketType {
case dataPacket:
data := qe.Data
clearProgress(chcOutput.StdErr)
io.WriteString(chcOutput.StdOut, data)
case errPacket:
log.Fatalln(qe.Err)
case donePacket:
count := qe.Stats.ResultRows
duration := qe.Stats.QueryDuration
clearProgress(chcOutput.StdErr)
if opts.Time {
if status == 200 {
chcOutput.printServiceMsg(fmt.Sprintf("\n%v rows in set. Elapsed: %v\n\n", count, duration))
} else {
chcOutput.printServiceMsg(fmt.Sprintf("\nElapsed: %v\n\n", duration))
}
}
break Loop2
case statusPacket:
status = qe.StatusCode
if status == 200 {
chcOutput.startOutput()
}
case progressPacket:
pi := qe.Progress
writeProgres(chcOutput.StdErr, pi.ReadRows, pi.ReadBytes, pi.TotalRowsApprox, uint64(pi.Elapsed*1000000000))
}
case <-cx.Done():
clearProgress(chcOutput.StdErr)
chcOutput.printServiceMsg(fmt.Sprintf("\nKilling query (id: %v)... ", queryID))
if killQuery(queryID) {
chcOutput.printServiceMsg("killed!\n\n")
} else {
chcOutput.printServiceMsg("failure!\n\n")
}
break Loop2
}
}
return status
// io.WriteString(stdErr, "queryToStdout finished" );
}