Skip to content

Commit 00ad596

Browse files
author
wuhua3
committed
support grey cluster
code review code review code review unit test codereview code review
1 parent 8c2ffe6 commit 00ad596

File tree

7 files changed

+84
-23
lines changed

7 files changed

+84
-23
lines changed

cluster/clusterGroup.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cluster
33
import (
44
motan "github.com/weibocom/motan-go/core"
55
vlog "github.com/weibocom/motan-go/log"
6+
"strconv"
67
"strings"
78
"sync/atomic"
89
)
@@ -14,6 +15,7 @@ const (
1415
type ClusterGroup struct {
1516
sandboxClusters []motan.Cluster
1617
backupClusters []motan.Cluster
18+
greyClusters []motan.Cluster
1719
backupIndex uint32
1820

1921
clusterSelector motan.ClusterSelector
@@ -44,6 +46,13 @@ func NewClusterGroup(context *motan.Context, extFactory motan.ExtensionFactory,
4446
vlog.Infof("init backup clusters success. master cluster url: %s, backup groups: %s", url.ToExtInfo(), backupGroup)
4547
}
4648

49+
// gray clusters
50+
greyGroups := url.GetParam(motan.GreyGroupsKey, motan.GetDefaultGreyGroups())
51+
if greyGroups != "" {
52+
clusterGroup.greyClusters = createMultiClusters(context, extFactory, url, proxy, greyGroups, false, true)
53+
vlog.Infof("init grey clusters success. master cluster url: %s, grey groups: %s", url.ToExtInfo(), greyGroups)
54+
}
55+
4756
// cluster selector
4857
clusterSelectorKey := url.GetParam(motan.ClusterSelectorKey, DefaultClusterSelectorName)
4958
clusterSelector := extFactory.GetClusterSelector(clusterSelectorKey)
@@ -72,6 +81,9 @@ func (c *ClusterGroup) GetRuntimeInfo() map[string]interface{} {
7281
for _, cluster := range c.backupClusters {
7382
info["backup-"+cluster.GetIdentity()] = cluster.GetRuntimeInfo()
7483
}
84+
for _, cluster := range c.greyClusters {
85+
info["grey-"+cluster.GetIdentity()] = cluster.GetRuntimeInfo()
86+
}
7587
return info
7688
}
7789

@@ -114,6 +126,9 @@ func (c *ClusterGroup) Destroy() {
114126
for _, cluster := range c.backupClusters {
115127
cluster.Destroy()
116128
}
129+
for _, cluster := range c.greyClusters {
130+
cluster.Destroy()
131+
}
117132
c.clusterSelector.Destroy()
118133
}
119134

@@ -129,6 +144,10 @@ func (c *ClusterGroup) GetBackupClusters() []motan.Cluster {
129144
return c.backupClusters
130145
}
131146

147+
func (c *ClusterGroup) GetGreyClusters() []motan.Cluster {
148+
return c.greyClusters
149+
}
150+
132151
func (c *ClusterGroup) SetRefersFilter(rf motan.RefersFilter) {
133152
c.masterCluster.SetRefersFilter(rf)
134153
for _, cluster := range c.sandboxClusters {
@@ -144,23 +163,20 @@ func createMultiClusters(context *motan.Context, extFactory motan.ExtensionFacto
144163
groups := motan.TrimSplitSet(groupsStr, motan.GroupNameSeparator)
145164
for group := range groups {
146165
if strings.HasPrefix(group, motan.GroupSuffixString) {
147-
group = url.Group + group[len(motan.GroupSuffixString):]
166+
group = group[len(motan.GroupSuffixString):]
167+
// if the url.Group end with the group, skip it
168+
if strings.HasSuffix(url.Group, group) {
169+
continue
170+
}
171+
group = url.Group + group
148172
}
149173
// if the group same as url.Group, skip it
150174
if group == url.Group {
151175
continue
152176
}
153177
newUrl := url.Copy()
154-
if lazyInit {
155-
newUrl.PutParam(motan.LazyInit, "true")
156-
} else {
157-
newUrl.PutParam(motan.LazyInit, "false")
158-
}
159-
if emptyNodeNotify {
160-
newUrl.PutParam(motan.ClusterEmptyNodeNotifyKey, "true")
161-
} else {
162-
newUrl.PutParam(motan.ClusterEmptyNodeNotifyKey, "false")
163-
}
178+
newUrl.PutParam(motan.LazyInit, strconv.FormatBool(proxy))
179+
newUrl.PutParam(motan.ClusterEmptyNodeNotifyKey, strconv.FormatBool(emptyNodeNotify))
164180
newUrl.Group = group
165181
cluster := NewCluster(context, extFactory, newUrl, proxy)
166182
clusters = append(clusters, cluster)

cluster/clusterGroup_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Test_NewClusterGroup(t *testing.T) {
4545
},
4646
},
4747
{
48-
"backup and sandbox cluster",
48+
"backup、 sandbox and grey cluster",
4949
&motan.URL{
5050
Protocol: "",
5151
Host: "",
@@ -55,13 +55,15 @@ func Test_NewClusterGroup(t *testing.T) {
5555
Parameters: map[string]string{
5656
motan.BackupGroupsKey: "backup1,backup2",
5757
motan.SandboxGroupsKey: "sandbox1",
58+
motan.GreyGroupsKey: "grey1",
5859
motan.ClusterSelectorKey: DefaultClusterSelectorName,
5960
},
6061
},
6162
func(t *testing.T, clusterGroup *ClusterGroup) {
6263
assert.NotNil(t, clusterGroup.masterCluster)
6364
assert.Equal(t, 1, len(clusterGroup.GetSandboxClusters()))
6465
assert.Equal(t, 2, len(clusterGroup.GetBackupClusters()))
66+
assert.Equal(t, 1, len(clusterGroup.GetGreyClusters()))
6567
cs, ok := clusterGroup.clusterSelector.(*DefaultClusterSelector)
6668
assert.True(t, ok)
6769
assert.Equal(t, clusterGroup, cs.clusterGroup)
@@ -81,6 +83,11 @@ func Test_NewClusterGroup(t *testing.T) {
8183
assert.True(t, ok)
8284
assert.NotNil(t, v)
8385
}
86+
for _, cluster := range clusterGroup.GetGreyClusters() {
87+
v, ok = runtimeInfo["grey-"+cluster.GetIdentity()]
88+
assert.True(t, ok)
89+
assert.NotNil(t, v)
90+
}
8491
},
8592
},
8693
}

cluster/clusterSelector.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,41 @@ import (
88
type DefaultClusterSelector struct {
99
clusterGroup motan.ClusterGroup
1010
defaultSandboxCluster motan.Cluster
11+
defaultGreyCluster motan.Cluster
1112
}
1213

1314
func (d *DefaultClusterSelector) Init(clusterGroup motan.ClusterGroup) {
1415
d.clusterGroup = clusterGroup
1516
if len(clusterGroup.GetSandboxClusters()) > 0 {
1617
d.defaultSandboxCluster = clusterGroup.GetSandboxClusters()[0]
1718
}
19+
if len(clusterGroup.GetGreyClusters()) > 0 {
20+
d.defaultGreyCluster = clusterGroup.GetGreyClusters()[0]
21+
}
1822
}
1923

2024
func (d *DefaultClusterSelector) Select(request motan.Request) motan.Cluster {
21-
if d.defaultSandboxCluster != nil && len(d.defaultSandboxCluster.GetRefers()) > 0 {
22-
routeGroup := strings.TrimSpace(request.GetAttachment(MRouteGroup))
23-
if routeGroup == DefaultSandboxRouteGroup {
25+
routeGroup := request.GetAttachment(MRouteGroup)
26+
if routeGroup != "" {
27+
var sandboxGroup string
28+
var greyGroup string
29+
if d.defaultSandboxCluster != nil && len(d.defaultSandboxCluster.GetRefers()) > 0 {
30+
sandboxGroup = d.defaultSandboxCluster.GetURL().Group
31+
}
32+
if d.defaultGreyCluster != nil && len(d.defaultGreyCluster.GetRefers()) > 0 {
33+
greyGroup = d.defaultGreyCluster.GetURL().Group
34+
}
35+
if sandboxGroup != "" && strings.TrimSpace(routeGroup) == DefaultSandboxRouteGroup {
2436
return d.defaultSandboxCluster
2537
}
2638
routeGroupList := motan.TrimSplit(routeGroup, ",")
2739
for _, group := range routeGroupList {
28-
if group == d.clusterGroup.GetURL().Group {
40+
if sandboxGroup == group {
2941
return d.defaultSandboxCluster
3042
}
43+
if greyGroup == group {
44+
return d.defaultGreyCluster
45+
}
3146
}
3247
}
3348
return d.clusterGroup.GetMasterCluster()

cluster/clusterSelector_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ func TestDefaultClusterSelector_Select(t *testing.T) {
4242

4343
sandboxGroups := []string{"sandbox1", "sandbox2"}
4444
sandboxClusters := createMultiClusters(ctx, ext, url, true, strings.Join(sandboxGroups, ","), true, true)
45+
greyGroups := []string{"grey1", "grey2", "grey3"}
46+
greyClusters := createMultiClusters(ctx, ext, url, true, strings.Join(greyGroups, ","), false, true)
4547

4648
clusterGroup := &ClusterGroup{
4749
sandboxClusters: sandboxClusters,
50+
greyClusters: greyClusters,
4851
backupIndex: 0,
4952
context: nil,
5053
url: url,
@@ -90,6 +93,9 @@ func TestDefaultClusterSelector_Select(t *testing.T) {
9093
for _, c := range sandboxClusters {
9194
c.Notify(RegistryURL, urlList)
9295
}
96+
for _, c := range greyClusters {
97+
c.Notify(RegistryURL, urlList)
98+
}
9399
attachment.Store(MRouteGroup, DefaultSandboxRouteGroup)
94100
for _ = range sandboxGroups {
95101
cluster = clusterSelector.Select(request)
@@ -105,11 +111,15 @@ func TestDefaultClusterSelector_Select(t *testing.T) {
105111
assert.Equal(t, masterGroup, cluster.GetURL().Group)
106112
}
107113

108-
// sandbox group when motan-route-group equal to the master cluster group
109-
attachment.Store(MRouteGroup, url.Group)
110-
for _ = range sandboxGroups {
111-
cluster = clusterSelector.Select(request)
112-
assert.NotNil(t, cluster)
113-
assert.Equal(t, clusterSelector.defaultSandboxCluster.GetURL().Group, cluster.GetURL().Group)
114-
}
114+
// sandbox group when the motan-route-group equal to the sandbox cluster group
115+
attachment.Store(MRouteGroup, clusterSelector.defaultSandboxCluster.GetURL().Group)
116+
cluster = clusterSelector.Select(request)
117+
assert.NotNil(t, cluster)
118+
assert.Equal(t, clusterSelector.defaultSandboxCluster.GetURL().Group, cluster.GetURL().Group)
119+
120+
// grey group when the motan-route-group equal to the grey cluster group
121+
attachment.Store(MRouteGroup, clusterSelector.defaultGreyCluster.GetURL().Group)
122+
cluster = clusterSelector.Select(request)
123+
assert.NotNil(t, cluster)
124+
assert.Equal(t, clusterSelector.defaultGreyCluster.GetURL().Group, cluster.GetURL().Group)
115125
}

core/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const (
7070
UnixSockProtocolFlag = "unix://"
7171
BackupGroupsKey = "backupGroups"
7272
SandboxGroupsKey = "sandboxGroups"
73+
GreyGroupsKey = "greyGroups"
7374
ClusterSelectorKey = "clusterSelector"
7475
ClusterEmptyNodeNotifyKey = "clusterEmptyNodeNotify"
7576
)

core/globalContext.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var (
8181
extFilters = make(map[string]bool)
8282

8383
defaultSandboxGroups = ""
84+
defaultGreyGroups = ""
8485
)
8586

8687
// SetDefaultSandboxGroups Called once before Context initialization
@@ -93,6 +94,16 @@ func GetDefaultSandboxGroups() string {
9394
return defaultSandboxGroups
9495
}
9596

97+
// SetDefaultGreyGroups Called once before Context initialization
98+
func SetDefaultGreyGroups(groups string) {
99+
defaultGreyGroups = groups
100+
}
101+
102+
// GetDefaultGreyGroups returns default grey groups
103+
func GetDefaultGreyGroups() string {
104+
return defaultGreyGroups
105+
}
106+
96107
// all env flag in motan-go
97108
var (
98109
Port = flag.Int("port", 0, "agent listen port")

core/motan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ type ClusterGroup interface {
344344
GetMasterCluster() Cluster
345345
GetSandboxClusters() []Cluster
346346
GetBackupClusters() []Cluster
347+
GetGreyClusters() []Cluster
347348
SetRefersFilter(rf RefersFilter)
348349
}
349350

0 commit comments

Comments
 (0)