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: add v3router to dubbogo #1187

Merged
merged 6 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
61 changes: 61 additions & 0 deletions cluster/router/v3router/dubbo_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 v3router

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)

// nolint
type DubboRouterRule struct {
uniformRules []*UniformRule
}

func newDubboRouterRule(dubboRoutes []*config.DubboRoute,
destinationMap map[string]map[string]string) (*DubboRouterRule, error) {

uniformRules := make([]*UniformRule, 0)
for _, v := range dubboRoutes {
uniformRule, err := newUniformRule(v, destinationMap)
if err != nil {
return nil, err
}
uniformRules = append(uniformRules, uniformRule)
}

return &DubboRouterRule{
uniformRules: uniformRules,
}, nil
}

func (drr *DubboRouterRule) route(invokers []protocol.Invoker, url *common.URL,
invocation protocol.Invocation) []protocol.Invoker {

resultInvokers := make([]protocol.Invoker, 0)
for _, v := range drr.uniformRules {
if resultInvokers = v.route(invokers, url, invocation); len(resultInvokers) == 0 {
continue
}
// once there is a uniformRule successfully get target invoker lists, return it
return resultInvokers
}
// return s empty invoker list
return resultInvokers
}
35 changes: 35 additions & 0 deletions cluster/router/v3router/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 v3router

import (
"github.com/apache/dubbo-go/cluster/router"
)

// UniformRouteFactory is uniform router's factory
type UniformRouteFactory struct{}

// NewUniformRouterFactory constructs a new PriorityRouterFactory
func NewUniformRouterFactory() router.PriorityRouterFactory {
return &UniformRouteFactory{}
}

// NewPriorityRouter construct a new UniformRouteFactory as PriorityRouter
func (f *UniformRouteFactory) NewPriorityRouter(vsConfigBytes, distConfigBytes []byte, notify chan struct{}) (router.PriorityRouter, error) {
return NewUniformRouterChain(vsConfigBytes, distConfigBytes, notify)
}
35 changes: 35 additions & 0 deletions cluster/router/v3router/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 v3router

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

// TestUniformRouterFacotry created a new factory that can new uniform router
func TestUniformRouterFacotry(t *testing.T) {
factory := NewUniformRouterFactory()
assert.NotNil(t, factory)
router, err := factory.NewPriorityRouter([]byte{}, []byte{}, make(chan struct{}))
assert.Nil(t, err)
assert.NotNil(t, router)
}
80 changes: 80 additions & 0 deletions cluster/router/v3router/judger/attachment_match_judger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 judger

import (
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
)

type AttachmentMatchJudger struct {
config.DubboAttachmentMatch
}

// nolint
func (amj *AttachmentMatchJudger) Judge(invocation protocol.Invocation) bool {
invAttaMap := invocation.Attachments()
if amj.EagleeyeContext != nil {
for k, v := range amj.EagleeyeContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
}
// exist this key
str, ok := invAttaValue.(string)
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
return false
}
}
}

if amj.DubboContext != nil {
for k, v := range amj.DubboContext {
invAttaValue, ok := invAttaMap[k]
if !ok {
if v.Empty == "" {
return false
}
}
// exist this key
str, ok := invAttaValue.(string)
wongoo marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return false
}
strJudger := NewStringMatchJudger(v)
if !strJudger.Judge(str) {
return false
}
}
}

return true
}

// nolint
func NewAttachmentMatchJudger(matchConf *config.DubboAttachmentMatch) *AttachmentMatchJudger {
return &AttachmentMatchJudger{
DubboAttachmentMatch: *matchConf,
}
}
49 changes: 49 additions & 0 deletions cluster/router/v3router/judger/attachment_match_judger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 judger

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol/invocation"
)

func TestAttachmentMatchJudger(t *testing.T) {
dubboCtxMap := make(map[string]*config.StringMatch)
dubboIvkMap := make(map[string]interface{})
dubboCtxMap["test-key"] = &config.StringMatch{
Exact: "abc",
}
dubboIvkMap["test-key"] = "abc"
assert.True(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))

dubboIvkMap["test-key"] = "abd"
assert.False(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{
DubboContext: dubboCtxMap,
}).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap)))

}
37 changes: 37 additions & 0 deletions cluster/router/v3router/judger/bool_match_judger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 judger

import "github.com/apache/dubbo-go/config"

// nolint
type BoolMatchJudger struct {
config.BoolMatch
}

// nolint
func (lsmj *BoolMatchJudger) Judge(input bool) bool {
return input == lsmj.Exact
}

// nolint
func newBoolMatchJudger(matchConf *config.BoolMatch) *BoolMatchJudger {
return &BoolMatchJudger{
BoolMatch: *matchConf,
}
}
48 changes: 48 additions & 0 deletions cluster/router/v3router/judger/bool_match_judger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 judger

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/config"
)

func TestBoolMatchJudger(t *testing.T) {
assert.True(t, newBoolMatchJudger(&config.BoolMatch{
Exact: true,
}).Judge(true))

assert.True(t, newBoolMatchJudger(&config.BoolMatch{
Exact: false,
}).Judge(false))

assert.False(t, newBoolMatchJudger(&config.BoolMatch{
Exact: true,
}).Judge(false))

assert.False(t, newBoolMatchJudger(&config.BoolMatch{
Exact: false,
}).Judge(true))
}
Loading