From da4f36ff9de4835d08d6575e6fad8e071f42cd0e Mon Sep 17 00:00:00 2001 From: Denis Volkov <3149929+Denchick@users.noreply.github.com> Date: Mon, 11 Nov 2024 08:07:27 +0100 Subject: [PATCH] Remove some dead pool code (#799) --- pkg/mock/pool/mock_pool.go | 136 +++++++---------------------------- pkg/pool/conn_keeper.go | 10 --- pkg/pool/conn_keeper_test.go | 9 --- pkg/pool/dbpool.go | 32 ++------- pkg/pool/pool.go | 5 +- pkg/pool/shardpool.go | 71 +----------------- router/route/route.go | 2 +- 7 files changed, 32 insertions(+), 233 deletions(-) diff --git a/pkg/mock/pool/mock_pool.go b/pkg/mock/pool/mock_pool.go index 56a4ee335..2e643195a 100644 --- a/pkg/mock/pool/mock_pool.go +++ b/pkg/mock/pool/mock_pool.go @@ -79,20 +79,6 @@ func (mr *MockConnectionKepperMockRecorder) IdleConnectionCount() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IdleConnectionCount", reflect.TypeOf((*MockConnectionKepper)(nil).IdleConnectionCount)) } -// List mocks base method. -func (m *MockConnectionKepper) List() []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "List") - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// List indicates an expected call of List. -func (mr *MockConnectionKepperMockRecorder) List() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockConnectionKepper)(nil).List)) -} - // Put mocks base method. func (m *MockConnectionKepper) Put(host shard.Shard) error { m.ctrl.T.Helper() @@ -257,20 +243,6 @@ func (mr *MockPoolMockRecorder) IdleConnectionCount() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IdleConnectionCount", reflect.TypeOf((*MockPool)(nil).IdleConnectionCount)) } -// List mocks base method. -func (m *MockPool) List() []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "List") - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// List indicates an expected call of List. -func (mr *MockPoolMockRecorder) List() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockPool)(nil).List)) -} - // Put mocks base method. func (m *MockPool) Put(host shard.Shard) error { m.ctrl.T.Helper() @@ -379,20 +351,6 @@ func (mr *MockMultiShardPoolMockRecorder) Connection(clid, shardKey, host interf return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connection", reflect.TypeOf((*MockMultiShardPool)(nil).Connection), clid, shardKey, host) } -// Cut mocks base method. -func (m *MockMultiShardPool) Cut(host string) []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Cut", host) - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// Cut indicates an expected call of Cut. -func (mr *MockMultiShardPoolMockRecorder) Cut(host interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cut", reflect.TypeOf((*MockMultiShardPool)(nil).Cut), host) -} - // Discard mocks base method. func (m *MockMultiShardPool) Discard(sh shard.Shard) error { m.ctrl.T.Helper() @@ -463,34 +421,6 @@ func (mr *MockMultiShardPoolMockRecorder) IdleConnectionCount() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IdleConnectionCount", reflect.TypeOf((*MockMultiShardPool)(nil).IdleConnectionCount)) } -// InitRule mocks base method. -func (m *MockMultiShardPool) InitRule(rule *config.BackendRule) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InitRule", rule) - ret0, _ := ret[0].(error) - return ret0 -} - -// InitRule indicates an expected call of InitRule. -func (mr *MockMultiShardPoolMockRecorder) InitRule(rule interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitRule", reflect.TypeOf((*MockMultiShardPool)(nil).InitRule), rule) -} - -// List mocks base method. -func (m *MockMultiShardPool) List() []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "List") - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// List indicates an expected call of List. -func (mr *MockMultiShardPoolMockRecorder) List() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockMultiShardPool)(nil).List)) -} - // Put mocks base method. func (m *MockMultiShardPool) Put(host shard.Shard) error { m.ctrl.T.Helper() @@ -547,6 +477,18 @@ func (mr *MockMultiShardPoolMockRecorder) Rule() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Rule", reflect.TypeOf((*MockMultiShardPool)(nil).Rule)) } +// SetRule mocks base method. +func (m *MockMultiShardPool) SetRule(rule *config.BackendRule) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetRule", rule) +} + +// SetRule indicates an expected call of SetRule. +func (mr *MockMultiShardPoolMockRecorder) SetRule(rule interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRule", reflect.TypeOf((*MockMultiShardPool)(nil).SetRule), rule) +} + // UsedConnectionCount mocks base method. func (m *MockMultiShardPool) UsedConnectionCount() int { m.ctrl.T.Helper() @@ -636,20 +578,6 @@ func (mr *MockDBPoolMockRecorder) Connection(clid, shardKey, host interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connection", reflect.TypeOf((*MockDBPool)(nil).Connection), clid, shardKey, host) } -// Cut mocks base method. -func (m *MockDBPool) Cut(host string) []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Cut", host) - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// Cut indicates an expected call of Cut. -func (mr *MockDBPoolMockRecorder) Cut(host interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cut", reflect.TypeOf((*MockDBPool)(nil).Cut), host) -} - // Discard mocks base method. func (m *MockDBPool) Discard(sh shard.Shard) error { m.ctrl.T.Helper() @@ -720,34 +648,6 @@ func (mr *MockDBPoolMockRecorder) IdleConnectionCount() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IdleConnectionCount", reflect.TypeOf((*MockDBPool)(nil).IdleConnectionCount)) } -// InitRule mocks base method. -func (m *MockDBPool) InitRule(rule *config.BackendRule) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InitRule", rule) - ret0, _ := ret[0].(error) - return ret0 -} - -// InitRule indicates an expected call of InitRule. -func (mr *MockDBPoolMockRecorder) InitRule(rule interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitRule", reflect.TypeOf((*MockDBPool)(nil).InitRule), rule) -} - -// List mocks base method. -func (m *MockDBPool) List() []shard.Shard { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "List") - ret0, _ := ret[0].([]shard.Shard) - return ret0 -} - -// List indicates an expected call of List. -func (mr *MockDBPoolMockRecorder) List() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockDBPool)(nil).List)) -} - // Put mocks base method. func (m *MockDBPool) Put(host shard.Shard) error { m.ctrl.T.Helper() @@ -804,6 +704,18 @@ func (mr *MockDBPoolMockRecorder) Rule() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Rule", reflect.TypeOf((*MockDBPool)(nil).Rule)) } +// SetRule mocks base method. +func (m *MockDBPool) SetRule(rule *config.BackendRule) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetRule", rule) +} + +// SetRule indicates an expected call of SetRule. +func (mr *MockDBPoolMockRecorder) SetRule(rule interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRule", reflect.TypeOf((*MockDBPool)(nil).SetRule), rule) +} + // SetShuffleHosts mocks base method. func (m *MockDBPool) SetShuffleHosts(arg0 bool) { m.ctrl.T.Helper() diff --git a/pkg/pool/conn_keeper.go b/pkg/pool/conn_keeper.go index d3848f380..8460660e7 100644 --- a/pkg/pool/conn_keeper.go +++ b/pkg/pool/conn_keeper.go @@ -128,16 +128,6 @@ func (r *ConnectionKepperData) RouterName() string { return r.Router } -// List returns a slice of shard.Shard objects. -// It is used to retrieve all the shards stored in the connection keeper. -// Returns an empty slice if no shards are found. -// -// Returns: -// - []shard.Shard: The list of shards stored in the ConnectionKepperData. -func (r *ConnectionKepperData) List() []shard.Shard { - return nil -} - // Rule returns a new instance of BackendRule based on the current ConnectionKepperData. // It copies the DB and Usr fields from the ConnectionKepperData and returns the new BackendRule. // The returned BackendRule is not a reference to the original ConnectionKepperData fields. diff --git a/pkg/pool/conn_keeper_test.go b/pkg/pool/conn_keeper_test.go index 9dc7f3c46..ee4cde86d 100644 --- a/pkg/pool/conn_keeper_test.go +++ b/pkg/pool/conn_keeper_test.go @@ -23,15 +23,6 @@ func TestCoordConnectionKepperRule(t *testing.T) { assert.Equal("usr", br.Usr) } -// TestCoordConnectionKepperList is a unit test function that tests the List method of the ConnectionKepperData struct. -func TestCoordConnectionKepperList(t *testing.T) { - assert := assert.New(t) - - k := pool.ConnectionKepperData{} - - assert.Nil(k.List()) -} - // TestCoordConnectionKepperHostname is a unit test function that tests the Hostname method of the ConnectionKepperData struct. // It asserts that the Hostname method returns the expected hostname value. func TestCoordConnectionKepperHostname(t *testing.T) { diff --git a/pkg/pool/dbpool.go b/pkg/pool/dbpool.go index f754cf700..685c72d9a 100644 --- a/pkg/pool/dbpool.go +++ b/pkg/pool/dbpool.go @@ -300,16 +300,13 @@ func (s *InstancePoolImpl) Connection( } } -// InitRule initializes the backend rule in the instance pool. -// It takes a pointer to a BackendRule as a parameter and returns an error. +// SetRule initializes the backend rule in the instance pool. +// It takes a pointer to a BackendRule as a parameter and saves it // // Parameters: // - rule: A pointer to a BackendRule representing the backend rule to be initialized. -// -// Returns: -// - error: An error if there is an error initializing the backend rule, nil otherwise. -func (s *InstancePoolImpl) InitRule(rule *config.BackendRule) error { - return s.pool.InitRule(rule) +func (s *InstancePoolImpl) SetRule(rule *config.BackendRule) { + s.pool.SetRule(rule) } // ShardMapping returns the shard mapping of the instance pool. @@ -320,15 +317,6 @@ func (s *InstancePoolImpl) ShardMapping() map[string]*config.Shard { return s.shardMapping } -// List returns a list of shards in the instance pool. -// -// Returns: -// - []shard.Shard: A list of shards in the instance pool. -func (s *InstancePoolImpl) List() []shard.Shard { - /* mutex? */ - return s.pool.List() -} - // ForEach iterates over each shard in the instance pool and calls the provided callback function. // It returns an error if the callback function returns an error. // @@ -382,18 +370,6 @@ func (s *InstancePoolImpl) ForEachPool(cb func(pool Pool) error) error { return s.pool.ForEachPool(cb) } -// Cut removes a shard from the instance pool based on the provided host. -// It returns the removed shard. -// -// Parameters: -// - host: The host of the shard to be removed. -// -// Returns: -// - []shard.Shard: The removed shard. -func (s *InstancePoolImpl) Cut(host string) []shard.Shard { - return s.pool.Cut(host) -} - // Discard removes a shard from the instance pool. // It returns an error if the removal fails. // diff --git a/pkg/pool/pool.go b/pkg/pool/pool.go index b1ad75dc9..728d70372 100644 --- a/pkg/pool/pool.go +++ b/pkg/pool/pool.go @@ -27,8 +27,6 @@ type ConnectionKepper interface { Hostname() string RouterName() string - List() []shard.Shard - Rule() *config.BackendRule } @@ -47,8 +45,7 @@ type MultiShardPool interface { Connection(clid uint, shardKey kr.ShardKey, host string) (shard.Shard, error) - InitRule(rule *config.BackendRule) error - Cut(host string) []shard.Shard + SetRule(rule *config.BackendRule) } type PoolIterator interface { diff --git a/pkg/pool/shardpool.go b/pkg/pool/shardpool.go index c3240ef98..8ccc3db5b 100644 --- a/pkg/pool/shardpool.go +++ b/pkg/pool/shardpool.go @@ -109,23 +109,6 @@ func (h *shardPool) Rule() *config.BackendRule { return h.beRule } -// Cut removes all shards from the shard pool and returns them as a slice. -// -// Parameters: -// - host: The host from which to cut the shards. -// -// Returns: -// - []shard.Shard: The removed shards as a slice. -func (h *shardPool) Cut(host string) []shard.Shard { - h.mu.Lock() - defer h.mu.Unlock() - - ret := h.pool - h.pool = nil - - return ret -} - // UsedConnectionCount returns the number of currently used connections in the shard pool. // // Returns: @@ -338,19 +321,6 @@ func (h *shardPool) ForEach(cb func(sh shard.Shardinfo) error) error { return nil } -// List returns a slice of shards in the shard pool. -// -// Returns: -// - []shard.Shard: The slice of shards in the shard pool. -// -// TODO : unit tests -func (h *shardPool) List() []shard.Shard { - h.mu.Lock() - defer h.mu.Unlock() - - return h.pool -} - /* pool with many hosts */ type cPool struct { @@ -419,22 +389,6 @@ func (c *cPool) ForEachPool(cb func(p Pool) error) error { return nil } -// List returns a slice of shard.Shard containing all the shards in the cPool. -// -// Returns: -// - []shard.Shard: The slice of shard.Shard containing all the shards in the cPool. -// -// TODO : unit tests -func (c *cPool) List() []shard.Shard { - var ret []shard.Shard - - c.pools.Range(func(key, value any) bool { - ret = append(ret, value.(Pool).List()...) - return true - }) - return ret -} - // Connection returns a shard connection for the given client ID, shard key, and host. // If a connection pool for the host does not exist, a new pool is created and stored. // Otherwise, the existing pool is retrieved from the cache. @@ -461,21 +415,6 @@ func (c *cPool) Connection(clid uint, shardKey kr.ShardKey, host string) (shard. return pool.Connection(clid, shardKey) } -// Cut removes and returns the shards associated with the specified host. -// It returns a slice of shard.Shard. -// -// Parameters: -// - host: The host for which to cut the shards. -// -// Returns: -// - []shard.Shard: The removed shards as a slice. -// -// TODO : unit tests -func (c *cPool) Cut(host string) []shard.Shard { - rt, _ := c.pools.LoadAndDelete(host) - return rt.([]shard.Shard) -} - // Put adds a shard to the pool. // It checks if the pool for the given host exists, and if so, it calls the Put method on that pool. // If the pool does not exist, it panics with the given host. @@ -516,20 +455,14 @@ func (c *cPool) Discard(sh shard.Shard) error { } } -// InitRule initializes the backend rule for the cPool. +// SetRule initializes the backend rule for the cPool. // It takes a pointer to a config.BackendRule as input and sets it as the backend rule for the cPool. -// Returns an error if any. // // Parameters: // - rule: The backend rule to be set for the cPool. -// -// Returns: -// - error: The error that occurred during the initialization of the backend rule. -// // TODO : unit tests -func (c *cPool) InitRule(rule *config.BackendRule) error { +func (c *cPool) SetRule(rule *config.BackendRule) { c.beRule = rule - return nil } var _ MultiShardPool = &cPool{} diff --git a/router/route/route.go b/router/route/route.go index db45a9016..29abf4eb7 100644 --- a/router/route/route.go +++ b/router/route/route.go @@ -64,7 +64,7 @@ func NewRoute(beRule *config.BackendRule, frRule *config.FrontendRule, mapping m clPool: client.NewClientPool(), params: shard.ParameterSet{}, } - _ = route.servPool.InitRule(beRule) + route.servPool.SetRule(beRule) return route }