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

Commit d54e43e

Browse files
committed
Make GC cleanup writeIds
Signed-off-by: Goutham Veeramachaneni <[email protected]>
1 parent 4690f00 commit d54e43e

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

head.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ func (s *stripeSeries) gc(mint int64) (map[uint64]struct{}, int) {
10211021
for _, series := range all {
10221022
series.Lock()
10231023
rmChunks += series.truncateChunksBefore(mint)
1024+
series.cleanupExtraWriteIds()
10241025

10251026
if len(series.chunks) > 0 {
10261027
series.Unlock()
@@ -1253,10 +1254,10 @@ func (s *memSeries) append(t int64, v float64, writeId uint64) (success, chunkCr
12531254
if s.writeIdCount == len(s.writeIds) {
12541255
// Ring buffer is full, expand by doubling.
12551256
newRing := make([]uint64, s.writeIdCount*2)
1256-
copy(newRing[s.writeIdCount+s.writeIdFirst:], s.writeIds[s.writeIdFirst:])
1257-
copy(newRing, s.writeIds[:s.writeIdFirst])
1257+
idx := copy(newRing[:], s.writeIds[s.writeIdFirst:])
1258+
copy(newRing[idx:], s.writeIds[:s.writeIdFirst])
12581259
s.writeIds = newRing
1259-
s.writeIdFirst += s.writeIdCount
1260+
s.writeIdFirst = 0
12601261
}
12611262
s.writeIds[(s.writeIdFirst+s.writeIdCount)%len(s.writeIds)] = writeId
12621263
s.writeIdCount++
@@ -1286,6 +1287,36 @@ func (s *memSeries) cleanupWriteIdsBelow(bound uint64) {
12861287
}
12871288
}
12881289

1290+
func (s *memSeries) cleanupExtraWriteIds() {
1291+
totalSamples := 0
1292+
for _, c := range s.chunks {
1293+
totalSamples += c.chunk.NumSamples()
1294+
}
1295+
1296+
if s.writeIdCount <= totalSamples {
1297+
return
1298+
}
1299+
1300+
s.writeIdFirst += (s.writeIdCount - totalSamples)
1301+
s.writeIdCount = totalSamples
1302+
1303+
newBufSize := len(s.writeIds)
1304+
for totalSamples < newBufSize/2 {
1305+
newBufSize = newBufSize / 2
1306+
}
1307+
1308+
if newBufSize == len(s.writeIds) {
1309+
return
1310+
}
1311+
1312+
newRing := make([]uint64, newBufSize)
1313+
idx := copy(newRing[:], s.writeIds[s.writeIdFirst:])
1314+
copy(newRing[idx:], s.writeIds[:s.writeIdFirst])
1315+
1316+
s.writeIds = newRing
1317+
s.writeIdFirst = 0
1318+
}
1319+
12891320
// computeChunkEndTime estimates the end timestamp based the beginning of a chunk,
12901321
// its current timestamp and the upper bound up to which we insert data.
12911322
// It assumes that the time range is 1/4 full.

head_test.go

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ func TestHead_Truncate(t *testing.T) {
133133
s4, _ := h.getOrCreate(4, labels.FromStrings("a", "2", "b", "2", "c", "1"))
134134

135135
s1.chunks = []*memChunk{
136-
{minTime: 0, maxTime: 999},
137-
{minTime: 1000, maxTime: 1999},
138-
{minTime: 2000, maxTime: 2999},
136+
{minTime: 0, maxTime: 999, chunk: chunkenc.NewXORChunk()},
137+
{minTime: 1000, maxTime: 1999, chunk: chunkenc.NewXORChunk()},
138+
{minTime: 2000, maxTime: 2999, chunk: chunkenc.NewXORChunk()},
139139
}
140140
s2.chunks = []*memChunk{
141-
{minTime: 1000, maxTime: 1999},
142-
{minTime: 2000, maxTime: 2999},
143-
{minTime: 3000, maxTime: 3999},
141+
{minTime: 1000, maxTime: 1999, chunk: chunkenc.NewXORChunk()},
142+
{minTime: 2000, maxTime: 2999, chunk: chunkenc.NewXORChunk()},
143+
{minTime: 3000, maxTime: 3999, chunk: chunkenc.NewXORChunk()},
144144
}
145145
s3.chunks = []*memChunk{
146-
{minTime: 0, maxTime: 999},
147-
{minTime: 1000, maxTime: 1999},
146+
{minTime: 0, maxTime: 999, chunk: chunkenc.NewXORChunk()},
147+
{minTime: 1000, maxTime: 1999, chunk: chunkenc.NewXORChunk()},
148148
}
149149
s4.chunks = []*memChunk{}
150150

@@ -154,12 +154,12 @@ func TestHead_Truncate(t *testing.T) {
154154
testutil.Ok(t, h.Truncate(2000))
155155

156156
testutil.Equals(t, []*memChunk{
157-
{minTime: 2000, maxTime: 2999},
157+
{minTime: 2000, maxTime: 2999, chunk: chunkenc.NewXORChunk()},
158158
}, h.series.getByID(s1.ref).chunks)
159159

160160
testutil.Equals(t, []*memChunk{
161-
{minTime: 2000, maxTime: 2999},
162-
{minTime: 3000, maxTime: 3999},
161+
{minTime: 2000, maxTime: 2999, chunk: chunkenc.NewXORChunk()},
162+
{minTime: 3000, maxTime: 3999, chunk: chunkenc.NewXORChunk()},
163163
}, h.series.getByID(s2.ref).chunks)
164164

165165
testutil.Assert(t, h.series.getByID(s3.ref) == nil, "")
@@ -674,8 +674,8 @@ func TestGCChunkAccess(t *testing.T) {
674674

675675
s, _ := h.getOrCreate(1, labels.FromStrings("a", "1"))
676676
s.chunks = []*memChunk{
677-
{minTime: 0, maxTime: 999},
678-
{minTime: 1000, maxTime: 1999},
677+
{minTime: 0, maxTime: 999, chunk: chunkenc.NewXORChunk()},
678+
{minTime: 1000, maxTime: 1999, chunk: chunkenc.NewXORChunk()},
679679
}
680680

681681
idx := h.indexRange(0, 1500)
@@ -714,8 +714,8 @@ func TestGCSeriesAccess(t *testing.T) {
714714

715715
s, _ := h.getOrCreate(1, labels.FromStrings("a", "1"))
716716
s.chunks = []*memChunk{
717-
{minTime: 0, maxTime: 999},
718-
{minTime: 1000, maxTime: 1999},
717+
{minTime: 0, maxTime: 999, chunk: chunkenc.NewXORChunk()},
718+
{minTime: 1000, maxTime: 1999, chunk: chunkenc.NewXORChunk()},
719719
}
720720

721721
idx := h.indexRange(0, 2000)
@@ -816,3 +816,35 @@ func TestMemSeriesIsolation(t *testing.T) {
816816
require.Equal(t, 1002, lastValue(1002))
817817
require.Equal(t, 1002, lastValue(1003))
818818
}
819+
820+
func TestHead_Truncate_WriteIDs(t *testing.T) {
821+
h, err := NewHead(nil, nil, nil, 1000)
822+
testutil.Ok(t, err)
823+
defer h.Close()
824+
825+
h.initTime(0)
826+
827+
s1, _ := h.getOrCreate(1, labels.FromStrings("a", "1", "b", "1"))
828+
829+
chk := chunkenc.NewXORChunk()
830+
app, err := chk.Appender()
831+
testutil.Ok(t, err)
832+
833+
app.Append(1, 0)
834+
app.Append(2, 0)
835+
app.Append(3, 0)
836+
837+
s1.chunks = []*memChunk{
838+
{minTime: 0, maxTime: 999, chunk: chk},
839+
{minTime: 1000, maxTime: 1999, chunk: chk},
840+
}
841+
842+
s1.writeIds = []uint64{5, 0, 0, 0, 1, 2, 3, 4}
843+
s1.writeIdFirst = 4
844+
s1.writeIdCount = 5
845+
846+
testutil.Ok(t, h.Truncate(1000))
847+
testutil.Equals(t, []uint64{3, 4, 5, 0}, s1.writeIds)
848+
testutil.Equals(t, 0, s1.writeIdFirst)
849+
testutil.Equals(t, 3, s1.writeIdCount)
850+
}

0 commit comments

Comments
 (0)