Skip to content

Commit

Permalink
Merge pull request #8975 from benbjohnson/tsi-copy-returned-bytes
Browse files Browse the repository at this point in the history
Copy returned bytes from TSI meta functions.
  • Loading branch information
benbjohnson authored Oct 18, 2017
2 parents 9b55ee2 + 264971f commit 62093d2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
- [#8908](https://github.com/influxdata/influxdb/issues/8908): Fix missing man pages in new packaging output
- [#8909](https://github.com/influxdata/influxdb/issues/8909): Fix use of `INFLUXD_OPTS` in service file
- [#8952](https://github.com/influxdata/influxdb/issues/8952): Fix WAL panic: runtime error: makeslice: cap out of range
- [#8975](https://github.com/influxdata/influxdb/pull/8975): Copy returned bytes from TSI meta functions.

## v1.3.4 [unreleased]

Expand Down
19 changes: 19 additions & 0 deletions pkg/bytesutil/bytesutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ func Intersect(a, b [][]byte) [][]byte {
return other
}

// Clone returns a copy of b.
func Clone(b []byte) []byte {
if b == nil {
return nil
}
buf := make([]byte, len(b))
copy(buf, b)
return buf
}

// CloneSlice returns a copy of a slice of byte slices.
func CloneSlice(a [][]byte) [][]byte {
other := make([][]byte, len(a))
for i := range a {
other[i] = Clone(a[i])
}
return other
}

type byteSlices [][]byte

func (a byteSlices) Len() int { return len(a) }
Expand Down
16 changes: 13 additions & 3 deletions tsdb/index/tsi1/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/pkg/bytesutil"
"github.com/influxdata/influxdb/pkg/estimator"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/tsdb"
Expand Down Expand Up @@ -402,7 +403,11 @@ func (i *Index) MeasurementExists(name []byte) (bool, error) {
func (i *Index) MeasurementNamesByExpr(expr influxql.Expr) ([][]byte, error) {
fs := i.RetainFileSet()
defer fs.Release()
return fs.MeasurementNamesByExpr(expr)

names, err := fs.MeasurementNamesByExpr(expr)

// Clone byte slices since they will be used after the fileset is released.
return bytesutil.CloneSlice(names), err
}

func (i *Index) MeasurementNamesByRegex(re *regexp.Regexp) ([][]byte, error) {
Expand All @@ -413,7 +418,8 @@ func (i *Index) MeasurementNamesByRegex(re *regexp.Regexp) ([][]byte, error) {
var a [][]byte
for e := itr.Next(); e != nil; e = itr.Next() {
if re.Match(e.Name()) {
a = append(a, e.Name())
// Clone bytes since they will be used after the fileset is released.
a = append(a, bytesutil.Clone(e.Name()))
}
}
return a, nil
Expand Down Expand Up @@ -726,7 +732,11 @@ func (i *Index) TagKeyCardinality(name, key []byte) int {
func (i *Index) MeasurementSeriesKeysByExpr(name []byte, expr influxql.Expr) ([][]byte, error) {
fs := i.RetainFileSet()
defer fs.Release()
return fs.MeasurementSeriesKeysByExpr(name, expr, i.fieldset)

keys, err := fs.MeasurementSeriesKeysByExpr(name, expr, i.fieldset)

// Clone byte slices since they will be used after the fileset is released.
return bytesutil.CloneSlice(keys), err
}

// TagSets returns an ordered list of tag sets for a measurement by dimension
Expand Down
3 changes: 2 additions & 1 deletion tsdb/index/tsi1/index_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"time"

"github.com/influxdata/influxdb/pkg/bytesutil"
"github.com/influxdata/influxdb/pkg/estimator/hll"
"github.com/influxdata/influxdb/pkg/mmap"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func (p *IndexFiles) MeasurementNames() [][]byte {
itr := p.MeasurementIterator()
var names [][]byte
for e := itr.Next(); e != nil; e = itr.Next() {
names = append(names, copyBytes(e.Name()))
names = append(names, bytesutil.Clone(e.Name()))
}
sort.Sort(byteSlices(names))
return names
Expand Down
10 changes: 0 additions & 10 deletions tsdb/index/tsi1/tsi1.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,6 @@ func (a byteSlices) Len() int { return len(a) }
func (a byteSlices) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byteSlices) Less(i, j int) bool { return bytes.Compare(a[i], a[j]) == -1 }

// copyBytes returns a copy of b.
func copyBytes(b []byte) []byte {
if b == nil {
return nil
}
buf := make([]byte, len(b))
copy(buf, b)
return buf
}

// assert will panic with a given formatted message if the given condition is false.
func assert(condition bool, msg string, v ...interface{}) {
if !condition {
Expand Down

0 comments on commit 62093d2

Please sign in to comment.