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

312 race in tests #314

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
25 changes: 22 additions & 3 deletions pkg/dbconn/metadatalock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbconn

import (
"context"
"database/sql"
"errors"
"fmt"
"time"
Expand All @@ -20,6 +21,8 @@ type MetadataLock struct {
cancel context.CancelFunc
closeCh chan error
refreshInterval time.Duration
ticker *time.Ticker
dbConn *sql.DB
}

func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger loggers.Advanced, optionFns ...func(*MetadataLock)) (*MetadataLock, error) {
Expand All @@ -46,6 +49,7 @@ func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger lo
if err != nil {
return nil, err
}
mdl.dbConn = dbConn

// Function to acquire the lock
getLock := func() error {
Expand Down Expand Up @@ -76,16 +80,16 @@ func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger lo
ctx, mdl.cancel = context.WithCancel(ctx)
mdl.closeCh = make(chan error)
go func() {
ticker := time.NewTicker(mdl.refreshInterval)
defer ticker.Stop()
mdl.ticker = time.NewTicker(mdl.refreshInterval)
defer mdl.ticker.Stop()
for {
select {
case <-ctx.Done():
// Close the dedicated connection to release the lock
logger.Warnf("releasing metadata lock: %s", lockName)
mdl.closeCh <- dbConn.Close()
return
case <-ticker.C:
case <-mdl.ticker.C:
if err = getLock(); err != nil {
logger.Errorf("could not refresh metadata lock: %s", err)
}
Expand All @@ -98,6 +102,21 @@ func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger lo
}

func (m *MetadataLock) Close() error {
// Handle odd race situation here where the cancel func is nil somehow
if m.cancel == nil {
// Make a best effort to cleanup
if m.ticker != nil {
m.ticker.Stop()
}
if m.closeCh != nil {
close(m.closeCh)
}
if m.dbConn != nil {
return m.dbConn.Close()
}
return nil
}

// Cancel the background refresh runner
m.cancel()

Expand Down
Loading