-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
receiver: avoid race of hashring #1371
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"net" | ||
"net/http" | ||
"strconv" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/go-kit/kit/log" | ||
|
@@ -46,19 +47,20 @@ type Handler struct { | |
logger log.Logger | ||
receiver *Writer | ||
router *route.Router | ||
hashring Hashring | ||
options *Options | ||
listener net.Listener | ||
|
||
mtx sync.RWMutex | ||
hashring Hashring | ||
|
||
// Metrics | ||
requestDuration *prometheus.HistogramVec | ||
requestsTotal *prometheus.CounterVec | ||
responseSize *prometheus.HistogramVec | ||
forwardRequestsTotal *prometheus.CounterVec | ||
|
||
// These fields are uint32 rather than boolean to be able to use atomic functions. | ||
storageReady uint32 | ||
hashringReady uint32 | ||
storageReady uint32 | ||
} | ||
|
||
func NewHandler(logger log.Logger, o *Options) *Handler { | ||
|
@@ -140,20 +142,19 @@ func (h *Handler) StorageReady() { | |
// Hashring sets the hashring for the handler and marks the hashring as ready. | ||
// If the hashring is nil, then the hashring is marked as not ready. | ||
func (h *Handler) Hashring(hashring Hashring) { | ||
if hashring == nil { | ||
atomic.StoreUint32(&h.hashringReady, 0) | ||
h.hashring = nil | ||
return | ||
} | ||
h.mtx.Lock() | ||
defer h.mtx.Unlock() | ||
|
||
h.hashring = hashring | ||
atomic.StoreUint32(&h.hashringReady, 1) | ||
} | ||
|
||
// Verifies whether the server is ready or not. | ||
func (h *Handler) isReady() bool { | ||
sr := atomic.LoadUint32(&h.storageReady) | ||
hr := atomic.LoadUint32(&h.hashringReady) | ||
return sr > 0 && hr > 0 | ||
h.mtx.RLock() | ||
hr := h.hashring != nil | ||
h.mtx.RUnlock() | ||
return sr > 0 && hr | ||
} | ||
|
||
// Checks if server is ready, calls f if it is, returns 503 if it is not. | ||
|
@@ -275,6 +276,15 @@ func (h *Handler) receive(w http.ResponseWriter, r *http.Request) { | |
func (h *Handler) forward(ctx context.Context, tenant string, r replica, wreq *prompb.WriteRequest) error { | ||
wreqs := make(map[string]*prompb.WriteRequest) | ||
replicas := make(map[string]replica) | ||
|
||
// It is possible that hashring is ready in testReady() but unready now, | ||
// so need to lock here. | ||
h.mtx.RLock() | ||
if h.hashring == nil { | ||
h.mtx.RUnlock() | ||
return errors.New("hashring is not ready") | ||
} | ||
|
||
// Batch all of the time series in the write request | ||
// into several smaller write requests that are | ||
// grouped by target endpoint. This ensures that | ||
|
@@ -285,6 +295,7 @@ func (h *Handler) forward(ctx context.Context, tenant string, r replica, wreq *p | |
for i := range wreq.Timeseries { | ||
endpoint, err := h.hashring.GetN(tenant, &wreq.Timeseries[i], r.n) | ||
if err != nil { | ||
h.mtx.RUnlock() | ||
return err | ||
} | ||
if _, ok := wreqs[endpoint]; !ok { | ||
|
@@ -294,6 +305,7 @@ func (h *Handler) forward(ctx context.Context, tenant string, r replica, wreq *p | |
wr := wreqs[endpoint] | ||
wr.Timeseries = append(wr.Timeseries, wreq.Timeseries[i]) | ||
} | ||
h.mtx.RUnlock() | ||
|
||
return h.parallelizeRequests(ctx, tenant, replicas, wreqs) | ||
} | ||
|
@@ -400,14 +412,25 @@ func (h *Handler) replicate(ctx context.Context, tenant string, wreq *prompb.Wri | |
wreqs := make(map[string]*prompb.WriteRequest) | ||
replicas := make(map[string]replica) | ||
var i uint64 | ||
|
||
// It is possible that hashring is ready in testReady() but unready now, | ||
// so need to lock here. | ||
h.mtx.RLock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for clarity, plz move this locking below the comment. |
||
if h.hashring == nil { | ||
h.mtx.RUnlock() | ||
return errors.New("hashring is not ready") | ||
} | ||
|
||
for i = 0; i < h.options.ReplicationFactor; i++ { | ||
endpoint, err := h.hashring.GetN(tenant, &wreq.Timeseries[0], i) | ||
if err != nil { | ||
h.mtx.RUnlock() | ||
return err | ||
} | ||
wreqs[endpoint] = wreq | ||
replicas[endpoint] = replica{i, true} | ||
} | ||
h.mtx.RUnlock() | ||
|
||
err := h.parallelizeRequests(ctx, tenant, replicas, wreqs) | ||
if errs, ok := err.(terrors.MultiError); ok { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this below the comment.