Skip to content

Commit

Permalink
Merge pull request #388 from cocowh/feat/group_suffix_wh
Browse files Browse the repository at this point in the history
group suffix
  • Loading branch information
rayzhang0603 authored Mar 14, 2024
2 parents e1e42da + 4395a46 commit af7c115
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ const (
DirectRPCEnvironmentName = "MESH_DIRECT_RPC"
FilterEnvironmentName = "MESH_FILTERS"
HandlerEnvironmentName = "MESH_ADMIN_EXT_HANDLERS"
RegGroupSuffix = "RPC_REG_GROUP_SUFFIX"
SubGroupSuffix = "MESH_MULTI_SUB_GROUP_SUFFIX"
)

// meta keys
Expand Down
52 changes: 51 additions & 1 deletion core/globalContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,57 @@ func (c *Context) parseMultipleServiceGroup(motanServiceMap map[string]*URL) {
}
}

func (c *Context) parseRegGroupSuffix(urlMap map[string]*URL) {
regGroupSuffix := os.Getenv(RegGroupSuffix)
if regGroupSuffix == "" {
return
}
filterMap := make(map[string]struct{}, len(urlMap))
for _, url := range urlMap {
filterMap[url.GetIdentityWithRegistry()] = struct{}{}
}
for k, url := range urlMap {
if strings.HasSuffix(url.Group, regGroupSuffix) {
continue
}
newUrl := url.Copy()
newUrl.Group += regGroupSuffix
if _, ok := filterMap[newUrl.GetIdentityWithRegistry()]; ok {
continue
}
filterMap[newUrl.GetIdentityWithRegistry()] = struct{}{}
urlMap[k] = newUrl
}
}

func (c *Context) parseSubGroupSuffix(urlMap map[string]*URL) {
subGroupSuffix := os.Getenv(SubGroupSuffix)
if subGroupSuffix == "" || c.AgentURL == nil {
return
}
filterMap := make(map[string]struct{}, len(urlMap)*2)
for _, url := range urlMap {
filterMap[url.GetIdentity()] = struct{}{}
}
for k, url := range urlMap {
if strings.HasSuffix(url.Group, subGroupSuffix) {
continue
}
groupWithSuffix := url.Group + subGroupSuffix
newUrl := url.Copy()
newUrl.Group += subGroupSuffix
if _, ok := filterMap[newUrl.GetIdentity()]; ok {
continue
}
filterMap[newUrl.GetIdentity()] = struct{}{}
urlMap["auto_"+k+groupWithSuffix] = newUrl
}
}

func (c *Context) parseRefers() {
c.RefersURLs = c.basicConfToURLs(refersSection)
referUrls := c.basicConfToURLs(refersSection)
c.parseSubGroupSuffix(referUrls)
c.RefersURLs = referUrls
}

func (c *Context) parseBasicRefers() {
Expand All @@ -542,6 +591,7 @@ func (c *Context) parseBasicRefers() {
func (c *Context) parseServices() {
urlsMap := c.basicConfToURLs(servicesSection)
c.parseMultipleServiceGroup(urlsMap)
c.parseRegGroupSuffix(urlsMap)
c.ServiceURLs = urlsMap
}

Expand Down
164 changes: 164 additions & 0 deletions core/globalContext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,170 @@ func TestContext_parseMultipleServiceGroup(t *testing.T) {
assert.Equal(t, data3["service1"].Group, "hello")
}

func TestContext_parseRegGroupSuffix(t *testing.T) {
regGroupSuffix := "-test"
countGroup := func(urlMap map[string]*URL) map[string]int {
groupMap := map[string]int{}
for _, i2 := range urlMap {
groupMap[i2.Group] += 1
}
return groupMap
}
cases := []struct {
UrlMap map[string]*URL
AssertFunc func(t *testing.T, urlMap map[string]*URL)
}{
{
UrlMap: map[string]*URL{
"service1": {
Group: "group1",
},
"service2": {
Group: "group1",
},
"service3": {
Group: "group2",
},
"service4": {
Group: "group3",
},
"service5": {
Group: "group3" + regGroupSuffix,
},
"service6": {
Group: "group4",
Parameters: map[string]string{
RegistryKey: "reg1",
},
},
"service7": {
Group: "group4",
Parameters: map[string]string{
RegistryKey: "reg2",
},
},
"service8": {
Group: "group5",
Parameters: map[string]string{
RegistryKey: "reg1",
},
},
"service9": {
Group: "group5" + regGroupSuffix,
Parameters: map[string]string{
RegistryKey: "reg2",
},
},
},
AssertFunc: func(t *testing.T, urlMap map[string]*URL) {
groupMap := countGroup(urlMap)
assert.Equal(t, 1, groupMap["group1"])
assert.Equal(t, 1, groupMap["group1"+regGroupSuffix])
assert.Equal(t, 1, groupMap["group2"+regGroupSuffix])
assert.Equal(t, 1, groupMap["group3"])
assert.Equal(t, 1, groupMap["group3"+regGroupSuffix])
assert.Equal(t, 2, groupMap["group4"+regGroupSuffix])
assert.Equal(t, 2, groupMap["group5"+regGroupSuffix])
},
},
}
os.Setenv(RegGroupSuffix, regGroupSuffix)
ctx := &Context{}
for _, s := range cases {
ctx.parseRegGroupSuffix(s.UrlMap)
s.AssertFunc(t, s.UrlMap)
}
os.Unsetenv(RegGroupSuffix)
}

func TestContext_parseSubGroupSuffix(t *testing.T) {
subGroupSuffix := "-test"
countGroup := func(urlMap map[string]*URL) map[string]int {
groupMap := map[string]int{}
for _, i2 := range urlMap {
groupMap[i2.Group] += 1
}
return groupMap
}
cases := []struct {
Ctx *Context
UrlMap map[string]*URL
AssertFunc func(t *testing.T, urlMap map[string]*URL)
}{
{
Ctx: &Context{
AgentURL: &URL{},
},
UrlMap: map[string]*URL{
"refer1": {
Group: "group1",
Path: "p1",
},
"refer2": {
Group: "group1",
Path: "p2",
},
"refer3": {
Group: "group2",
},
"refer4": {
Group: "group3",
},
"refer5": {
Group: "group3" + subGroupSuffix,
},
},
AssertFunc: func(t *testing.T, urlMap map[string]*URL) {
groupMap := countGroup(urlMap)
assert.Equal(t, 2, groupMap["group1"])
assert.Equal(t, 2, groupMap["group1"+subGroupSuffix])
assert.Equal(t, 1, groupMap["group2"])
assert.Equal(t, 1, groupMap["group2"+subGroupSuffix])
assert.Equal(t, 1, groupMap["group3"])
assert.Equal(t, 1, groupMap["group3"+subGroupSuffix])
},
},
{
Ctx: &Context{},
UrlMap: map[string]*URL{
"refer1": {
Group: "group1",
Path: "p1",
},
"refer2": {
Group: "group1",
Path: "p2",
},
"refer3": {
Group: "group2",
},
"refer4": {
Group: "group3",
},
"refer5": {
Group: "group3" + subGroupSuffix,
},
},
AssertFunc: func(t *testing.T, urlMap map[string]*URL) {
groupMap := countGroup(urlMap)
assert.Equal(t, 2, groupMap["group1"])
assert.Equal(t, 0, groupMap["group1"+subGroupSuffix])
assert.Equal(t, 1, groupMap["group2"])
assert.Equal(t, 0, groupMap["group2"+subGroupSuffix])
assert.Equal(t, 1, groupMap["group3"])
assert.Equal(t, 1, groupMap["group3"+subGroupSuffix])
assert.Equal(t, 5, len(urlMap))
},
},
}
os.Setenv(SubGroupSuffix, subGroupSuffix)
for _, s := range cases {
s.Ctx.parseSubGroupSuffix(s.UrlMap)
s.AssertFunc(t, s.UrlMap)
}
os.Unsetenv(SubGroupSuffix)
}

func TestContext_mergeDefaultFilter(t *testing.T) {
c := Context{AgentURL: &URL{
Parameters: map[string]string{"defaultFilter": "a,b,d"},
Expand Down
13 changes: 13 additions & 0 deletions dynamicConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (c *DynamicConfigurer) Register(url *core.URL) error {
}

func (c *DynamicConfigurer) doRegister(url *core.URL) error {
regGroupSuffix := os.Getenv(core.RegGroupSuffix)
if regGroupSuffix != "" && !strings.HasSuffix(url.Group, regGroupSuffix) {
url.Group += regGroupSuffix
}
c.regLock.Lock()
defer c.regLock.Unlock()
if _, ok := c.registerNodes[url.GetIdentityWithRegistry()]; ok {
Expand Down Expand Up @@ -128,6 +132,15 @@ func (c *DynamicConfigurer) Subscribe(url *core.URL) error {
if err != nil {
return err
}
subGroupSuffix := os.Getenv(core.SubGroupSuffix)
if subGroupSuffix != "" && !strings.HasSuffix(url.Group, subGroupSuffix) {
newUrl := url.Copy()
newUrl.Group = url.Group + subGroupSuffix
err = c.doSubscribe(newUrl)
if err != nil {
return err
}
}
c.saveSnapshot()
return nil
}
Expand Down
98 changes: 97 additions & 1 deletion dynamicConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package motan

import (
"bytes"
"github.com/weibocom/motan-go/cluster"
motan "github.com/weibocom/motan-go/core"
mserver "github.com/weibocom/motan-go/server"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -97,7 +100,100 @@ func TestDynamicConfigurerMultiRegistry(t *testing.T) {
},
}
for _, j := range urls {
configurer.doRegister(j)
err := configurer.doRegister(j)
assert.Nil(t, err)
exporter, ok := a.serviceExporters.Load(j.GetIdentityWithRegistry())
assert.True(t, ok)
defaultExporter, ok := exporter.(*mserver.DefaultExporter)
assert.True(t, ok)
assert.Equal(t, defaultExporter.GetURL().GetIdentityWithRegistry(), j.GetIdentityWithRegistry())
}
assert.Equal(t, len(configurer.registerNodes), 4)

// test RegGroupSuffix env
regGroupSuffix := "-test"
configurer2 := &DynamicConfigurer{
agent: a,
registerNodes: make(map[string]*motan.URL),
subscribeNodes: make(map[string]*motan.URL),
}
url := &motan.URL{
Protocol: "motan2",
Host: "127.0.0.1",
Port: 1910,
Path: "test_path_env",
Group: "test_group_env",
Parameters: map[string]string{
"registry": "r1",
},
}
urlCopy := url.Copy()

os.Setenv(motan.RegGroupSuffix, regGroupSuffix)
err := configurer2.doRegister(url)
assert.Nil(t, err)
assert.Equal(t, len(configurer2.registerNodes), 1)

urlCopy.Group += regGroupSuffix
regUrl, ok := configurer2.registerNodes[url.GetIdentityWithRegistry()]
assert.True(t, ok)
assert.Equal(t, urlCopy.Group, regUrl.Group)

exporter, ok := a.serviceExporters.Load(urlCopy.GetIdentityWithRegistry())
assert.True(t, ok)
defaultExporter, ok := exporter.(*mserver.DefaultExporter)
assert.True(t, ok)
assert.Equal(t, defaultExporter.GetURL().GetIdentityWithRegistry(), urlCopy.GetIdentityWithRegistry())
os.Unsetenv(motan.RegGroupSuffix)
}

func TestDynamicConfigurer_Subscribe(t *testing.T) {
a := NewAgent(nil)
a.Context = &motan.Context{
RegistryURLs: make(map[string]*motan.URL),
RefersURLs: make(map[string]*motan.URL),
BasicReferURLs: make(map[string]*motan.URL),
ServiceURLs: make(map[string]*motan.URL),
BasicServiceURLs: make(map[string]*motan.URL),
}
a.agentURL = &motan.URL{}
configure := &DynamicConfigurer{
agent: a,
registerNodes: make(map[string]*motan.URL),
subscribeNodes: make(map[string]*motan.URL),
}
urls := []*motan.URL{
{
Protocol: "motan2",
Path: "test_path",
Group: "test_group",
Parameters: map[string]string{
"registry": "r1",
},
},
}

subGroupSuffix := "-test"
os.Setenv(motan.SubGroupSuffix, subGroupSuffix)
for _, url := range urls {
err := configure.Subscribe(url)
assert.Nil(t, err)
key := getClusterKey(url.Group, url.GetStringParamsWithDefault(motan.VersionKey, motan.DefaultReferVersion), url.Protocol, url.Path)
c, ok := a.clusterMap.Load(key)
assert.True(t, ok)
motanCluster, ok := c.(*cluster.MotanCluster)
assert.True(t, ok)
assert.Equal(t, motanCluster.GetURL().GetIdentityWithRegistry(), url.GetIdentityWithRegistry())

// test SubGroupSuffix env
copyUrl := url.Copy()
copyUrl.Group += subGroupSuffix
key2 := getClusterKey(copyUrl.Group, copyUrl.GetStringParamsWithDefault(motan.VersionKey, motan.DefaultReferVersion), copyUrl.Protocol, copyUrl.Path)
c2, ok := a.clusterMap.Load(key2)
assert.True(t, ok)
motanCluster2, ok := c2.(*cluster.MotanCluster)
assert.True(t, ok)
assert.Equal(t, motanCluster2.GetURL().GetIdentityWithRegistry(), copyUrl.GetIdentityWithRegistry())
}
os.Unsetenv(motan.SubGroupSuffix)
}

0 comments on commit af7c115

Please sign in to comment.