From 3cc6d3862d0c41fd9e1a568d98d333519914a28b Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Mon, 13 Jan 2025 17:52:39 +0300 Subject: [PATCH] Improve peer performance Signed-off-by: bcmmbaga --- management/server/peer.go | 23 +++-------------------- management/server/store/sql_store.go | 13 +++++++++++++ management/server/store/store.go | 1 + 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/management/server/peer.go b/management/server/peer.go index 5dfdda857..b5a77a674 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -953,7 +953,7 @@ func (am *DefaultAccountManager) getValidatedPeerWithMap(ctx context.Context, is return nil, nil, nil, err } - approvedPeersMap, err := am.GetValidatedPeers(ctx, account.Id) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra) if err != nil { return nil, nil, nil, err } @@ -1313,29 +1313,12 @@ func (am *DefaultAccountManager) getInactivePeers(ctx context.Context, accountID // GetPeerGroups returns groups that the peer is part of. func (am *DefaultAccountManager) GetPeerGroups(ctx context.Context, accountID, peerID string) ([]*types.Group, error) { - return getPeerGroups(ctx, am.Store, accountID, peerID) -} - -// getPeerGroups returns the IDs of the groups that the peer is part of. -func getPeerGroups(ctx context.Context, transaction store.Store, accountID, peerID string) ([]*types.Group, error) { - groups, err := transaction.GetAccountGroups(ctx, store.LockingStrengthShare, accountID) - if err != nil { - return nil, err - } - - peerGroups := make([]*types.Group, 0) - for _, group := range groups { - if slices.Contains(group.Peers, peerID) { - peerGroups = append(peerGroups, group) - } - } - - return peerGroups, nil + return am.Store.GetPeerGroups(ctx, store.LockingStrengthShare, accountID, peerID) } // getPeerGroupIDs returns the IDs of the groups that the peer is part of. func getPeerGroupIDs(ctx context.Context, transaction store.Store, accountID string, peerID string) ([]string, error) { - groups, err := getPeerGroups(ctx, transaction, accountID, peerID) + groups, err := transaction.GetPeerGroups(ctx, store.LockingStrengthShare, accountID, peerID) if err != nil { return nil, err } diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index 0344538e2..82934dd11 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -1218,6 +1218,19 @@ func (s *SqlStore) RemoveResourceFromGroup(ctx context.Context, accountId string return nil } +// GetPeerGroups retrieves all groups assigned to a specific peer in a given account. +func (s *SqlStore) GetPeerGroups(ctx context.Context, lockStrength LockingStrength, accountId string, peerId string) ([]*types.Group, error) { + var groups []*types.Group + query := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). + Find(&groups, "account_id = ? AND peers LIKE ?", accountId, fmt.Sprintf(`%%"%s"%%`, peerId)) + + if query.Error != nil { + return nil, query.Error + } + + return groups, nil +} + // GetAccountPeers retrieves peers for an account. func (s *SqlStore) GetAccountPeers(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*nbpeer.Peer, error) { var peers []*nbpeer.Peer diff --git a/management/server/store/store.go b/management/server/store/store.go index d7799be41..245df1c3e 100644 --- a/management/server/store/store.go +++ b/management/server/store/store.go @@ -100,6 +100,7 @@ type Store interface { GetPeerLabelsInAccount(ctx context.Context, lockStrength LockingStrength, accountId string) ([]string, error) AddPeerToAllGroup(ctx context.Context, lockStrength LockingStrength, accountID string, peerID string) error AddPeerToGroup(ctx context.Context, lockStrength LockingStrength, accountId string, peerId string, groupID string) error + GetPeerGroups(ctx context.Context, lockStrength LockingStrength, accountId string, peerId string) ([]*types.Group, error) AddResourceToGroup(ctx context.Context, accountId string, groupID string, resource *types.Resource) error RemoveResourceFromGroup(ctx context.Context, accountId string, groupID string, resourceID string) error AddPeerToAccount(ctx context.Context, lockStrength LockingStrength, peer *nbpeer.Peer) error