Skip to content

Commit

Permalink
rounds nanoseconds boundaries to milliseconds (grafana#771)
Browse files Browse the repository at this point in the history
* rounds nanoseconds boundaries to milliseconds

* convert also store query
  • Loading branch information
cyriltovena authored Jul 18, 2019
1 parent 688056b commit 4bf7766
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/ingester/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/util"
"github.com/grafana/loki/pkg/chunkenc"
loki_util "github.com/grafana/loki/pkg/util"
)

var (
Expand Down Expand Up @@ -261,12 +262,12 @@ func (i *Ingester) flushChunks(ctx context.Context, fp model.Fingerprint, labelP

wireChunks := make([]chunk.Chunk, 0, len(cs))
for _, c := range cs {
firstTime, lastTime := c.chunk.Bounds()
firstTime, lastTime := loki_util.RoundToMilliseconds(c.chunk.Bounds())
c := chunk.NewChunk(
userID, fp, metric,
chunkenc.NewFacade(c.chunk),
model.TimeFromUnixNano(firstTime.UnixNano()),
model.TimeFromUnixNano(lastTime.UnixNano()),
firstTime,
lastTime,
)

start := time.Now()
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/grafana/loki/pkg/iter"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/logql"
"github.com/grafana/loki/pkg/util"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (s *store) LazyQuery(ctx context.Context, req *logproto.QueryRequest) (iter
}

matchers = append(matchers, nameLabelMatcher)
from, through := model.TimeFromUnixNano(req.Start.UnixNano()), model.TimeFromUnixNano(req.End.UnixNano())
from, through := util.RoundToMilliseconds(req.Start, req.End)
chks, fetchers, err := s.GetChunkRefs(ctx, from, through, matchers...)
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/conv.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package util

import (
"math"
"sort"
"strings"
"time"

"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/grafana/loki/pkg/logql"
Expand Down Expand Up @@ -41,3 +43,10 @@ func ModelLabelSetToMap(m model.LabelSet) map[string]string {
}
return result
}

// RoundToMilliseconds returns milliseconds precision time from nanoseconds.
// from will be rounded down to the nearest milliseconds while through is rounded up.
func RoundToMilliseconds(from, through time.Time) (model.Time, model.Time) {
return model.Time(int64(math.Floor(float64(from.UnixNano()) / float64(time.Millisecond)))),
model.Time(int64(math.Ceil(float64(through.UnixNano()) / float64(time.Millisecond))))
}
59 changes: 59 additions & 0 deletions pkg/util/conv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package util

import (
"reflect"
"testing"
"time"

"github.com/prometheus/common/model"
)

func TestRoundToMilliseconds(t *testing.T) {
tests := []struct {
name string
from time.Time
through time.Time
wantFrom model.Time
wantThrough model.Time
}{
{
"0",
time.Unix(0, 0),
time.Unix(0, 1),
model.Time(0),
model.Time(1),
},
{
"equal",
time.Unix(0, time.Millisecond.Nanoseconds()),
time.Unix(0, time.Millisecond.Nanoseconds()),
model.Time(1),
model.Time(1),
},
{
"exact",
time.Unix(0, time.Millisecond.Nanoseconds()),
time.Unix(0, 2*time.Millisecond.Nanoseconds()),
model.Time(1),
model.Time(2),
},
{
"rounding",
time.Unix(0, time.Millisecond.Nanoseconds()+10),
time.Unix(0, 2*time.Millisecond.Nanoseconds()+10),
model.Time(1),
model.Time(3),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
from, through := RoundToMilliseconds(tt.from, tt.through)
if !reflect.DeepEqual(from, tt.wantFrom) {
t.Errorf("RoundToMilliseconds() from = %v, want %v", from, tt.wantFrom)
}
if !reflect.DeepEqual(through, tt.wantThrough) {
t.Errorf("RoundToMilliseconds() through = %v, want %v", through, tt.wantThrough)
}
})
}
}

0 comments on commit 4bf7766

Please sign in to comment.