From 453357fa55791cc590cafb6f0a5599436b9bd703 Mon Sep 17 00:00:00 2001 From: "maksim.konovalov" Date: Tue, 4 Feb 2025 13:38:44 +0300 Subject: [PATCH] pool: add Instance info to connection ConnectionInfo 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. --- CHANGELOG.md | 1 + pool/connection_pool.go | 21 ++++++++++++++++++--- pool/connection_pool_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82f9766f7..6dc5bdd5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pool/connection_pool.go b/pool/connection_pool.go index 9be46665e..4d591cb08 100644 --- a/pool/connection_pool.go +++ b/pool/connection_pool.go @@ -93,6 +93,7 @@ ConnectionInfo structure for information about connection statuses: type ConnectionInfo struct { ConnectedNow bool ConnRole Role + Instance Instance } /* @@ -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, + }} } } diff --git a/pool/connection_pool_test.go b/pool/connection_pool_test.go index c4e43e2d3..de37fc06e 100644 --- a/pool/connection_pool_test.go +++ b/pool/connection_pool_test.go @@ -3495,6 +3495,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)