diff --git a/management/internals/controllers/network_map/controller/controller.go b/management/internals/controllers/network_map/controller/controller.go index 97eebfe6e..9538b0c34 100644 --- a/management/internals/controllers/network_map/controller/controller.go +++ b/management/internals/controllers/network_map/controller/controller.go @@ -144,7 +144,7 @@ func (c *Controller) sendUpdateAccountPeers(ctx context.Context, accountID strin if c.experimentalNetworkMap(accountID) { account = c.getAccountFromHolderOrInit(accountID) } else { - account, err = c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountID) + account, err = c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountID) if err != nil { return fmt.Errorf("failed to get account: %v", err) } @@ -300,7 +300,7 @@ func (c *Controller) UpdateAccountPeer(ctx context.Context, accountId string, pe return fmt.Errorf("peer %s doesn't have a channel, skipping network map update", peerId) } - account, err := c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountId) + account, err := c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountId) if err != nil { return fmt.Errorf("failed to send out updates to peer %s: %v", peerId, err) } @@ -414,7 +414,7 @@ func (c *Controller) GetValidatedPeerWithMap(ctx context.Context, isRequiresAppr if c.experimentalNetworkMap(accountID) { account = c.getAccountFromHolderOrInit(accountID) } else { - account, err = c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountID) + account, err = c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountID) if err != nil { return nil, nil, nil, 0, err } @@ -506,7 +506,7 @@ func (c *Controller) recalculateNetworkMapCache(account *types.Account, validate func (c *Controller) RecalculateNetworkMapCache(ctx context.Context, accountId string) error { if c.experimentalNetworkMap(accountId) { - account, err := c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountId) + account, err := c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountId) if err != nil { return err } @@ -548,7 +548,7 @@ func (c *Controller) getAccountFromHolderOrInit(accountID string) *types.Account if a != nil { return a } - account, err := c.holder.LoadOrStoreFunc(accountID, c.requestBuffer.GetAccountWithoutUsersWithBackpressure) + account, err := c.holder.LoadOrStoreFunc(accountID, c.requestBuffer.GetAccountLightWithBackpressure) if err != nil { return nil } @@ -715,7 +715,7 @@ func (c *Controller) OnPeersUpdated(ctx context.Context, accountID string, peerI func (c *Controller) OnPeersAdded(ctx context.Context, accountID string, peerIDs []string) error { for _, peerID := range peerIDs { if c.experimentalNetworkMap(accountID) { - account, err := c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountID) + account, err := c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountID) if err != nil { return err } @@ -761,7 +761,7 @@ func (c *Controller) OnPeersDeleted(ctx context.Context, accountID string, peerI c.peersUpdateManager.CloseChannel(ctx, peerID) if c.experimentalNetworkMap(accountID) { - account, err := c.requestBuffer.GetAccountWithoutUsersWithBackpressure(ctx, accountID) + account, err := c.requestBuffer.GetAccountLightWithBackpressure(ctx, accountID) if err != nil { log.WithContext(ctx).Errorf("failed to get account %s: %v", accountID, err) continue diff --git a/management/server/account/request_buffer.go b/management/server/account/request_buffer.go index e528d9651..49128ca0d 100644 --- a/management/server/account/request_buffer.go +++ b/management/server/account/request_buffer.go @@ -7,5 +7,6 @@ import ( ) type RequestBuffer interface { - GetAccountWithoutUsersWithBackpressure(ctx context.Context, accountID string) (*types.Account, error) + // GetAccountLightWithBackpressure returns account without users, setup keys, and onboarding data with request buffering + GetAccountLightWithBackpressure(ctx context.Context, accountID string) (*types.Account, error) } diff --git a/management/server/account_request_buffer.go b/management/server/account_request_buffer.go index 5d84b1042..1ff1ab10b 100644 --- a/management/server/account_request_buffer.go +++ b/management/server/account_request_buffer.go @@ -25,13 +25,13 @@ type AccountResult struct { } type AccountRequestBuffer struct { - store store.Store - getAccountRequests map[string][]*AccountRequest - getAccountWithoutUsersRequests map[string][]*AccountRequest - mu sync.Mutex - getAccountRequestCh chan *AccountRequest - getAccountWithoutUsersRequestCh chan *AccountRequest - bufferInterval time.Duration + store store.Store + getAccountRequests map[string][]*AccountRequest + getAccountLightRequests map[string][]*AccountRequest + mu sync.Mutex + getAccountRequestCh chan *AccountRequest + getAccountLightRequestCh chan *AccountRequest + bufferInterval time.Duration } func NewAccountRequestBuffer(ctx context.Context, store store.Store) *AccountRequestBuffer { @@ -47,16 +47,16 @@ func NewAccountRequestBuffer(ctx context.Context, store store.Store) *AccountReq log.WithContext(ctx).Infof("set account request buffer interval to %s", bufferInterval) ac := AccountRequestBuffer{ - store: store, - getAccountRequests: make(map[string][]*AccountRequest), - getAccountWithoutUsersRequests: make(map[string][]*AccountRequest), - getAccountRequestCh: make(chan *AccountRequest), - getAccountWithoutUsersRequestCh: make(chan *AccountRequest), - bufferInterval: bufferInterval, + store: store, + getAccountRequests: make(map[string][]*AccountRequest), + getAccountLightRequests: make(map[string][]*AccountRequest), + getAccountRequestCh: make(chan *AccountRequest), + getAccountLightRequestCh: make(chan *AccountRequest), + bufferInterval: bufferInterval, } go ac.processGetAccountRequests(ctx) - go ac.processGetAccountWithoutUsersRequests(ctx) + go ac.processGetAccountLightRequests(ctx) return &ac } @@ -75,18 +75,19 @@ func (ac *AccountRequestBuffer) GetAccountWithBackpressure(ctx context.Context, return result.Account, result.Err } -func (ac *AccountRequestBuffer) GetAccountWithoutUsersWithBackpressure(ctx context.Context, accountID string) (*types.Account, error) { +// GetAccountLightWithBackpressure returns account without users, setup keys, and onboarding data with request buffering +func (ac *AccountRequestBuffer) GetAccountLightWithBackpressure(ctx context.Context, accountID string) (*types.Account, error) { req := &AccountRequest{ AccountID: accountID, ResultChan: make(chan *AccountResult, 1), } - log.WithContext(ctx).Tracef("requesting account without users %s with backpressure", accountID) + log.WithContext(ctx).Tracef("requesting account light %s with backpressure", accountID) startTime := time.Now() - ac.getAccountWithoutUsersRequestCh <- req + ac.getAccountLightRequestCh <- req result := <-req.ResultChan - log.WithContext(ctx).Tracef("got account without users with backpressure after %s", time.Since(startTime)) + log.WithContext(ctx).Tracef("got account light with backpressure after %s", time.Since(startTime)) return result.Account, result.Err } @@ -130,10 +131,10 @@ func (ac *AccountRequestBuffer) processGetAccountRequests(ctx context.Context) { } } -func (ac *AccountRequestBuffer) processGetAccountWithoutUsersBatch(ctx context.Context, accountID string) { +func (ac *AccountRequestBuffer) processGetAccountLightBatch(ctx context.Context, accountID string) { ac.mu.Lock() - requests := ac.getAccountWithoutUsersRequests[accountID] - delete(ac.getAccountWithoutUsersRequests, accountID) + requests := ac.getAccountLightRequests[accountID] + delete(ac.getAccountLightRequests, accountID) ac.mu.Unlock() if len(requests) == 0 { @@ -141,8 +142,8 @@ func (ac *AccountRequestBuffer) processGetAccountWithoutUsersBatch(ctx context.C } startTime := time.Now() - account, err := ac.store.GetAccountWithoutUsers(ctx, accountID) - log.WithContext(ctx).Tracef("getting account without users %s in batch took %s", accountID, time.Since(startTime)) + account, err := ac.store.GetAccountLight(ctx, accountID) + log.WithContext(ctx).Tracef("getting account light %s in batch took %s", accountID, time.Since(startTime)) result := &AccountResult{Account: account, Err: err} for _, req := range requests { @@ -151,16 +152,16 @@ func (ac *AccountRequestBuffer) processGetAccountWithoutUsersBatch(ctx context.C } } -func (ac *AccountRequestBuffer) processGetAccountWithoutUsersRequests(ctx context.Context) { +func (ac *AccountRequestBuffer) processGetAccountLightRequests(ctx context.Context) { for { select { - case req := <-ac.getAccountWithoutUsersRequestCh: + case req := <-ac.getAccountLightRequestCh: ac.mu.Lock() - ac.getAccountWithoutUsersRequests[req.AccountID] = append(ac.getAccountWithoutUsersRequests[req.AccountID], req) - if len(ac.getAccountWithoutUsersRequests[req.AccountID]) == 1 { + ac.getAccountLightRequests[req.AccountID] = append(ac.getAccountLightRequests[req.AccountID], req) + if len(ac.getAccountLightRequests[req.AccountID]) == 1 { go func(ctx context.Context, accountID string) { time.Sleep(ac.bufferInterval) - ac.processGetAccountWithoutUsersBatch(ctx, accountID) + ac.processGetAccountLightBatch(ctx, accountID) }(ctx, req.AccountID) } ac.mu.Unlock() diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index dd4f0a231..9f8e9fabf 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -796,11 +796,12 @@ func (s *SqlStore) GetAccount(ctx context.Context, accountID string) (*types.Acc return s.getAccountGorm(ctx, accountID) } -func (s *SqlStore) GetAccountWithoutUsers(ctx context.Context, accountID string) (*types.Account, error) { +// GetAccountLight returns account without users, setup keys, and onboarding data +func (s *SqlStore) GetAccountLight(ctx context.Context, accountID string) (*types.Account, error) { if s.pool != nil { - return s.getAccountWithoutUsersPgx(ctx, accountID) + return s.getAccountLightPgx(ctx, accountID) } - return s.getAccountWithoutUsersGorm(ctx, accountID) + return s.getAccountLightGorm(ctx, accountID) } func (s *SqlStore) getAccountGorm(ctx context.Context, accountID string) (*types.Account, error) { @@ -904,19 +905,18 @@ func (s *SqlStore) getAccountGorm(ctx context.Context, accountID string) (*types return &account, nil } -func (s *SqlStore) getAccountWithoutUsersGorm(ctx context.Context, accountID string) (*types.Account, error) { +func (s *SqlStore) getAccountLightGorm(ctx context.Context, accountID string) (*types.Account, error) { start := time.Now() defer func() { elapsed := time.Since(start) if elapsed > 1*time.Second { - log.WithContext(ctx).Tracef("GetAccountWithoutUsers for account %s exceeded 1s, took: %v", accountID, elapsed) + log.WithContext(ctx).Tracef("GetAccountLight for account %s exceeded 1s, took: %v", accountID, elapsed) } }() var account types.Account result := s.db.Model(&account). Preload("Policies.Rules"). - Preload("SetupKeysG"). Preload("PeersG"). Preload("GroupsG.GroupPeers"). Preload("RoutesG"). @@ -925,7 +925,6 @@ func (s *SqlStore) getAccountWithoutUsersGorm(ctx context.Context, accountID str Preload("Networks"). Preload("NetworkRouters"). Preload("NetworkResources"). - Preload("Onboarding"). Take(&account, idQueryCondition, accountID) if result.Error != nil { log.WithContext(ctx).Errorf("error when getting account %s from the store: %s", accountID, result.Error) @@ -935,17 +934,7 @@ func (s *SqlStore) getAccountWithoutUsersGorm(ctx context.Context, accountID str return nil, status.NewGetAccountFromStoreError(result.Error) } - account.SetupKeys = make(map[string]*types.SetupKey, len(account.SetupKeysG)) - for _, key := range account.SetupKeysG { - if key.UpdatedAt.IsZero() { - key.UpdatedAt = key.CreatedAt - } - if key.AutoGroups == nil { - key.AutoGroups = []string{} - } - account.SetupKeys[key.Key] = &key - } - account.SetupKeysG = nil + account.SetupKeys = make(map[string]*types.SetupKey) account.Peers = make(map[string]*nbpeer.Peer, len(account.PeersG)) for _, peer := range account.PeersG { @@ -1275,25 +1264,14 @@ func (s *SqlStore) getAccountPgx(ctx context.Context, accountID string) (*types. return account, nil } -func (s *SqlStore) getAccountWithoutUsersPgx(ctx context.Context, accountID string) (*types.Account, error) { +func (s *SqlStore) getAccountLightPgx(ctx context.Context, accountID string) (*types.Account, error) { account, err := s.getAccount(ctx, accountID) if err != nil { return nil, err } var wg sync.WaitGroup - errChan := make(chan error, 11) - - wg.Add(1) - go func() { - defer wg.Done() - keys, err := s.getSetupKeys(ctx, accountID) - if err != nil { - errChan <- err - return - } - account.SetupKeysG = keys - }() + errChan := make(chan error, 9) wg.Add(1) go func() { @@ -1394,16 +1372,6 @@ func (s *SqlStore) getAccountWithoutUsersPgx(ctx context.Context, accountID stri account.NetworkResources = resources }() - wg.Add(1) - go func() { - defer wg.Done() - err := s.getAccountOnboarding(ctx, accountID, account) - if err != nil { - errChan <- err - return - } - }() - wg.Wait() close(errChan) for e := range errChan { @@ -1462,11 +1430,7 @@ func (s *SqlStore) getAccountWithoutUsersPgx(ctx context.Context, accountID stri peersByGroupID[gp.GroupID] = append(peersByGroupID[gp.GroupID], gp.PeerID) } - account.SetupKeys = make(map[string]*types.SetupKey, len(account.SetupKeysG)) - for i := range account.SetupKeysG { - key := &account.SetupKeysG[i] - account.SetupKeys[key.Key] = key - } + account.SetupKeys = make(map[string]*types.SetupKey) account.Peers = make(map[string]*nbpeer.Peer, len(account.PeersG)) for i := range account.PeersG { diff --git a/management/server/store/store.go b/management/server/store/store.go index d3cac56d9..ad50dd48c 100644 --- a/management/server/store/store.go +++ b/management/server/store/store.go @@ -51,7 +51,8 @@ type Store interface { GetAccountsCounter(ctx context.Context) (int64, error) GetAllAccounts(ctx context.Context) []*types.Account GetAccount(ctx context.Context, accountID string) (*types.Account, error) - GetAccountWithoutUsers(ctx context.Context, accountID string) (*types.Account, error) + // GetAccountLight returns account without users, setup keys, and onboarding data + GetAccountLight(ctx context.Context, accountID string) (*types.Account, error) GetAccountMeta(ctx context.Context, lockStrength LockingStrength, accountID string) (*types.AccountMeta, error) GetAccountOnboarding(ctx context.Context, accountID string) (*types.AccountOnboarding, error) AccountExists(ctx context.Context, lockStrength LockingStrength, id string) (bool, error)