Skip to content

Commit 1eb799d

Browse files
author
maksim.konovalov
committed
pool: add Instance info to connection ConnectionInfo
Instance info in ConnectionInfo enables pool state monitoring and dynamic tarantool topology tracking.
1 parent d8e2284 commit 1eb799d

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1111
### Added
1212

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

1516
### Changed
1617

pool/connection_pool.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ ConnectionInfo structure for information about connection statuses:
9393
type ConnectionInfo struct {
9494
ConnectedNow bool
9595
ConnRole Role
96+
Instance Instance
9697
}
9798

9899
/*
@@ -393,13 +394,25 @@ func (p *ConnectionPool) GetInfo() map[string]ConnectionInfo {
393394
return info
394395
}
395396

396-
for name := range p.ends {
397+
for name, end := range p.ends {
397398
conn, role := p.getConnectionFromPool(name)
399+
400+
connInfo := ConnectionInfo{
401+
ConnectedNow: false,
402+
ConnRole: UnknownRole,
403+
Instance: Instance{
404+
Name: name,
405+
Dialer: end.dialer,
406+
Opts: end.opts,
407+
},
408+
}
409+
398410
if conn != nil {
399-
info[name] = ConnectionInfo{ConnectedNow: conn.ConnectedNow(), ConnRole: role}
400-
} else {
401-
info[name] = ConnectionInfo{ConnectedNow: false, ConnRole: UnknownRole}
411+
connInfo.ConnRole = role
412+
connInfo.ConnectedNow = conn.ConnectedNow()
402413
}
414+
415+
info[name] = connInfo
403416
}
404417

405418
return info

pool/connection_pool_test.go

+45-6
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ func TestConnect_empty(t *testing.T) {
242242
func TestConnect_unavailable(t *testing.T) {
243243
servers := []string{"err1", "err2"}
244244
ctx, cancel := test_helpers.GetPoolConnectContext()
245-
connPool, err := pool.Connect(ctx, makeInstances([]string{"err1", "err2"}, connOpts))
245+
insts := makeInstances([]string{"err1", "err2"}, connOpts)
246+
247+
connPool, err := pool.Connect(ctx, insts)
246248
cancel()
247249

248250
if connPool != nil {
@@ -252,8 +254,10 @@ func TestConnect_unavailable(t *testing.T) {
252254
require.NoError(t, err, "failed to create a pool")
253255
require.NotNilf(t, connPool, "pool is nil after Connect")
254256
require.Equal(t, map[string]pool.ConnectionInfo{
255-
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
256-
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
257+
servers[0]: pool.ConnectionInfo{
258+
ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[0]},
259+
servers[1]: pool.ConnectionInfo{
260+
ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[1]},
257261
}, connPool.GetInfo())
258262
}
259263

@@ -1156,15 +1160,19 @@ func TestConnectionHandlerOpenError(t *testing.T) {
11561160
}
11571161
ctx, cancel := test_helpers.GetPoolConnectContext()
11581162
defer cancel()
1159-
connPool, err := pool.ConnectWithOpts(ctx, makeInstances(poolServers, connOpts), poolOpts)
1163+
1164+
insts := makeInstances(poolServers, connOpts)
1165+
connPool, err := pool.ConnectWithOpts(ctx, insts, poolOpts)
11601166
if err == nil {
11611167
defer connPool.Close()
11621168
}
11631169
require.NoError(t, err, "failed to connect")
11641170
require.NotNil(t, connPool, "pool expected")
11651171
require.Equal(t, map[string]pool.ConnectionInfo{
1166-
servers[0]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
1167-
servers[1]: pool.ConnectionInfo{ConnectedNow: false, ConnRole: pool.UnknownRole},
1172+
servers[0]: pool.ConnectionInfo{
1173+
ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[0]},
1174+
servers[1]: pool.ConnectionInfo{
1175+
ConnectedNow: false, ConnRole: pool.UnknownRole, Instance: insts[1]},
11681176
}, connPool.GetInfo())
11691177
connPool.Close()
11701178

@@ -3495,6 +3503,37 @@ func runTestMain(m *testing.M) int {
34953503
return m.Run()
34963504
}
34973505

3506+
func TestConnectionPool_GetInfo_equal_instance_info(t *testing.T) {
3507+
var tCases [][]pool.Instance
3508+
3509+
tCases = append(tCases, makeInstances([]string{servers[0], servers[1]}, connOpts))
3510+
tCases = append(tCases, makeInstances([]string{
3511+
servers[0],
3512+
servers[1],
3513+
servers[3]},
3514+
connOpts))
3515+
tCases = append(tCases, makeInstances([]string{servers[0]}, connOpts))
3516+
3517+
for _, tc := range tCases {
3518+
ctx, cancel := test_helpers.GetPoolConnectContext()
3519+
connPool, err := pool.Connect(ctx, tc)
3520+
cancel()
3521+
require.Nilf(t, err, "failed to connect")
3522+
require.NotNilf(t, connPool, "conn is nil after Connect")
3523+
3524+
info := connPool.GetInfo()
3525+
3526+
var infoInstances []pool.Instance
3527+
3528+
for _, infoInst := range info {
3529+
infoInstances = append(infoInstances, infoInst.Instance)
3530+
}
3531+
3532+
require.ElementsMatch(t, tc, infoInstances)
3533+
connPool.Close()
3534+
}
3535+
}
3536+
34983537
func TestMain(m *testing.M) {
34993538
code := runTestMain(m)
35003539
os.Exit(code)

0 commit comments

Comments
 (0)