Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed May 10, 2017
1 parent 6961cbf commit 1c9df0a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
6 changes: 3 additions & 3 deletions filters/tags_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (tn tagFiltersByNameAsc) Less(i, j int) bool { return bytes.Compare(tn[i].n
// TagsFilterOptions provide a set of tag filter options.
type TagsFilterOptions struct {
// Name of the name tag.
NameTagName []byte
NameTagKey []byte

// Function to extract name and tags from an id.
NameAndTagsFn id.NameAndTagsFn
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewTagsFilter(
return nil, err
}
bName := []byte(name)
if bytes.Equal(opts.NameTagName, bName) {
if bytes.Equal(opts.NameTagKey, bName) {
nameFilter = valFilter
} else {
tagFilters = append(tagFilters, tagFilter{
Expand All @@ -103,7 +103,7 @@ func (f *tagsFilter) String() string {
var buf bytes.Buffer
numTagFilters := len(f.tagFilters)
if f.nameFilter != nil {
buf.WriteString(fmt.Sprintf("%s:%s", f.opts.NameTagName, f.nameFilter.String()))
buf.WriteString(fmt.Sprintf("%s:%s", f.opts.NameTagKey, f.nameFilter.String()))
if numTagFilters > 0 {
buf.WriteString(separator)
}
Expand Down
2 changes: 1 addition & 1 deletion filters/tags_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestTagsFilterString(t *testing.T) {

func testTagsFilterOptions() TagsFilterOptions {
return TagsFilterOptions{
NameTagName: []byte("name"),
NameTagKey: []byte("name"),
NameAndTagsFn: func(b []byte) ([]byte, []byte, error) { return nil, b, nil },
SortedTagIteratorFn: NewMockSortedTagIterator,
}
Expand Down
2 changes: 1 addition & 1 deletion metric/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ID interface {
}

// NameAndTagsFn returns the name and the tag pairs given an id.
type NameAndTagsFn func(id []byte) ([]byte, []byte, error)
type NameAndTagsFn func(id []byte) (name []byte, tags []byte, err error)

// NewIDFn creates a new metric ID based on the metric name and metric tag pairs.
type NewIDFn func(name []byte, tags []TagPair) []byte
Expand Down
56 changes: 28 additions & 28 deletions metric/id/m3/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const (
var (
errInvalidM3Metric = fmt.Errorf("invalid m3 metric")
m3Prefix = []byte("m3+")
nameTagName = []byte("name")
rollupTagPair = id.TagPair{
Name: []byte("m3_rollup"),
Value: []byte("true"),
Expand Down Expand Up @@ -80,25 +79,26 @@ func NewRollupID(name []byte, tagPairs []id.TagPair) []byte {
}

// TODO(xichen): pool the mids.
type mid struct {
id []byte
iterFn id.SortedTagIteratorFn
type metricID struct {
id []byte
iterPool id.SortedTagIteratorPool
}

// NewID creates a new m3 metric id.
func NewID(id []byte, iterFn id.SortedTagIteratorFn) id.ID {
return mid{id: id, iterFn: iterFn}
func NewID(id []byte, iterPool id.SortedTagIteratorPool) id.ID {
return metricID{id: id, iterPool: iterPool}
}

func (id mid) Bytes() []byte { return id.id }
func (id metricID) Bytes() []byte { return id.id }

func (id mid) TagValue(tagName []byte) ([]byte, bool) {
func (id metricID) TagValue(tagName []byte) ([]byte, bool) {
_, tagPairs, err := NameAndTags(id.Bytes())
if err != nil {
return nil, false
}

it := id.iterFn(tagPairs)
it := id.iterPool.Get()
it.Reset(tagPairs)
defer it.Close()

for it.Next() {
Expand Down Expand Up @@ -127,52 +127,52 @@ func NameAndTags(id []byte) ([]byte, []byte, error) {
}

type sortedTagIterator struct {
tagPairs []byte
idx int
tagName []byte
tagValue []byte
err error
pool id.SortedTagIteratorPool
sortedTagPairs []byte
idx int
tagName []byte
tagValue []byte
err error
pool id.SortedTagIteratorPool
}

// NewSortedTagIterator creates a new sorted tag iterator.
func NewSortedTagIterator(tagPairs []byte) id.SortedTagIterator {
return NewPooledSortedTagIterator(tagPairs, nil)
func NewSortedTagIterator(sortedTagPairs []byte) id.SortedTagIterator {
return NewPooledSortedTagIterator(sortedTagPairs, nil)
}

// NewPooledSortedTagIterator creates a new pooled sorted tag iterator.
func NewPooledSortedTagIterator(tagPairs []byte, pool id.SortedTagIteratorPool) id.SortedTagIterator {
func NewPooledSortedTagIterator(sortedTagPairs []byte, pool id.SortedTagIteratorPool) id.SortedTagIterator {
it := &sortedTagIterator{pool: pool}
it.Reset(tagPairs)
it.Reset(sortedTagPairs)
return it
}

func (it *sortedTagIterator) Reset(tagPairs []byte) {
it.tagPairs = tagPairs
func (it *sortedTagIterator) Reset(sortedTagPairs []byte) {
it.sortedTagPairs = sortedTagPairs
it.idx = 0
it.tagName = nil
it.tagValue = nil
it.err = nil
}

func (it *sortedTagIterator) Next() bool {
if it.err != nil || it.idx >= len(it.tagPairs) {
if it.err != nil || it.idx >= len(it.sortedTagPairs) {
return false
}
nameSplitterIdx := bytes.IndexByte(it.tagPairs[it.idx:], tagNameSplitter)
nameSplitterIdx := bytes.IndexByte(it.sortedTagPairs[it.idx:], tagNameSplitter)
if nameSplitterIdx == -1 {
it.err = errInvalidM3Metric
return false
}
nameSplitterIdx = it.idx + nameSplitterIdx
pairSplitterIdx := bytes.IndexByte(it.tagPairs[nameSplitterIdx+1:], tagPairSplitter)
pairSplitterIdx := bytes.IndexByte(it.sortedTagPairs[nameSplitterIdx+1:], tagPairSplitter)
if pairSplitterIdx != -1 {
pairSplitterIdx = nameSplitterIdx + 1 + pairSplitterIdx
} else {
pairSplitterIdx = len(it.tagPairs)
pairSplitterIdx = len(it.sortedTagPairs)
}
it.tagName = it.tagPairs[it.idx:nameSplitterIdx]
it.tagValue = it.tagPairs[nameSplitterIdx+1 : pairSplitterIdx]
it.tagName = it.sortedTagPairs[it.idx:nameSplitterIdx]
it.tagValue = it.sortedTagPairs[nameSplitterIdx+1 : pairSplitterIdx]
it.idx = pairSplitterIdx
return true
}
Expand All @@ -186,7 +186,7 @@ func (it *sortedTagIterator) Err() error {
}

func (it *sortedTagIterator) Close() {
it.tagPairs = nil
it.sortedTagPairs = nil
it.idx = 0
it.tagName = nil
it.tagValue = nil
Expand Down
5 changes: 2 additions & 3 deletions metric/id/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ func (tp TagPairsByNameAsc) Less(i, j int) bool {
// SortedTagIteratorFn creates a new sorted tag iterator given id tag pairs.
type SortedTagIteratorFn func(tagPairs []byte) SortedTagIterator

// SortedTagIterator iterates over a set of tag names and values
// sorted by tag names in ascending order.
// SortedTagIterator iterates over a set of tag pairs sorted by tag names.
type SortedTagIterator interface {
// Reset resets the iterator.
Reset(tagPairs []byte)
Reset(sortedTagPairs []byte)

// Next returns true if there are more tag names and values.
Next() bool
Expand Down
12 changes: 6 additions & 6 deletions rules/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/m3db/m3metrics/filters"
"github.com/m3db/m3metrics/generated/proto/schema"
mid "github.com/m3db/m3metrics/metric/id"
metricID "github.com/m3db/m3metrics/metric/id"
"github.com/m3db/m3metrics/policy"
)

Expand All @@ -52,14 +52,14 @@ type activeRuleSet struct {
rollupRules []*rollupRule
cutoverTimesAsc []int64
tagFilterOpts filters.TagsFilterOptions
newRollupIDFn mid.NewIDFn
newRollupIDFn metricID.NewIDFn
}

func newActiveRuleSet(
mappingRules []*mappingRule,
rollupRules []*rollupRule,
tagFilterOpts filters.TagsFilterOptions,
newRollupIDFn mid.NewIDFn,
newRollupIDFn metricID.NewIDFn,
) *activeRuleSet {
uniqueCutoverTimes := make(map[int64]struct{})
for _, mappingRule := range mappingRules {
Expand Down Expand Up @@ -224,7 +224,7 @@ func (as *activeRuleSet) toRollupResults(id []byte, cutoverNanos int64, targets
return nil
}

var tagPairs []mid.TagPair
var tagPairs []metricID.TagPair
rollups := make([]RollupResult, 0, len(targets))
for _, target := range targets {
tagPairs = tagPairs[:0]
Expand All @@ -240,7 +240,7 @@ func (as *activeRuleSet) toRollupResults(id []byte, cutoverNanos int64, targets
tagName, tagVal := tagIter.Current()
res := bytes.Compare(tagName, target.Tags[targetTagIdx])
if res == 0 {
tagPairs = append(tagPairs, mid.TagPair{Name: tagName, Value: tagVal})
tagPairs = append(tagPairs, metricID.TagPair{Name: tagName, Value: tagVal})
targetTagIdx++
hasMoreTags = tagIter.Next()
continue
Expand Down Expand Up @@ -316,7 +316,7 @@ type ruleSet struct {
mappingRules []*mappingRule
rollupRules []*rollupRule
tagsFilterOpts filters.TagsFilterOptions
newRollupIDFn mid.NewIDFn
newRollupIDFn metricID.NewIDFn
}

// NewRuleSet creates a new ruleset.
Expand Down
2 changes: 1 addition & 1 deletion rules/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ func testRollupRulesConfig() []*schema.RollupRule {

func testTagsFilterOptions() filters.TagsFilterOptions {
return filters.TagsFilterOptions{
NameTagName: []byte("name"),
NameTagKey: []byte("name"),
NameAndTagsFn: func(b []byte) ([]byte, []byte, error) { return nil, b, nil },
SortedTagIteratorFn: filters.NewMockSortedTagIterator,
}
Expand Down

0 comments on commit 1c9df0a

Please sign in to comment.