-
Notifications
You must be signed in to change notification settings - Fork 0
/
zx.go
421 lines (349 loc) · 11.1 KB
/
zx.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
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"io"
"regexp"
"strings"
"time"
"github.com/gotomicro/dmgo/util"
)
const (
SQL_SELECT_STANDBY = "select distinct mailIni.inst_name, mailIni.INST_IP, mailIni.INST_PORT, archIni.arch_status " +
"from v$arch_status archIni " +
"left join (select * from V$DM_MAL_INI) mailIni on archIni.arch_dest = mailIni.inst_name " +
"left join V$MAL_LINK_STATUS on CTL_LINK_STATUS = 'CONNECTED' AND DATA_LINK_STATUS = 'CONNECTED' " +
"where archIni.arch_type in ('TIMELY', 'REALTIME') AND archIni.arch_status = 'VALID'"
SQL_SELECT_STANDBY2 = "select distinct " +
"mailIni.mal_inst_name, mailIni.mal_INST_HOST, mailIni.mal_INST_PORT, archIni.arch_status " +
"from v$arch_status archIni " + "left join (select * from V$DM_MAL_INI) mailIni " +
"on archIni.arch_dest = mailIni.mal_inst_name " + "left join V$MAL_LINK_STATUS " +
"on CTL_LINK_STATUS = 'CONNECTED' AND DATA_LINK_STATUS = 'CONNECTED' " +
"where archIni.arch_type in ('TIMELY', 'REALTIME') AND archIni.arch_status = 'VALID'"
)
type rwUtil struct {
}
var RWUtil = rwUtil{}
func (RWUtil rwUtil) connect(c *Connector, ctx context.Context) (*Connection, error) {
c.loginMode = LOGIN_MODE_PRIMARY_ONLY
connection, err := c.connect(ctx)
if err != nil {
return nil, err
}
connection.rwInfo.rwCounter = getRwCounterInstance(connection, connection.StandbyCount)
err = RWUtil.connectStandby(connection)
return connection, err
}
func (RWUtil rwUtil) reconnect(connection *Connection) error {
if connection.rwInfo == nil {
return nil
}
RWUtil.removeStandby(connection)
err := connection.reconnect()
if err != nil {
return err
}
connection.rwInfo.cleanup()
connection.rwInfo.rwCounter = getRwCounterInstance(connection, connection.StandbyCount)
err = RWUtil.connectStandby(connection)
return err
}
func (RWUtil rwUtil) recoverStandby(connection *Connection) error {
if connection.closed.IsSet() || RWUtil.isStandbyAlive(connection) {
return nil
}
ts := time.Now().UnixNano() / 1000000
freq := int64(connection.dmConnector.rwStandbyRecoverTime)
if freq <= 0 || ts-connection.rwInfo.tryRecoverTs < freq {
return nil
}
err := RWUtil.connectStandby(connection)
connection.rwInfo.tryRecoverTs = ts
return err
}
func (RWUtil rwUtil) connectStandby(connection *Connection) error {
var err error
db, err := RWUtil.chooseValidStandby(connection)
if err != nil {
return err
}
if db == nil {
return nil
}
standbyConnectorValue := *connection.dmConnector
standbyConnector := &standbyConnectorValue
standbyConnector.host = db.host
standbyConnector.port = db.port
standbyConnector.rwStandby = true
standbyConnector.group = nil
standbyConnector.loginMode = LOGIN_MODE_STANDBY_ONLY
standbyConnector.switchTimes = 0
connection.rwInfo.connStandby, err = standbyConnector.connectSingle(context.Background())
if err != nil {
return err
}
if connection.rwInfo.connStandby.SvrMode != SERVER_MODE_STANDBY || connection.rwInfo.connStandby.SvrStat != SERVER_STATUS_OPEN {
RWUtil.removeStandby(connection)
}
return nil
}
func (RWUtil rwUtil) chooseValidStandby(connection *Connection) (*ep, error) {
stmt, rs, err := connection.driverQuery(SQL_SELECT_STANDBY2)
if err != nil {
stmt, rs, err = connection.driverQuery(SQL_SELECT_STANDBY)
}
defer func() {
if rs != nil {
rs.close()
}
if stmt != nil {
stmt.close()
}
}()
if err == nil {
count := int32(rs.CurrentRows.getRowCount())
if count > 0 {
connection.rwInfo.rwCounter = getRwCounterInstance(connection, count)
i := int32(0)
rowIndex := connection.rwInfo.rwCounter.random(count)
dest := make([]driver.Value, 3)
for err := rs.next(dest); err != io.EOF; err = rs.next(dest) {
if i == rowIndex {
ep := newEP(dest[1].(string), dest[2].(int32))
return ep, nil
}
i++
}
}
}
if err != nil {
return nil, errors.New("choose valid standby error!" + err.Error())
}
return nil, nil
}
func (RWUtil rwUtil) afterExceptionOnStandby(connection *Connection, e error) {
if e.(*DmError).ErrCode == ECGO_COMMUNITION_ERROR.ErrCode {
RWUtil.removeStandby(connection)
}
}
func (RWUtil rwUtil) removeStandby(connection *Connection) {
if connection.rwInfo.connStandby != nil {
connection.rwInfo.connStandby.close()
connection.rwInfo.connStandby = nil
}
}
func (RWUtil rwUtil) isCreateStandbyStmt(stmt *DmStatement) bool {
return stmt != nil && stmt.rwInfo.readOnly && RWUtil.isStandbyAlive(stmt.dmConn)
}
func (RWUtil rwUtil) executeByConn(conn *Connection, query string, execute1 func() (interface{}, error), execute2 func(otherConn *Connection) (interface{}, error)) (interface{}, error) {
if err := RWUtil.recoverStandby(conn); err != nil {
return nil, err
}
RWUtil.distributeSqlByConn(conn, query)
turnToPrimary := false
ret, err := execute1()
if err != nil {
if conn.rwInfo.connCurrent == conn.rwInfo.connStandby {
RWUtil.afterExceptionOnStandby(conn, err)
turnToPrimary = true
} else {
return nil, err
}
}
curConn := conn.rwInfo.connCurrent
var otherConn *Connection
if curConn != conn {
otherConn = conn
} else {
otherConn = conn.rwInfo.connStandby
}
switch curConn.lastExecInfo.retSqlType {
case Dm_build_690, Dm_build_691, Dm_build_695, Dm_build_702, Dm_build_701, Dm_build_693:
{
if otherConn != nil {
execute2(otherConn)
}
}
case Dm_build_700:
{
sqlhead := regexp.MustCompile("[ (]").Split(strings.TrimSpace(query), 2)[0]
if util.StringUtil.EqualsIgnoreCase(sqlhead, "SP_SET_PARA_VALUE") || util.StringUtil.EqualsIgnoreCase(sqlhead, "SP_SET_SESSION_READONLY") {
if otherConn != nil {
execute2(otherConn)
}
}
}
case Dm_build_699:
{
if conn.dmConnector.rwHA && curConn == conn.rwInfo.connStandby &&
(curConn.lastExecInfo.rsDatas == nil || len(curConn.lastExecInfo.rsDatas) == 0) {
turnToPrimary = true
}
}
}
if turnToPrimary {
conn.rwInfo.toPrimary()
conn.rwInfo.connCurrent = conn
return execute2(conn)
}
return ret, nil
}
func (RWUtil rwUtil) executeByStmt(stmt *DmStatement, execute1 func() (interface{}, error), execute2 func(otherStmt *DmStatement) (interface{}, error)) (interface{}, error) {
orgStmt := stmt.rwInfo.stmtCurrent
query := stmt.nativeSql
if err := RWUtil.recoverStandby(stmt.dmConn); err != nil {
return nil, err
}
RWUtil.distributeSqlByStmt(stmt)
if orgStmt != stmt.rwInfo.stmtCurrent {
RWUtil.copyStatement(orgStmt, stmt.rwInfo.stmtCurrent)
stmt.rwInfo.stmtCurrent.nativeSql = orgStmt.nativeSql
}
turnToPrimary := false
ret, err := execute1()
if err != nil {
if stmt.rwInfo.stmtCurrent == stmt.rwInfo.stmtStandby {
RWUtil.afterExceptionOnStandby(stmt.dmConn, err)
turnToPrimary = true
} else {
return nil, err
}
}
curStmt := stmt.rwInfo.stmtCurrent
var otherStmt *DmStatement
if curStmt != stmt {
otherStmt = stmt
} else {
otherStmt = stmt.rwInfo.stmtStandby
}
switch curStmt.execInfo.retSqlType {
case Dm_build_690, Dm_build_691, Dm_build_695, Dm_build_702, Dm_build_701, Dm_build_693:
{
if otherStmt != nil {
RWUtil.copyStatement(curStmt, otherStmt)
execute2(otherStmt)
}
}
case Dm_build_700:
{
var tmpsql string
if query != "" {
tmpsql = strings.TrimSpace(query)
} else if stmt.nativeSql != "" {
tmpsql = strings.TrimSpace(stmt.nativeSql)
} else {
tmpsql = ""
}
sqlhead := regexp.MustCompile("[ (]").Split(tmpsql, 2)[0]
if util.StringUtil.EqualsIgnoreCase(sqlhead, "SP_SET_PARA_VALUE") || util.StringUtil.EqualsIgnoreCase(sqlhead, "SP_SET_SESSION_READONLY") {
if otherStmt != nil {
RWUtil.copyStatement(curStmt, otherStmt)
execute2(otherStmt)
}
}
}
case Dm_build_699:
{
if stmt.dmConn.dmConnector.rwHA && curStmt == stmt.rwInfo.stmtStandby &&
(curStmt.execInfo.rsDatas == nil || len(curStmt.execInfo.rsDatas) == 0) {
turnToPrimary = true
}
}
}
if turnToPrimary {
stmt.dmConn.rwInfo.toPrimary()
stmt.rwInfo.stmtCurrent = stmt
RWUtil.copyStatement(stmt.rwInfo.stmtStandby, stmt)
return execute2(stmt)
}
return ret, nil
}
func (RWUtil rwUtil) checkReadonlyByConn(conn *Connection, sql string) bool {
readonly := true
if sql != "" && !conn.dmConnector.rwIgnoreSql {
tmpsql := strings.TrimSpace(sql)
sqlhead := strings.SplitN(tmpsql, " ", 2)[0]
if util.StringUtil.EqualsIgnoreCase(sqlhead, "INSERT") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "UPDATE") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "DELETE") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "CREATE") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "TRUNCATE") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "DROP") ||
util.StringUtil.EqualsIgnoreCase(sqlhead, "ALTER") {
readonly = false
} else {
readonly = true
}
}
return readonly
}
func (RWUtil rwUtil) checkReadonlyByStmt(stmt *DmStatement) bool {
return RWUtil.checkReadonlyByConn(stmt.dmConn, stmt.nativeSql)
}
func (RWUtil rwUtil) distributeSqlByConn(conn *Connection, query string) RWSiteEnum {
var dest RWSiteEnum
if !RWUtil.isStandbyAlive(conn) {
dest = conn.rwInfo.toPrimary()
} else if !RWUtil.checkReadonlyByConn(conn, query) {
dest = conn.rwInfo.toPrimary()
} else if (conn.rwInfo.distribute == PRIMARY && !conn.trxFinish) ||
(conn.rwInfo.distribute == STANDBY && !conn.rwInfo.connStandby.trxFinish) {
dest = conn.rwInfo.distribute
} else if conn.IsoLevel != int32(sql.LevelSerializable) {
dest = conn.rwInfo.toAny()
} else {
dest = conn.rwInfo.toPrimary()
}
if dest == PRIMARY {
conn.rwInfo.connCurrent = conn
} else {
conn.rwInfo.connCurrent = conn.rwInfo.connStandby
}
return dest
}
func (RWUtil rwUtil) distributeSqlByStmt(stmt *DmStatement) RWSiteEnum {
var dest RWSiteEnum
if !RWUtil.isStandbyAlive(stmt.dmConn) {
dest = stmt.dmConn.rwInfo.toPrimary()
} else if !RWUtil.checkReadonlyByStmt(stmt) {
dest = stmt.dmConn.rwInfo.toPrimary()
} else if (stmt.dmConn.rwInfo.distribute == PRIMARY && !stmt.dmConn.trxFinish) ||
(stmt.dmConn.rwInfo.distribute == STANDBY && !stmt.dmConn.rwInfo.connStandby.trxFinish) {
dest = stmt.dmConn.rwInfo.distribute
} else if stmt.dmConn.IsoLevel != int32(sql.LevelSerializable) {
dest = stmt.dmConn.rwInfo.toAny()
} else {
dest = stmt.dmConn.rwInfo.toPrimary()
}
if dest == STANDBY && !RWUtil.isStandbyStatementValid(stmt) {
var err error
stmt.rwInfo.stmtStandby, err = stmt.dmConn.rwInfo.connStandby.prepare(stmt.nativeSql)
if err != nil {
dest = stmt.dmConn.rwInfo.toPrimary()
}
}
if dest == PRIMARY {
stmt.rwInfo.stmtCurrent = stmt
} else {
stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
}
return dest
}
func (RWUtil rwUtil) isStandbyAlive(connection *Connection) bool {
return connection.rwInfo.connStandby != nil && !connection.rwInfo.connStandby.closed.IsSet()
}
func (RWUtil rwUtil) isStandbyStatementValid(statement *DmStatement) bool {
return statement.rwInfo.stmtStandby != nil && !statement.rwInfo.stmtStandby.closed
}
func (RWUtil rwUtil) copyStatement(srcStmt *DmStatement, destStmt *DmStatement) {
destStmt.nativeSql = srcStmt.nativeSql
destStmt.params = srcStmt.params
destStmt.paramCount = srcStmt.paramCount
destStmt.curRowBindIndicator = srcStmt.curRowBindIndicator
}