This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First round of fixes to address comments
- Loading branch information
Jerome Froelich
committed
Apr 19, 2017
1 parent
5d4a46c
commit 4792c45
Showing
23 changed files
with
587 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ message CompressedPolicy { | |
|
||
message ActivePolicies { | ||
repeated CompressedPolicy compressedPolicies = 1; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package policy | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/m3db/m3cluster/kv" | ||
"github.com/m3db/m3metrics/generated/proto/schema" | ||
"github.com/m3db/m3x/instrument" | ||
"github.com/m3db/m3x/log" | ||
) | ||
|
||
var ( | ||
errNilPolicies = errors.New("policies cannot be nil") | ||
errNilCompressedPolicy = errors.New("compressed policy cannot be nil") | ||
errNilInternalPolicy = errors.New("internal policy for compressed policy cannot be nil") | ||
) | ||
|
||
// CompressionMap is a mapping from Policy to integer. | ||
type CompressionMap map[Policy]int64 | ||
|
||
// NewCompressionMap constructs a new CompressionMap from a slice of | ||
// compressed policies. | ||
func NewCompressionMap(policies []*schema.CompressedPolicy) (CompressionMap, error) { | ||
if policies == nil { | ||
return nil, errNilPolicies | ||
} | ||
|
||
compressionMap := make(CompressionMap, len(policies)) | ||
for _, compressedPolicy := range policies { | ||
if compressedPolicy == nil { | ||
return nil, errNilCompressedPolicy | ||
} | ||
|
||
internalPolicy := compressedPolicy.GetPolicy() | ||
if internalPolicy == nil { | ||
return nil, errNilInternalPolicy | ||
} | ||
|
||
policy, err := NewPolicyFromSchema(internalPolicy) | ||
if err != nil { | ||
return nil, err | ||
} | ||
compressionMap[policy] = compressedPolicy.Id | ||
} | ||
|
||
return compressionMap, nil | ||
} | ||
|
||
// Compressor maintains a mapping of policies to integers. It is useful when | ||
// encoding policies so that an integer representation of a policy can be | ||
// sent instead of the entire Policy. | ||
type Compressor interface { | ||
ID(p Policy) (int64, bool) | ||
} | ||
|
||
type noopCompressor struct{} | ||
|
||
// NewNoopCompressor returns a new Compressor which never matches a policy. | ||
func NewNoopCompressor() Compressor { | ||
return noopCompressor{} | ||
} | ||
|
||
func (m noopCompressor) ID(p Policy) (int64, bool) { | ||
return 0, false | ||
} | ||
|
||
type staticCompressor struct { | ||
policies CompressionMap | ||
} | ||
|
||
// NewStaticCompressor returns a new static Compressor. | ||
func NewStaticCompressor(policies CompressionMap) Compressor { | ||
return staticCompressor{policies: policies} | ||
} | ||
|
||
func (m staticCompressor) ID(p Policy) (int64, bool) { | ||
i, ok := m.policies[p] | ||
return i, ok | ||
} | ||
|
||
type dynamicCompressor struct { | ||
sync.RWMutex | ||
|
||
policies CompressionMap | ||
log xlog.Logger | ||
} | ||
|
||
// NewDynamicCompressor returns a new dynamic Compressor. | ||
func NewDynamicCompressor(w kv.ValueWatch, opts instrument.Options) Compressor { | ||
m := &dynamicCompressor{ | ||
policies: make(CompressionMap), | ||
log: opts.Logger(), | ||
} | ||
m.update(w.Get()) | ||
|
||
go m.run(w) | ||
|
||
return m | ||
} | ||
|
||
func (m *dynamicCompressor) ID(p Policy) (int64, bool) { | ||
m.RLock() | ||
i, ok := m.policies[p] | ||
m.RUnlock() | ||
return i, ok | ||
} | ||
|
||
func (m *dynamicCompressor) run(w kv.ValueWatch) { | ||
for { | ||
_, ok := <-w.C() | ||
if ok { | ||
m.log.Debug("received an update to compressor policies") | ||
m.update(w.Get()) | ||
} else { | ||
m.log.Debug("watch for compressor policies was closed") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (m *dynamicCompressor) update(v kv.Value) { | ||
if v == nil { | ||
m.log.Warn("received a nil Value for compressor update") | ||
return | ||
} | ||
|
||
activePolicies := schema.ActivePolicies{} | ||
if err := v.Unmarshal(&activePolicies); err != nil { | ||
m.log.WithFields( | ||
xlog.NewLogField("version", v.Version()), | ||
xlog.NewLogErrField(err), | ||
).Error("failed to unmarshal compressor update") | ||
return | ||
} | ||
|
||
compressedPolicies := activePolicies.GetCompressedPolicies() | ||
if compressedPolicies == nil { | ||
m.log.Warn("encoded policies in update to compressor is nil") | ||
return | ||
} | ||
|
||
newPolicies, err := NewCompressionMap(compressedPolicies) | ||
if err != nil { | ||
m.log.WithFields( | ||
xlog.NewLogErrField(err), | ||
).Error("failed to construct new compressed policies map") | ||
} | ||
|
||
m.Lock() | ||
m.policies = newPolicies | ||
m.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.