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

implement least active load balance #602

Merged
merged 11 commits into from
Feb 3, 2024
8 changes: 7 additions & 1 deletion pkg/remoting/getty/getty_remoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package getty

import (
"fmt"
"github.com/seata/seata-go/pkg/remoting/rpc"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

乱了,change 下

"sync"
"time"

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判断处理 err

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判断处理 err

rpc.EndCount(s.RemoteAddr())
return err
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/remoting/loadbalance/least_active_loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package loadbalance

import (
"github.com/seata/seata-go/pkg/remoting/rpc"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把所有的 import 风格规整下哈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

"math/rand"
"sync"
"time"
Expand All @@ -27,23 +28,23 @@ import (

func LeastActiveLoadBalance(sessions *sync.Map, xid string) getty.Session {
var session getty.Session
var leastActive int64 = -1
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 {
interval := session.GetActive().UnixNano()
if leastActive == -1 || interval < leastActive {
leastActive = interval
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 interval == leastActive {
} else if active == leastActive {
leastIndexes = append(leastIndexes, session)
leastCount++
}
Expand Down
51 changes: 24 additions & 27 deletions pkg/remoting/loadbalance/least_active_loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,44 @@ package loadbalance

import (
"fmt"
"sync"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/seata/seata-go/pkg/remoting/mock"
"github.com/seata/seata-go/pkg/remoting/rpc"
"github.com/stretchr/testify/assert"
"strconv"
"sync"
"testing"
)

func TestLeastActiveLoadBalance(t *testing.T) {
ctrl := gomock.NewController(t)
sessions := &sync.Map{}

now := time.Now()
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().GetActive().Return(now.Add(-time.Second * 7)).AnyTimes()
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string {
return "127.0.0.1:8000"
})
session.EXPECT().IsClosed().Return(false).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", 1))

session = mock.NewMockTestSession(ctrl)
session.EXPECT().GetActive().Return(now.Add(-time.Second * 5)).AnyTimes()
session.EXPECT().IsClosed().Return(true).AnyTimes()
addr := "127.0.0.5:8000"
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string {
return "127.0.0.1:8001"
return addr
})
session.EXPECT().IsClosed().Return(false).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", 2))
sessions.Store(session, "session-5")
rpc.BeginCount(addr)

session = mock.NewMockTestSession(ctrl)
session.EXPECT().GetActive().Return(now.Add(-time.Second * 10)).AnyTimes()
session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string {
return "127.0.0.1:8002"
})
session.EXPECT().IsClosed().Return(true).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", 3))
countTwo := "127.0.0.1:8000"
rpc.BeginCount(countTwo)

result := LeastActiveLoadBalance(sessions, "test_xid")
assert.True(t, result.RemoteAddr() == "127.0.0.1:8000")
assert.False(t, result.RemoteAddr() == countTwo)
assert.False(t, result.RemoteAddr() == addr)
assert.False(t, result.IsClosed())
}
47 changes: 47 additions & 0 deletions pkg/remoting/rpc/rpc_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rpc

import (
"sync"
"sync/atomic"
)

var ServiceStatusMap sync.Map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServiceStatusMap 不需要被其它包访问,用小写非导出的形式会不会更好些。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,我改一下


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
}
49 changes: 49 additions & 0 deletions pkg/remoting/rpc/rpc_status_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 rpc

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

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))
}