diff --git a/.gitignore b/.gitignore index 12df738aa72..e43b6b51f57 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ cmd/lite/lite pkg/ingester/client/cortex.pb.go pkg/querier/frontend/frontend.pb.go pkg/ring/ring.pb.go +pkg/chunk/storage/caching_storage_client.pb.go images/ diff --git a/pkg/chunk/aws/storage_client.go b/pkg/chunk/aws/storage_client.go index 9353280ead5..e47b9a666f6 100644 --- a/pkg/chunk/aws/storage_client.go +++ b/pkg/chunk/aws/storage_client.go @@ -30,6 +30,7 @@ import ( "github.com/weaveworks/common/instrument" "github.com/weaveworks/common/user" "github.com/weaveworks/cortex/pkg/chunk" + chunk_util "github.com/weaveworks/cortex/pkg/chunk/util" "github.com/weaveworks/cortex/pkg/util" ) @@ -301,7 +302,11 @@ func (a storageClient) BatchWrite(ctx context.Context, input chunk.WriteBatch) e return backoff.Err() } -func (a storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { +func (a storageClient) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) bool) error { + return chunk_util.DoParallelQueries(ctx, a.query, queries, callback) +} + +func (a storageClient) query(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { sp, ctx := ot.StartSpanFromContext(ctx, "QueryPages", ot.Tag{Key: "tableName", Value: query.TableName}, ot.Tag{Key: "hashValue", Value: query.HashValue}) defer sp.Finish() @@ -371,7 +376,7 @@ func (a storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, c return nil } -func (a storageClient) queryPage(ctx context.Context, input *dynamodb.QueryInput, page dynamoDBRequest) (dynamoDBReadResponse, error) { +func (a storageClient) queryPage(ctx context.Context, input *dynamodb.QueryInput, page dynamoDBRequest) (*dynamoDBReadResponse, error) { backoff := util.NewBackoff(ctx, a.cfg.backoffConfig) defer func() { dynamoQueryRetryCount.WithLabelValues("queryPage").Observe(float64(backoff.NumRetries())) @@ -401,7 +406,9 @@ func (a storageClient) queryPage(ctx context.Context, input *dynamodb.QueryInput } queryOutput := page.Data().(*dynamodb.QueryOutput) - return dynamoDBReadResponse(queryOutput.Items), nil + return &dynamoDBReadResponse{ + items: queryOutput.Items, + }, nil } return nil, fmt.Errorf("QueryPage error: %s for table %v, last error %v", backoff.Err(), *input.TableName, err) } @@ -785,18 +792,33 @@ func (a storageClient) putS3Chunk(ctx context.Context, key string, buf []byte) e } // Slice of values returned; map key is attribute name -type dynamoDBReadResponse []map[string]*dynamodb.AttributeValue +type dynamoDBReadResponse struct { + items []map[string]*dynamodb.AttributeValue +} + +func (b *dynamoDBReadResponse) Iterator() chunk.ReadBatchIterator { + return &dynamoDBReadResponseIterator{ + i: -1, + dynamoDBReadResponse: b, + } +} + +type dynamoDBReadResponseIterator struct { + i int + *dynamoDBReadResponse +} -func (b dynamoDBReadResponse) Len() int { - return len(b) +func (b *dynamoDBReadResponseIterator) Next() bool { + b.i++ + return b.i < len(b.items) } -func (b dynamoDBReadResponse) RangeValue(i int) []byte { - return b[i][rangeKey].B +func (b *dynamoDBReadResponseIterator) RangeValue() []byte { + return b.items[b.i][rangeKey].B } -func (b dynamoDBReadResponse) Value(i int) []byte { - chunkValue, ok := b[i][valueKey] +func (b *dynamoDBReadResponseIterator) Value() []byte { + chunkValue, ok := b.items[b.i][valueKey] if !ok { return nil } diff --git a/pkg/chunk/cache/fifo_cache.go b/pkg/chunk/cache/fifo_cache.go index a5a2f8218f7..9d88e357bb3 100644 --- a/pkg/chunk/cache/fifo_cache.go +++ b/pkg/chunk/cache/fifo_cache.go @@ -5,8 +5,6 @@ import ( "sync" "time" - ot "github.com/opentracing/opentracing-go" - otlog "github.com/opentracing/opentracing-go/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -132,9 +130,6 @@ func (c *FifoCache) Stop() error { // Put stores the value against the key. func (c *FifoCache) Put(ctx context.Context, key string, value interface{}) { - span, ctx := ot.StartSpanFromContext(ctx, c.name+"-cache-put") - defer span.Finish() - c.entriesAdded.Inc() if c.size == 0 { return @@ -202,9 +197,6 @@ func (c *FifoCache) Put(ctx context.Context, key string, value interface{}) { // Get returns the stored value against the key and when the key was last updated. func (c *FifoCache) Get(ctx context.Context, key string) (interface{}, bool) { - span, ctx := ot.StartSpanFromContext(ctx, c.name+"-cache-get") - defer span.Finish() - c.totalGets.Inc() if c.size == 0 { return nil, false @@ -217,17 +209,15 @@ func (c *FifoCache) Get(ctx context.Context, key string) (interface{}, bool) { if ok { updated := c.entries[index].updated if time.Now().Sub(updated) < c.validity { - span.LogFields(otlog.Bool("hit", true)) + return c.entries[index].value, true } c.totalMisses.Inc() c.staleGets.Inc() - span.LogFields(otlog.Bool("hit", false), otlog.Bool("stale", true)) return nil, false } - span.LogFields(otlog.Bool("hit", false), otlog.Bool("stale", false)) c.totalMisses.Inc() return nil, false } diff --git a/pkg/chunk/cassandra/storage_client.go b/pkg/chunk/cassandra/storage_client.go index 88d443db8bf..b0a6922770b 100644 --- a/pkg/chunk/cassandra/storage_client.go +++ b/pkg/chunk/cassandra/storage_client.go @@ -12,6 +12,7 @@ import ( "github.com/prometheus/common/model" "github.com/weaveworks/cortex/pkg/chunk" + "github.com/weaveworks/cortex/pkg/chunk/util" ) const ( @@ -185,7 +186,11 @@ func (s *storageClient) BatchWrite(ctx context.Context, batch chunk.WriteBatch) return nil } -func (s *storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { +func (s *storageClient) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) bool) error { + return util.DoParallelQueries(ctx, s.query, queries, callback) +} + +func (s *storageClient) query(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { var q *gocql.Query switch { @@ -218,7 +223,7 @@ func (s *storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, defer iter.Close() scanner := iter.Scanner() for scanner.Next() { - var b readBatch + b := &readBatch{} if err := scanner.Scan(&b.rangeValue, &b.value); err != nil { return errors.WithStack(err) } @@ -231,27 +236,35 @@ func (s *storageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, // readBatch represents a batch of rows read from Cassandra. type readBatch struct { + consumed bool rangeValue []byte value []byte } -// Len implements chunk.ReadBatch; in Cassandra we 'stream' results back -// one-by-one, so this always returns 1. -func (readBatch) Len() int { - return 1 +func (r *readBatch) Iterator() chunk.ReadBatchIterator { + return &readBatchIter{ + readBatch: r, + } +} + +type readBatchIter struct { + consumed bool + *readBatch } -func (b readBatch) RangeValue(index int) []byte { - if index != 0 { - panic("index != 0") +func (b *readBatchIter) Next() bool { + if b.consumed { + return false } + b.consumed = true + return true +} + +func (b *readBatchIter) RangeValue() []byte { return b.rangeValue } -func (b readBatch) Value(index int) []byte { - if index != 0 { - panic("index != 0") - } +func (b *readBatchIter) Value() []byte { return b.value } diff --git a/pkg/chunk/chunk_store.go b/pkg/chunk/chunk_store.go index c8abb23441c..a0d0819e3f4 100644 --- a/pkg/chunk/chunk_store.go +++ b/pkg/chunk/chunk_store.go @@ -347,53 +347,23 @@ func (c *store) lookupChunksByMetricName(ctx context.Context, from, through mode } func (c *store) lookupEntriesByQueries(ctx context.Context, queries []IndexQuery) ([]IndexEntry, error) { - incomingEntries := make(chan []IndexEntry) - incomingErrors := make(chan error) - for _, query := range queries { - go func(query IndexQuery) { - entries, err := c.lookupEntriesByQuery(ctx, query) - if err != nil { - incomingErrors <- err - } else { - incomingEntries <- entries - } - }(query) - } - - // Combine the results into one slice - var entries []IndexEntry - var lastErr error - for i := 0; i < len(queries); i++ { - select { - case incoming := <-incomingEntries: - entries = append(entries, incoming...) - case err := <-incomingErrors: - lastErr = err - } - } - - return entries, lastErr -} - -func (c *store) lookupEntriesByQuery(ctx context.Context, query IndexQuery) ([]IndexEntry, error) { var entries []IndexEntry - - if err := c.storage.QueryPages(ctx, query, func(resp ReadBatch) (shouldContinue bool) { - for i := 0; i < resp.Len(); i++ { + err := c.storage.QueryPages(ctx, queries, func(query IndexQuery, resp ReadBatch) bool { + iter := resp.Iterator() + for iter.Next() { entries = append(entries, IndexEntry{ TableName: query.TableName, HashValue: query.HashValue, - RangeValue: resp.RangeValue(i), - Value: resp.Value(i), + RangeValue: iter.RangeValue(), + Value: iter.Value(), }) } return true - }); err != nil { + }) + if err != nil { level.Error(util.WithContext(ctx, util.Logger)).Log("msg", "error querying storage", "err", err) - return nil, err } - - return entries, nil + return entries, err } func (c *store) parseIndexEntries(ctx context.Context, entries []IndexEntry, matcher *labels.Matcher) ([]string, error) { diff --git a/pkg/chunk/gcp/storage_client.go b/pkg/chunk/gcp/storage_client.go index 9135236acc7..aca2b4198ed 100644 --- a/pkg/chunk/gcp/storage_client.go +++ b/pkg/chunk/gcp/storage_client.go @@ -14,11 +14,13 @@ import ( "github.com/pkg/errors" "github.com/weaveworks/cortex/pkg/chunk" + chunk_util "github.com/weaveworks/cortex/pkg/chunk/util" "github.com/weaveworks/cortex/pkg/util" ) const ( columnFamily = "f" + columnPrefix = columnFamily + ":" column = "c" separator = "\000" maxRowReads = 100 @@ -187,7 +189,84 @@ func (s *storageClientColumnKey) BatchWrite(ctx context.Context, batch chunk.Wri return nil } -func (s *storageClientColumnKey) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { +func (s *storageClientColumnKey) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) bool) error { + sp, ctx := ot.StartSpanFromContext(ctx, "QueryPages") + defer sp.Finish() + + // A limitation of this approach is that this only fetches whole rows; but + // whatever, we filter them in the cache on the client. But for unit tests to + // pass, we must do this. + callback = chunk_util.QueryFilter(callback) + + type tableQuery struct { + name string + queries map[string]chunk.IndexQuery + rows bigtable.RowList + } + + tableQueries := map[string]tableQuery{} + for _, query := range queries { + tq, ok := tableQueries[query.TableName] + if !ok { + tq = tableQuery{ + name: query.TableName, + queries: map[string]chunk.IndexQuery{}, + } + } + tq.queries[query.HashValue] = query + tq.rows = append(tq.rows, query.HashValue) + tableQueries[query.TableName] = tq + } + + errs := make(chan error) + for _, tq := range tableQueries { + table := s.client.Open(tq.name) + + for i := 0; i < len(tq.rows); i += maxRowReads { + page := tq.rows[i:util.Min(i+maxRowReads, len(tq.rows))] + go func(page bigtable.RowList, tq tableQuery) { + var processingErr error + // rows are returned in key order, not order in row list + err := table.ReadRows(ctx, page, func(row bigtable.Row) bool { + query, ok := tq.queries[row.Key()] + if !ok { + processingErr = errors.WithStack(fmt.Errorf("Got row for unknown chunk: %s", row.Key())) + return false + } + + val, ok := row[columnFamily] + if !ok { + // There are no matching rows. + return true + } + + return callback(query, &columnKeyBatch{ + items: val, + }) + }) + + if processingErr != nil { + errs <- processingErr + } else { + errs <- err + } + }(page, tq) + } + } + + var lastErr error + for _, tq := range tableQueries { + for i := 0; i < len(tq.rows); i += maxRowReads { + err := <-errs + if err != nil { + lastErr = err + } + } + } + return lastErr +} + +func (s *storageClientColumnKey) query(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { const null = string('\xff') sp, ctx := ot.StartSpanFromContext(ctx, "QueryPages", ot.Tag{Key: "tableName", Value: query.TableName}, ot.Tag{Key: "hashValue", Value: query.HashValue}) @@ -227,31 +306,40 @@ func (s *storageClientColumnKey) QueryPages(ctx context.Context, query chunk.Ind val = filteredItems } - callback(bigtableReadBatchColumnKey{ - items: val, - columnPrefix: columnFamily + ":", + callback(&columnKeyBatch{ + items: val, }) return nil } -// bigtableReadBatchColumnKey represents a batch of values read from Bigtable. -type bigtableReadBatchColumnKey struct { - items []bigtable.ReadItem - columnPrefix string +// columnKeyBatch represents a batch of values read from Bigtable. +type columnKeyBatch struct { + items []bigtable.ReadItem +} + +func (c *columnKeyBatch) Iterator() chunk.ReadBatchIterator { + return &columnKeyIterator{ + i: -1, + columnKeyBatch: c, + } } -func (b bigtableReadBatchColumnKey) Len() int { - return len(b.items) +type columnKeyIterator struct { + i int + *columnKeyBatch } -func (b bigtableReadBatchColumnKey) RangeValue(index int) []byte { - return []byte( - strings.TrimPrefix(b.items[index].Column, b.columnPrefix), - ) +func (c *columnKeyIterator) Next() bool { + c.i++ + return c.i < len(c.items) } -func (b bigtableReadBatchColumnKey) Value(index int) []byte { - return b.items[index].Value +func (c *columnKeyIterator) RangeValue() []byte { + return []byte(strings.TrimPrefix(c.items[c.i].Column, columnPrefix)) +} + +func (c *columnKeyIterator) Value() []byte { + return c.items[c.i].Value } func (s *storageClientColumnKey) PutChunks(ctx context.Context, chunks []chunk.Chunk) error { @@ -368,7 +456,11 @@ func (s *storageClientColumnKey) GetChunks(ctx context.Context, input []chunk.Ch return output, nil } -func (s *storageClientV1) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { +func (s *storageClientV1) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) bool) error { + return chunk_util.DoParallelQueries(ctx, s.query, queries, callback) +} + +func (s *storageClientV1) query(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { const null = string('\xff') sp, ctx := ot.StartSpanFromContext(ctx, "QueryPages", ot.Tag{Key: "tableName", Value: query.TableName}, ot.Tag{Key: "hashValue", Value: query.HashValue}) @@ -398,7 +490,9 @@ func (s *storageClientV1) QueryPages(ctx context.Context, query chunk.IndexQuery err := table.ReadRows(ctx, rowRange, func(r bigtable.Row) bool { if query.ValueEqual == nil || bytes.Equal(r[columnFamily][0].Value, query.ValueEqual) { - return callback(bigtableReadBatchV1(r)) + return callback(&rowBatch{ + row: r, + }) } return true @@ -410,27 +504,40 @@ func (s *storageClientV1) QueryPages(ctx context.Context, query chunk.IndexQuery return nil } -// bigtableReadBatchV1 represents a batch of rows read from Bigtable. As the +// rowBatch represents a batch of rows read from Bigtable. As the // bigtable interface gives us rows one-by-one, a batch always only contains // a single row. -type bigtableReadBatchV1 bigtable.Row +type rowBatch struct { + row bigtable.Row +} + +func (b *rowBatch) Iterator() chunk.ReadBatchIterator { + return &rowBatchIterator{ + rowBatch: b, + } +} -func (bigtableReadBatchV1) Len() int { - return 1 +type rowBatchIterator struct { + consumed bool + *rowBatch } -func (b bigtableReadBatchV1) RangeValue(index int) []byte { - if index != 0 { - panic("index != 0") + +func (b *rowBatchIterator) Next() bool { + if b.consumed { + return false } + b.consumed = true + return true +} + +func (b *rowBatchIterator) RangeValue() []byte { // String before the first separator is the hashkey - parts := strings.SplitN(bigtable.Row(b).Key(), separator, 2) + parts := strings.SplitN(b.row.Key(), separator, 2) return []byte(parts[1]) } -func (b bigtableReadBatchV1) Value(index int) []byte { - if index != 0 { - panic("index != 0") - } - cf, ok := b[columnFamily] + +func (b *rowBatchIterator) Value() []byte { + cf, ok := b.row[columnFamily] if !ok || len(cf) != 1 { panic("bad response from bigtable") } diff --git a/pkg/chunk/inmemory_storage_client.go b/pkg/chunk/inmemory_storage_client.go index 0add546b2a4..4978dede575 100644 --- a/pkg/chunk/inmemory_storage_client.go +++ b/pkg/chunk/inmemory_storage_client.go @@ -169,13 +169,26 @@ func (m *MockStorage) BatchWrite(ctx context.Context, batch WriteBatch) error { } // QueryPages implements StorageClient. -func (m *MockStorage) QueryPages(ctx context.Context, query IndexQuery, callback func(result ReadBatch) (shouldContinue bool)) error { - logger := util.WithContext(ctx, util.Logger) - level.Debug(logger).Log("msg", "QueryPages", "query", query.HashValue) - +func (m *MockStorage) QueryPages(ctx context.Context, queries []IndexQuery, callback func(IndexQuery, ReadBatch) (shouldContinue bool)) error { m.mtx.RLock() defer m.mtx.RUnlock() + for _, query := range queries { + err := m.query(ctx, query, func(b ReadBatch) bool { + return callback(query, b) + }) + if err != nil { + return err + } + } + + return nil +} + +func (m *MockStorage) query(ctx context.Context, query IndexQuery, callback func(ReadBatch) (shouldContinue bool)) error { + logger := util.WithContext(ctx, util.Logger) + level.Debug(logger).Log("msg", "QueryPages", "query", query.HashValue) + table, ok := m.tables[query.TableName] if !ok { return fmt.Errorf("table not found") @@ -243,10 +256,10 @@ func (m *MockStorage) QueryPages(ctx context.Context, query IndexQuery, callback result := mockReadBatch{} for _, item := range items { - result = append(result, item) + result.items = append(result.items, item) } - callback(result) + callback(&result) return nil } @@ -300,16 +313,31 @@ func (b *mockWriteBatch) Add(tableName, hashValue string, rangeValue []byte, val }{tableName, hashValue, rangeValue, value}) } -type mockReadBatch []mockItem +type mockReadBatch struct { + items []mockItem +} + +func (b *mockReadBatch) Iterator() ReadBatchIterator { + return &mockReadBatchIter{ + index: -1, + mockReadBatch: b, + } +} + +type mockReadBatchIter struct { + index int + *mockReadBatch +} -func (b mockReadBatch) Len() int { - return len(b) +func (b *mockReadBatchIter) Next() bool { + b.index++ + return b.index < len(b.items) } -func (b mockReadBatch) RangeValue(i int) []byte { - return b[i].rangeValue +func (b *mockReadBatchIter) RangeValue() []byte { + return b.items[b.index].rangeValue } -func (b mockReadBatch) Value(i int) []byte { - return b[i].value +func (b *mockReadBatchIter) Value() []byte { + return b.items[b.index].value } diff --git a/pkg/chunk/series_store.go b/pkg/chunk/series_store.go index 082381237f1..03bb07b576a 100644 --- a/pkg/chunk/series_store.go +++ b/pkg/chunk/series_store.go @@ -30,22 +30,22 @@ var ( Namespace: "cortex", Name: "chunk_store_series_pre_intersection_per_query", Help: "Distribution of #series (pre intersection) per query.", - // A reasonable upper bound is around 100k - 10*(8^8) = 167k. - Buckets: prometheus.ExponentialBuckets(10, 8, 8), + // A reasonable upper bound is around 100k - 10*(8^5) = 327k. + Buckets: prometheus.ExponentialBuckets(10, 8, 5), }) postIntersectionPerQuery = promauto.NewHistogram(prometheus.HistogramOpts{ Namespace: "cortex", Name: "chunk_store_series_post_intersection_per_query", Help: "Distribution of #series (post intersection) per query.", - // A reasonable upper bound is around 100k - 10*(8^8) = 167k. - Buckets: prometheus.ExponentialBuckets(10, 8, 8), + // A reasonable upper bound is around 100k - 10*(8^5) = 327k. + Buckets: prometheus.ExponentialBuckets(10, 8, 5), }) chunksPerQuery = promauto.NewHistogram(prometheus.HistogramOpts{ Namespace: "cortex", Name: "chunk_store_chunks_per_query", Help: "Distribution of #chunks per query.", - // For v. high cardinality could go upto 1m chunks per query - 10*(8^9) = 1.3m. - Buckets: prometheus.ExponentialBuckets(10, 8, 9), + // For 100k series for 7 week, could be 1.2m - 10*(8^6) = 2.6m. + Buckets: prometheus.ExponentialBuckets(10, 8, 6), }) ) diff --git a/pkg/chunk/storage/caching_storage_client.go b/pkg/chunk/storage/caching_storage_client.go index 00e9ed922e4..770e68c0a0b 100644 --- a/pkg/chunk/storage/caching_storage_client.go +++ b/pkg/chunk/storage/caching_storage_client.go @@ -1,19 +1,20 @@ package storage import ( - "bytes" "context" "encoding/hex" "hash/fnv" - "strings" + "sync" "time" + "github.com/go-kit/kit/log/level" proto "github.com/golang/protobuf/proto" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/weaveworks/cortex/pkg/chunk" "github.com/weaveworks/cortex/pkg/chunk/cache" + chunk_util "github.com/weaveworks/cortex/pkg/chunk/util" + "github.com/weaveworks/cortex/pkg/util" ) var ( @@ -42,7 +43,7 @@ var ( // IndexCache describes the cache for the Index. type IndexCache interface { Store(ctx context.Context, key string, val ReadBatch) - Fetch(ctx context.Context, key string) (val ReadBatch, ok bool, err error) + Fetch(ctx context.Context, keys []string) (batches []ReadBatch, misses []string) Stop() error } @@ -64,26 +65,77 @@ func (c *indexCache) Store(ctx context.Context, key string, val ReadBatch) { return } -func (c *indexCache) Fetch(ctx context.Context, key string) (ReadBatch, bool, error) { +func (c *indexCache) Fetch(ctx context.Context, keys []string) (batches []ReadBatch, missed []string) { cacheGets.Inc() - found, valBytes, _, err := c.Cache.Fetch(ctx, []string{hashKey(key)}) - if len(found) != 1 || err != nil { - return ReadBatch{}, false, err + // Build a map from hash -> key; NB there can be collisions here; we'll fetch + // the last hash. + hashedKeys := make(map[string]string, len(keys)) + for _, key := range keys { + hashedKeys[hashKey(key)] = key } - var rb ReadBatch - if err := proto.Unmarshal(valBytes[0], &rb); err != nil { - return rb, false, err + // Build a list of hashes; could be less than keys due to collisions. + hashes := make([]string, 0, len(keys)) + for hash := range hashedKeys { + hashes = append(hashes, hash) } - // Make sure the hash(key) is not a collision by looking at the key in the value. - if key == rb.Key && time.Now().Before(time.Unix(0, rb.Expiry)) { + // Look up the hashes in a single batch. If we get an error, we just "miss" all + // of the keys. Eventually I want to push all the errors to the leafs of the cache + // tree, to the caches only return found & missed. + foundHashes, bufs, _, err := c.Cache.Fetch(ctx, hashes) + if err != nil { + level.Warn(util.Logger).Log("msg", "error fetching index entries", "err", err) + return nil, keys + } + + // Reverse the hash, unmarshal the index entries, check we got what we expected + // and that its still valid. + batches = make([]ReadBatch, 0, len(foundHashes)) + for j, foundHash := range foundHashes { + key := hashedKeys[foundHash] + var readBatch ReadBatch + + if err := proto.Unmarshal(bufs[j], &readBatch); err != nil { + level.Warn(util.Logger).Log("msg", "error unmarshalling index entry from cache", "err", err) + cacheCorruptErrs.Inc() + continue + } + + // Make sure the hash(key) is not a collision in the cache by looking at the + // key in the value. + if key != readBatch.Key || time.Now().After(time.Unix(0, readBatch.Expiry)) { + cacheCorruptErrs.Inc() + continue + } + cacheHits.Inc() - return rb, true, nil + batches = append(batches, readBatch) } - return ReadBatch{}, false, nil + // Finally work out what we're missing. + misses := make(map[string]struct{}, len(keys)) + for _, key := range keys { + misses[key] = struct{}{} + } + for i := range batches { + delete(misses, batches[i].Key) + } + missed = make([]string, 0, len(misses)) + for miss := range misses { + missed = append(missed, miss) + } + + return batches, missed +} + +func hashKey(key string) string { + hasher := fnv.New64a() + hasher.Write([]byte(key)) // This'll never error. + + // Hex because memcache errors for the bytes produced by the hash. + return hex.EncodeToString(hasher.Sum(nil)) } type cachingStorageClient struct { @@ -104,102 +156,108 @@ func newCachingStorageClient(client chunk.StorageClient, cache cache.Cache, vali } } -func (s *cachingStorageClient) QueryPages(ctx context.Context, query chunk.IndexQuery, callback func(result chunk.ReadBatch) (shouldContinue bool)) error { - value, ok, err := s.cache.Fetch(ctx, queryKey(query)) - if err != nil { - cacheCorruptErrs.Inc() +func (s *cachingStorageClient) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) (shouldContinue bool)) error { + // We cache the entire row, so filter client side. + callback = chunk_util.QueryFilter(callback) + + // Build list of keys to lookup in the cache. + keys := make([]string, 0, len(queries)) + queriesByKey := make(map[string][]chunk.IndexQuery, len(queries)) + for _, query := range queries { + key := queryKey(query) + keys = append(keys, key) + queriesByKey[key] = append(queriesByKey[key], query) } - if ok && err == nil { - filteredBatch, _ := filterBatchByQuery(query, []chunk.ReadBatch{value}) - callback(filteredBatch) + batches, misses := s.cache.Fetch(ctx, keys) + for _, batch := range batches { + queries := queriesByKey[batch.Key] + for _, query := range queries { + callback(query, batch) + } + } + if len(misses) == 0 { return nil } - batches := []chunk.ReadBatch{} - cacheableQuery := chunk.IndexQuery{ - TableName: query.TableName, - HashValue: query.HashValue, - } // Just reads the entire row and caches it. + // Build list of cachable queries for the queries that missed the cache. + cacheableMissed := []chunk.IndexQuery{} + for _, key := range misses { + // Only need to consider one of the queries as they have the same table & hash. + queries := queriesByKey[key] + cacheableMissed = append(cacheableMissed, chunk.IndexQuery{ + TableName: queries[0].TableName, + HashValue: queries[0].HashValue, + }) + } + var resultsMtx sync.Mutex + results := map[string]ReadBatch{} expiryTime := time.Now().Add(s.validity) - err = s.StorageClient.QueryPages(ctx, cacheableQuery, copyingCallback(&batches)) + err := s.StorageClient.QueryPages(ctx, cacheableMissed, func(cacheableQuery chunk.IndexQuery, r chunk.ReadBatch) bool { + resultsMtx.Lock() + defer resultsMtx.Unlock() + key := queryKey(cacheableQuery) + existing, ok := results[key] + if !ok { + existing = ReadBatch{ + Key: key, + Expiry: expiryTime.UnixNano(), + } + } + for iter := r.Iterator(); iter.Next(); { + existing.Entries = append(existing.Entries, Entry{Column: iter.RangeValue(), Value: iter.Value()}) + } + results[key] = existing + return true + }) if err != nil { return err } - filteredBatch, totalBatches := filterBatchByQuery(query, batches) - callback(filteredBatch) - - totalBatches.Key = queryKey(query) - totalBatches.Expiry = expiryTime.UnixNano() - - s.cache.Store(ctx, totalBatches.Key, totalBatches) + resultsMtx.Lock() + defer resultsMtx.Unlock() + for key, batch := range results { + queries := queriesByKey[key] + for _, query := range queries { + callback(query, batch) + } + s.cache.Store(ctx, key, batch) + } return nil } -// Len implements chunk.ReadBatch. -func (b ReadBatch) Len() int { return len(b.Entries) } - -// RangeValue implements chunk.ReadBatch. -func (b ReadBatch) RangeValue(i int) []byte { return b.Entries[i].Column } - -// Value implements chunk.ReadBatch. -func (b ReadBatch) Value(i int) []byte { return b.Entries[i].Value } - -func copyingCallback(readBatches *[]chunk.ReadBatch) func(chunk.ReadBatch) bool { - return func(result chunk.ReadBatch) bool { - *readBatches = append(*readBatches, result) - return true +// Iterator implements chunk.ReadBatch. +func (b ReadBatch) Iterator() chunk.ReadBatchIterator { + return &readBatchIterator{ + index: -1, + readBatch: b, } } -func queryKey(q chunk.IndexQuery) string { - const sep = "\xff" - return q.TableName + sep + q.HashValue +type readBatchIterator struct { + index int + readBatch ReadBatch } -func filterBatchByQuery(query chunk.IndexQuery, batches []chunk.ReadBatch) (filteredBatch, totalBatch ReadBatch) { - filter := func([]byte, []byte) bool { return true } - - if len(query.RangeValuePrefix) != 0 { - filter = func(rangeValue []byte, value []byte) bool { - return strings.HasPrefix(string(rangeValue), string(query.RangeValuePrefix)) - } - } - if len(query.RangeValueStart) != 0 { - filter = func(rangeValue []byte, value []byte) bool { - return string(rangeValue) >= string(query.RangeValueStart) - } - } - if len(query.ValueEqual) != 0 { - // This is on top of the existing filters. - existingFilter := filter - filter = func(rangeValue []byte, value []byte) bool { - return existingFilter(rangeValue, value) && bytes.Equal(value, query.ValueEqual) - } - } - - filteredBatch.Entries = make([]*Entry, 0, len(batches)) // On the higher side for most queries. On the lower side for column key schema. - totalBatch.Entries = make([]*Entry, 0, len(batches)) - for _, batch := range batches { - for i := 0; i < batch.Len(); i++ { - totalBatch.Entries = append(totalBatch.Entries, &Entry{Column: batch.RangeValue(i), Value: batch.Value(i)}) - - if filter(batch.RangeValue(i), batch.Value(i)) { - filteredBatch.Entries = append(filteredBatch.Entries, &Entry{Column: batch.RangeValue(i), Value: batch.Value(i)}) - } - } - } +// Len implements chunk.ReadBatchIterator. +func (b *readBatchIterator) Next() bool { + b.index++ + return b.index < len(b.readBatch.Entries) +} - return +// RangeValue implements chunk.ReadBatchIterator. +func (b *readBatchIterator) RangeValue() []byte { + return b.readBatch.Entries[b.index].Column } -func hashKey(key string) string { - hasher := fnv.New64a() - hasher.Write([]byte(key)) // This'll never error. +// Value implements chunk.ReadBatchIterator. +func (b *readBatchIterator) Value() []byte { + return b.readBatch.Entries[b.index].Value +} - // Hex because memcache errors for the bytes produced by the hash. - return hex.EncodeToString(hasher.Sum(nil)) +func queryKey(q chunk.IndexQuery) string { + const sep = "\xff" + return q.TableName + sep + q.HashValue } diff --git a/pkg/chunk/storage/caching_storage_client.pb.go b/pkg/chunk/storage/caching_storage_client.pb.go deleted file mode 100644 index 47c09c47d91..00000000000 --- a/pkg/chunk/storage/caching_storage_client.pb.go +++ /dev/null @@ -1,714 +0,0 @@ -// Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: caching_storage_client.proto - -/* - Package storage is a generated protocol buffer package. - - It is generated from these files: - caching_storage_client.proto - - It has these top-level messages: - Entry - ReadBatch -*/ -package storage - -import proto "github.com/gogo/protobuf/proto" -import fmt "fmt" -import math "math" - -import bytes "bytes" - -import strings "strings" -import reflect "reflect" - -import io "io" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package - -type Entry struct { - Column []byte `protobuf:"bytes,1,opt,name=Column,json=column,proto3" json:"Column,omitempty"` - Value []byte `protobuf:"bytes,2,opt,name=Value,json=value,proto3" json:"Value,omitempty"` -} - -func (m *Entry) Reset() { *m = Entry{} } -func (*Entry) ProtoMessage() {} -func (*Entry) Descriptor() ([]byte, []int) { return fileDescriptorCachingStorageClient, []int{0} } - -func (m *Entry) GetColumn() []byte { - if m != nil { - return m.Column - } - return nil -} - -func (m *Entry) GetValue() []byte { - if m != nil { - return m.Value - } - return nil -} - -type ReadBatch struct { - Entries []*Entry `protobuf:"bytes,1,rep,name=entries" json:"entries,omitempty"` - Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` - // The time at which the key expires. - Expiry int64 `protobuf:"varint,3,opt,name=expiry,proto3" json:"expiry,omitempty"` -} - -func (m *ReadBatch) Reset() { *m = ReadBatch{} } -func (*ReadBatch) ProtoMessage() {} -func (*ReadBatch) Descriptor() ([]byte, []int) { return fileDescriptorCachingStorageClient, []int{1} } - -func (m *ReadBatch) GetEntries() []*Entry { - if m != nil { - return m.Entries - } - return nil -} - -func (m *ReadBatch) GetKey() string { - if m != nil { - return m.Key - } - return "" -} - -func (m *ReadBatch) GetExpiry() int64 { - if m != nil { - return m.Expiry - } - return 0 -} - -func init() { - proto.RegisterType((*Entry)(nil), "storage.Entry") - proto.RegisterType((*ReadBatch)(nil), "storage.ReadBatch") -} -func (this *Entry) Equal(that interface{}) bool { - if that == nil { - return this == nil - } - - that1, ok := that.(*Entry) - if !ok { - that2, ok := that.(Entry) - if ok { - that1 = &that2 - } else { - return false - } - } - if that1 == nil { - return this == nil - } else if this == nil { - return false - } - if !bytes.Equal(this.Column, that1.Column) { - return false - } - if !bytes.Equal(this.Value, that1.Value) { - return false - } - return true -} -func (this *ReadBatch) Equal(that interface{}) bool { - if that == nil { - return this == nil - } - - that1, ok := that.(*ReadBatch) - if !ok { - that2, ok := that.(ReadBatch) - if ok { - that1 = &that2 - } else { - return false - } - } - if that1 == nil { - return this == nil - } else if this == nil { - return false - } - if len(this.Entries) != len(that1.Entries) { - return false - } - for i := range this.Entries { - if !this.Entries[i].Equal(that1.Entries[i]) { - return false - } - } - if this.Key != that1.Key { - return false - } - if this.Expiry != that1.Expiry { - return false - } - return true -} -func (this *Entry) GoString() string { - if this == nil { - return "nil" - } - s := make([]string, 0, 6) - s = append(s, "&storage.Entry{") - s = append(s, "Column: "+fmt.Sprintf("%#v", this.Column)+",\n") - s = append(s, "Value: "+fmt.Sprintf("%#v", this.Value)+",\n") - s = append(s, "}") - return strings.Join(s, "") -} -func (this *ReadBatch) GoString() string { - if this == nil { - return "nil" - } - s := make([]string, 0, 7) - s = append(s, "&storage.ReadBatch{") - if this.Entries != nil { - s = append(s, "Entries: "+fmt.Sprintf("%#v", this.Entries)+",\n") - } - s = append(s, "Key: "+fmt.Sprintf("%#v", this.Key)+",\n") - s = append(s, "Expiry: "+fmt.Sprintf("%#v", this.Expiry)+",\n") - s = append(s, "}") - return strings.Join(s, "") -} -func valueToGoStringCachingStorageClient(v interface{}, typ string) string { - rv := reflect.ValueOf(v) - if rv.IsNil() { - return "nil" - } - pv := reflect.Indirect(rv).Interface() - return fmt.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv) -} -func (m *Entry) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Entry) MarshalTo(dAtA []byte) (int, error) { - var i int - _ = i - var l int - _ = l - if len(m.Column) > 0 { - dAtA[i] = 0xa - i++ - i = encodeVarintCachingStorageClient(dAtA, i, uint64(len(m.Column))) - i += copy(dAtA[i:], m.Column) - } - if len(m.Value) > 0 { - dAtA[i] = 0x12 - i++ - i = encodeVarintCachingStorageClient(dAtA, i, uint64(len(m.Value))) - i += copy(dAtA[i:], m.Value) - } - return i, nil -} - -func (m *ReadBatch) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalTo(dAtA) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *ReadBatch) MarshalTo(dAtA []byte) (int, error) { - var i int - _ = i - var l int - _ = l - if len(m.Entries) > 0 { - for _, msg := range m.Entries { - dAtA[i] = 0xa - i++ - i = encodeVarintCachingStorageClient(dAtA, i, uint64(msg.Size())) - n, err := msg.MarshalTo(dAtA[i:]) - if err != nil { - return 0, err - } - i += n - } - } - if len(m.Key) > 0 { - dAtA[i] = 0x12 - i++ - i = encodeVarintCachingStorageClient(dAtA, i, uint64(len(m.Key))) - i += copy(dAtA[i:], m.Key) - } - if m.Expiry != 0 { - dAtA[i] = 0x18 - i++ - i = encodeVarintCachingStorageClient(dAtA, i, uint64(m.Expiry)) - } - return i, nil -} - -func encodeVarintCachingStorageClient(dAtA []byte, offset int, v uint64) int { - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return offset + 1 -} -func (m *Entry) Size() (n int) { - var l int - _ = l - l = len(m.Column) - if l > 0 { - n += 1 + l + sovCachingStorageClient(uint64(l)) - } - l = len(m.Value) - if l > 0 { - n += 1 + l + sovCachingStorageClient(uint64(l)) - } - return n -} - -func (m *ReadBatch) Size() (n int) { - var l int - _ = l - if len(m.Entries) > 0 { - for _, e := range m.Entries { - l = e.Size() - n += 1 + l + sovCachingStorageClient(uint64(l)) - } - } - l = len(m.Key) - if l > 0 { - n += 1 + l + sovCachingStorageClient(uint64(l)) - } - if m.Expiry != 0 { - n += 1 + sovCachingStorageClient(uint64(m.Expiry)) - } - return n -} - -func sovCachingStorageClient(x uint64) (n int) { - for { - n++ - x >>= 7 - if x == 0 { - break - } - } - return n -} -func sozCachingStorageClient(x uint64) (n int) { - return sovCachingStorageClient(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (this *Entry) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&Entry{`, - `Column:` + fmt.Sprintf("%v", this.Column) + `,`, - `Value:` + fmt.Sprintf("%v", this.Value) + `,`, - `}`, - }, "") - return s -} -func (this *ReadBatch) String() string { - if this == nil { - return "nil" - } - s := strings.Join([]string{`&ReadBatch{`, - `Entries:` + strings.Replace(fmt.Sprintf("%v", this.Entries), "Entry", "Entry", 1) + `,`, - `Key:` + fmt.Sprintf("%v", this.Key) + `,`, - `Expiry:` + fmt.Sprintf("%v", this.Expiry) + `,`, - `}`, - }, "") - return s -} -func valueToStringCachingStorageClient(v interface{}) string { - rv := reflect.ValueOf(v) - if rv.IsNil() { - return "nil" - } - pv := reflect.Indirect(rv).Interface() - return fmt.Sprintf("*%v", pv) -} -func (m *Entry) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Entry: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Entry: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Column", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCachingStorageClient - } - postIndex := iNdEx + byteLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Column = append(m.Column[:0], dAtA[iNdEx:postIndex]...) - if m.Column == nil { - m.Column = []byte{} - } - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Value", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthCachingStorageClient - } - postIndex := iNdEx + byteLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Value = append(m.Value[:0], dAtA[iNdEx:postIndex]...) - if m.Value == nil { - m.Value = []byte{} - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipCachingStorageClient(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthCachingStorageClient - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *ReadBatch) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: ReadBatch: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: ReadBatch: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Entries", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthCachingStorageClient - } - postIndex := iNdEx + msglen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Entries = append(m.Entries, &Entry{}) - if err := m.Entries[len(m.Entries)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthCachingStorageClient - } - postIndex := iNdEx + intStringLen - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Key = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 3: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Expiry", wireType) - } - m.Expiry = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Expiry |= (int64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - default: - iNdEx = preIndex - skippy, err := skipCachingStorageClient(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthCachingStorageClient - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipCachingStorageClient(dAtA []byte) (n int, err error) { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if dAtA[iNdEx-1] < 0x80 { - break - } - } - return iNdEx, nil - case 1: - iNdEx += 8 - return iNdEx, nil - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - iNdEx += length - if length < 0 { - return 0, ErrInvalidLengthCachingStorageClient - } - return iNdEx, nil - case 3: - for { - var innerWire uint64 - var start int = iNdEx - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowCachingStorageClient - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - innerWire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - innerWireType := int(innerWire & 0x7) - if innerWireType == 4 { - break - } - next, err := skipCachingStorageClient(dAtA[start:]) - if err != nil { - return 0, err - } - iNdEx = start + next - } - return iNdEx, nil - case 4: - return iNdEx, nil - case 5: - iNdEx += 4 - return iNdEx, nil - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - } - panic("unreachable") -} - -var ( - ErrInvalidLengthCachingStorageClient = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowCachingStorageClient = fmt.Errorf("proto: integer overflow") -) - -func init() { proto.RegisterFile("caching_storage_client.proto", fileDescriptorCachingStorageClient) } - -var fileDescriptorCachingStorageClient = []byte{ - // 229 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x49, 0x4e, 0x4c, 0xce, - 0xc8, 0xcc, 0x4b, 0x8f, 0x2f, 0x2e, 0xc9, 0x2f, 0x4a, 0x4c, 0x4f, 0x8d, 0x4f, 0xce, 0xc9, 0x4c, - 0xcd, 0x2b, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x87, 0x8a, 0x2a, 0x99, 0x72, 0xb1, - 0xba, 0xe6, 0x95, 0x14, 0x55, 0x0a, 0x89, 0x71, 0xb1, 0x39, 0xe7, 0xe7, 0x94, 0xe6, 0xe6, 0x49, - 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0xb1, 0x25, 0x83, 0x79, 0x42, 0x22, 0x5c, 0xac, 0x61, 0x89, - 0x39, 0xa5, 0xa9, 0x12, 0x4c, 0x60, 0x61, 0xd6, 0x32, 0x10, 0x47, 0x29, 0x9e, 0x8b, 0x33, 0x28, - 0x35, 0x31, 0xc5, 0x29, 0xb1, 0x24, 0x39, 0x43, 0x48, 0x83, 0x8b, 0x3d, 0x35, 0xaf, 0xa4, 0x28, - 0x33, 0xb5, 0x58, 0x82, 0x51, 0x81, 0x59, 0x83, 0xdb, 0x88, 0x4f, 0x0f, 0x6a, 0xbc, 0x1e, 0xd8, - 0xec, 0x20, 0x98, 0xb4, 0x90, 0x00, 0x17, 0x73, 0x76, 0x6a, 0x25, 0xd8, 0x28, 0xce, 0x20, 0x10, - 0x13, 0x64, 0x6d, 0x6a, 0x45, 0x41, 0x66, 0x51, 0xa5, 0x04, 0xb3, 0x02, 0xa3, 0x06, 0x73, 0x10, - 0x94, 0xe7, 0xa4, 0x73, 0xe1, 0xa1, 0x1c, 0xc3, 0x8d, 0x87, 0x72, 0x0c, 0x1f, 0x1e, 0xca, 0x31, - 0x36, 0x3c, 0x92, 0x63, 0x5c, 0xf1, 0x48, 0x8e, 0xf1, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, - 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x7c, 0xf1, 0x48, 0x8e, 0xe1, 0xc3, 0x23, 0x39, 0xc6, 0x09, 0x8f, - 0xe5, 0x18, 0x92, 0xd8, 0xc0, 0xbe, 0x32, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x00, 0x1b, 0x46, - 0xe1, 0xf5, 0x00, 0x00, 0x00, -} diff --git a/pkg/chunk/storage/caching_storage_client.proto b/pkg/chunk/storage/caching_storage_client.proto index f10dbe34437..cc133b82884 100644 --- a/pkg/chunk/storage/caching_storage_client.proto +++ b/pkg/chunk/storage/caching_storage_client.proto @@ -1,15 +1,21 @@ syntax = "proto3"; + package storage; +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; + +option (gogoproto.marshaler_all) = true; +option (gogoproto.unmarshaler_all) = true; + message Entry { - bytes Column = 1; - bytes Value = 2; + bytes Column = 1 [(gogoproto.customtype) = "github.com/weaveworks/cortex/pkg/util/wire.Bytes", (gogoproto.nullable) = false]; + bytes Value = 2 [(gogoproto.customtype) = "github.com/weaveworks/cortex/pkg/util/wire.Bytes", (gogoproto.nullable) = false]; } message ReadBatch { - repeated Entry entries = 1; + repeated Entry entries = 1 [(gogoproto.nullable) = false]; string key = 2; - // The time at which the key expires. + // The time at which the key expires. int64 expiry = 3; -} \ No newline at end of file +} diff --git a/pkg/chunk/storage/caching_storage_client_test.go b/pkg/chunk/storage/caching_storage_client_test.go new file mode 100644 index 00000000000..1cc1f1391e5 --- /dev/null +++ b/pkg/chunk/storage/caching_storage_client_test.go @@ -0,0 +1,153 @@ +package storage + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/weaveworks/cortex/pkg/chunk" + "github.com/weaveworks/cortex/pkg/chunk/cache" +) + +type mockStore struct { + chunk.StorageClient + queries int + results ReadBatch +} + +func (m *mockStore) QueryPages(ctx context.Context, queries []chunk.IndexQuery, callback func(chunk.IndexQuery, chunk.ReadBatch) (shouldContinue bool)) error { + for _, query := range queries { + m.queries++ + callback(query, m.results) + } + return nil +} + +func TestCachingStorageClientBasic(t *testing.T) { + store := &mockStore{ + results: ReadBatch{ + Entries: []Entry{{ + Column: []byte("foo"), + Value: []byte("bar"), + }}, + }, + } + cache := cache.NewFifoCache("test", 10, 10*time.Second) + client := newCachingStorageClient(store, cache, 1*time.Second) + queries := []chunk.IndexQuery{{ + TableName: "table", + HashValue: "baz", + }} + err := client.QueryPages(context.Background(), queries, func(_ chunk.IndexQuery, _ chunk.ReadBatch) bool { + return true + }) + require.NoError(t, err) + assert.EqualValues(t, 1, store.queries) + + // If we do the query to the cache again, the underlying store shouldn't see it. + err = client.QueryPages(context.Background(), queries, func(_ chunk.IndexQuery, _ chunk.ReadBatch) bool { + return true + }) + require.NoError(t, err) + assert.EqualValues(t, 1, store.queries) +} + +func TestCachingStorageClient(t *testing.T) { + store := &mockStore{ + results: ReadBatch{ + Entries: []Entry{{ + Column: []byte("foo"), + Value: []byte("bar"), + }}, + }, + } + cache := cache.NewFifoCache("test", 10, 10*time.Second) + client := newCachingStorageClient(store, cache, 1*time.Second) + queries := []chunk.IndexQuery{ + {TableName: "table", HashValue: "foo"}, + {TableName: "table", HashValue: "bar"}, + {TableName: "table", HashValue: "baz"}, + } + results := 0 + err := client.QueryPages(context.Background(), queries, func(query chunk.IndexQuery, batch chunk.ReadBatch) bool { + iter := batch.Iterator() + for iter.Next() { + results++ + } + return true + }) + require.NoError(t, err) + assert.EqualValues(t, len(queries), store.queries) + assert.EqualValues(t, len(queries), results) + + // If we do the query to the cache again, the underlying store shouldn't see it. + results = 0 + err = client.QueryPages(context.Background(), queries, func(query chunk.IndexQuery, batch chunk.ReadBatch) bool { + iter := batch.Iterator() + for iter.Next() { + results++ + } + return true + }) + require.NoError(t, err) + assert.EqualValues(t, len(queries), store.queries) + assert.EqualValues(t, len(queries), results) +} + +func TestCachingStorageClientCollision(t *testing.T) { + // These two queries should result in one query to the cache & index, but + // two results, as we cache entire rows. + store := &mockStore{ + results: ReadBatch{ + Entries: []Entry{ + { + Column: []byte("bar"), + Value: []byte("bar"), + }, + { + Column: []byte("baz"), + Value: []byte("baz"), + }, + }, + }, + } + cache := cache.NewFifoCache("test", 10, 10*time.Second) + client := newCachingStorageClient(store, cache, 1*time.Second) + queries := []chunk.IndexQuery{ + {TableName: "table", HashValue: "foo", RangeValuePrefix: []byte("bar")}, + {TableName: "table", HashValue: "foo", RangeValuePrefix: []byte("baz")}, + } + + var results ReadBatch + err := client.QueryPages(context.Background(), queries, func(query chunk.IndexQuery, batch chunk.ReadBatch) bool { + iter := batch.Iterator() + for iter.Next() { + results.Entries = append(results.Entries, Entry{ + Column: iter.RangeValue(), + Value: iter.Value(), + }) + } + return true + }) + require.NoError(t, err) + assert.EqualValues(t, 1, store.queries) + assert.EqualValues(t, store.results, results) + + // If we do the query to the cache again, the underlying store shouldn't see it. + results = ReadBatch{} + err = client.QueryPages(context.Background(), queries, func(query chunk.IndexQuery, batch chunk.ReadBatch) bool { + iter := batch.Iterator() + for iter.Next() { + results.Entries = append(results.Entries, Entry{ + Column: iter.RangeValue(), + Value: iter.Value(), + }) + } + return true + }) + require.NoError(t, err) + assert.EqualValues(t, 1, store.queries) + assert.EqualValues(t, store.results, results) +} diff --git a/pkg/chunk/storage/index_test.go b/pkg/chunk/storage/index_test.go index 91d8532bb96..24bca687ed2 100644 --- a/pkg/chunk/storage/index_test.go +++ b/pkg/chunk/storage/index_test.go @@ -21,15 +21,18 @@ func TestIndexBasic(t *testing.T) { // Make sure we get back the correct entries by hash value. for i := 0; i < 30; i++ { - entry := chunk.IndexQuery{ - TableName: tableName, - HashValue: fmt.Sprintf("hash%d", i), + entries := []chunk.IndexQuery{ + { + TableName: tableName, + HashValue: fmt.Sprintf("hash%d", i), + }, } var have []chunk.IndexEntry - err := client.QueryPages(context.Background(), entry, func(read chunk.ReadBatch) bool { - for j := 0; j < read.Len(); j++ { + err := client.QueryPages(context.Background(), entries, func(_ chunk.IndexQuery, read chunk.ReadBatch) bool { + iter := read.Iterator() + for iter.Next() { have = append(have, chunk.IndexEntry{ - RangeValue: read.RangeValue(j), + RangeValue: iter.RangeValue(), }) } return true @@ -167,13 +170,14 @@ func TestQueryPages(t *testing.T) { run := true for run { var have []chunk.IndexEntry - err = client.QueryPages(context.Background(), tt.query, func(read chunk.ReadBatch) bool { - for i := 0; i < read.Len(); i++ { + err = client.QueryPages(context.Background(), []chunk.IndexQuery{tt.query}, func(_ chunk.IndexQuery, read chunk.ReadBatch) bool { + iter := read.Iterator() + for iter.Next() { have = append(have, chunk.IndexEntry{ TableName: tt.query.TableName, HashValue: tt.query.HashValue, - RangeValue: read.RangeValue(i), - Value: read.Value(i), + RangeValue: iter.RangeValue(), + Value: iter.Value(), }) } return true diff --git a/pkg/chunk/storage_client.go b/pkg/chunk/storage_client.go index c86f573b77b..ecf83f7d622 100644 --- a/pkg/chunk/storage_client.go +++ b/pkg/chunk/storage_client.go @@ -9,7 +9,7 @@ type StorageClient interface { BatchWrite(context.Context, WriteBatch) error // For the read path. - QueryPages(ctx context.Context, query IndexQuery, callback func(result ReadBatch) (shouldContinue bool)) error + QueryPages(ctx context.Context, queries []IndexQuery, callback func(IndexQuery, ReadBatch) (shouldContinue bool)) error // For storing and retrieving chunks. PutChunks(ctx context.Context, chunks []Chunk) error @@ -23,7 +23,12 @@ type WriteBatch interface { // ReadBatch represents the results of a QueryPages. type ReadBatch interface { - Len() int - RangeValue(index int) []byte - Value(index int) []byte + Iterator() ReadBatchIterator +} + +// ReadBatchIterator is an iterator over a ReadBatch. +type ReadBatchIterator interface { + Next() bool + RangeValue() []byte + Value() []byte } diff --git a/pkg/chunk/util/util.go b/pkg/chunk/util/util.go new file mode 100644 index 00000000000..4470de21b37 --- /dev/null +++ b/pkg/chunk/util/util.go @@ -0,0 +1,88 @@ +package util + +import ( + "bytes" + "context" + + "github.com/weaveworks/cortex/pkg/chunk" +) + +// DoSingleQuery is the interface for indexes that don't support batching yet. +type DoSingleQuery func( + ctx context.Context, query chunk.IndexQuery, + callback func(chunk.ReadBatch) bool, +) error + +// DoParallelQueries translates between our interface for query batching, +// and indexes that don't yet support batching. +func DoParallelQueries( + ctx context.Context, doSingleQuery DoSingleQuery, queries []chunk.IndexQuery, + callback func(chunk.IndexQuery, chunk.ReadBatch) bool, +) error { + incomingErrors := make(chan error) + for _, query := range queries { + go func(query chunk.IndexQuery) { + incomingErrors <- doSingleQuery(ctx, query, func(r chunk.ReadBatch) bool { + return callback(query, r) + }) + }(query) + } + var lastErr error + for i := 0; i < len(queries); i++ { + err := <-incomingErrors + if err != nil { + + lastErr = err + } + } + return lastErr +} + +// Callback from an IndexQuery. +type Callback func(chunk.IndexQuery, chunk.ReadBatch) bool + +type filteringBatch struct { + query chunk.IndexQuery + chunk.ReadBatch +} + +func (f filteringBatch) Iterator() chunk.ReadBatchIterator { + return &filteringBatchIter{ + query: f.query, + ReadBatchIterator: f.ReadBatch.Iterator(), + } +} + +type filteringBatchIter struct { + query chunk.IndexQuery + chunk.ReadBatchIterator +} + +func (f *filteringBatchIter) Next() bool { + for f.ReadBatchIterator.Next() { + rangeValue, value := f.ReadBatchIterator.RangeValue(), f.ReadBatchIterator.Value() + + if len(f.query.RangeValuePrefix) != 0 && !bytes.HasPrefix(rangeValue, f.query.RangeValuePrefix) { + continue + } + if len(f.query.RangeValueStart) != 0 && bytes.Compare(f.query.RangeValueStart, rangeValue) > 0 { + continue + } + if len(f.query.ValueEqual) != 0 && !bytes.Equal(value, f.query.ValueEqual) { + continue + } + + return true + } + + return false +} + +// QueryFilter wraps a callback to ensure the results are filtered correctly; +// useful for the cache and BigTable backend, which only ever fetches the whole +// row. +func QueryFilter(callback Callback) Callback { + return func(query chunk.IndexQuery, batch chunk.ReadBatch) bool { + return callback(query, &filteringBatch{query, batch}) + } +} diff --git a/pkg/ingester/ingester.go b/pkg/ingester/ingester.go index 8d960b77cbb..e4a76157068 100644 --- a/pkg/ingester/ingester.go +++ b/pkg/ingester/ingester.go @@ -55,20 +55,20 @@ var ( queriedSamples = promauto.NewHistogram(prometheus.HistogramOpts{ Name: "cortex_ingester_queried_samples", Help: "The total number of samples returned from queries.", - // Could easily return 10m samples per query - 80*(8^9) = 10.7m. - Buckets: prometheus.ExponentialBuckets(80, 8, 9), + // Could easily return 10m samples per query - 10*(8^7) = 20.9m. + Buckets: prometheus.ExponentialBuckets(10, 8, 7), }) queriedSeries = promauto.NewHistogram(prometheus.HistogramOpts{ Name: "cortex_ingester_queried_series", Help: "The total number of series returned from queries.", - // A reasonable upper bound is around 100k - 10*(8^8) = 167k. - Buckets: prometheus.ExponentialBuckets(10, 8, 8), + // A reasonable upper bound is around 100k - 10*(8^5) = 327k. + Buckets: prometheus.ExponentialBuckets(10, 8, 5), }) queriedChunks = promauto.NewHistogram(prometheus.HistogramOpts{ Name: "cortex_ingester_queried_chunks", Help: "The total number of chunks returned from queries.", - // A small number of chunks per series - 10*(4^8) = 655k. - Buckets: prometheus.DefBuckets, + // A small number of chunks per series - 10*(8^6) = 2.6m. + Buckets: prometheus.ExponentialBuckets(10, 8, 6), }) )