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

Sort & validate TSI key value insertion. #8995

Merged
merged 1 commit into from
Oct 23, 2017
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [#8690](https://github.com/influxdata/influxdb/issues/8690): Implicitly decide on a lower limit for fill queries when none is present.
- [#8947](https://github.com/influxdata/influxdb/pull/8947): Add `EXPLAIN ANALYZE` command, which produces a detailed execution plan of a `SELECT` statement.
- [#8963](https://github.com/influxdata/influxdb/pull/8963): Streaming inmem2tsi conversion.
- [#8995](https://github.com/influxdata/influxdb/pull/8995): Sort & validate TSI key value insertion.

### Bugfixes

Expand Down
10 changes: 9 additions & 1 deletion tsdb/index/tsi1/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,16 @@ func (f *LogFile) writeTagsetTo(w io.Writer, name string, info *logFileCompactIn
tagSetInfo := mmInfo.tagSet[k]
assert(tagSetInfo != nil, "tag set info not found")

// Sort tag values.
values := make([]string, 0, len(tag.tagValues))
for v := range tag.tagValues {
values = append(values, v)
}
sort.Strings(values)

// Add each value.
for v, value := range tag.tagValues {
for _, v := range values {
value := tag.tagValues[v]
tagValueInfo := tagSetInfo.tagValues[v]
sort.Sort(uint32Slice(tagValueInfo.seriesIDs))

Expand Down
16 changes: 15 additions & 1 deletion tsdb/index/tsi1/tag_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ type TagBlockEncoder struct {
trailer TagBlockTrailer

// Track tag keys.
keys []tagKeyEncodeEntry
keys []tagKeyEncodeEntry
prevValue []byte
}

// NewTagBlockEncoder returns a new TagBlockEncoder.
Expand Down Expand Up @@ -527,6 +528,9 @@ func (enc *TagBlockEncoder) EncodeKey(key []byte, deleted bool) error {

enc.keys = append(enc.keys, entry)

// Clear previous value.
enc.prevValue = nil

return nil
}

Expand All @@ -539,6 +543,13 @@ func (enc *TagBlockEncoder) EncodeValue(value []byte, deleted bool, seriesIDs []
return fmt.Errorf("zero length tag value not allowed")
}

// Validate that keys are in-order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often will we call EncodeValue? I only ask as this bytes.Compare call could be an expensive way to test the encoder is working properly. Or is returning an error a "normal path" for this encoder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called once for every tag value in the index. I think the cost should be relatively small compared to the merging and writing. We can remove it if it becomes a performance bottleneck but I think it'd be good to keep it in the short-term at least to ensure we don't store unsorted data.

if cmp := bytes.Compare(enc.prevValue, value); cmp == 1 {
return fmt.Errorf("tag value out of order: prev=%s, new=%s", enc.prevValue, value)
} else if cmp == 0 {
return fmt.Errorf("tag value already encoded: %s", value)
}

// Save offset to hash map.
enc.offsets.Put(value, enc.n)

Expand Down Expand Up @@ -583,6 +594,9 @@ func (enc *TagBlockEncoder) EncodeValue(value []byte, deleted bool, seriesIDs []
return err
}

// Save previous value.
enc.prevValue = value

return nil
}

Expand Down