-
Notifications
You must be signed in to change notification settings - Fork 125
/
monitoring.go
267 lines (241 loc) · 8.1 KB
/
monitoring.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
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"database/sql/driver"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
const urlQueriesResultFmt = "/queries/%s/result"
// queryResultStatus is status returned from server
type queryResultStatus int
// Query Status defined at server side
const (
SFQueryRunning queryResultStatus = iota
SFQueryAborting
SFQuerySuccess
SFQueryFailedWithError
SFQueryAborted
SFQueryQueued
SFQueryFailedWithIncident
SFQueryDisconnected
SFQueryResumingWarehouse
// SFQueryQueueRepairingWarehouse present in QueryDTO.java.
SFQueryQueueRepairingWarehouse
SFQueryRestarted
// SFQueryBlocked is when a statement is waiting on a lock on resource held
// by another statement.
SFQueryBlocked
SFQueryNoData
)
func (qs queryResultStatus) String() string {
return [...]string{"RUNNING", "ABORTING", "SUCCESS", "FAILED_WITH_ERROR",
"ABORTED", "QUEUED", "FAILED_WITH_INCIDENT", "DISCONNECTED",
"RESUMING_WAREHOUSE", "QUEUED_REPAIRING_WAREHOUSE", "RESTARTED",
"BLOCKED", "NO_DATA"}[qs]
}
func (qs queryResultStatus) isRunning() bool {
switch qs {
case SFQueryRunning, SFQueryResumingWarehouse, SFQueryQueued,
SFQueryQueueRepairingWarehouse, SFQueryNoData:
return true
default:
return false
}
}
func (qs queryResultStatus) isError() bool {
switch qs {
case SFQueryAborting, SFQueryFailedWithError, SFQueryAborted,
SFQueryFailedWithIncident, SFQueryDisconnected, SFQueryBlocked:
return true
default:
return false
}
}
var strQueryStatusMap = map[string]queryResultStatus{"RUNNING": SFQueryRunning,
"ABORTING": SFQueryAborting, "SUCCESS": SFQuerySuccess,
"FAILED_WITH_ERROR": SFQueryFailedWithError, "ABORTED": SFQueryAborted,
"QUEUED": SFQueryQueued, "FAILED_WITH_INCIDENT": SFQueryFailedWithIncident,
"DISCONNECTED": SFQueryDisconnected,
"RESUMING_WAREHOUSE": SFQueryResumingWarehouse,
"QUEUED_REPAIRING_WAREHOUSE": SFQueryQueueRepairingWarehouse,
"RESTARTED": SFQueryRestarted,
"BLOCKED": SFQueryBlocked, "NO_DATA": SFQueryNoData}
type retStatus struct {
Status string `json:"status"`
SQLText string `json:"sqlText"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
Stats retStats `json:"stats"`
}
type retStats struct {
ScanBytes int64 `json:"scanBytes"`
ProducedRows int64 `json:"producedRows"`
}
type statusResponse struct {
Data struct {
Queries []retStatus `json:"queries"`
} `json:"data"`
Message string `json:"message"`
Code string `json:"code"`
Success bool `json:"success"`
}
func strToQueryStatus(in string) queryResultStatus {
return strQueryStatusMap[in]
}
// SnowflakeQueryStatus is the query status metadata of a snowflake query
type SnowflakeQueryStatus struct {
SQLText string
StartTime int64
EndTime int64
ErrorCode string
ErrorMessage string
ScanBytes int64
ProducedRows int64
}
// SnowflakeConnection is a wrapper to snowflakeConn that exposes API functions
type SnowflakeConnection interface {
GetQueryStatus(ctx context.Context, queryID string) (*SnowflakeQueryStatus, error)
}
// checkQueryStatus returns the status given the query ID. If successful,
// the error will be nil, indicating there is a complete query result to fetch.
// Other than nil, there are three error types that can be returned:
// 1. ErrQueryStatus, if GS cannot return any kind of status due to any reason,
// i.e. connection, permission, if a query was just submitted, etc.
// 2, ErrQueryReportedError, if the requested query was terminated or aborted
// and GS returned an error status included in query. SFQueryFailedWithError
// 3, ErrQueryIsRunning, if the requested query is still running and might have
// a complete result later, these statuses were listed in query. SFQueryRunning
func (sc *snowflakeConn) checkQueryStatus(
ctx context.Context,
qid string) (
*retStatus, error) {
headers := make(map[string]string)
param := make(url.Values)
param.Set(requestGUIDKey, NewUUID().String())
if tok, _, _ := sc.rest.TokenAccessor.GetTokens(); tok != "" {
headers[headerAuthorizationKey] = fmt.Sprintf(headerSnowflakeToken, tok)
}
resultPath := fmt.Sprintf("%s/%s", monitoringQueriesPath, qid)
url := sc.rest.getFullURL(resultPath, ¶m)
res, err := sc.rest.FuncGet(ctx, sc.rest, url, headers, sc.rest.RequestTimeout)
if err != nil {
logger.WithContext(ctx).Errorf("failed to get response. err: %v", err)
return nil, err
}
defer res.Body.Close()
var statusResp = statusResponse{}
if err = json.NewDecoder(res.Body).Decode(&statusResp); err != nil {
logger.WithContext(ctx).Errorf("failed to decode JSON. err: %v", err)
return nil, err
}
if !statusResp.Success || len(statusResp.Data.Queries) == 0 {
logger.WithContext(ctx).Errorf("status query returned not-success or no status returned.")
return nil, (&SnowflakeError{
Number: ErrQueryStatus,
Message: "status query returned not-success or no status returned. Please retry",
}).exceptionTelemetry(sc)
}
queryRet := statusResp.Data.Queries[0]
if queryRet.ErrorCode != "" {
return &queryRet, (&SnowflakeError{
Number: ErrQueryStatus,
Message: errMsgQueryStatus,
MessageArgs: []interface{}{queryRet.ErrorCode, queryRet.ErrorMessage},
IncludeQueryID: true,
QueryID: qid,
}).exceptionTelemetry(sc)
}
// returned errorCode is 0. Now check what is the returned status of the query.
qStatus := strToQueryStatus(queryRet.Status)
if qStatus.isError() {
return &queryRet, (&SnowflakeError{
Number: ErrQueryReportedError,
Message: fmt.Sprintf("%s: status from server: [%s]",
queryRet.ErrorMessage, queryRet.Status),
IncludeQueryID: true,
QueryID: qid,
}).exceptionTelemetry(sc)
}
if qStatus.isRunning() {
return &queryRet, (&SnowflakeError{
Number: ErrQueryIsRunning,
Message: fmt.Sprintf("%s: status from server: [%s]",
queryRet.ErrorMessage, queryRet.Status),
IncludeQueryID: true,
QueryID: qid,
}).exceptionTelemetry(sc)
}
//success
return &queryRet, nil
}
func (sc *snowflakeConn) getQueryResultResp(
ctx context.Context,
resultPath string) (
*execResponse, error) {
headers := getHeaders()
paramsMutex.Lock()
if serviceName, ok := sc.cfg.Params[serviceName]; ok {
headers[httpHeaderServiceName] = *serviceName
}
paramsMutex.Unlock()
param := make(url.Values)
param.Set(requestIDKey, getOrGenerateRequestIDFromContext(ctx).String())
param.Set("clientStartTime", strconv.FormatInt(sc.currentTimeProvider.currentTime(), 10))
param.Set(requestGUIDKey, NewUUID().String())
token, _, _ := sc.rest.TokenAccessor.GetTokens()
if token != "" {
headers[headerAuthorizationKey] = fmt.Sprintf(headerSnowflakeToken, token)
}
url := sc.rest.getFullURL(resultPath, ¶m)
respd, err := getQueryResultWithRetriesForAsyncMode(ctx, sc.rest, url, headers, sc.rest.RequestTimeout)
if err != nil {
logger.WithContext(ctx).Errorf("error: %v", err)
return nil, err
}
return respd, nil
}
// Fetch query result for a query id from /queries/<qid>/result endpoint.
func (sc *snowflakeConn) rowsForRunningQuery(
ctx context.Context, qid string,
rows *snowflakeRows) error {
resultPath := fmt.Sprintf(urlQueriesResultFmt, qid)
resp, err := sc.getQueryResultResp(ctx, resultPath)
if err != nil {
logger.WithContext(ctx).Errorf("error: %v", err)
return err
}
if !resp.Success {
code, err := strconv.Atoi(resp.Code)
if err != nil {
return err
}
return (&SnowflakeError{
Number: code,
SQLState: resp.Data.SQLState,
Message: resp.Message,
QueryID: resp.Data.QueryID,
}).exceptionTelemetry(sc)
}
rows.addDownloader(populateChunkDownloader(ctx, sc, resp.Data))
return nil
}
// prepare a Rows object to return for query of 'qid'
func (sc *snowflakeConn) buildRowsForRunningQuery(
ctx context.Context,
qid string) (
driver.Rows, error) {
rows := new(snowflakeRows)
rows.sc = sc
rows.queryID = qid
rows.ctx = ctx
if err := sc.rowsForRunningQuery(ctx, qid, rows); err != nil {
return nil, err
}
err := rows.ChunkDownloader.start()
return rows, err
}