-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1d88a4
commit a6d900f
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 cluster_impl | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/cluster" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/protocol" | ||
) | ||
|
||
type broadcastCluster struct{} | ||
|
||
const broadcast = "broadcast" | ||
|
||
func init() { | ||
extension.SetCluster(broadcast, NewBroadcastCluster) | ||
} | ||
|
||
func NewBroadcastCluster() cluster.Cluster { | ||
return &broadcastCluster{} | ||
} | ||
|
||
func (cluster *broadcastCluster) Join(directory cluster.Directory) protocol.Invoker { | ||
return newBroadcastClusterInvoker(directory) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
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 cluster_impl | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/cluster" | ||
"github.com/apache/dubbo-go/common/logger" | ||
"github.com/apache/dubbo-go/protocol" | ||
) | ||
|
||
type broadcastClusterInvoker struct { | ||
baseClusterInvoker | ||
} | ||
|
||
func newBroadcastClusterInvoker(directory cluster.Directory) protocol.Invoker { | ||
return &broadcastClusterInvoker{ | ||
baseClusterInvoker: newBaseClusterInvoker(directory), | ||
} | ||
} | ||
|
||
func (invoker *broadcastClusterInvoker) Invoke(invocation protocol.Invocation) protocol.Result { | ||
invokers := invoker.directory.List(invocation) | ||
err := invoker.checkInvokers(invokers, invocation) | ||
if err != nil { | ||
return &protocol.RPCResult{Err: err} | ||
} | ||
err = invoker.checkWhetherDestroyed() | ||
if err != nil { | ||
return &protocol.RPCResult{Err: err} | ||
} | ||
|
||
var result protocol.Result | ||
for _, ivk := range invokers { | ||
result = ivk.Invoke(invocation) | ||
if result.Error() != nil { | ||
logger.Warnf("broadcast invoker invoke err: %v when use invoker: %v\n", result.Error(), ivk) | ||
err = result.Error() | ||
} | ||
} | ||
if err != nil { | ||
return &protocol.RPCResult{Err: err} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
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 cluster_impl | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/cluster/directory" | ||
"github.com/apache/dubbo-go/cluster/loadbalance" | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/protocol" | ||
"github.com/apache/dubbo-go/protocol/invocation" | ||
"github.com/apache/dubbo-go/protocol/mock" | ||
) | ||
|
||
var ( | ||
broadcastUrl, _ = common.NewURL(context.TODO(), "dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") | ||
) | ||
|
||
func registerBroadcast(t *testing.T, mockInvokers ...*mock.MockInvoker) protocol.Invoker { | ||
extension.SetLoadbalance("random", loadbalance.NewRandomLoadBalance) | ||
|
||
invokers := []protocol.Invoker{} | ||
for i, ivk := range mockInvokers { | ||
invokers = append(invokers, ivk) | ||
if i == 0 { | ||
ivk.EXPECT().GetUrl().Return(broadcastUrl) | ||
} | ||
} | ||
staticDir := directory.NewStaticDirectory(invokers) | ||
|
||
broadcastCluster := NewBroadcastCluster() | ||
clusterInvoker := broadcastCluster.Join(staticDir) | ||
return clusterInvoker | ||
} | ||
|
||
func Test_BroadcastInvokeSuccess(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
invokers := make([]*mock.MockInvoker, 0) | ||
|
||
mockResult := &protocol.RPCResult{Rest: rest{tried: 0, success: true}} | ||
for i := 0; i < 3; i++ { | ||
invoker := mock.NewMockInvoker(ctrl) | ||
invokers = append(invokers, invoker) | ||
invoker.EXPECT().Invoke(gomock.Any()).Return(mockResult) | ||
} | ||
|
||
clusterInvoker := registerBroadcast(t, invokers...) | ||
|
||
result := clusterInvoker.Invoke(&invocation.RPCInvocation{}) | ||
assert.Equal(t, mockResult, result) | ||
} | ||
|
||
func Test_BroadcastInvokeFailed(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
invokers := make([]*mock.MockInvoker, 0) | ||
|
||
mockResult := &protocol.RPCResult{Rest: rest{tried: 0, success: true}} | ||
mockFailedResult := &protocol.RPCResult{Err: errors.New("just failed")} | ||
for i := 0; i < 10; i++ { | ||
invoker := mock.NewMockInvoker(ctrl) | ||
invokers = append(invokers, invoker) | ||
invoker.EXPECT().Invoke(gomock.Any()).Return(mockResult) | ||
} | ||
{ | ||
invoker := mock.NewMockInvoker(ctrl) | ||
invokers = append(invokers, invoker) | ||
invoker.EXPECT().Invoke(gomock.Any()).Return(mockFailedResult) | ||
} | ||
for i := 0; i < 10; i++ { | ||
invoker := mock.NewMockInvoker(ctrl) | ||
invokers = append(invokers, invoker) | ||
invoker.EXPECT().Invoke(gomock.Any()).Return(mockResult) | ||
} | ||
|
||
clusterInvoker := registerBroadcast(t, invokers...) | ||
|
||
result := clusterInvoker.Invoke(&invocation.RPCInvocation{}) | ||
assert.Equal(t, mockFailedResult.Err, result.Error()) | ||
} |