-
Notifications
You must be signed in to change notification settings - Fork 77
/
mysql.go
436 lines (376 loc) · 10.9 KB
/
mysql.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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
//
// https://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 mysql
import (
"context"
"crypto/tls"
"fmt"
"reflect"
"sync/atomic"
"time"
"github.com/go-sql-driver/mysql"
"github.com/vdaas/vald/internal/db/rdb/mysql/dbr"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/net"
"github.com/vdaas/vald/internal/net/tcp"
)
const (
vectorTableName = "backup_vector"
podIPTableName = "pod_ip"
idColumnName = "id"
uuidColumnName = "uuid"
vectorColumnName = "vector"
ipColumnName = "ip"
asterisk = "*"
)
// MySQL represents the interface to handle MySQL operation.
type MySQL interface {
Open(ctx context.Context) error
Close(ctx context.Context) error
Getter
Setter
}
type mySQLClient struct {
db string
host string
port int
user string
pass string
name string
charset string
timezone string
initialPingTimeLimit time.Duration
initialPingDuration time.Duration
connMaxLifeTime time.Duration
dialer tcp.Dialer
dialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
tlsConfig *tls.Config
maxOpenConns int
maxIdleConns int
session dbr.Session
connected atomic.Value
eventReceiver EventReceiver
dbr dbr.DBR
}
// New creates the new mySQLClient with option.
// It will return error when set option is failed.
func New(opts ...Option) (MySQL, error) {
m := &mySQLClient{
dbr: dbr.New(),
}
for _, opt := range append(defaultOpts, opts...) {
if err := opt(m); err != nil {
return nil, errors.ErrOptionFailed(err, reflect.ValueOf(opt))
}
}
return m, nil
}
// Open opens the connection with MySQL.
// It will return error when connecting to MySQL ends with fail.
func (m *mySQLClient) Open(ctx context.Context) error {
if m.dialer != nil {
m.dialer.StartDialerCache(ctx)
m.dialerFunc = m.dialer.GetDialer()
}
var addParam string
network := "tcp"
if m.dialerFunc != nil {
mysql.RegisterDialContext(network, func(ctx context.Context, addr string) (net.Conn, error) {
return m.dialerFunc(ctx, network, addr)
})
}
if m.tlsConfig != nil {
tlsConfName := "tls"
mysql.RegisterTLSConfig(tlsConfName, m.tlsConfig)
addParam += "&tls=" + tlsConfName
}
conn, err := m.dbr.Open(
m.db,
fmt.Sprintf(
"%s:%s@%s(%s:%d)/%s?charset=%s&parseTime=true&loc=%s%s",
m.user, m.pass, network, m.host, m.port, m.name,
m.charset, m.timezone, addParam,
),
m.eventReceiver,
)
if err != nil {
return err
}
conn.SetConnMaxLifetime(m.connMaxLifeTime)
conn.SetMaxIdleConns(m.maxIdleConns)
conn.SetMaxOpenConns(m.maxOpenConns)
m.session = dbr.NewSession(conn, nil)
m.connected.Store(true)
return m.Ping(ctx)
}
// Ping check the connection of MySQL database.
// If the connection is closed, it returns error.
func (m *mySQLClient) Ping(ctx context.Context) (err error) {
pctx, cancel := context.WithTimeout(ctx, m.initialPingTimeLimit)
defer cancel()
tick := time.NewTicker(m.initialPingDuration)
for {
select {
case <-pctx.Done():
if err != nil {
err = errors.Wrap(errors.ErrMySQLConnectionPingFailed, err.Error())
} else {
err = errors.ErrMySQLConnectionPingFailed
}
cerr := pctx.Err()
if cerr != nil {
err = errors.Wrap(err, cerr.Error())
}
return err
case <-tick.C:
err = m.session.PingContext(pctx)
if err == nil {
return nil
}
log.Error(err)
}
}
}
// Close closes the connection of MySQL database.
// If the connection is already closed or closing conncection is failed, it returns error.
func (m *mySQLClient) Close(ctx context.Context) (err error) {
if m.connected.Load().(bool) {
err = m.session.Close()
if err == nil{
m.connected.Store(false)
}
}
return nil
}
// GetVector gets the vector data and podIPs which have index of vector.
func (m *mySQLClient) GetVector(ctx context.Context, uuid string) (Vector, error) {
if !m.connected.Load().(bool) {
return nil, errors.ErrMySQLConnectionClosed
}
var data *data
_, err := m.session.Select(asterisk).From(vectorTableName).Where(m.dbr.Eq(uuidColumnName, uuid)).Limit(1).LoadContext(ctx, &data)
if err != nil {
return nil, err
}
if data == nil {
return nil, errors.ErrRequiredElementNotFoundByUUID(uuid)
}
var podIPs []podIP
_, err = m.session.Select(asterisk).From(podIPTableName).Where(m.dbr.Eq(idColumnName, data.ID)).LoadContext(ctx, &podIPs)
if err != nil {
return nil, err
}
return &vector{
data: *data,
podIPs: podIPs,
}, nil
}
// GetIPs gets the pod ips which have index of requested uuids' vector data's vector.
func (m *mySQLClient) GetIPs(ctx context.Context, uuid string) ([]string, error) {
if !m.connected.Load().(bool) {
return nil, errors.ErrMySQLConnectionClosed
}
var id int64
_, err := m.session.Select(idColumnName).From(vectorTableName).Where(m.dbr.Eq(uuidColumnName, uuid)).Limit(1).LoadContext(ctx, &id)
if err != nil {
return nil, err
}
if id == 0 {
return nil, errors.ErrRequiredElementNotFoundByUUID(uuid)
}
var podIPs []podIP
_, err = m.session.Select(asterisk).From(podIPTableName).Where(m.dbr.Eq(idColumnName, id)).LoadContext(ctx, &podIPs)
if err != nil {
return nil, err
}
ips := make([]string, 0, len(podIPs))
for _, podIP := range podIPs {
ips = append(ips, podIP.IP)
}
return ips, nil
}
func validateVector(vec Vector) error {
if len(vec.GetVector()) == 0 {
return errors.ErrRequiredMemberNotFilled("vector")
}
return nil
}
// SetVector records vector data at backup_vector table and set of (podIP, uuid) at podIPtable through same transaction.
// If error occurs it will rollback by defer function.
func (m *mySQLClient) SetVector(ctx context.Context, vec Vector) error {
if !m.connected.Load().(bool) {
return errors.ErrMySQLConnectionClosed
}
tx, err := m.session.Begin()
if err != nil {
return err
}
defer tx.RollbackUnlessCommitted()
err = validateVector(vec)
if err != nil {
return err
}
_, err = tx.InsertBySql("INSERT INTO "+vectorTableName+"(uuid, vector) VALUES (?, ?) ON DUPLICATE KEY UPDATE vector = ?",
vec.GetUUID(),
vec.GetVector(),
vec.GetVector()).ExecContext(ctx)
if err != nil {
return err
}
var id int64
_, err = tx.Select(idColumnName).From(vectorTableName).Where(m.dbr.Eq(uuidColumnName, vec.GetUUID())).Limit(1).LoadContext(ctx, &id)
if err != nil {
return err
}
if id == 0 {
return errors.ErrRequiredElementNotFoundByUUID(vec.GetUUID())
}
_, err = tx.DeleteFrom(podIPTableName).Where(m.dbr.Eq(idColumnName, id)).ExecContext(ctx)
if err != nil {
return err
}
stmt := tx.InsertInto(podIPTableName).Columns(idColumnName, ipColumnName)
for _, ip := range vec.GetIPs() {
stmt.Record(&podIP{ID: id, IP: ip})
}
_, err = stmt.ExecContext(ctx)
if err != nil {
return err
}
return tx.Commit()
}
// SetVectors records multiple vector data like as SetVector().
func (m *mySQLClient) SetVectors(ctx context.Context, vecs ...Vector) error {
if !m.connected.Load().(bool) {
return errors.ErrMySQLConnectionClosed
}
tx, err := m.session.Begin()
if err != nil {
return err
}
defer tx.RollbackUnlessCommitted()
for _, vec := range vecs {
err = validateVector(vec)
if err != nil {
return err
}
_, err = tx.InsertBySql("INSERT INTO "+vectorTableName+"(uuid, vector) VALUES (?, ?) ON DUPLICATE KEY UPDATE vector = ?",
vec.GetUUID(),
vec.GetVector(),
vec.GetVector()).ExecContext(ctx)
if err != nil {
return err
}
}
for _, vec := range vecs {
var id int64
_, err = tx.Select(idColumnName).From(vectorTableName).Where(m.dbr.Eq(uuidColumnName, vec.GetUUID())).Limit(1).LoadContext(ctx, &id)
if err != nil {
return err
}
if id == 0 {
return errors.ErrRequiredElementNotFoundByUUID(vec.GetUUID())
}
_, err = tx.DeleteFrom(podIPTableName).Where(m.dbr.Eq(idColumnName, id)).ExecContext(ctx)
if err != nil {
return err
}
stmt := tx.InsertInto(podIPTableName).Columns(idColumnName, ipColumnName)
for _, ip := range vec.GetIPs() {
stmt.Record(&podIP{ID: id, IP: ip})
}
_, err = stmt.ExecContext(ctx)
if err != nil {
return err
}
}
return tx.Commit()
}
func (m *mySQLClient) deleteVector(ctx context.Context, val interface{}) error {
if !m.connected.Load().(bool) {
return errors.ErrMySQLConnectionClosed
}
tx, err := m.session.Begin()
if err != nil {
return err
}
if tx == nil {
return errors.ErrMySQLTransactionNotCreated
}
defer tx.RollbackUnlessCommitted()
_, err = tx.DeleteFrom(vectorTableName).Where(m.dbr.Eq(uuidColumnName, val)).ExecContext(ctx)
if err != nil {
return err
}
_, err = tx.DeleteFrom(podIPTableName).Where(m.dbr.Eq(uuidColumnName, val)).ExecContext(ctx)
if err != nil {
return err
}
return tx.Commit()
}
// DeleteVector deletes vector data from backup_vector table and podIPs from pod_ip table using vector's uuid.
func (m *mySQLClient) DeleteVector(ctx context.Context, uuid string) error {
return m.deleteVector(ctx, uuid)
}
// DeleteVectors is the same as DeleteVector() but it deletes multiple records.
func (m *mySQLClient) DeleteVectors(ctx context.Context, uuids ...string) error {
return m.deleteVector(ctx, uuids)
}
// SetIPs insert the vector's uuid and the podIPs into database.
func (m *mySQLClient) SetIPs(ctx context.Context, uuid string, ips ...string) error {
if !m.connected.Load().(bool) {
return errors.ErrMySQLConnectionClosed
}
tx, err := m.session.Begin()
if err != nil {
return err
}
defer tx.RollbackUnlessCommitted()
var id int64
_, err = tx.Select(idColumnName).From(vectorTableName).Where(m.dbr.Eq(uuidColumnName, uuid)).Limit(1).LoadContext(ctx, &id)
if err != nil {
return err
}
if id == 0 {
return errors.ErrRequiredElementNotFoundByUUID(uuid)
}
stmt := tx.InsertInto(podIPTableName).Columns(idColumnName, ipColumnName)
for _, ip := range ips {
stmt.Record(&podIP{ID: id, IP: ip})
}
_, err = stmt.ExecContext(ctx)
if err != nil {
return err
}
return tx.Commit()
}
// RemoveIPs delete the podIPs from database by podIPs.
func (m *mySQLClient) RemoveIPs(ctx context.Context, ips ...string) error {
if !m.connected.Load().(bool) {
return errors.ErrMySQLConnectionClosed
}
tx, err := m.session.Begin()
if err != nil {
return err
}
defer tx.RollbackUnlessCommitted()
_, err = tx.DeleteFrom(podIPTableName).Where(m.dbr.Eq(ipColumnName, ips)).ExecContext(ctx)
if err != nil {
return err
}
return tx.Commit()
}