Skip to content

Commit 3cccb54

Browse files
committed
Avoid duplicate label index writes by combining LabelEntryCacheKeys and LabelWriteEntries
This avoids re-writing yesterday's labels when we write a chunk that spans the day boundary. Signed-off-by: Bryan Boreham <[email protected]>
1 parent 53438df commit 3cccb54

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

pkg/chunk/schema.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ type Schema interface {
3434
GetWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error)
3535

3636
// Should only be used with the seriesStore. TODO: Make seriesStore implement a different interface altogether.
37-
GetLabelWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error)
37+
// returns cache key string and []IndexEntry per bucket, matched in order
38+
GetCacheKeysAndLabelWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]string, [][]IndexEntry, error)
3839
GetChunkWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error)
39-
GetLabelEntryCacheKeys(from, through model.Time, userID string, labels labels.Labels) []string
4040

4141
// When doing a read, use these methods to return the list of entries you should query
4242
GetReadQueriesForMetric(from, through model.Time, userID string, metricName string) ([]IndexQuery, error)
@@ -97,17 +97,31 @@ func (s schema) GetWriteEntries(from, through model.Time, userID string, metricN
9797
return result, nil
9898
}
9999

100-
func (s schema) GetLabelWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error) {
101-
var result []IndexEntry
100+
// returns cache key string and []IndexEntry per bucket, matched in order
101+
func (s schema) GetCacheKeysAndLabelWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]string, [][]IndexEntry, error) {
102+
var keys []string
103+
var indexEntries [][]IndexEntry
102104

103105
for _, bucket := range s.buckets(from, through, userID) {
106+
key := strings.Join([]string{
107+
bucket.tableName,
108+
bucket.hashKey,
109+
string(labelsSeriesID(labels)),
110+
},
111+
"-",
112+
)
113+
// This is just encoding to remove invalid characters so that we can put them in memcache.
114+
// We're not hashing them as the length of the key is well within memcache bounds. tableName + userid + day + 32Byte(seriesID)
115+
key = hex.EncodeToString([]byte(key))
116+
keys = append(keys, key)
117+
104118
entries, err := s.entries.GetLabelWriteEntries(bucket, metricName, labels, chunkID)
105119
if err != nil {
106-
return nil, err
120+
return nil, nil, err
107121
}
108-
result = append(result, entries...)
122+
indexEntries = append(indexEntries, entries)
109123
}
110-
return result, nil
124+
return keys, indexEntries, nil
111125
}
112126

113127
func (s schema) GetChunkWriteEntries(from, through model.Time, userID string, metricName string, labels labels.Labels, chunkID string) ([]IndexEntry, error) {
@@ -124,27 +138,6 @@ func (s schema) GetChunkWriteEntries(from, through model.Time, userID string, me
124138

125139
}
126140

127-
// Should only used for v9Schema
128-
func (s schema) GetLabelEntryCacheKeys(from, through model.Time, userID string, labels labels.Labels) []string {
129-
var result []string
130-
for _, bucket := range s.buckets(from, through, userID) {
131-
key := strings.Join([]string{
132-
bucket.tableName,
133-
bucket.hashKey,
134-
string(labelsSeriesID(labels)),
135-
},
136-
"-",
137-
)
138-
// This is just encoding to remove invalid characters so that we can put them in memcache.
139-
// We're not hashing them as the length of the key is well within memcache bounds. tableName + userid + day + 32Byte(seriesID)
140-
key = hex.EncodeToString([]byte(key))
141-
142-
result = append(result, key)
143-
}
144-
145-
return result
146-
}
147-
148141
func (s schema) GetReadQueriesForMetric(from, through model.Time, userID string, metricName string) ([]IndexQuery, error) {
149142
var result []IndexQuery
150143

pkg/chunk/series_store.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,23 +372,25 @@ func (c *seriesStore) PutOne(ctx context.Context, from, through model.Time, chun
372372
func (c *seriesStore) calculateIndexEntries(from, through model.Time, chunk Chunk) (WriteBatch, []string, error) {
373373
seenIndexEntries := map[string]struct{}{}
374374
entries := []IndexEntry{}
375-
keysToCache := []string{}
376375

377376
metricName := chunk.Metric.Get(labels.MetricName)
378377
if metricName == "" {
379378
return nil, nil, fmt.Errorf("no MetricNameLabel for chunk")
380379
}
381-
keys := c.schema.GetLabelEntryCacheKeys(from, through, chunk.UserID, chunk.Metric)
382380

381+
keys, labelEntries, err := c.schema.GetCacheKeysAndLabelWriteEntries(from, through, chunk.UserID, metricName, chunk.Metric, chunk.ExternalKey())
382+
if err != nil {
383+
return nil, nil, err
384+
}
383385
_, _, missing := c.writeDedupeCache.Fetch(context.Background(), keys)
384-
if len(missing) != 0 {
385-
labelEntries, err := c.schema.GetLabelWriteEntries(from, through, chunk.UserID, metricName, chunk.Metric, chunk.ExternalKey())
386-
if err != nil {
387-
return nil, nil, err
386+
// keys and labelEntries are matched in order, but Fetch() may
387+
// return missing keys in any order so check against all of them.
388+
for _, missingKey := range missing {
389+
for i, key := range keys {
390+
if key == missingKey {
391+
entries = append(entries, labelEntries[i]...)
392+
}
388393
}
389-
390-
entries = append(entries, labelEntries...)
391-
keysToCache = missing
392394
}
393395

394396
chunkEntries, err := c.schema.GetChunkWriteEntries(from, through, chunk.UserID, metricName, chunk.Metric, chunk.ExternalKey())
@@ -410,5 +412,5 @@ func (c *seriesStore) calculateIndexEntries(from, through model.Time, chunk Chun
410412
}
411413
}
412414

413-
return result, keysToCache, nil
415+
return result, missing, nil
414416
}

0 commit comments

Comments
 (0)