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

api/blocks: fix race between get/set #6791

Merged
merged 1 commit into from
Oct 11, 2023
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
24 changes: 20 additions & 4 deletions pkg/api/blocks/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package v1

import (
"net/http"
"sync"
"time"

"github.com/go-kit/log"
Expand All @@ -25,10 +26,12 @@ import (

// BlocksAPI is a very simple API used by Thanos Block Viewer.
type BlocksAPI struct {
baseAPI *api.BaseAPI
logger log.Logger
globalBlocksInfo *BlocksInfo
loadedBlocksInfo *BlocksInfo
baseAPI *api.BaseAPI
logger log.Logger
globalBlocksInfo *BlocksInfo
loadedBlocksInfo *BlocksInfo

globalLock, loadedLock sync.Mutex
disableCORS bool
bkt objstore.Bucket
disableAdminOperations bool
Expand Down Expand Up @@ -131,8 +134,15 @@ func (bapi *BlocksAPI) markBlock(r *http.Request) (interface{}, []error, *api.Ap
func (bapi *BlocksAPI) blocks(r *http.Request) (interface{}, []error, *api.ApiError, func()) {
viewParam := r.URL.Query().Get("view")
if viewParam == "loaded" {
bapi.loadedLock.Lock()
defer bapi.loadedLock.Unlock()

return bapi.loadedBlocksInfo, nil, nil, func() {}
}

bapi.globalLock.Lock()
defer bapi.globalLock.Unlock()

return bapi.globalBlocksInfo, nil, nil, func() {}
}

Expand All @@ -151,10 +161,16 @@ func (b *BlocksInfo) set(blocks []metadata.Meta, err error) {

// SetGlobal updates the global blocks' metadata in the API.
func (bapi *BlocksAPI) SetGlobal(blocks []metadata.Meta, err error) {
bapi.globalLock.Lock()
defer bapi.globalLock.Unlock()

bapi.globalBlocksInfo.set(blocks, err)
}

// SetLoaded updates the local blocks' metadata in the API.
func (bapi *BlocksAPI) SetLoaded(blocks []metadata.Meta, err error) {
bapi.loadedLock.Lock()
defer bapi.loadedLock.Unlock()

bapi.loadedBlocksInfo.set(blocks, err)
}
Loading