-
Notifications
You must be signed in to change notification settings - Fork 0
/
i.go
233 lines (199 loc) · 4.91 KB
/
i.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
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"context"
"strconv"
"strings"
"time"
)
const (
STATUS_VALID_TIME = 20 * time.Second // ms
// sort 值
SORT_SERVER_MODE_INVALID = -1 // 不允许连接的模式
SORT_SERVER_NOT_ALIVE = -2 // 站点无法连接
SORT_UNKNOWN = 0 // 站点还未连接过,模式未知
SORT_PRIMARY = 10
SORT_STANDBY = 20
SORT_NORMAL = 30
// OPEN>MOUNT>SUSPEND
SORT_OPEN = 3
SORT_MOUNT = 2
SORT_SUSPEND = 1
)
type DB struct {
host string
port int
alive bool
statusRefreshTs int // 状态更新的时间点
serverMode int
serverStatus int
sort int
}
func newDB(host string, port int) *DB {
db := new(DB)
db.host = host
db.port = port
db.serverMode = -1
db.serverStatus = -1
db.sort = SORT_UNKNOWN
return db
}
func (db *DB) getSort(checkTime bool) int {
if checkTime {
if int64(time.Now().Nanosecond()-db.statusRefreshTs) < int64(STATUS_VALID_TIME) {
return db.sort
} else {
return SORT_UNKNOWN
}
}
return db.sort
}
func (db *DB) calcSort(loginMode int) int {
sort := 0
switch loginMode {
case LOGIN_MODE_ALL_AND_PRIMARY_FIRST:
{
// 主机优先:PRIMARY>NORMAL>STANDBY
switch db.serverMode {
case SERVER_MODE_NORMAL:
sort += SORT_NORMAL * 10
case SERVER_MODE_PRIMARY:
sort += SORT_PRIMARY * 100
case SERVER_MODE_STANDBY:
sort += SORT_STANDBY
}
}
case LOGIN_MODE_ALL_AND_STANDBY_FIRST:
{
// STANDBY优先: STANDBY>PRIMARY>NORMAL
switch db.serverMode {
case SERVER_MODE_NORMAL:
sort += SORT_NORMAL
case SERVER_MODE_PRIMARY:
sort += SORT_PRIMARY * 10
case SERVER_MODE_STANDBY:
sort += SORT_STANDBY * 100
}
}
case LOGIN_MODE_ALL_AND_NORMAL_FIRST:
{
// NORMAL优先:NORMAL>PRIMARY>STANDBY
switch db.serverMode {
case SERVER_MODE_NORMAL:
sort += SORT_NORMAL * 100
case SERVER_MODE_PRIMARY:
sort += SORT_PRIMARY * 10
case SERVER_MODE_STANDBY:
sort += SORT_STANDBY
}
}
case LOGIN_MODE_PRIMARY:
if db.serverMode != SERVER_MODE_PRIMARY {
return SORT_SERVER_MODE_INVALID
}
sort += SORT_PRIMARY
case LOGIN_MODE_STANDBY:
if db.serverMode != SERVER_MODE_STANDBY {
return SORT_SERVER_MODE_INVALID
}
sort += SORT_STANDBY
}
switch db.serverStatus {
case SERVER_STATUS_MOUNT:
sort += SORT_MOUNT
case SERVER_STATUS_OPEN:
sort += SORT_OPEN
case SERVER_STATUS_SUSPEND:
sort += SORT_SUSPEND
}
return sort
}
func (db *DB) refreshStatus(alive bool, conn *DmConnection) {
db.alive = alive
db.statusRefreshTs = time.Now().Nanosecond()
if alive {
db.serverMode = int(conn.SvrMode)
db.serverStatus = int(conn.SvrStat)
db.sort = db.calcSort(conn.dmConnector.loginMode)
} else {
db.serverMode = -1
db.serverStatus = -1
db.sort = SORT_SERVER_NOT_ALIVE
}
}
func (db *DB) connect(connector *DmConnector, first bool) (*DmConnection, error) {
connector.host = db.host
connector.port = db.port
conn, err := connector.connectSingle(context.Background())
if err != nil {
db.refreshStatus(false, conn)
return nil, err
}
db.refreshStatus(true, conn)
// 模式不匹配, 这里使用的是连接之前的sort,连接之后server的状态可能发生改变sort也可能改变
if conn.dmConnector.loginStatus == SERVER_STATUS_OPEN && int(conn.SvrStat) != SERVER_STATUS_OPEN {
conn.close()
return nil, ECGO_INVALID_SERVER_MODE.throw()
}
if !db.checkServerMode(first, conn) {
conn.close()
return nil, ECGO_INVALID_SERVER_MODE.throw()
}
return conn, nil
}
func (db *DB) checkServerMode(first bool, conn *DmConnection) bool {
if !first {
switch conn.dmConnector.loginMode {
case LOGIN_MODE_PRIMARY:
return int(conn.SvrMode) == SERVER_MODE_PRIMARY
case LOGIN_MODE_STANDBY:
return int(conn.SvrMode) == SERVER_MODE_STANDBY
default:
return true
}
}
switch conn.dmConnector.loginMode {
case LOGIN_MODE_ALL_AND_PRIMARY_FIRST, LOGIN_MODE_PRIMARY:
return int(conn.SvrMode) == SERVER_MODE_PRIMARY
case LOGIN_MODE_ALL_AND_STANDBY_FIRST, LOGIN_MODE_STANDBY:
return int(conn.SvrMode) == SERVER_MODE_STANDBY
case LOGIN_MODE_ALL_AND_NORMAL_FIRST:
return int(conn.SvrMode) == SERVER_MODE_NORMAL
}
return false
}
func (db *DB) getServerStatusDesc(serverStatus int) string {
ret := ""
switch db.serverStatus {
case SERVER_STATUS_OPEN:
ret = "OPEN"
case SERVER_STATUS_MOUNT:
ret = "MOUNT"
case SERVER_STATUS_SUSPEND:
ret = "SUSPEND"
default:
ret = "UNKNOW"
}
return ret
}
func (db *DB) getServerModeDesc(serverMode int) string {
ret := ""
switch db.serverMode {
case SERVER_MODE_NORMAL:
ret = "NORMAL"
case SERVER_MODE_PRIMARY:
ret = "PRIMARY"
case SERVER_MODE_STANDBY:
ret = "STANDBY"
default:
ret = "UNKNOW"
}
return ret
}
func (db *DB) String() string {
return strings.TrimSpace(db.host) + ":" + strconv.Itoa(db.port) +
" (" + db.getServerModeDesc(db.serverMode) + ", " + db.getServerStatusDesc(db.serverStatus) + ")"
}