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

Update head time when reading wal #334

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
29 changes: 26 additions & 3 deletions head.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,15 @@ func NewHead(r prometheus.Registerer, l log.Logger, wal WAL, chunkRange int64) (
// processWALSamples adds a partition of samples it receives to the head and passes
// them on to other workers.
// Samples before the mint timestamp are discarded.
// Returns the mint and maxt of all processed samples.
func (h *Head) processWALSamples(
mint int64,
partition, total uint64,
input <-chan []RefSample, output chan<- []RefSample,
) (unknownRefs uint64) {
) (unknownRefs uint64, smint, smaxt int64) {
defer close(output)
smint = math.MaxInt64 // Start with Maxint64 and go down from here.
smaxt = math.MinInt64 // Start with MinInt64 and go up from here.

for samples := range input {
for _, s := range samples {
Expand All @@ -221,10 +224,18 @@ func (h *Head) processWALSamples(
h.metrics.chunksCreated.Inc()
h.metrics.chunks.Inc()
}

if s.T < smint {
smint = s.T
}

if s.T > smaxt {
smaxt = s.T
}
}
output <- samples
}
return unknownRefs
return unknownRefs, smint, smaxt
}

// ReadWAL initializes the head by consuming the write ahead log.
Expand Down Expand Up @@ -253,8 +264,20 @@ func (h *Head) ReadWAL() error {
output := make(chan []RefSample, 300)

go func(i int, input <-chan []RefSample, output chan<- []RefSample) {
unknown := h.processWALSamples(mint, uint64(i), uint64(n), input, output)
unknown, smint, smaxt := h.processWALSamples(mint, uint64(i), uint64(n), input, output)
atomic.AddUint64(&unknownRefs, unknown)

if h.minTime == math.MinInt64 {
h.minTime = smint
}

if h.minTime > smint {
h.minTime = smint
}

if h.maxTime < smaxt {
h.maxTime = smaxt
}
wg.Done()
}(i, input, output)

Expand Down
8 changes: 5 additions & 3 deletions head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func TestHead_ReadWAL(t *testing.T) {
},
[]RefSample{
{Ref: 0, T: 99, V: 1},
{Ref: 10, T: 100, V: 2},
{Ref: 10, T: 1, V: 2},
{Ref: 100, T: 100, V: 3},
},
[]RefSeries{
{Ref: 50, Labels: labels.FromStrings("a", "4")},
},
[]RefSample{
{Ref: 10, T: 101, V: 5},
{Ref: 10, T: 2, V: 5},
{Ref: 50, T: 101, V: 6},
},
}
Expand All @@ -93,6 +93,8 @@ func TestHead_ReadWAL(t *testing.T) {

testutil.Ok(t, head.ReadWAL())
testutil.Equals(t, uint64(100), head.lastSeriesID)
testutil.Equals(t, int64(1), head.MinTime())
testutil.Equals(t, int64(101), head.MaxTime())

s10 := head.series.getByID(10)
s11 := head.series.getByID(11)
Expand All @@ -113,7 +115,7 @@ func TestHead_ReadWAL(t *testing.T) {
return x
}

testutil.Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
testutil.Equals(t, []sample{{1, 2}, {2, 5}}, expandChunk(s10.iterator(0)))
testutil.Equals(t, 0, len(s11.chunks))
testutil.Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
testutil.Equals(t, []sample{{100, 3}}, expandChunk(s100.iterator(0)))
Expand Down