-
Notifications
You must be signed in to change notification settings - Fork 0
/
zw.go
220 lines (191 loc) · 5.17 KB
/
zw.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
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"math/rand"
"strconv"
"time"
"github.com/gotomicro/dmgo/util"
)
var rwMap = make(map[string]*rwCounter)
type rwCounter struct {
ntrx_primary int64
ntrx_total int64
primaryPercent float64
standbyPercent float64
standbyNTrxMap map[string]int64
standbyIdMap map[string]int32
standbyCount int32
flag []int32
increments []int32
}
func newRWCounter(primaryPercent int32, standbyCount int32) *rwCounter {
rwc := new(rwCounter)
rwc.standbyNTrxMap = make(map[string]int64)
rwc.standbyIdMap = make(map[string]int32)
rwc.reset(primaryPercent, standbyCount)
return rwc
}
func (rwc *rwCounter) reset(primaryPercent int32, standbyCount int32) {
rwc.ntrx_primary = 0
rwc.ntrx_total = 0
rwc.standbyCount = standbyCount
rwc.increments = make([]int32, standbyCount+1)
rwc.flag = make([]int32, standbyCount+1)
var gcd = util.GCD(primaryPercent*standbyCount, 100-primaryPercent)
rwc.increments[0] = primaryPercent * standbyCount / gcd
for i, tmp := 1, (100-primaryPercent)/gcd; i < len(rwc.increments); i++ {
rwc.increments[i] = tmp
}
copy(rwc.flag, rwc.increments)
if standbyCount > 0 {
rwc.primaryPercent = float64(primaryPercent) / 100.0
rwc.standbyPercent = float64(100-primaryPercent) / 100.0 / float64(standbyCount)
} else {
rwc.primaryPercent = 1
rwc.standbyPercent = 0
}
}
// 连接创建成功后调用,需要服务器返回standbyCount
func getRwCounterInstance(conn *Connection, standbyCount int32) *rwCounter {
key := conn.dmConnector.host + "_" + strconv.Itoa(int(conn.dmConnector.port)) + "_" + strconv.Itoa(int(conn.dmConnector.rwPercent))
rwc, ok := rwMap[key]
if !ok {
rwc = newRWCounter(conn.dmConnector.rwPercent, standbyCount)
rwMap[key] = rwc
} else if rwc.standbyCount != standbyCount {
rwc.reset(conn.dmConnector.rwPercent, standbyCount)
}
return rwc
}
/**
* @return 主机;
*/
func (rwc *rwCounter) countPrimary() RWSiteEnum {
rwc.adjustNtrx()
rwc.increasePrimaryNtrx()
return PRIMARY
}
/**
* @param dest 主机; 备机; any;
* @return 主机; 备机
*/
func (rwc *rwCounter) count(dest RWSiteEnum, standby *Connection) RWSiteEnum {
rwc.adjustNtrx()
switch dest {
case ANYSITE:
{
if rwc.primaryPercent == 1 || (rwc.flag[0] > rwc.getStandbyFlag(standby) && rwc.flag[0] > util.Sum(rwc.flag[1:])) {
rwc.increasePrimaryNtrx()
dest = PRIMARY
} else {
rwc.increaseStandbyNtrx(standby)
dest = STANDBY
}
}
case STANDBY:
{
rwc.increaseStandbyNtrx(standby)
}
case PRIMARY:
{
rwc.increasePrimaryNtrx()
}
}
return dest
}
/**
* 防止ntrx超出有效范围,等比调整
*/
func (rwc *rwCounter) adjustNtrx() {
if rwc.ntrx_total >= INT64_MAX {
var min int64
var i = 0
for _, num := range rwc.standbyNTrxMap {
if i == 0 || num < min {
min = num
}
i++
}
if rwc.ntrx_primary < min {
min = rwc.ntrx_primary
}
rwc.ntrx_primary /= min
rwc.ntrx_total /= min
for k, v := range rwc.standbyNTrxMap {
rwc.standbyNTrxMap[k] = v / min
}
}
if rwc.flag[0] <= 0 && util.Sum(rwc.flag[1:]) <= 0 {
// 如果主库事务数以及所有备库事务数的总和 都 <= 0, 重置事务计数,给每个库的事务计数加上初始计数值
for i := 0; i < len(rwc.flag); i++ {
rwc.flag[i] += rwc.increments[i]
}
}
}
func (rwc *rwCounter) increasePrimaryNtrx() {
rwc.ntrx_primary++
rwc.flag[0]--
rwc.ntrx_total++
}
//func (rwc *rwCounter) getStandbyNtrx(standby *DmConnection) int64 {
// key := standby.dmConnector.host + ":" + strconv.Itoa(int(standby.dmConnector.port))
// ret, ok := rwc.standbyNTrxMap[key]
// if !ok {
// ret = 0
// }
//
// return ret
//}
func (rwc *rwCounter) getStandbyId(standby *Connection) int32 {
key := standby.dmConnector.host + ":" + strconv.Itoa(int(standby.dmConnector.port))
sid, ok := rwc.standbyIdMap[key]
if !ok {
sid = int32(len(rwc.standbyIdMap) + 1) // 下标0是primary
if sid > rwc.standbyCount {
// 不在有效备库中
return -1
}
rwc.standbyIdMap[key] = sid
}
return sid
}
func (rwc *rwCounter) getStandbyFlag(standby *Connection) int32 {
sid := rwc.getStandbyId(standby)
if sid > 0 && sid < int32(len(rwc.flag)) {
// 保证备库有效
return rwc.flag[sid]
}
return 0
}
func (rwc *rwCounter) increaseStandbyNtrx(standby *Connection) {
key := standby.dmConnector.host + ":" + strconv.Itoa(int(standby.dmConnector.port))
ret, ok := rwc.standbyNTrxMap[key]
if ok {
ret += 1
} else {
ret = 1
}
rwc.standbyNTrxMap[key] = ret
sid, ok := rwc.standbyIdMap[key]
if !ok {
sid = int32(len(rwc.standbyIdMap) + 1) // 下标0是primary
rwc.standbyIdMap[key] = sid
}
rwc.flag[sid]--
rwc.ntrx_total++
}
func (rwc *rwCounter) random(rowCount int32) int32 {
rand.Seed(time.Now().UnixNano())
if rowCount > rwc.standbyCount {
return rand.Int31n(rwc.standbyCount)
} else {
return rand.Int31n(rowCount)
}
}
func (rwc *rwCounter) String() string {
return "PERCENT(P/S) : " + strconv.FormatFloat(rwc.primaryPercent, 'f', -1, 64) + "/" + strconv.FormatFloat(rwc.standbyPercent, 'f', -1, 64) + "\nNTRX_PRIMARY : " +
strconv.FormatInt(rwc.ntrx_primary, 10) + "\nNTRX_TOTAL : " + strconv.FormatInt(rwc.ntrx_total, 10) + "\nNTRX_STANDBY : "
}