-
Notifications
You must be signed in to change notification settings - Fork 287
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
implement least active load balance #602
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c02f321
feat: implement least active load balance
oldmee 3ec0355
Merge branch 'seata:master' into master
oldmee f765091
feat: add unit test
oldmee f886b87
fix: wrong function name
oldmee 48b27d8
feat: implement least active load balance
oldmee a20293f
style: format import
oldmee 219cccf
style: format import
oldmee 9c6262f
feat: alter the variable scope
oldmee caf851d
Merge branch 'seata:master' into master
oldmee 54e281f
feat: handle the error
oldmee 670913f
Merge branch 'master' into master
luky116 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ import ( | |
getty "github.com/apache/dubbo-getty" | ||
|
||
"github.com/seata/seata-go/pkg/protocol/message" | ||
"github.com/seata/seata-go/pkg/remoting/rpc" | ||
"github.com/seata/seata-go/pkg/util/log" | ||
) | ||
|
||
|
@@ -61,14 +62,19 @@ func (g *GettyRemoting) SendSync(msg message.RpcMessage, s getty.Session, callba | |
if s == nil { | ||
s = sessionManager.selectSession(msg) | ||
} | ||
return g.sendAsync(s, msg, callback) | ||
rpc.BeginCount(s.RemoteAddr()) | ||
result, err := g.sendAsync(s, msg, callback) | ||
rpc.EndCount(s.RemoteAddr()) | ||
return result, err | ||
} | ||
|
||
func (g *GettyRemoting) SendASync(msg message.RpcMessage, s getty.Session, callback callbackMethod) error { | ||
if s == nil { | ||
s = sessionManager.selectSession(msg) | ||
} | ||
rpc.BeginCount(s.RemoteAddr()) | ||
_, err := g.sendAsync(s, msg, callback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 判断处理 err |
||
rpc.EndCount(s.RemoteAddr()) | ||
return err | ||
} | ||
|
||
|
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,65 @@ | ||
/* | ||
* 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 ( | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
"github.com/seata/seata-go/pkg/remoting/rpc" | ||
|
||
getty "github.com/apache/dubbo-getty" | ||
) | ||
|
||
func LeastActiveLoadBalance(sessions *sync.Map, xid string) getty.Session { | ||
var session getty.Session | ||
var leastActive int32 = -1 | ||
leastCount := 0 | ||
var leastIndexes []getty.Session | ||
sessions.Range(func(key, value interface{}) bool { | ||
session = key.(getty.Session) | ||
if session.IsClosed() { | ||
sessions.Delete(session) | ||
} else { | ||
active := rpc.GetStatus(session.RemoteAddr()).GetActive() | ||
if leastActive == -1 || active < leastActive { | ||
leastActive = active | ||
leastCount = 1 | ||
if len(leastIndexes) > 0 { | ||
leastIndexes = leastIndexes[:0] | ||
} | ||
leastIndexes = append(leastIndexes, session) | ||
} else if active == leastActive { | ||
leastIndexes = append(leastIndexes, session) | ||
leastCount++ | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
if leastCount == 0 { | ||
return nil | ||
} | ||
|
||
if leastCount == 1 { | ||
return leastIndexes[0] | ||
} else { | ||
return leastIndexes[rand.New(rand.NewSource(time.Now().UnixNano())).Intn(leastCount)] | ||
} | ||
} |
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,63 @@ | ||
/* | ||
* 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" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/seata/seata-go/pkg/remoting/mock" | ||
"github.com/seata/seata-go/pkg/remoting/rpc" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLeastActiveLoadBalance(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
sessions := &sync.Map{} | ||
|
||
for i := 1; i <= 3; i++ { | ||
session := mock.NewMockTestSession(ctrl) | ||
session.EXPECT().IsClosed().Return(false).AnyTimes() | ||
addr := "127.0.0." + strconv.Itoa(i) + ":8000" | ||
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string { | ||
return addr | ||
}) | ||
sessions.Store(session, fmt.Sprintf("session-%d", i)) | ||
rpc.BeginCount(addr) | ||
} | ||
|
||
session := mock.NewMockTestSession(ctrl) | ||
session.EXPECT().IsClosed().Return(true).AnyTimes() | ||
addr := "127.0.0.5:8000" | ||
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string { | ||
return addr | ||
}) | ||
sessions.Store(session, "session-5") | ||
rpc.BeginCount(addr) | ||
|
||
countTwo := "127.0.0.1:8000" | ||
rpc.BeginCount(countTwo) | ||
|
||
result := LeastActiveLoadBalance(sessions, "test_xid") | ||
assert.False(t, result.RemoteAddr() == countTwo) | ||
assert.False(t, result.RemoteAddr() == addr) | ||
assert.False(t, result.IsClosed()) | ||
} |
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
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,64 @@ | ||
/* | ||
* 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 rpc | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
var serviceStatusMap sync.Map | ||
|
||
type Status struct { | ||
Active int32 | ||
Total int32 | ||
} | ||
|
||
// RemoveStatus remove the RpcStatus of this service | ||
func RemoveStatus(service string) { | ||
serviceStatusMap.Delete(service) | ||
} | ||
|
||
// BeginCount begin count | ||
func BeginCount(service string) { | ||
status := GetStatus(service) | ||
atomic.AddInt32(&status.Active, 1) | ||
} | ||
|
||
// EndCount end count | ||
func EndCount(service string) { | ||
status := GetStatus(service) | ||
atomic.AddInt32(&status.Active, -1) | ||
atomic.AddInt32(&status.Total, 1) | ||
} | ||
|
||
// GetStatus get status | ||
func GetStatus(service string) *Status { | ||
a, _ := serviceStatusMap.LoadOrStore(service, new(Status)) | ||
return a.(*Status) | ||
} | ||
|
||
// GetActive get active. | ||
func (s *Status) GetActive() int32 { | ||
return s.Active | ||
} | ||
|
||
// GetTotal get total. | ||
func (s *Status) GetTotal() int32 { | ||
return s.Total | ||
} |
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,50 @@ | ||
/* | ||
* 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 rpc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var service = "127.0.0.1:8000" | ||
|
||
func TestStatus(t *testing.T) { | ||
rpcStatus1 := GetStatus(service) | ||
assert.NotNil(t, rpcStatus1) | ||
rpcStatus2 := GetStatus(service) | ||
assert.Equal(t, rpcStatus1, rpcStatus2) | ||
} | ||
|
||
func TestRemoveStatus(t *testing.T) { | ||
old := GetStatus(service) | ||
RemoveStatus(service) | ||
assert.Equal(t, GetStatus(service), old) | ||
} | ||
|
||
func TestBeginCount(t *testing.T) { | ||
BeginCount(service) | ||
assert.Equal(t, GetStatus(service).GetActive(), int32(1)) | ||
} | ||
|
||
func TestEndCount(t *testing.T) { | ||
EndCount(service) | ||
assert.Equal(t, GetStatus(service).GetActive(), int32(0)) | ||
assert.Equal(t, GetStatus(service).GetTotal(), int32(1)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
判断处理 err