Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 14454fc

Browse files
committed
make sure access to chunk is always serialized
1 parent bcf9b81 commit 14454fc

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

mdata/aggmetric.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ func (a *AggMetric) addAggregators(ts uint32, val float64) {
339339
}
340340
}
341341

342+
// pushToCache adds the chunk into the cache if it is hot
343+
// assumes lock held by caller
342344
func (a *AggMetric) pushToCache(c *chunk.Chunk) {
343345
if a.cachePusher == nil {
344346
return
@@ -348,14 +350,14 @@ func (a *AggMetric) pushToCache(c *chunk.Chunk) {
348350
if a.Key.Archive != 0 {
349351
intervalHint = a.Key.Archive.Span()
350352
}
351-
go a.cachePusher.AddIfHot(
352-
a.Key,
353-
0,
354-
chunk.NewBareIterGen(c.T0, intervalHint, c.Encode(a.ChunkSpan)),
355-
)
353+
354+
itergen := chunk.NewBareIterGen(c.Series.T0, intervalHint, c.Encode(a.ChunkSpan))
355+
go a.cachePusher.AddIfHot(a.Key, 0, itergen)
356356
}
357357

358358
// write a chunk to persistent storage. This should only be called while holding a.Lock()
359+
// never persist a chunk that may receive further updates!
360+
// (because the stores will read out chunk data on the unlocked chunk)
359361
func (a *AggMetric) persist(pos int) {
360362
chunk := a.Chunks[pos]
361363
pre := time.Now()

mdata/chunk/chunk.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
)
1010

1111
// Chunk is a chunk of data. not concurrency safe.
12+
// last check that the methods are being called safely by Dieter on 20/11/2018
13+
// checked: String, Push, Finish, Encode
14+
// for the most part, confirming serialized access is easy by tracking all callers.
15+
// The main exception is the ChunkWriteRequest mechanism. any CWR created is processed
16+
// "later", and chunk data (the series, etc) will be accessed via Encode() and reads of
17+
// some of the members. But it can be proven
18+
// that we only call NewChunkWriteRequest() on chunks that are no longer being modified.
1219
type Chunk struct {
1320
Series tsz.SeriesLong
1421
NumPoints uint32
@@ -48,6 +55,7 @@ func (c *Chunk) Finish() {
4855
// Encode encodes the chunk
4956
// note: chunks don't know their own span, the caller/owner manages that,
5057
// so for formats that encode it, it needs to be passed in.
58+
// the returned value contains no references to the chunk. data is copied.
5159
func (c *Chunk) Encode(span uint32) []byte {
5260
return encode(span, FormatGoTszLongWithSpan, c.Series.Bytes())
5361
}

mdata/chunk/encode.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
// encode is a helper function to encode a chunk of data into various formats
10+
// input data is copied
1011
func encode(span uint32, format Format, data []byte) []byte {
1112
switch format {
1213
case FormatStandardGoTszWithSpan, FormatGoTszLongWithSpan:

0 commit comments

Comments
 (0)