Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimise write window check #25558

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
40 changes: 34 additions & 6 deletions coordinator/points_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,37 @@ func (s *ShardMapping) MapPoint(shardInfo *meta.ShardInfo, p models.Point) {
s.Shards[shardInfo.ID] = shardInfo
}

func withinWriteWindow(rp *meta.RetentionPolicyInfo, p models.Point) bool {
if (rp != nil) &&
(((rp.FutureWriteLimit > 0) && p.Time().After(time.Now().Add(rp.FutureWriteLimit))) ||
((rp.PastWriteLimit > 0) && p.Time().Before(time.Now().Add(-rp.PastWriteLimit)))) {
type WriteWindow struct {
before time.Time
checkBefore bool
after time.Time
checkAfter bool
}

func NewWriteWindow(rp *meta.RetentionPolicyInfo) *WriteWindow {
w := &WriteWindow{checkAfter: false, checkBefore: false}

if rp == nil {
// Used in tests
return w
}
now := time.Now()
if rp.FutureWriteLimit > 0 {
w.after = now.Add(rp.FutureWriteLimit)
w.checkAfter = true
}
if rp.PastWriteLimit > 0 {
w.before = now.Add(-rp.PastWriteLimit)
w.checkBefore = true
}
return w
}

func (w *WriteWindow) WithinWindow(t time.Time) bool {
if w.checkBefore && t.Before(w.before) {
return false
}
if w.checkAfter && t.After(w.after) {
return false
}
return true
Expand Down Expand Up @@ -197,11 +224,12 @@ func (w *PointsWriter) MapShards(wp *WritePointsRequest) (*ShardMapping, error)
min = time.Now().Add(-rp.Duration)
}

ww := NewWriteWindow(rp)
for _, p := range wp.Points {
// Either the point is outside the scope of the RP, we already have
// a suitable shard group for the point, or it is outside the write window
// for the RP, and we don't want to unnecessarily create a shard for it
if p.Time().Before(min) || list.Covers(p.Time()) || !withinWriteWindow(rp, p) {
if p.Time().Before(min) || list.Covers(p.Time()) || !ww.WithinWindow(p.Time()) {
continue
}

Expand All @@ -221,7 +249,7 @@ func (w *PointsWriter) MapShards(wp *WritePointsRequest) (*ShardMapping, error)
mapping := NewShardMapping(len(wp.Points))
for _, p := range wp.Points {
sg := list.ShardGroupAt(p.Time())
if sg == nil || !withinWriteWindow(rp, p) {
if sg == nil || !ww.WithinWindow(p.Time()) {
// We didn't create a shard group because the point was outside the
// scope of the RP, or the point is outside the write window for the RP.
mapping.Dropped = append(mapping.Dropped, p)
Expand Down