From 1e55b7987f0252706adc1412edb0546e9065d285 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 22 Dec 2017 09:43:34 +0100 Subject: [PATCH] Improve comments, handle allPostingsKey properly --- block.go | 6 +++--- chunks/chunks.go | 3 --- compact.go | 2 +- index/postings.go | 16 +++++++++++++--- querier.go | 3 +-- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/block.go b/block.go index 4ef58e913..400cdac78 100644 --- a/block.go +++ b/block.go @@ -63,7 +63,7 @@ type IndexReader interface { // and indices. Symbols() (map[string]struct{}, error) - // LabelValues returns the possible label values + // LabelValues returns the possible label values. LabelValues(names ...string) (index.StringTuples, error) // Postings returns the postings list iterator for the label pair. @@ -81,10 +81,10 @@ type IndexReader interface { // Returns ErrNotFound if the ref does not resolve to a known series. Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta) error - // LabelIndices returns the label pairs for which indices exist. + // LabelIndices returns a list of string tuples for which a label value index exists. LabelIndices() ([][]string, error) - // Close released the underlying resources of the reader. + // Close releases the underlying resources of the reader. Close() error } diff --git a/chunks/chunks.go b/chunks/chunks.go index d07cf11a6..e32ea0f99 100644 --- a/chunks/chunks.go +++ b/chunks/chunks.go @@ -63,9 +63,6 @@ var ( errInvalidChecksum = fmt.Errorf("invalid checksum") ) -// The table gets initialized with sync.Once but may still cause a race -// with any other use of the crc32 package anywhere. Thus we initialize it -// before. var castagnoliTable *crc32.Table func init() { diff --git a/compact.go b/compact.go index 9a73ca07b..5c532759f 100644 --- a/compact.go +++ b/compact.go @@ -516,7 +516,7 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta, allSymbols[s] = struct{}{} } - all, err := indexr.Postings("", "") + all, err := indexr.Postings(index.AllPostingsKey()) if err != nil { return err } diff --git a/index/postings.go b/index/postings.go index bc63b4dac..f942edaad 100644 --- a/index/postings.go +++ b/index/postings.go @@ -23,6 +23,13 @@ import ( "github.com/prometheus/tsdb/labels" ) +var allPostingsKey = labels.Label{} + +// AllPostingsKey returns the label key that is used to store the postings list of all existing IDs. +func AllPostingsKey() (name, value string) { + return allPostingsKey.Name, allPostingsKey.Value +} + // MemPostings holds postings list for series ID per label pair. They may be written // to out of order. // ensureOrder() must be called once before any reads are done. This allows for quick @@ -83,11 +90,9 @@ func (p *MemPostings) Get(name, value string) Postings { // All returns a postings list over all documents ever added. func (p *MemPostings) All() Postings { - return p.Get(allPostingsKey.Name, allPostingsKey.Value) + return p.Get(AllPostingsKey()) } -var allPostingsKey = labels.Label{} - // EnsureOrder ensures that all postings lists are sorted. After it returns all further // calls to add and addFor will insert new IDs in a sorted manner. func (p *MemPostings) EnsureOrder() { @@ -126,13 +131,18 @@ func (p *MemPostings) EnsureOrder() { func (p *MemPostings) Delete(deleted map[uint64]struct{}) { var keys []labels.Label + // Collect all keys relevant for deletion once. New keys added afterwards + // can by definition not be affected by any of the given deletes. p.mtx.RLock() for l := range p.m { keys = append(keys, l) } p.mtx.RUnlock() + // For each key we first analyse whether the postings list is affected by the deletes. + // If yes, we actually reallocate a new postings list. for _, l := range keys { + // Only lock for processing one postings list so we don't block reads for too long. p.mtx.Lock() found := false diff --git a/querier.go b/querier.go index 48d7ba9c2..d4dad9306 100644 --- a/querier.go +++ b/querier.go @@ -334,11 +334,10 @@ func postingsForUnsetLabelMatcher(ix IndexReader, m labels.Matcher) (index.Posti rit = append(rit, it) } - allPostings, err := ix.Postings("", "") + allPostings, err := ix.Postings(index.AllPostingsKey()) if err != nil { return nil, err } - return index.Without(allPostings, index.Merge(rit...)), nil }