Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat consist load balance #600

Merged
merged 234 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
234 commits
Select commit Hold shift + click to select a range
3a50c2a
Initial commit
AlexStocks May 3, 2022
4c6ad0f
add config file
luky116 May 4, 2022
004badf
init
luky116 May 9, 2022
f646586
add rm
luky116 May 12, 2022
580ab4a
add processor
luky116 May 14, 2022
0939057
bug fix
luky116 May 14, 2022
b6e759a
use processor on message
luky116 May 14, 2022
4016001
change default config
luky116 May 14, 2022
4579084
Merge pull request #1 from luky116/init
AlexStocks May 14, 2022
9f2665a
init github Actions
liiibpm May 18, 2022
3fd0b4c
init github Actions
liiibpm May 18, 2022
1944ae1
Merge pull request #2 from liiibpm/main
AlexStocks May 28, 2022
9095355
add tcc process
luky116 Jun 7, 2022
dd38d07
Adjust the structure of the project
luky116 Jun 7, 2022
634657b
Adjust the structure of the project
luky116 Jun 8, 2022
6d0ea72
Adjust the structure of the project
luky116 Jun 13, 2022
83cccac
fix linter error
AlexStocks Jun 13, 2022
bfa69cc
Merge pull request #4 from luky116/init
AlexStocks Jun 13, 2022
7ceb462
fix cli
luky116 Jun 13, 2022
8a78ef8
Merge pull request #5 from luky116/init
AlexStocks Jun 13, 2022
986e5f8
add apache license
luky116 Jun 13, 2022
38b6804
Merge pull request #6 from luky116/init
AlexStocks Jun 14, 2022
4c36bc4
fix config
luky116 Jun 14, 2022
114147c
add unit test
Jun 15, 2022
ed9b0a7
add go imports shell
luky116 Jun 16, 2022
a1da44e
Adjust project directory
luky116 Jun 17, 2022
1044c64
remove iota
Jun 17, 2022
29d735b
fix cli bug
Jun 17, 2022
681f298
remove the useless comments
luky116 Jun 17, 2022
7511240
Merge remote-tracking branch 'my/addUnitTest' into addUnitTest
luky116 Jun 17, 2022
38ef36c
Merge pull request #10 from luky116/addUnitTest
AlexStocks Jun 17, 2022
e334fc5
fix branch commit bug
luky116 Jun 18, 2022
0a92500
Merge pull request #15 from luky116/fixBranchCommitBug
AlexStocks Jun 19, 2022
abb4f0d
remove goetty package
luky116 Jun 19, 2022
0c507d4
Merge pull request #18 from luky116/removeGoetty
AlexStocks Jun 20, 2022
1527d84
Optimize codec code
Jun 20, 2022
5f48f35
Merge pull request #19 from luky116/optimizeCodec
AlexStocks Jun 23, 2022
b441c09
Create enhancement.md
luky116 Jun 25, 2022
0665905
Merge pull request #21 from luky116/patch-1
AlexStocks Jun 25, 2022
2b5e21c
style:change bool to struct{}
Jun 29, 2022
3c20d22
Merge pull request #34 from baerwang/optimization
AlexStocks Jun 29, 2022
2fc1aaf
fix commit and rollback codec
luky116 Jun 29, 2022
55f6e13
realize branchReport
liiibpm Jun 30, 2022
27bac0a
add seata-go samples
luky116 Jun 30, 2022
6c37558
solve conflice
luky116 Jun 30, 2022
682a97c
add processor unittest
cgDeepLearn Jul 1, 2022
fdc2e0b
Merge pull request #31 from luky116/fix-codec
AlexStocks Jul 1, 2022
fa3a28b
Merge pull request #36 from luky116/add-samples
AlexStocks Jul 2, 2022
cd28efc
remove blank and annotation
cgDeepLearn Jul 2, 2022
2c8e490
Merge branch 'seata:main' into main
liiibpm Jul 2, 2022
14a3850
branchReport return value judgment
liiibpm Jul 2, 2022
8b4dedf
optimize-zap-log
106umao Jul 4, 2022
32a3b94
update changed the format of log
106umao Jul 6, 2022
22f7be4
remove the duplicate code same with sample/local
106umao Jul 6, 2022
598b039
optimize format of logging
106umao Jul 9, 2022
9d10508
optimize format of logging
106umao Jul 9, 2022
0473b67
optimize format of logging
106umao Jul 9, 2022
6f862b2
Merge pull request #37 from liiibpm/main
AlexStocks Jul 10, 2022
c3c5d46
Merge pull request #39 from cgDeepLearn/processor-unittest
AlexStocks Jul 10, 2022
84f8ec2
Update logging.go
AlexStocks Jul 10, 2022
58ee495
Merge pull request #45 from 106umao/optimize-zap-log
AlexStocks Jul 10, 2022
511859f
Merge pull request #46 from 106umao/duplicate-code
AlexStocks Jul 10, 2022
ae5eab8
feature add action context for tcc
106umao Jul 11, 2022
12e2194
add err check for unmarshal json
106umao Jul 11, 2022
d130a9e
Merge pull request #48 from 106umao/ActionContextForTcc
AlexStocks Jul 12, 2022
5e2319e
Feat add two phase (#122)
luky116 Jul 19, 2022
ed33bad
fix getty auto close bug (#130)
luky116 Jul 19, 2022
d356756
optimize named for the resource manager api and tcc resource, adjust …
106umao Jul 23, 2022
233e919
docs: add readme ,contributing and pr template doc (#153)
luky116 Jul 26, 2022
1290838
add license (#146)
Penglq Jul 26, 2022
e1b03ed
bugfix: fix rollback response status bug (#155)
luky116 Jul 28, 2022
62f61c1
feature add unit test and labeler workflow (#165)
106umao Jul 28, 2022
6500da6
fix typo in reademe (#167)
106umao Jul 28, 2022
ab0f45b
doc: add seata server startup docker compose file (#172)
luky116 Jul 30, 2022
d8eca5c
doc: add release 0.1.0 changes note (#169)
luky116 Jul 30, 2022
d7f6ffa
optimize add ut for message (#154)
106umao Aug 1, 2022
74c0e5c
optimieze add ut for tm (#163)
106umao Aug 1, 2022
c37c161
[modify] git ignore add build products in 'dist/' (#177)
elrond-g Aug 4, 2022
05bf7b1
bugfix fix ut bug of msg, it will cause ci failed (#176)
106umao Aug 5, 2022
6c8fb25
optimize: optimise way of init seata (#187)
luky116 Aug 8, 2022
b07c2cf
Feature add tcc branch report (#190)
wang1309 Aug 9, 2022
d147103
refactor(pkg/rm): optimize function's parameters into one struct (#196)
Aug 10, 2022
fbbd682
feature add integration for grpc (#158)
106umao Aug 11, 2022
1797722
optimize: remove unnecessary codes (#208)
a631807682 Aug 11, 2022
7f3d6d9
Add unit testing for getty (#203)
betterwinsone Aug 11, 2022
45bf050
optimize workflow, add condecov and issue, stale robot (#202)
106umao Aug 12, 2022
df69626
Feature add tcc branch report unit test (#210)
wang1309 Aug 12, 2022
e1dcdfe
optimize: support instance BusinessActionContext outside the TCC try …
luky116 Aug 13, 2022
dc93beb
add dubbo transtation filter test (#204)
jasondeng1997 Aug 14, 2022
ed7bef5
optimize ResourceManagerInbound function's parameters into one struct…
liiibpm Aug 15, 2022
ebad10e
feature add tcc grpc sample,adjust register resource and branch regis…
106umao Aug 16, 2022
c5ce148
fix enhancement: make time parameters easier to read (#215)
jasondeng1997 Aug 16, 2022
b591b73
feature/datasource_at merge to master (#213)
chuntaojun Aug 17, 2022
40133c5
add rm test (#192)
Aug 22, 2022
bd18186
optimize: add some todo comment, add a undo hook sample (#238)
luky116 Aug 23, 2022
fb985f5
replace the underline naming in the code with the hump naming method …
jasondeng1997 Aug 23, 2022
739821f
fix: do register resource where execute OpenConnector function (#237)
luky116 Aug 24, 2022
37b8e40
optimize Add unit testing for common (#229)
miaoxueyu Aug 24, 2022
c230965
bugfix: fix infinite loop of asyncCallback (#230)
PangXing Aug 24, 2022
47682c4
feature: add undo log manager delete (#240)
wang1309 Sep 2, 2022
70a93e2
frature: add update sql parser (#243)
luky116 Sep 3, 2022
0db6531
feature: add license github action (#254)
luky116 Sep 5, 2022
5da5904
fix global transation time out (#258)
luky116 Sep 6, 2022
3b395cf
feature add fence for tcc, and add fence sample in tcc local mode. (#…
106umao Sep 15, 2022
7167a33
frature: add update sql parser and remove tidb parser (#264)
jasondeng1997 Sep 17, 2022
328aee7
fix ci failed because mock is invalid (#263)
106umao Sep 17, 2022
47053ee
optimize: nested loop retries (#261)
106umao Sep 18, 2022
f60f768
feat: add http tcc (#280)
georgehao Sep 21, 2022
5e029a8
feat: optimize retry (#284)
georgehao Sep 22, 2022
b753049
optimize: split client.Init into rm.Init and tm.Init methods (#286)
jasondeng1997 Sep 25, 2022
533afe8
optimize: optimize global transaction usage (#281)
106umao Sep 26, 2022
485312c
feat: add has undo log table (#245)
wang1309 Sep 26, 2022
7167474
(WIP)refactor:seata conn (#287)
chuntaojun Sep 27, 2022
0042b76
feat: add mysql update undo log builder (#288)
luky116 Sep 28, 2022
b9b7d8c
change dubbo-go version (#302)
luky116 Oct 8, 2022
d0f98f4
test:add DoParser ut (#299)
baerwang Oct 8, 2022
ba06918
config github action not to automatically close ps or issue (#305)
luky116 Oct 8, 2022
38924b0
add mysql delete undo log builder (#296)
raspberry-hu Oct 8, 2022
8bbc5b5
feat: add sync worker and fmt (#303)
georgehao Oct 9, 2022
15d6a1a
Feat add mysql update after undo log builder (#289)
luky116 Oct 9, 2022
93aa852
format:format code
wangrui130-tal Oct 11, 2022
1db393f
format:format code
wangrui130-tal Oct 11, 2022
eadf8bf
feat:add mysql table meta func && sql addEscape, delEscape func issue…
wang1309 Oct 13, 2022
61a4c23
feat: init compressor type (#309)
luky116 Oct 15, 2022
7d1dfdf
refactor compress (#318)
luky116 Oct 15, 2022
4109db9
Feat multi undo log builder (#301)
luky116 Oct 15, 2022
0ab3ea2
Merge remote-tracking branch 'upstream/master'
Oct 16, 2022
14ab243
Merge remote-tracking branch 'upstream/master'
wang1309 Oct 16, 2022
b20f33b
feat: fix fanout test data race (#326)
georgehao Oct 23, 2022
8fbdd3f
Refactor seata conn (#295)
chuntaojun Oct 23, 2022
661a647
feat(compressor): deflate compress (#321)
Oct 23, 2022
eea542f
Add LZ4 compressor support. (#324)
WyattJia Oct 23, 2022
ea67dc4
add zstd compress (#327)
betterwinsone Oct 28, 2022
4c9195f
Feat add gzip (#322)
wang1309 Oct 28, 2022
10c6872
feat:add undo log manager-flush undo log func #269 (#307)
betterwinsone Oct 29, 2022
7c68aef
Feat add zip #315 (#329)
raspberry-hu Oct 30, 2022
c97ff1b
Feat/multi update (#325)
georgehao Oct 30, 2022
2f4277f
feature: add select for update (#319)
luky116 Oct 30, 2022
48794de
wip:multi delete sql (#330)
baerwang Oct 31, 2022
446ed79
Merge remote-tracking branch 'upstream/master'
wangrui130-tal Nov 1, 2022
c73bf46
Merge remote-tracking branch 'upstream/master'
wangrui130-tal Nov 1, 2022
2dd7ac0
test: strengthen ut (#332)
baerwang Nov 5, 2022
f592e1b
optimize at (#336)
luky116 Nov 12, 2022
f9de2fc
Merge remote-tracking branch 'upstream/master'
Nov 12, 2022
a686a43
Merge remote-tracking branch 'upstream/master'
wang1309 Nov 12, 2022
38b554e
Feat add undo func (#320)
wang1309 Nov 13, 2022
e8cfa01
optimize: add insert undo log function (#337)
luky116 Nov 13, 2022
6713885
doc: add sample undo_log table sql
Code-Fight Nov 17, 2022
4c61c3a
optimize at commit branch (#346)
luky116 Nov 18, 2022
e0db439
fix:timeout config & panic log lost (#350)
Code-Fight Nov 19, 2022
e3d7b14
optimize meta data (#352)
luky116 Nov 19, 2022
268a72e
optimize commit (#354)
luky116 Nov 20, 2022
eaee521
optimize commit (#354)
luky116 Nov 20, 2022
b72477c
feat: modify some receiver name (#353)
georgehao Nov 20, 2022
1c22b98
Optimize rollback (#356)
luky116 Nov 20, 2022
25ef488
feat: add gin for at and fix async worker bug (#357)
georgehao Nov 20, 2022
24ea75b
feature: support ONLY_CARE_UPDATE_COLUMNS (#355)
Code-Fight Nov 20, 2022
9229a4b
Rollback (#358)
georgehao Nov 20, 2022
fed3ad4
fix bug (#359)
luky116 Nov 20, 2022
7846472
Rollback1 (#360)
georgehao Nov 20, 2022
d7254d9
Fix meta data (#363)
luky116 Nov 21, 2022
1dc20df
Fix decode image(#365)
luky116 Nov 22, 2022
6880911
add data check before rollbeck (#366)
luky116 Nov 23, 2022
b181fba
doc: v1.0.2-RC1 release file (#338)
raspberry-hu Nov 23, 2022
9bb236e
optimize: remove unless function (#369)
Code-Fight Nov 25, 2022
ae4ac1d
doc: update 1.0.2-RC1 change log (#370)
luky116 Nov 26, 2022
a2511f0
fix: at sample (#374)
luky116 Nov 26, 2022
6727499
optimize: simplify to make codes more readable (#367)
Charlie17Li Nov 30, 2022
2cbbf2f
feat:add yml config (#285)
zhangymPerson Nov 30, 2022
beabfc3
feat: support for propagation of global transactions (#262)
106umao Dec 1, 2022
05de141
feature:build undo log by insert target SQL (#333)
Code-Fight Dec 3, 2022
f3a7c86
Merge remote-tracking branch 'upstream/master'
Dec 6, 2022
ad8f6be
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 6, 2022
f71bfa7
fix at smaple sql (#385)
jasondeng1997 Dec 6, 2022
1eccddf
fix: loop recursion problem in OpenConnector (#387)
106umao Dec 6, 2022
c9fffa5
add tcc fence config logic (#383)
luky116 Dec 7, 2022
9f6d875
optimize some format (#392)
georgehao Dec 8, 2022
85d7aea
optimize at base executor (#394)
luky116 Dec 10, 2022
1b67327
feat: add tm config (#398)
Dec 12, 2022
1ef83f7
feat: add getty config (#399)
lxfeng1997 Dec 13, 2022
e6aab85
fix branch register response (#401)
luky116 Dec 13, 2022
35f29ac
optimize protocol init (#400)
Code-Fight Dec 16, 2022
f1320b5
optimize RM init (#390)
Code-Fight Dec 16, 2022
8846c93
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 16, 2022
0f1fcce
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 16, 2022
bead900
feat: support xa mysql connection (#380)
Charlie17Li Dec 16, 2022
7563891
Feat xa branch xid (#389)
wang1309 Dec 16, 2022
a82fb71
[AT] add insert on duplicate (#405)
Iunyasha Dec 17, 2022
6e314e6
optimize: refactor at executor (#397)
luky116 Dec 20, 2022
c7ff490
refactor: use new tm config in tm module. (#411)
Dec 23, 2022
bcbb864
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 24, 2022
d598b76
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 24, 2022
7f368a4
roptimize: efactor delete and insert executor (#409)
luky116 Dec 24, 2022
a1d9108
optimize: add log init (#408)
Code-Fight Dec 24, 2022
8965710
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 24, 2022
447de4c
Merge remote-tracking branch 'upstream/master'
wang1309 Dec 24, 2022
3d420a1
feature: add transport config (#406)
Chovyyyyyy Dec 24, 2022
857a35b
refactor(compressor): rename defalte_compress_test.go to deflate_comp…
Dec 29, 2022
04d1523
feat: add RM config (#412)
liiibpm Dec 31, 2022
03ad2e6
optimize readme (#417)
luky116 Jan 1, 2023
f3fb86a
feat: add service and seata config (#413)
lxfeng1997 Jan 1, 2023
856eb1a
feat: add Undo config (#410)
jasondeng1997 Jan 1, 2023
abfb512
fix undo config (#418)
luky116 Jan 1, 2023
0a5cd26
opt: optimize comments and dead code (#388)
106umao Jan 1, 2023
33151a4
Apply getty config (#421)
lxfeng1997 Jan 2, 2023
f87fbea
feat: use undo config (#419)
luky116 Jan 2, 2023
f55f05f
remove unused config (#422)
luky116 Jan 2, 2023
2eb98b3
bugfix: repair init getty failed (#423)
lxfeng1997 Jan 2, 2023
9eacf87
fix getty config (#424)
luky116 Jan 2, 2023
9f1165e
bugfix: at model execution failed (#429)
lxfeng1997 Jan 2, 2023
e86afc7
Optimize getty config (#430)
lxfeng1997 Jan 2, 2023
2c732df
use config (#436)
luky116 Jan 3, 2023
f374404
doc: add v1.0.2-RC3 change log (#431)
luky116 Jan 3, 2023
3a107da
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 3, 2023
a71af57
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 3, 2023
72a5fee
rename change-log to 1.0.3 (#443)
luky116 Jan 5, 2023
40f50b7
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 7, 2023
55e90d2
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 7, 2023
8eb2521
Merge branch 'master' of github.com:wang1309/seata-go
wang1309 Jan 7, 2023
26915c7
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 7, 2023
866b4d0
Merge remote-tracking branch 'upstream/master'
wang1309 Jan 14, 2023
77130c7
Merge remote-tracking branch 'upstream/master'
wang1309 Mar 9, 2023
c9be94a
Merge remote-tracking branch 'upstream/master'
wang1309 Apr 4, 2023
f6c537a
Merge remote-tracking branch 'upstream/master'
wang1309 May 20, 2023
910fc81
Merge remote-tracking branch 'upstream/master'
wang1309 Aug 5, 2023
a1ba6d1
add consistent hash func
wang1309 Aug 6, 2023
3e98bec
optimize code
wang1309 Aug 6, 2023
69a4f22
fixed ci
wang1309 Aug 6, 2023
4a667b6
fixed ci
wang1309 Aug 6, 2023
9d4bc1e
optimize && lock use fault
wang1309 Aug 13, 2023
20154d3
Merge branch 'master' into feat_consistLoadBalance
luky116 Feb 3, 2024
0661ce5
Merge remote-tracking branch 'upstream/master'
wang1309 Feb 3, 2024
9732f3e
Merge branch 'master' into feat_consistLoadBalance
wang1309 Feb 3, 2024
98722cd
Merge branch 'master' into feat_consistLoadBalance
luky116 Feb 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/rawbytes"

"github.com/seata/seata-go/pkg/discovery"

"github.com/seata/seata-go/pkg/datasource/sql"
Expand Down
1 change: 1 addition & 0 deletions pkg/remoting/getty/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

getty "github.com/apache/dubbo-getty"
gxsync "github.com/dubbogo/gost/sync"

"github.com/seata/seata-go/pkg/discovery"
"github.com/seata/seata-go/pkg/protocol/codec"
"github.com/seata/seata-go/pkg/remoting/config"
Expand Down
162 changes: 162 additions & 0 deletions pkg/remoting/loadbalance/consistent_hash_loadbalance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 loadbalance

import (
"crypto/md5"
"fmt"
"sort"
"sync"

getty "github.com/apache/dubbo-getty"
)

var (
once sync.Once
defaultVirtualNodeNumber = 10
consistentInstance *Consistent
)

type Consistent struct {
sync.RWMutex
virtualNodeCount int
// consistent hashCircle
hashCircle map[int64]getty.Session
sortedHashNodes []int64
}

func (c *Consistent) put(key int64, session getty.Session) {
c.Lock()
defer c.Unlock()
c.hashCircle[key] = session
}

func (c *Consistent) hash(key string) int64 {
hashByte := md5.Sum([]byte(key))
var res int64
for i := 0; i < 4; i++ {
res <<= 8
res |= int64(hashByte[i]) & 0xff
}

return res
}

// pick get a node
func (c *Consistent) pick(sessions *sync.Map, key string) getty.Session {
hashKey := c.hash(key)
index := sort.Search(len(c.sortedHashNodes), func(i int) bool {
return c.sortedHashNodes[i] >= hashKey
})

if index == len(c.sortedHashNodes) {
return RandomLoadBalance(sessions, key)
}

c.RLock()
session, ok := c.hashCircle[c.sortedHashNodes[index]]
if !ok {
c.RUnlock()
return RandomLoadBalance(sessions, key)
}
c.RUnlock()

if session.IsClosed() {
go c.refreshHashCircle(sessions)
return c.firstKey()
}

return session
}

// refreshHashCircle refresh hashCircle
func (c *Consistent) refreshHashCircle(sessions *sync.Map) {
var sortedHashNodes []int64
hashCircle := make(map[int64]getty.Session)
var session getty.Session
sessions.Range(func(key, value interface{}) bool {
session = key.(getty.Session)
for i := 0; i < defaultVirtualNodeNumber; i++ {
if !session.IsClosed() {
position := c.hash(fmt.Sprintf("%s%d", session.RemoteAddr(), i))
hashCircle[position] = session
sortedHashNodes = append(sortedHashNodes, position)
} else {
sessions.Delete(key)
}
}
return true
})

// virtual node sort
sort.Slice(sortedHashNodes, func(i, j int) bool {
return sortedHashNodes[i] < sortedHashNodes[j]
})

c.sortedHashNodes = sortedHashNodes
c.hashCircle = hashCircle
}

func (c *Consistent) firstKey() getty.Session {
c.RLock()
defer c.RUnlock()

if len(c.sortedHashNodes) > 0 {
return c.hashCircle[c.sortedHashNodes[0]]
}

return nil
}

func newConsistenceInstance(sessions *sync.Map) *Consistent {
once.Do(func() {
consistentInstance = &Consistent{
hashCircle: make(map[int64]getty.Session),
}
// construct hash circle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

构建哈希环的这部分代码和refreshHashCircle函数中的高度重合,是不是可以重构下,调用refreshHashCircle函数。

sessions.Range(func(key, value interface{}) bool {
session := key.(getty.Session)
for i := 0; i < defaultVirtualNodeNumber; i++ {
if !session.IsClosed() {
position := consistentInstance.hash(fmt.Sprintf("%s%d", session.RemoteAddr(), i))
consistentInstance.put(position, session)
consistentInstance.sortedHashNodes = append(consistentInstance.sortedHashNodes, position)
} else {
sessions.Delete(key)
}
}
return true
})

// virtual node sort
sort.Slice(consistentInstance.sortedHashNodes, func(i, j int) bool {
return consistentInstance.sortedHashNodes[i] < consistentInstance.sortedHashNodes[j]
})
})

return consistentInstance
}

func ConsistentHashLoadBalance(sessions *sync.Map, xid string) getty.Session {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前getty模块目前调用Select时的sessions地址是不变的,所以这里使用单例模式也就没有问题。
不过外部的代码随时可能改变,出于代码健壮性考虑,是不是处理下sessions地址改变的情况会更好?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

突然想起来,我这里有一个 refreshHashCircle 方法,如果 session 地址发生变更,session 相当于 close了,我会重新构造 hashCircle,所以应该能覆盖到这种变更的case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wang
ConsistentHashLoadBalance实例的创建过程使用了once,当SessionManager管理的sessions *sync.Map地址发生变化,再次调用ConsistentHashLoadBalance,ConsistentHashLoadBalance内部的sessions还是原来的旧地址,新旧地址指向两组完全不同的sessions。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wang ConsistentHashLoadBalance实例的创建过程使用了once,当SessionManager管理的sessions *sync.Map地址发生变化,再次调用ConsistentHashLoadBalance,ConsistentHashLoadBalance内部的sessions还是原来的旧地址,新旧地址指向两组完全不同的sessions。

对对对,早上还是不够清醒,哈哈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在 consistentInstance.pick(sessions, xid) 的时候会把最新的 sessions 传进去,如果检测到无效地址会重新构建单例对象的hash环,应该是cover住了这个场景。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外部sessions中的元素可能会增加、减少,pick函数中只检测closed是不够的。

记录一下上次传入的sessions地址,每次被调用时比较下,地址不同就refresh。这个思路你觉得怎么样?

if consistentInstance == nil {
newConsistenceInstance(sessions)
}

// pick a node
return consistentInstance.pick(sessions, xid)
}
52 changes: 52 additions & 0 deletions pkg/remoting/loadbalance/consistent_hash_loadbalance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 loadbalance

import (
"fmt"
"sync"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/seata/seata-go/pkg/remoting/mock"
)

func TestConsistentHashLoadBalance(t *testing.T) {
ctrl := gomock.NewController(t)
sessions := &sync.Map{}

for i := 0; i < 3; i++ {
session := mock.NewMockTestSession(ctrl)
session.EXPECT().IsClosed().Return(false).AnyTimes()
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string {
return "127.0.0.1:8000"
})
sessions.Store(session, fmt.Sprintf("session-%d", i))
}

result := ConsistentHashLoadBalance(sessions, "test_xid")
assert.NotNil(t, result)
assert.False(t, result.IsClosed())

sessions.Range(func(key, value interface{}) bool {
t.Logf("key: %v, value: %v", key, value)
return true
})
}
2 changes: 2 additions & 0 deletions pkg/remoting/loadbalance/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func Select(loadBalanceType string, sessions *sync.Map, xid string) getty.Sessio
return RandomLoadBalance(sessions, xid)
case xidLoadBalance:
return XidLoadBalance(sessions, xid)
case consistentHashLoadBalance:
return ConsistentHashLoadBalance(sessions, xid)
case leastActiveLoadBalance:
return LeastActiveLoadBalance(sessions, xid)
case roundRobinLoadBalance:
Expand Down
3 changes: 2 additions & 1 deletion pkg/remoting/loadbalance/random_loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/seata/seata-go/pkg/remoting/mock"
"github.com/stretchr/testify/assert"

"github.com/seata/seata-go/pkg/remoting/mock"
)

func TestRandomLoadBalance_Normal(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/remoting/loadbalance/xid_loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/seata/seata-go/pkg/remoting/mock"
"github.com/stretchr/testify/assert"

"github.com/seata/seata-go/pkg/remoting/mock"
)

func TestXidLoadBalance(t *testing.T) {
Expand Down
Loading