Skip to content

Commit

Permalink
optimize account reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Aug 1, 2024
1 parent 2bd722f commit 3288679
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
33 changes: 28 additions & 5 deletions controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package controllers
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
"time"

"sigs.k8s.io/controller-runtime/pkg/event"

"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/google/uuid"
Expand Down Expand Up @@ -203,9 +204,6 @@ func (r *AccountReconciler) syncAccount(ctx context.Context, owner string, userN
}
account, err := r.AccountV2.NewAccount(&pkgtypes.UserQueryOpts{Owner: owner})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("failed to create %s account: %v", owner, err)
}
return account, nil
Expand Down Expand Up @@ -290,11 +288,36 @@ func (r *AccountReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controll
r.AccountSystemNamespace = env.GetEnvWithDefault(ACCOUNTNAMESPACEENV, DEFAULTACCOUNTNAMESPACE)
return ctrl.NewControllerManagedBy(mgr).
For(&userv1.User{}, builder.WithPredicates(OnlyCreatePredicate{})).
Watches(&accountv1.Payment{}, &handler.EnqueueRequestForObject{}).
Watches(&accountv1.Payment{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(PaymentPredicate{})).
WithOptions(rateOpts).
Complete(r)
}

type PaymentPredicate struct{}

func (PaymentPredicate) Create(e event.CreateEvent) bool {
if payment, ok := e.Object.(*accountv1.Payment); ok {
fmt.Println("payment create", payment.Status.TradeNO, payment.Status.Status)
return payment.Status.TradeNO != "" && payment.Status.Status != pay.PaymentSuccess
}
return false
}

func (PaymentPredicate) Update(e event.UpdateEvent) bool {
if payment, ok := e.ObjectNew.(*accountv1.Payment); ok {
return payment.Status.TradeNO != "" && payment.Status.Status != pay.PaymentSuccess
}
return false
}

func (PaymentPredicate) Delete(_ event.DeleteEvent) bool {
return false
}

func (PaymentPredicate) Generic(_ event.GenericEvent) bool {
return false
}

func RawParseRechargeConfig() (activities pkgtypes.Activities, discountsteps []int64, discountratios []float64, returnErr error) {
// local test
//config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
Expand Down
4 changes: 4 additions & 0 deletions controllers/account/controllers/debt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (r *DebtReconciler) reconcile(ctx context.Context, owner string) error {
account, err := r.AccountV2.GetAccount(&pkgtypes.UserQueryOpts{Owner: owner})
if account == nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
_, err = r.AccountV2.NewAccount(&pkgtypes.UserQueryOpts{Owner: owner})
if err != nil {
return fmt.Errorf("failed to create account %s: %v", owner, err)
}
userOwner := &userv1.User{}
if err := r.Get(ctx, types.NamespacedName{Name: owner, Namespace: r.accountSystemNamespace}, userOwner); err != nil {
// if user not exist, skip
Expand Down
20 changes: 12 additions & 8 deletions controllers/pkg/database/cockroach/accountv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (c *Cockroach) getAccount(ops *types.UserQueryOpts) (*types.Account, error)
if ops.IgnoreEmpty && errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("failed to search account from db: %w", err)
return nil, err
}
return &account, nil
}
Expand Down Expand Up @@ -307,16 +307,20 @@ func (c *Cockroach) updateBalance(tx *gorm.DB, ops *types.UserQueryOpts, amount
}
ops.UID = user.UserUID
}
var account types.Account
//TODO update UserUid = ?
if err := tx.Where(&types.Account{UserUID: ops.UID}).First(&account).Error; err != nil {
return fmt.Errorf("failed to get account: %w", err)
var account = &types.Account{}
if err := tx.Where(&types.Account{UserUID: ops.UID}).First(account).Error; err != nil {
// if not found, create a new account and retry
if !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("failed to get account: %w", err)
}
if account, err = c.NewAccount(ops); err != nil {
return fmt.Errorf("failed to create account: %v", err)
}
}

if err := c.updateWithAccount(isDeduction, add, &account, amount); err != nil {
if err := c.updateWithAccount(isDeduction, add, account, amount); err != nil {
return err
}
if err := tx.Save(&account).Error; err != nil {
if err := tx.Save(account).Error; err != nil {
return fmt.Errorf("failed to update account balance: %w", err)
}
return nil
Expand Down

0 comments on commit 3288679

Please sign in to comment.