@@ -402,14 +402,17 @@ func (h *rangeHead) Tombstones() (TombstoneReader, error) {
402402type initAppender struct {
403403 app Appender
404404 head * Head
405+
406+ writeId uint64
407+ cleanupWriteIdsBelow uint64
405408}
406409
407410func (a * initAppender ) Add (lset labels.Labels , t int64 , v float64 ) (uint64 , error ) {
408411 if a .app != nil {
409412 return a .app .Add (lset , t , v )
410413 }
411414 a .head .initTime (t )
412- a .app = a .head .appender (0 , 0 )
415+ a .app = a .head .appender (a . writeId , a . cleanupWriteIdsBelow )
413416
414417 return a .app .Add (lset , t , v )
415418}
@@ -442,7 +445,7 @@ func (h *Head) Appender(writeId, cleanupWriteIdsBelow uint64) Appender {
442445 // The head cache might not have a starting point yet. The init appender
443446 // picks up the first appended timestamp as the base.
444447 if h .MinTime () == math .MinInt64 {
445- return & initAppender {head : h }
448+ return & initAppender {head : h , writeId : writeId , cleanupWriteIdsBelow : cleanupWriteIdsBelow }
446449 }
447450 return h .appender (writeId , cleanupWriteIdsBelow )
448451}
@@ -1296,13 +1299,16 @@ func computeChunkEndTime(start, cur, max int64) int64 {
12961299
12971300func (s * memSeries ) iterator (id int , isolation * IsolationState ) chunkenc.Iterator {
12981301 c := s .chunk (id )
1302+
12991303 // TODO(fabxc): Work around! A querier may have retrieved a pointer to a series' chunk,
13001304 // which got then garbage collected before it got accessed.
13011305 // We must ensure to not garbage collect as long as any readers still hold a reference.
13021306 if c == nil {
13031307 return chunkenc .NewNopIterator ()
13041308 }
13051309
1310+ ix := id - s .firstChunkID
1311+
13061312 numSamples := c .chunk .NumSamples ()
13071313 stopAfter := numSamples
13081314
@@ -1311,7 +1317,7 @@ func (s *memSeries) iterator(id int, isolation *IsolationState) chunkenc.Iterato
13111317 previousSamples := 0 // Samples before this chunk.
13121318 for j , d := range s .chunks {
13131319 totalSamples += d .chunk .NumSamples ()
1314- if j < id {
1320+ if j < ix {
13151321 previousSamples += d .chunk .NumSamples ()
13161322 }
13171323 }
0 commit comments