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

fix(store/odsq4): fix order of opened flag stored #3735

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
23 changes: 11 additions & 12 deletions store/file/ods_q4.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ var _ eds.AccessorStreamer = (*ODSQ4)(nil)
type ODSQ4 struct {
ods *ODS

pathQ4 string
q4Mu sync.Mutex
q4Opened atomic.Bool
q4 *q4
pathQ4 string
q4Mu sync.Mutex
q4OpenAttempted atomic.Bool
q4 *q4
}

// CreateODSQ4 creates ODS and Q4 files under the given FS paths.
Expand Down Expand Up @@ -73,29 +73,28 @@ func ODSWithQ4(ods *ODS, pathQ4 string) *ODSQ4 {

func (odsq4 *ODSQ4) tryLoadQ4() *q4 {
// If Q4 was attempted to be opened before, return.
if odsq4.q4Opened.Load() {
if odsq4.q4OpenAttempted.Load() {
return odsq4.q4
}

odsq4.q4Mu.Lock()
defer odsq4.q4Mu.Unlock()
// update bool to make sure we try opening only once
// no matter if the try was successful or failed.
if opened := odsq4.q4Opened.Swap(true); opened {
if odsq4.q4OpenAttempted.Load() {
walldiss marked this conversation as resolved.
Show resolved Hide resolved
return odsq4.q4
}

q4, err := openQ4(odsq4.pathQ4, odsq4.ods.hdr)
// store q4 opened bool before updating atomic value to allow next read attempts to use it
odsq4.q4 = q4
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
// even if error occurred, store q4 opened bool to avoid trying to open it again
odsq4.q4OpenAttempted.Store(true)
if errors.Is(err, os.ErrNotExist) {
return nil
}

if err != nil {
log.Errorf("opening Q4 file %s: %s", odsq4.pathQ4, err)
return nil
}

odsq4.q4 = q4
return q4
}

Expand Down Expand Up @@ -168,7 +167,7 @@ func (odsq4 *ODSQ4) Close() error {

odsq4.q4Mu.Lock() // wait in case file is being opened
defer odsq4.q4Mu.Unlock()
if odsq4.q4Opened.Load() {
if odsq4.q4 != nil {
errQ4 := odsq4.q4.close()
if errQ4 != nil {
errQ4 = fmt.Errorf("closing Q4 file: %w", errQ4)
Expand Down
Loading