Skip to content

Commit

Permalink
optimize transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Apr 3, 2024
1 parent 8eedb82 commit c0954ea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
2 changes: 2 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package cmd

// TODO: analysis grafana json tmpl, generate common tmpl for other users

const (
Name = "openalysis"
CMD = "oa"
Expand Down
31 changes: 17 additions & 14 deletions cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,42 +90,45 @@ func Restart(ctx context.Context) {
slog.Info("openalysis service stopped")
}

const SavePointName = "UpdateTask"

func StartCron(ctx context.Context, c *cron.Cron, errC chan error) {
if _, err := c.AddFunc(config.GlobalConfig.Backend.Cron, func() {
slog.Info("update task starts now")
startUpdate := time.Now()
i := 0
for {
if i == config.GlobalConfig.Backend.Retry {
errC <- ErrReachedRetryTimes
}
// TODO: optimize transaction handling
i++
tx := storage.DB.Begin()
err := UpdateTask(ctx, tx)
if err == nil {
tx.Commit()
tx.SavePoint(SavePointName)
j := 0
for {
if j == config.GlobalConfig.Backend.Retry {
errC <- ErrReachedRetryTimes
}
stx := storage.DB.Begin()
err := UpdateContributorCount(ctx, stx)
j++
err := UpdateContributorCount(ctx, tx)
if err == nil {
stx.Commit()
tx.Commit()
break
}
slog.Error("error update contributor count", "err", err.Error())
stx.Rollback()
tx.RollbackTo(SavePointName)
slog.Info("transaction rollback and retry")
j++
if j == config.GlobalConfig.Backend.Retry {
tx.Rollback()
errC <- ErrReachedRetryTimes
break
}
}
break
}
slog.Error("error doing update task", "err", err.Error())
tx.Rollback()
slog.Info("transaction rollback and retry")
i++
if i == config.GlobalConfig.Backend.Retry {
errC <- ErrReachedRetryTimes
break
}
}
slog.Info("update task completed", "time", time.Since(startUpdate).String())
}); err != nil {
Expand Down
31 changes: 31 additions & 0 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ func TestQueryCursor(t *testing.T) {
fmt.Println(cursor.EndCursor == "")
fmt.Println(cursor.LastUpdate.IsZero())
}

func TestTx(t *testing.T) {
err := config.GlobalConfig.ReadInConfig("../default.yaml")
if err != nil {
t.Fatal(err.Error())
}
err = Init()
if err != nil {
t.Fatal(err.Error())
}
tx := DB.Begin()
err = CreateGroup(context.Background(), tx, &model.Group{
Name: "hello",
IssueCount: 2,
PullRequestCount: 3,
StarCount: 4,
ForkCount: 5,
ContributorCount: 6,
})
if err != nil {
t.Fatal(err)
}
var gs []model.Group
if err := tx.Find(&gs).Error; err != nil {
t.Fatal(err)
}
for _, g := range gs {
fmt.Println(g)
}
tx.Commit()
}

0 comments on commit c0954ea

Please sign in to comment.