Skip to content

Commit 72f0397

Browse files
authored
fix get user id without cache (#5635)
1 parent f11d607 commit 72f0397

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

controllers/account/controllers/payment_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func (r *PaymentReconciler) reconcileNewPayment(payment *accountv1.Payment) erro
295295
return fmt.Errorf("user ID is empty")
296296
}
297297
payment.Spec.UserCR = payment.Spec.UserID
298-
id, err := r.Account.AccountV2.GetUserID(&pkgtypes.UserQueryOpts{Owner: payment.Spec.UserCR})
298+
id, err := r.Account.AccountV2.GetUserID(&pkgtypes.UserQueryOpts{Owner: payment.Spec.UserCR, WithOutCache: true})
299299
if err != nil {
300300
return fmt.Errorf("get user ID failed: %w", err)
301301
}

controllers/pkg/database/cockroach/accountv2.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ func (c *Cockroach) GetUserID(ops *types.UserQueryOpts) (id string, err error) {
438438
return "", fmt.Errorf("empty query opts")
439439
}
440440
if ops.Owner != "" {
441-
id, err = c.getUserIDByOwner(ops.Owner)
442-
if ops.IgnoreEmpty && err == gorm.ErrRecordNotFound {
441+
id, err = c.getUserIDByOwner(ops.Owner, !ops.WithOutCache)
442+
if ops.IgnoreEmpty && errors.Is(err, gorm.ErrRecordNotFound) {
443443
return "", nil
444444
}
445445
return id, err
@@ -481,6 +481,15 @@ func (c *Cockroach) getUserUIDByOwnerWithCache(owner string) (uuid.UUID, error)
481481
if v, ok := c.ownerUsrUIDMap.Load(owner); ok {
482482
return v.(uuid.UUID), nil
483483
}
484+
userUID, err := c.getUserUIDByOwner(owner)
485+
if err != nil {
486+
return uuid.Nil, err
487+
}
488+
c.ownerUsrUIDMap.Store(owner, userUID)
489+
return userUID, nil
490+
}
491+
492+
func (c *Cockroach) getUserUIDByOwner(owner string) (uuid.UUID, error) {
484493
var user struct {
485494
UID uuid.UUID `gorm:"column:userUid;type:uuid"`
486495
}
@@ -491,15 +500,24 @@ func (c *Cockroach) getUserUIDByOwnerWithCache(owner string) (uuid.UUID, error)
491500
}
492501
return uuid.Nil, fmt.Errorf("failed to get userUID: %v", err)
493502
}
494-
c.ownerUsrUIDMap.Store(owner, user.UID)
495503
return user.UID, nil
496504
}
497505

498-
func (c *Cockroach) getUserIDByOwner(owner string) (string, error) {
499-
if v, ok := c.ownerUsrIDMap.Load(owner); ok {
500-
return v.(string), nil
506+
func (c *Cockroach) getUserIDByOwner(owner string, withCache bool) (string, error) {
507+
if withCache {
508+
if v, ok := c.ownerUsrIDMap.Load(owner); ok {
509+
return v.(string), nil
510+
}
511+
}
512+
var (
513+
userUID uuid.UUID
514+
err error
515+
)
516+
if withCache {
517+
userUID, err = c.getUserUIDByOwnerWithCache(owner)
518+
} else {
519+
userUID, err = c.getUserUIDByOwner(owner)
501520
}
502-
userUID, err := c.getUserUIDByOwnerWithCache(owner)
503521
if err != nil {
504522
return "", err
505523
}

controllers/pkg/types/dbquery.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
)
2222

2323
type UserQueryOpts struct {
24-
UID uuid.UUID
25-
ID string
26-
Namespace string
27-
Owner string
28-
IgnoreEmpty bool
24+
UID uuid.UUID
25+
WithOutCache bool
26+
ID string
27+
Namespace string
28+
Owner string
29+
IgnoreEmpty bool
2930
}
3031

3132
type GetTransfersReq struct {

0 commit comments

Comments
 (0)