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
63 changes: 63 additions & 0 deletions pkg/remoting/loadbalance/least_active_loadbalance.go
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 (
"math/rand"
"sync"
"time"

getty "github.com/apache/dubbo-getty"
)

func LeastActiveLoadBalance(sessions *sync.Map, xid string) getty.Session {
var session getty.Session
var leastActive int64 = -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()
Copy link
Contributor

Choose a reason for hiding this comment

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

按照你目前的实现,最终变成了轮询,而不是最少活跃。原因:getty中的active指的是连接上次活跃的毫秒时间戳,无法代表连接的活跃度。

在seata java中,active指的是连接活跃度,在发送之前active cas 加1,在发送之后active cas 减1。在高并发使用场景下,对于最不被频繁使用的连接,其active值会首先变得更小,从而被优先选中。

建议:在least_active_loadbalance.go中实现类似于seata java的活跃度计算能力,注意使用cas处理并发增减活跃度,而不是锁。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

感谢,我再看看👀

if leastActive == -1 || interval < leastActive {
leastActive = interval
leastCount = 1
if len(leastIndexes) > 0 {
leastIndexes = leastIndexes[:0]
}
leastIndexes = append(leastIndexes, session)
} else if interval == 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)]
}
}
65 changes: 65 additions & 0 deletions pkg/remoting/loadbalance/least_active_loadbalance_test.go
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 (
"fmt"
"sync"
"testing"
"time"

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

"github.com/seata/seata-go/pkg/remoting/mock"
)

func TestConsistentHashLoadBalance(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

应该是LeastActive?

ctrl := gomock.NewController(t)
sessions := &sync.Map{}

now := time.Now()

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().RemoteAddr().AnyTimes().DoAndReturn(func() string {
return "127.0.0.1:8001"
})
session.EXPECT().IsClosed().Return(false).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", 2))

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

result := LeastActiveLoadBalance(sessions, "test_xid")
assert.True(t, result.RemoteAddr() == "127.0.0.1:8000")
assert.False(t, result.IsClosed())
}
2 changes: 2 additions & 0 deletions pkg/remoting/loadbalance/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func Select(loadBalanceType string, sessions *sync.Map, xid string) getty.Sessio
return RandomLoadBalance(sessions, xid)
case xidLoadBalance:
return XidLoadBalance(sessions, xid)
case leastActiveLoadBalance:
return LeastActiveLoadBalance(sessions, xid)
default:
return RandomLoadBalance(sessions, xid)
}
Expand Down