Skip to content
Merged
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
22 changes: 22 additions & 0 deletions chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"

"github.com/docker/docker/pkg/ioutils"
"github.com/golang/snappy"
Expand Down Expand Up @@ -49,6 +51,26 @@ func NewChunk(fp model.Fingerprint, metric model.Metric, c *prom_chunk.Desc) Chu
}
}

func parseChunkID(id string) (model.Fingerprint, model.Time, model.Time, error) {
parts := strings.Split(id, ":")
if len(parts) != 3 {
return 0, 0, 0, fmt.Errorf("invalid chunk ID")
}
fingerprint, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return 0, 0, 0, err
}
firstTime, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, 0, 0, err
}
lastTime, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return 0, 0, 0, err
}
return model.Fingerprint(fingerprint), model.Time(firstTime), model.Time(lastTime), nil
}

func (c *Chunk) reader() (io.ReadSeeker, error) {
// Encode chunk metadata into snappy-compressed buffer
var metadata bytes.Buffer
Expand Down
23 changes: 18 additions & 5 deletions chunk/chunk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,27 @@ func (c *AWSStore) Get(ctx context.Context, from, through model.Time, matchers .
}

// TODO push ctx all the way through, so we can do cancellation (eventually!)
missing, err := c.lookupChunks(userID, from, through, matchers)
chunks, err := c.lookupChunks(userID, from, through, matchers)
if err != nil {
return nil, err
}

queryChunks.Observe(float64(len(missing)))
filtered := make([]Chunk, 0, len(chunks))
for _, chunk := range chunks {
_, chunkFrom, chunkThrough, err := parseChunkID(chunk.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise the from & through terminology was present before this PR, but I think start & end would be better. Up to you whether you want to change in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like from & through - end tends to be a reserved word in many languages, and through implies inclusivity.

if err != nil {
return nil, err
}
if chunkThrough < from || through < chunkFrom {
continue
}
filtered = append(filtered, chunk)
}

queryChunks.Observe(float64(len(filtered)))

var fromCache []Chunk
var missing = filtered
if c.chunkCache != nil {
fromCache, missing, err = c.chunkCache.FetchChunkData(userID, missing)
if err != nil {
Expand All @@ -445,9 +458,9 @@ func (c *AWSStore) Get(ctx context.Context, from, through model.Time, matchers .

// TODO instead of doing this sort, propagate an index and assign chunks
// into the result based on that index.
chunks := append(fromCache, fromS3...)
sort.Sort(ByID(chunks))
return chunks, nil
allChunks := append(fromCache, fromS3...)
sort.Sort(ByID(allChunks))
return allChunks, nil
}

func extractMetricName(matchers []*metric.LabelMatcher) (model.LabelValue, []*metric.LabelMatcher, error) {
Expand Down
36 changes: 19 additions & 17 deletions chunk/chunk_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,32 @@ func TestChunkStore(t *testing.T) {

chunks, _ := chunk.New().Add(model.SamplePair{Timestamp: now, Value: 0})

chunk1 := Chunk{
ID: "chunk1",
From: now.Add(-time.Hour),
Through: now,
Metric: model.Metric{
chunk1 := NewChunk(
model.Fingerprint(1),
model.Metric{
model.MetricNameLabel: "foo",
"bar": "baz",
"toms": "code",
},
Encoding: chunk.DoubleDelta,
Data: chunks[0],
}
chunk2 := Chunk{
ID: "chunk2",
From: now.Add(-time.Hour),
Through: now,
Metric: model.Metric{
&chunk.Desc{
ChunkFirstTime: now.Add(-time.Hour),
ChunkLastTime: now,
C: chunks[0],
},
)
chunk2 := NewChunk(
model.Fingerprint(2),
model.Metric{
model.MetricNameLabel: "foo",
"bar": "beep",
"toms": "code",
},
Encoding: chunk.DoubleDelta,
Data: chunks[0],
}
&chunk.Desc{
ChunkFirstTime: now.Add(-time.Hour),
ChunkLastTime: now,
C: chunks[0],
},
)

err := store.Put(ctx, []Chunk{chunk1, chunk2})
if err != nil {
Expand All @@ -96,7 +98,7 @@ func TestChunkStore(t *testing.T) {
}

if !reflect.DeepEqual(expect, chunks) {
t.Fatalf("wrong chunks - " + diff(expect, chunks))
t.Fatalf("%s: wrong chunks - %s", name, diff(expect, chunks))
}
}

Expand Down