Skip to content

Commit

Permalink
feature/balancing-faces: add external load balancing methods support
Browse files Browse the repository at this point in the history
 I added the ability to use custom balancing methods when connecting with a pool.
  • Loading branch information
Maksim Konovalov committed Sep 9, 2024
1 parent 8b2be01 commit 993f644
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
also added logs for error case of `ConnectionPool.tryConnect()` calls in
`ConnectionPool.controller()` and `ConnectionPool.reconnect()`
- Methods that are implemented but not included in the pooler interface (#395).
- Add support for external load balancing methods when connecting via Pool (#400).

### Changed

Expand Down
19 changes: 19 additions & 0 deletions pool/balancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pool

import "github.com/tarantool/go-tarantool/v2"

type BalancerFactory interface {
Create(size int) BalancingPool
}

type BalancingPool interface {
GetConnection(string) *tarantool.Connection
DeleteConnection(string) *tarantool.Connection
AddConnection(id string, conn *tarantool.Connection)
GetNextConnection() *tarantool.Connection
GetConnections() map[string]*tarantool.Connection
}

func IsEmpty(pool BalancingPool) bool {
return len(pool.GetConnections()) == 0
}
27 changes: 17 additions & 10 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type Opts struct {
CheckTimeout time.Duration
// ConnectionHandler provides an ability to handle connection updates.
ConnectionHandler ConnectionHandler
// BalancerFactory - a factory for creating balancing,
// contains the pool size as well as the connections for which it is used.
BalancerFactory BalancerFactory
}

/*
Expand Down Expand Up @@ -110,9 +113,9 @@ type ConnectionPool struct {

state state
done chan struct{}
roPool *roundRobinStrategy
rwPool *roundRobinStrategy
anyPool *roundRobinStrategy
roPool BalancingPool
rwPool BalancingPool
anyPool BalancingPool
poolsMutex sync.RWMutex
watcherContainer watcherContainer
}
Expand Down Expand Up @@ -153,6 +156,10 @@ func newEndpoint(name string, dialer tarantool.Dialer, opts tarantool.Opts) *end
// opts. Instances must have unique names.
func ConnectWithOpts(ctx context.Context, instances []Instance,
opts Opts) (*ConnectionPool, error) {
if opts.BalancerFactory == nil {
opts.BalancerFactory = &RoundRobinFactory{}
}

unique := make(map[string]bool)
for _, instance := range instances {
if _, ok := unique[instance.Name]; ok {
Expand All @@ -166,9 +173,9 @@ func ConnectWithOpts(ctx context.Context, instances []Instance,
}

size := len(instances)
rwPool := newRoundRobinStrategy(size)
roPool := newRoundRobinStrategy(size)
anyPool := newRoundRobinStrategy(size)
rwPool := opts.BalancerFactory.Create(size)
roPool := opts.BalancerFactory.Create(size)
anyPool := opts.BalancerFactory.Create(size)

connPool := &ConnectionPool{
ends: make(map[string]*endpoint),
Expand Down Expand Up @@ -218,15 +225,15 @@ func (p *ConnectionPool) ConnectedNow(mode Mode) (bool, error) {
}
switch mode {
case ANY:
return !p.anyPool.IsEmpty(), nil
return !IsEmpty(p.anyPool), nil
case RW:
return !p.rwPool.IsEmpty(), nil
return !IsEmpty(p.rwPool), nil
case RO:
return !p.roPool.IsEmpty(), nil
return !IsEmpty(p.roPool), nil
case PreferRW:
fallthrough
case PreferRO:
return !p.rwPool.IsEmpty() || !p.roPool.IsEmpty(), nil
return !IsEmpty(p.rwPool) || !IsEmpty(p.roPool), nil
default:
return false, ErrNoHealthyInstance
}
Expand Down
28 changes: 18 additions & 10 deletions pool/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@ import (
"github.com/tarantool/go-tarantool/v2"
)

type roundRobinStrategy struct {
var _ BalancingPool = (*RoundRobinStrategy)(nil)

type RoundRobinStrategy struct {
conns []*tarantool.Connection
indexById map[string]uint
mutex sync.RWMutex
size uint64
current uint64
}

func newRoundRobinStrategy(size int) *roundRobinStrategy {
return &roundRobinStrategy{
type RoundRobinFactory struct{}

func (r *RoundRobinFactory) Create(size int) BalancingPool {
return NewRoundRobinStrategy(size)
}

func NewRoundRobinStrategy(size int) BalancingPool {
return &RoundRobinStrategy{
conns: make([]*tarantool.Connection, 0, size),
indexById: make(map[string]uint, size),
size: 0,
current: 0,
}
}

func (r *roundRobinStrategy) GetConnection(id string) *tarantool.Connection {
func (r *RoundRobinStrategy) GetConnection(id string) *tarantool.Connection {
r.mutex.RLock()
defer r.mutex.RUnlock()

Expand All @@ -36,7 +44,7 @@ func (r *roundRobinStrategy) GetConnection(id string) *tarantool.Connection {
return r.conns[index]
}

func (r *roundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
func (r *RoundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
r.mutex.Lock()
defer r.mutex.Unlock()

Expand Down Expand Up @@ -64,14 +72,14 @@ func (r *roundRobinStrategy) DeleteConnection(id string) *tarantool.Connection {
return conn
}

func (r *roundRobinStrategy) IsEmpty() bool {
func (r *RoundRobinStrategy) IsEmpty() bool {
r.mutex.RLock()
defer r.mutex.RUnlock()

return r.size == 0
}

func (r *roundRobinStrategy) GetNextConnection() *tarantool.Connection {
func (r *RoundRobinStrategy) GetNextConnection() *tarantool.Connection {
r.mutex.RLock()
defer r.mutex.RUnlock()

Expand All @@ -81,7 +89,7 @@ func (r *roundRobinStrategy) GetNextConnection() *tarantool.Connection {
return r.conns[r.nextIndex()]
}

func (r *roundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
func (r *RoundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
r.mutex.RLock()
defer r.mutex.RUnlock()

Expand All @@ -93,7 +101,7 @@ func (r *roundRobinStrategy) GetConnections() map[string]*tarantool.Connection {
return conns
}

func (r *roundRobinStrategy) AddConnection(id string, conn *tarantool.Connection) {
func (r *RoundRobinStrategy) AddConnection(id string, conn *tarantool.Connection) {
r.mutex.Lock()
defer r.mutex.Unlock()

Expand All @@ -106,7 +114,7 @@ func (r *roundRobinStrategy) AddConnection(id string, conn *tarantool.Connection
}
}

func (r *roundRobinStrategy) nextIndex() uint64 {
func (r *RoundRobinStrategy) nextIndex() uint64 {
next := atomic.AddUint64(&r.current, 1)
return (next - 1) % r.size
}
12 changes: 6 additions & 6 deletions pool/round_robin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
)

func TestRoundRobinAddDelete(t *testing.T) {
rr := newRoundRobinStrategy(10)
rr := NewRoundRobinStrategy(10)

addrs := []string{validAddr1, validAddr2}
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}
Expand All @@ -26,13 +26,13 @@ func TestRoundRobinAddDelete(t *testing.T) {
t.Errorf("Unexpected connection on address %s", addr)
}
}
if !rr.IsEmpty() {
if !IsEmpty(rr) {
t.Errorf("RoundRobin does not empty")
}
}

func TestRoundRobinAddDuplicateDelete(t *testing.T) {
rr := newRoundRobinStrategy(10)
rr := NewRoundRobinStrategy(10)

conn1 := &tarantool.Connection{}
conn2 := &tarantool.Connection{}
Expand All @@ -43,7 +43,7 @@ func TestRoundRobinAddDuplicateDelete(t *testing.T) {
if rr.DeleteConnection(validAddr1) != conn2 {
t.Errorf("Unexpected deleted connection")
}
if !rr.IsEmpty() {
if !IsEmpty(rr) {
t.Errorf("RoundRobin does not empty")
}
if rr.DeleteConnection(validAddr1) != nil {
Expand All @@ -52,7 +52,7 @@ func TestRoundRobinAddDuplicateDelete(t *testing.T) {
}

func TestRoundRobinGetNextConnection(t *testing.T) {
rr := newRoundRobinStrategy(10)
rr := NewRoundRobinStrategy(10)

addrs := []string{validAddr1, validAddr2}
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}
Expand All @@ -70,7 +70,7 @@ func TestRoundRobinGetNextConnection(t *testing.T) {
}

func TestRoundRobinStrategy_GetConnections(t *testing.T) {
rr := newRoundRobinStrategy(10)
rr := NewRoundRobinStrategy(10)

addrs := []string{validAddr1, validAddr2}
conns := []*tarantool.Connection{&tarantool.Connection{}, &tarantool.Connection{}}
Expand Down

0 comments on commit 993f644

Please sign in to comment.