Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions pkg/chunk/aws/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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"
)

Expand Down Expand Up @@ -288,7 +289,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()

Expand Down Expand Up @@ -358,7 +363,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()))
Expand Down Expand Up @@ -388,7 +393,10 @@ func (a storageClient) queryPage(ctx context.Context, input *dynamodb.QueryInput
}

queryOutput := page.Data().(*dynamodb.QueryOutput)
return dynamoDBReadResponse(queryOutput.Items), nil
return &dynamoDBReadResponse{
i: -1,
items: queryOutput.Items,
}, nil
}
return nil, fmt.Errorf("QueryPage error: %s for table %v, last error %v", backoff.Err(), *input.TableName, err)
}
Expand Down Expand Up @@ -772,18 +780,22 @@ 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 {
i int
items []map[string]*dynamodb.AttributeValue
}

func (b dynamoDBReadResponse) Len() int {
return len(b)
func (b *dynamoDBReadResponse) Next() bool {
b.i++
return b.i < len(b.items)
}

func (b dynamoDBReadResponse) RangeValue(i int) []byte {
return b[i][rangeKey].B
func (b *dynamoDBReadResponse) RangeValue() []byte {
return b.items[b.i][rangeKey].B
}

func (b dynamoDBReadResponse) Value(i int) []byte {
chunkValue, ok := b[i][valueKey]
func (b *dynamoDBReadResponse) Value() []byte {
chunkValue, ok := b.items[b.i][valueKey]
if !ok {
return nil
}
Expand Down
12 changes: 1 addition & 11 deletions pkg/chunk/cache/fifo_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -103,9 +101,6 @@ func NewFifoCache(name string, size int, validity time.Duration) *FifoCache {

// 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
Expand Down Expand Up @@ -173,9 +168,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
Expand All @@ -188,17 +180,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
}
28 changes: 16 additions & 12 deletions pkg/chunk/cassandra/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"

"github.com/weaveworks/cortex/pkg/chunk"
"github.com/weaveworks/cortex/pkg/chunk/util"
)

const (
Expand Down Expand Up @@ -172,7 +173,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 {
Expand Down Expand Up @@ -205,7 +210,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)
}
Expand All @@ -218,27 +223,26 @@ 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 (b *readBatch) Next() bool {
if b.consumed {
return false
}
b.consumed = true
return true
}

func (b readBatch) RangeValue(index int) []byte {
if index != 0 {
panic("index != 0")
}
func (b *readBatch) RangeValue() []byte {
return b.rangeValue
}

func (b readBatch) Value(index int) []byte {
if index != 0 {
panic("index != 0")
}
func (b *readBatch) Value() []byte {
return b.value
}

Expand Down
46 changes: 6 additions & 40 deletions pkg/chunk/chunk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,53 +412,19 @@ 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 {
for resp.Next() {
entries = append(entries, IndexEntry{
TableName: query.TableName,
HashValue: query.HashValue,
RangeValue: resp.RangeValue(i),
Value: resp.Value(i),
RangeValue: resp.RangeValue(),
Value: resp.Value(),
})
}
return true
}); 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) {
Expand Down
Loading