Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve retry logic #110

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/kine/drivers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ var (
) AS high`
)

const retryCount = 500

type Stripped string

func (s Stripped) String() string {
Expand Down Expand Up @@ -317,7 +319,7 @@ func getPrefixRange(prefix string) (start, end string) {
}

func (d *Generic) query(ctx context.Context, txName, sql string, args ...interface{}) (rows *sql.Rows, err error) {
i := uint(0)
i := 0
start := time.Now()

done, err := d.AdmissionControlPolicy.Admit(ctx, txName)
Expand All @@ -333,7 +335,7 @@ func (d *Generic) query(ctx context.Context, txName, sql string, args ...interfa
}()

strippedSQL := Stripped(sql)
for ; i < 500; i++ {
for ; i < retryCount; i++ {
if i > 2 {
logrus.Debugf("QUERY (try: %d) %v : %s", i, args, strippedSQL)
} else {
Expand Down Expand Up @@ -418,7 +420,7 @@ func (d *Generic) queryRowPrepared(ctx context.Context, txName, sql string, prep
}

func (d *Generic) executePrepared(ctx context.Context, txName, sql string, prepared *sql.Stmt, args ...interface{}) (result sql.Result, err error) {
i := uint(0)
i := 0
start := time.Now()
defer func() {
if err != nil {
Expand All @@ -438,7 +440,7 @@ func (d *Generic) executePrepared(ctx context.Context, txName, sql string, prepa
}

strippedSQL := Stripped(sql)
for ; i < 500; i++ {
for ; i < retryCount; i++ {
if i > 2 {
logrus.Debugf("EXEC (try: %d) %v : %s", i, args, strippedSQL)
} else {
Expand Down Expand Up @@ -474,9 +476,21 @@ func (d *Generic) GetCompactRevision(ctx context.Context) (int64, int64, error)
return 0, 0, fmt.Errorf("denied: %w", err)
}

row := d.DB.QueryRow(revisionIntervalSQL)
for i := 0; i < retryCount; i++ {
if i > 2 {
logrus.Debugf("EXEC (try: %d): %s", i, revisionIntervalSQL)
} else {
logrus.Tracef("EXEC (try: %d): %s", i, revisionIntervalSQL)
}
row := d.DB.QueryRow(revisionIntervalSQL)
err = row.Scan(&compact, &target)
if err != nil && d.Retry != nil && d.Retry(err) {
time.Sleep(jitter.Deviation(nil, 0.3)(2 * time.Millisecond))
continue
}
break
}
done()
err = row.Scan(&compact, &target)
if err == sql.ErrNoRows {
return 0, 0, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/kine/drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ func NewVariant(ctx context.Context, driverName, dataSourceName string) (server.
opts.admissionControlPolicyLimitMaxConcurrentTxn,
)

if driverName == "sqlite3" {
dialect.Retry = func(err error) bool {
if err, ok := err.(sqlite3.Error); ok {
return err.Code == sqlite3.ErrBusy
}
return false
}
}

return logstructured.New(sqllog.New(dialect)), dialect, nil
}

Expand Down
Loading