From 106eaf39d1df35510eb55c918ab545f47cd97543 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Wed, 11 Oct 2017 10:12:29 +0200 Subject: [PATCH 1/2] Ensure workers terminated fully before reading unknownRefs --- head.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/head.go b/head.go index 8f5beb4fb..8ba1a71bc 100644 --- a/head.go +++ b/head.go @@ -233,16 +233,20 @@ func (h *Head) ReadWAL() error { // They are connected through a ring of channels which ensures that all sample batches // read from the WAL are processed in order. var ( + wg sync.WaitGroup n = runtime.GOMAXPROCS(0) firstInput = make(chan []RefSample, 300) input = firstInput ) + wg.Add(n) + for i := 0; i < n; i++ { output := make(chan []RefSample, 300) go func(i int, input <-chan []RefSample, output chan<- []RefSample) { unknown := h.processWALSamples(mint, uint64(i), uint64(n), input, output) atomic.AddUint64(&unknownRefs, unknown) + wg.Done() }(i, input, output) // The output feeds the next worker goroutine. For the last worker, @@ -288,6 +292,8 @@ func (h *Head) ReadWAL() error { close(firstInput) for range input { } + wg.Wait() + if err != nil { return errors.Wrap(err, "consume WAL") } From 88305e7612fb7d58eac9611f5fbb0c27151b3a90 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Wed, 11 Oct 2017 10:17:59 +0200 Subject: [PATCH 2/2] Access chunk time range while holding lock --- head.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/head.go b/head.go index 8ba1a71bc..abe668248 100644 --- a/head.go +++ b/head.go @@ -756,10 +756,11 @@ func (h *headChunkReader) Chunk(ref uint64) (chunks.Chunk, error) { s.Lock() c := s.chunk(int(cid)) + mint, maxt := c.minTime, c.maxTime s.Unlock() // Do not expose chunks that are outside of the specified range. - if c == nil || !intervalOverlap(c.minTime, c.maxTime, h.mint, h.maxt) { + if c == nil || !intervalOverlap(mint, maxt, h.mint, h.maxt) { return nil, ErrNotFound } return &safeChunk{