-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.go
197 lines (180 loc) · 6.63 KB
/
utils.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
/*
Copyright 2021 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vreplication
import (
"encoding/json"
"fmt"
"strconv"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)
const (
vreplicationLogTableName = "_vt.vreplication_log"
)
const (
// Enum values for type column of _vt.vreplication_log
// LogStreamCreate is used when a row in _vt.vreplication is inserted via VReplicationExec
LogStreamCreate = "Stream Created"
// LogStreamUpdate is used when a row in _vt.vreplication is updated via VReplicationExec
LogStreamUpdate = "Stream Updated"
// LogStreamDelete is used when a row in _vt.vreplication is deleted via VReplicationExec
LogStreamDelete = "Stream Deleted"
// LogMessage is used for generic log messages
LogMessage = "Message"
// LogCopyStart is used when the copy phase is started
LogCopyStart = "Started Copy Phase"
// LogCopyEnd is used when the copy phase is done
LogCopyEnd = "Ended Copy Phase"
// LogStateChange is used when the state of the stream changes
LogStateChange = "State Changed"
// TODO: LogError is not used atm. Currently irrecoverable errors, resumable errors and informational messages
// are all treated the same: the message column is updated and state left as Running.
// Every five seconds we reset the message and retry streaming so that we can automatically resume from a temporary
// loss of connectivity or a reparent. We need to detect if errors are not recoverable and set an Error status.
// Since this device (of overloading the message) is strewn across the code, and incorrectly flagging resumable
// errors can stall workflows, we have deferred implementing it.
// LogError indicates that there is an error from which we cannot recover and the operator needs to intervene.
LogError = "Error"
)
func getLastLog(dbClient *vdbClient, vreplID uint32) (id int64, typ, state, message string, err error) {
var qr *sqltypes.Result
query := fmt.Sprintf("select id, type, state, message from _vt.vreplication_log where vrepl_id = %d order by id desc limit 1", vreplID)
if qr, err = dbClient.Execute(query); err != nil {
return 0, "", "", "", err
}
if len(qr.Rows) != 1 {
return 0, "", "", "", nil
}
row := qr.Rows[0]
id, _ = evalengine.ToInt64(row[0])
typ = row[1].ToString()
state = row[2].ToString()
message = row[3].ToString()
return id, typ, state, message, nil
}
func insertLog(dbClient *vdbClient, typ string, vreplID uint32, state, message string) error {
// getLastLog returns the last log for a stream. During insertion, if the type/state/message match we do not insert
// a new log but increment the count. This prevents spamming of the log table in case the same message is logged continuously.
id, _, lastLogState, lastLogMessage, err := getLastLog(dbClient, vreplID)
if err != nil {
return err
}
if typ == LogStateChange && state == lastLogState {
// handles case where current state is Running, controller restarts after an error and initializes the state Running
return nil
}
var query string
if id > 0 && message == lastLogMessage {
query = fmt.Sprintf("update _vt.vreplication_log set count = count + 1 where id = %d", id)
} else {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("insert into _vt.vreplication_log(vrepl_id, type, state, message) values(%s, %s, %s, %s)",
strconv.Itoa(int(vreplID)), encodeString(typ), encodeString(state), encodeString(message))
query = buf.ParsedQuery().Query
}
if _, err = dbClient.ExecuteFetch(query, 10000); err != nil {
return fmt.Errorf("could not insert into log table: %v: %v", query, err)
}
return nil
}
// insertLogWithParams is called when a stream is created. The attributes of the stream are stored as a json string
func insertLogWithParams(dbClient *vdbClient, action string, vreplID uint32, params map[string]string) error {
var message string
if params != nil {
obj, _ := json.Marshal(params)
message = string(obj)
}
if err := insertLog(dbClient, action, vreplID, params["state"], message); err != nil {
return err
}
return nil
}
// isUnrecoverableError returns true if vreplication cannot recover from the given error and should completely terminate
func isUnrecoverableError(err error) bool {
if err == nil {
return false
}
sqlErr, isSQLErr := mysql.NewSQLErrorFromError(err).(*mysql.SQLError)
if !isSQLErr {
return false
}
if sqlErr.Num == mysql.ERUnknownError {
return false
}
switch sqlErr.Num {
case
// in case-insensitive alphabetical order
mysql.ERAccessDeniedError,
mysql.ERBadFieldError,
mysql.ERBadNullError,
mysql.ERCantDropFieldOrKey,
mysql.ERDataOutOfRange,
mysql.ERDataTooLong,
mysql.ERDBAccessDenied,
mysql.ERDupEntry,
mysql.ERDupFieldName,
mysql.ERDupKeyName,
mysql.ERDupUnique,
mysql.ERFeatureDisabled,
mysql.ERFunctionNotDefined,
mysql.ERIllegalValueForType,
mysql.ERInvalidCastToJSON,
mysql.ERInvalidJSONBinaryData,
mysql.ERInvalidJSONCharset,
mysql.ERInvalidJSONText,
mysql.ERInvalidJSONTextInParams,
mysql.ERJSONDocumentTooDeep,
mysql.ERJSONValueTooBig,
mysql.ERNoDefault,
mysql.ERNoDefaultForField,
mysql.ERNonUniq,
mysql.ERNonUpdateableTable,
mysql.ERNoSuchTable,
mysql.ERNotAllowedCommand,
mysql.ERNotSupportedYet,
mysql.EROptionPreventsStatement,
mysql.ERParseError,
mysql.ERPrimaryCantHaveNull,
mysql.ErrCantCreateGeometryObject,
mysql.ErrGISDataWrongEndianess,
mysql.ErrNonPositiveRadius,
mysql.ErrNotImplementedForCartesianSRS,
mysql.ErrNotImplementedForProjectedSRS,
mysql.ErrWrongValueForType,
mysql.ERSPDoesNotExist,
mysql.ERSpecifiedAccessDenied,
mysql.ERSyntaxError,
mysql.ERTooBigRowSize,
mysql.ERTooBigSet,
mysql.ERTruncatedWrongValue,
mysql.ERTruncatedWrongValueForField,
mysql.ERUnknownCollation,
mysql.ERUnknownProcedure,
mysql.ERUnknownTable,
mysql.ERWarnDataOutOfRange,
mysql.ERWarnDataTruncated,
mysql.ERWrongFKDef,
mysql.ERWrongFieldSpec,
mysql.ERWrongParamCountToProcedure,
mysql.ERWrongParametersToProcedure,
mysql.ERWrongUsage,
mysql.ERWrongValue,
mysql.ERWrongValueCountOnRow:
log.Errorf("Got unrecoverable error: %v", sqlErr)
return true
}
return false
}