Skip to content

Commit

Permalink
pool: add Instance info to connection ConnectionInfo
Browse files Browse the repository at this point in the history
The Instance info has been added to ConnectionInfo, which is necessary to monitor the current state of the pool. This should help implement dynamic tracking of tarantool topology changes.
  • Loading branch information
maksim.konovalov committed Feb 4, 2025
1 parent d8e2284 commit 2d27324
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
### Added

- Extend box with replication information (#427).
- The Instance info has been added to ConnectionInfo for GetInfo response (#429).

### Changed

Expand Down
21 changes: 18 additions & 3 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ ConnectionInfo structure for information about connection statuses:
type ConnectionInfo struct {
ConnectedNow bool
ConnRole Role
Instance Instance
}

/*
Expand Down Expand Up @@ -393,12 +394,26 @@ func (p *ConnectionPool) GetInfo() map[string]ConnectionInfo {
return info
}

for name := range p.ends {
for name, end := range p.ends {
conn, role := p.getConnectionFromPool(name)
if conn != nil {
info[name] = ConnectionInfo{ConnectedNow: conn.ConnectedNow(), ConnRole: role}
info[name] = ConnectionInfo{
ConnectedNow: conn.ConnectedNow(),
ConnRole: role,
Instance: Instance{
Name: name,
Dialer: end.dialer,
Opts: end.opts,
}}
} else {
info[name] = ConnectionInfo{ConnectedNow: false, ConnRole: UnknownRole}
info[name] = ConnectionInfo{
ConnectedNow: false,
ConnRole: UnknownRole,
Instance: Instance{
Name: name,
Dialer: end.dialer,
Opts: end.opts,
}}
}
}

Expand Down
49 changes: 43 additions & 6 deletions pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ func TestConnect_empty(t *testing.T) {
func TestConnect_unavailable(t *testing.T) {
servers := []string{"err1", "err2"}
ctx, cancel := test_helpers.GetPoolConnectContext()
connPool, err := pool.Connect(ctx, makeInstances([]string{"err1", "err2"}, connOpts))
insts := makeInstances([]string{"err1", "err2"}, connOpts)

connPool, err := pool.Connect(ctx, insts)
cancel()

if connPool != nil {
Expand All @@ -252,8 +254,8 @@ func TestConnect_unavailable(t *testing.T) {
require.NoError(t, err, "failed to create a pool")
require.NotNilf(t, connPool, "pool is nil after Connect")
require.Equal(t, map[string]pool.ConnectionInfo{
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[0]},

Check failure on line 257 in pool/connection_pool_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 109 characters long, which exceeds the maximum of 100 characters. (lll)
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[1]},

Check failure on line 258 in pool/connection_pool_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 109 characters long, which exceeds the maximum of 100 characters. (lll)
}, connPool.GetInfo())
}

Expand Down Expand Up @@ -1156,15 +1158,17 @@ func TestConnectionHandlerOpenError(t *testing.T) {
}
ctx, cancel := test_helpers.GetPoolConnectContext()
defer cancel()
connPool, err := pool.ConnectWithOpts(ctx, makeInstances(poolServers, connOpts), poolOpts)

insts := makeInstances(poolServers, connOpts)
connPool, err := pool.ConnectWithOpts(ctx, insts, poolOpts)
if err == nil {
defer connPool.Close()
}
require.NoError(t, err, "failed to connect")
require.NotNil(t, connPool, "pool expected")
require.Equal(t, map[string]pool.ConnectionInfo{
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[0]},

Check failure on line 1170 in pool/connection_pool_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 109 characters long, which exceeds the maximum of 100 characters. (lll)
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[1]},
}, connPool.GetInfo())
connPool.Close()

Expand Down Expand Up @@ -3495,6 +3499,39 @@ func runTestMain(m *testing.M) int {
return m.Run()
}

func TestConnectionPool_GetInfo(t *testing.T) {
t.Run("equal instance info", func(t *testing.T) {
var tCases [][]pool.Instance

tCases = append(tCases, makeInstances([]string{servers[0], servers[1]}, connOpts))
tCases = append(tCases, makeInstances([]string{
servers[0],
servers[1],
servers[3]},
connOpts))
tCases = append(tCases, makeInstances([]string{servers[0]}, connOpts))

for _, tc := range tCases {
ctx, cancel := test_helpers.GetPoolConnectContext()
connPool, err := pool.Connect(ctx, tc)
cancel()
require.Nilf(t, err, "failed to connect")
require.NotNilf(t, connPool, "conn is nil after Connect")

info := connPool.GetInfo()

var infoInstances []pool.Instance

for _, infoInst := range info {
infoInstances = append(infoInstances, infoInst.Instance)
}

require.ElementsMatch(t, tc, infoInstances)
connPool.Close()
}
})
}

func TestMain(m *testing.M) {
code := runTestMain(m)
os.Exit(code)
Expand Down

0 comments on commit 2d27324

Please sign in to comment.