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

Commit

Permalink
Rename Ns to Nanos and make default policies configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed May 5, 2017
1 parent 83219f2 commit e658d02
Show file tree
Hide file tree
Showing 22 changed files with 490 additions and 481 deletions.
14 changes: 7 additions & 7 deletions metric/aggregated/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ import (
// Metric is a metric, which is essentially a named value at certain time.
type Metric struct {
metric.ID
TimeNs int64
Value float64
TimeNanos int64
Value float64
}

// String is the string representation of a metric.
func (m Metric) String() string {
return fmt.Sprintf(
"{id:%s,timestamp:%s,value:%f}",
m.ID.String(),
time.Unix(0, m.TimeNs).String(),
time.Unix(0, m.TimeNanos).String(),
m.Value,
)
}

// ChunkedMetric is a metric with a chunked ID.
type ChunkedMetric struct {
metric.ChunkedID
TimeNs int64
Value float64
TimeNanos int64
Value float64
}

// RawMetric is a metric in its raw form (e.g., encoded bytes associated with
Expand All @@ -58,8 +58,8 @@ type RawMetric interface {
// ID is the metric identifier.
ID() (metric.ID, error)

// TimeNs is the metric timestamp in nanoseconds.
TimeNs() (int64, error)
// TimeNanos is the metric timestamp in nanoseconds.
TimeNanos() (int64, error)

// Value is the metric value.
Value() (float64, error)
Expand Down
56 changes: 28 additions & 28 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,18 @@ var (
// EmptyPolicy represents an empty policy.
EmptyPolicy Policy

// EmptyStagedPolicies represents an empty staged policies.
EmptyStagedPolicies StagedPolicies

// DefaultPoliciesList represents a default policies list.
DefaultPoliciesList = PoliciesList{EmptyStagedPolicies}

// defaultPolicies are the default policies.
// TODO(xichen): possibly make this dynamically configurable in the future.
defaultPolicies = []Policy{
// DefaultPolicies are the default policies.
DefaultPolicies = []Policy{
NewPolicy(10*time.Second, xtime.Second, 2*24*time.Hour),
NewPolicy(time.Minute, xtime.Minute, 30*24*time.Hour),
}

// DefaultStagedPolicies represents a default staged policies.
DefaultStagedPolicies StagedPolicies

// DefaultPoliciesList represents a default policies list.
DefaultPoliciesList = PoliciesList{DefaultStagedPolicies}

errNilPolicySchema = errors.New("nil policy schema")
)

Expand Down Expand Up @@ -154,7 +153,7 @@ func (pr ByResolutionAsc) Less(i, j int) bool {
// StagedPolicies represent a list of policies at a specified version.
type StagedPolicies struct {
// Cutover is when the policies take effect.
CutoverNs int64
CutoverNanos int64

// Tombstoned determines whether the associated (rollup) metric has been tombstoned.
Tombstoned bool
Expand All @@ -164,29 +163,34 @@ type StagedPolicies struct {
}

// NewStagedPolicies create a new staged policies.
func NewStagedPolicies(cutoverNs int64, tombstoned bool, policies []Policy) StagedPolicies {
return StagedPolicies{CutoverNs: cutoverNs, Tombstoned: tombstoned, policies: policies}
func NewStagedPolicies(cutoverNanos int64, tombstoned bool, policies []Policy) StagedPolicies {
return StagedPolicies{CutoverNanos: cutoverNanos, Tombstoned: tombstoned, policies: policies}
}

// Reset resets the staged policies.
func (p *StagedPolicies) Reset() { *p = EmptyStagedPolicies }
func (p *StagedPolicies) Reset() { *p = DefaultStagedPolicies }

// Policies returns the policies.
func (p StagedPolicies) Policies() []Policy {
if p.hasDefaultPolicies() {
return defaultPolicies
}
return p.policies
// IsDefault returns whether this is a default staged policies.
func (p StagedPolicies) IsDefault() bool {
return p.CutoverNanos == 0 && !p.Tombstoned && p.hasDefaultPolicies()
}

// Policies returns the policies and whether the policies are the default policies.
func (p StagedPolicies) Policies() ([]Policy, bool) {
return p.policies, p.hasDefaultPolicies()
}

// SamePolicies returns whether two staged policies have the same policy list,
// assuming the policies are sorted in the same order.
func (p StagedPolicies) SamePolicies(other StagedPolicies) bool {
if p.hasDefaultPolicies() && other.hasDefaultPolicies() {
currPolicies, currIsDefault := p.Policies()
otherPolicies, otherIsDefault := other.Policies()
if currIsDefault && otherIsDefault {
return true
}
currPolicies := p.Policies()
otherPolicies := other.Policies()
if currIsDefault || otherIsDefault {
return false
}
if len(currPolicies) != len(otherPolicies) {
return false
}
Expand All @@ -201,7 +205,7 @@ func (p StagedPolicies) SamePolicies(other StagedPolicies) bool {
// String is the representation of staged policies.
func (p StagedPolicies) String() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("{cutover:%s,tombstoned:%v,policies:[", time.Unix(0, p.CutoverNs).String(), p.Tombstoned))
buf.WriteString(fmt.Sprintf("{cutover:%s,tombstoned:%v,policies:[", time.Unix(0, p.CutoverNanos).String(), p.Tombstoned))
for i := range p.policies {
buf.WriteString(p.policies[i].String())
if i < len(p.policies)-1 {
Expand All @@ -212,10 +216,6 @@ func (p StagedPolicies) String() string {
return buf.String()
}

func (p StagedPolicies) isEmpty() bool {
return p.CutoverNs == 0 && !p.Tombstoned && p.hasDefaultPolicies()
}

func (p StagedPolicies) hasDefaultPolicies() bool {
return len(p.policies) == 0
}
Expand All @@ -225,5 +225,5 @@ type PoliciesList []StagedPolicies

// IsDefault determines whether this is a default policies list.
func (l PoliciesList) IsDefault() bool {
return len(l) == 1 && l[0].isEmpty()
return len(l) == 1 && l[0].IsDefault()
}
34 changes: 17 additions & 17 deletions policy/policy_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,76 @@ import (
)

var (
testNowNs = time.Now().UnixNano()
testNowNanos = time.Now().UnixNano()
)

func BenchmarkStagedPoliciesAsStruct(b *testing.B) {
sp := NewStagedPolicies(testNowNs, false, defaultPolicies)
sp := NewStagedPolicies(testNowNanos, false, DefaultPolicies)
for n := 0; n < b.N; n++ {
validatePolicyByValue(b, sp)
}
}

func BenchmarkStagedPoliciesAsPointer(b *testing.B) {
sp := NewStagedPolicies(testNowNs, false, defaultPolicies)
sp := NewStagedPolicies(testNowNanos, false, DefaultPolicies)
for n := 0; n < b.N; n++ {
validatePolicyByPointer(b, &sp)
}
}

func BenchmarkStagedPoliciesAsInterface(b *testing.B) {
sp := &testStagedPolicies{cutoverNs: testNowNs, policies: defaultPolicies}
sp := &testStagedPolicies{cutoverNanos: testNowNanos, policies: DefaultPolicies}
for n := 0; n < b.N; n++ {
validatePolicyByInterface(b, sp)
}
}

func BenchmarkStagedPoliciesAsStructExported(b *testing.B) {
sp := testStagedPolicies{cutoverNs: testNowNs, policies: defaultPolicies}
sp := testStagedPolicies{cutoverNanos: testNowNanos, policies: DefaultPolicies}
for n := 0; n < b.N; n++ {
validatePolicyByStructExported(b, sp)
}
}

type testStagedPoliciesInt64 interface {
CutoverNs() int64
CutoverNanos() int64
}

// StagedPolicies represent a list of policies at a specified version.
type testStagedPolicies struct {
cutoverNs int64
tombstoned bool
policies []Policy
cutoverNanos int64
tombstoned bool
policies []Policy
}

func (v testStagedPolicies) ValCutoverNs() int64 {
return v.cutoverNs
func (v testStagedPolicies) ValCutoverNanos() int64 {
return v.cutoverNanos
}

func (v *testStagedPolicies) CutoverNs() int64 {
return v.cutoverNs
func (v *testStagedPolicies) CutoverNanos() int64 {
return v.cutoverNanos
}

func validatePolicyByValue(b *testing.B, sp StagedPolicies) {
if sp.CutoverNs != testNowNs {
if sp.CutoverNanos != testNowNanos {
b.FailNow()
}
}

func validatePolicyByPointer(b *testing.B, sp *StagedPolicies) {
if sp.CutoverNs != testNowNs {
if sp.CutoverNanos != testNowNanos {
b.FailNow()
}
}

func validatePolicyByInterface(b *testing.B, sp testStagedPoliciesInt64) {
if sp.CutoverNs() != testNowNs {
if sp.CutoverNanos() != testNowNanos {
b.FailNow()
}
}

func validatePolicyByStructExported(b *testing.B, sp testStagedPolicies) {
if sp.ValCutoverNs() != testNowNs {
if sp.ValCutoverNanos() != testNowNanos {
b.FailNow()
}
}
21 changes: 11 additions & 10 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ func TestPoliciesByResolutionAsc(t *testing.T) {
}

func TestStagedPoliciesHasDefaultPolicies(t *testing.T) {
sp := NewStagedPolicies(testNowNs, true, nil)
require.Equal(t, testNowNs, sp.CutoverNs)
require.True(t, sp.hasDefaultPolicies())
require.Equal(t, defaultPolicies, sp.Policies())
sp := NewStagedPolicies(testNowNanos, true, nil)
require.Equal(t, testNowNanos, sp.CutoverNanos)
_, isDefault := sp.Policies()
require.True(t, isDefault)
}

func TestStagedPoliciesHasCustomPolicies(t *testing.T) {
policies := []Policy{
NewPolicy(10*time.Second, xtime.Second, 6*time.Hour),
NewPolicy(10*time.Second, xtime.Second, 2*time.Hour),
}
sp := NewStagedPolicies(testNowNs, false, policies)
require.Equal(t, testNowNs, sp.CutoverNs)
require.False(t, sp.hasDefaultPolicies())
require.Equal(t, policies, sp.Policies())
sp := NewStagedPolicies(testNowNanos, false, policies)
require.Equal(t, testNowNanos, sp.CutoverNanos)
actual, isDefault := sp.Policies()
require.False(t, isDefault)
require.Equal(t, policies, actual)
}

func TestStagedPoliciesSamePoliciesDefaultPolicies(t *testing.T) {
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestStagedPoliciesIsEmpty(t *testing.T) {
},
}
for _, input := range inputs {
require.Equal(t, input.expected, input.sp.isEmpty())
require.Equal(t, input.expected, input.sp.IsDefault())
}
}

Expand All @@ -187,7 +188,7 @@ func TestPoliciesListIsDefault(t *testing.T) {
expected: false,
},
{
pl: []StagedPolicies{EmptyStagedPolicies, EmptyStagedPolicies},
pl: []StagedPolicies{DefaultStagedPolicies, DefaultStagedPolicies},
expected: false,
},
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/msgpack/aggregated_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (enc *aggregatedEncoder) encodeMetricAsRaw(m aggregated.Metric) []byte {
enc.buf.resetData()
enc.encodeMetricProlog()
enc.buf.encodeID(m.ID)
enc.buf.encodeVarint(m.TimeNs)
enc.buf.encodeVarint(m.TimeNanos)
enc.buf.encodeFloat64(m.Value)
return enc.buf.encoder().Bytes()
}
Expand All @@ -112,7 +112,7 @@ func (enc *aggregatedEncoder) encodeChunkedMetricAsRaw(m aggregated.ChunkedMetri
enc.buf.resetData()
enc.encodeMetricProlog()
enc.buf.encodeChunkedID(m.ChunkedID)
enc.buf.encodeVarint(m.TimeNs)
enc.buf.encodeVarint(m.TimeNanos)
enc.buf.encodeFloat64(m.Value)
return enc.buf.encoder().Bytes()
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/msgpack/aggregated_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAggregatedEncodeMetric(t *testing.T) {
int64(metricVersion),
int(numFieldsForType(metricType)),
[]byte(testMetric.ID),
testMetric.TimeNs,
testMetric.TimeNanos,
testMetric.Value,
}
require.Equal(t, expected, *result)
Expand Down
22 changes: 11 additions & 11 deletions protocol/msgpack/aggregated_roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ import (

var (
testMetric = aggregated.Metric{
ID: metric.ID("foo"),
TimeNs: time.Now().UnixNano(),
Value: 123.45,
ID: metric.ID("foo"),
TimeNanos: time.Now().UnixNano(),
Value: 123.45,
}
testChunkedMetric = aggregated.ChunkedMetric{
ChunkedID: metric.ChunkedID{
Prefix: []byte("foo."),
Data: []byte("bar"),
Suffix: []byte(".baz"),
},
TimeNs: time.Now().UnixNano(),
Value: 123.45,
TimeNanos: time.Now().UnixNano(),
Value: 123.45,
}
testMetric2 = aggregated.Metric{
ID: metric.ID("bar"),
TimeNs: time.Now().UnixNano(),
Value: 678.90,
ID: metric.ID("bar"),
TimeNanos: time.Now().UnixNano(),
Value: 678.90,
}
testPolicy = policy.NewPolicy(time.Second, xtime.Second, time.Hour)
)
Expand Down Expand Up @@ -142,9 +142,9 @@ func validateAggregatedRoundtripWithEncoderAndIterator(
id = append(id, inputMetric.ChunkedID.Suffix...)
expected = append(expected, metricWithPolicy{
metric: aggregated.Metric{
ID: id,
TimeNs: inputMetric.TimeNs,
Value: inputMetric.Value,
ID: id,
TimeNanos: inputMetric.TimeNanos,
Value: inputMetric.Value,
},
policy: input.policy,
})
Expand Down
8 changes: 4 additions & 4 deletions protocol/msgpack/raw_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func (m *rawMetric) ID() (metric.ID, error) {
return m.metric.ID, nil
}

func (m *rawMetric) TimeNs() (int64, error) {
func (m *rawMetric) TimeNanos() (int64, error) {
m.decodeID()
m.decodeTime()
if err := m.it.err(); err != nil {
return 0, err
}
return m.metric.TimeNs, nil
return m.metric.TimeNanos, nil
}

func (m *rawMetric) Value() (float64, error) {
Expand Down Expand Up @@ -151,11 +151,11 @@ func (m *rawMetric) decodeTime() {
if m.it.err() != nil || m.timeDecoded {
return
}
timeNs := m.it.decodeVarint()
timeNanos := m.it.decodeVarint()
if m.it.err() != nil {
return
}
m.metric.TimeNs = timeNs
m.metric.TimeNanos = timeNanos
m.timeDecoded = true
}

Expand Down
Loading

0 comments on commit e658d02

Please sign in to comment.