-
Notifications
You must be signed in to change notification settings - Fork 2.3k
query: add memcached autodiscovery support #4487
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af820e7
add memcached autodiscovery support
roystchiang 0e8cbbc
pass logger to provider struct
roystchiang 933b325
fix linter issues
roystchiang f3b2dfb
remove copypasted comment
roystchiang e80b1f3
address comments
roystchiang 22ed612
Update docs/components/store.md
roystchiang 6c852d0
add lock/unlock for autodiscovery provider
roystchiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| // Copyright (c) The Thanos Authors. | ||
| // Licensed under the Apache License 2.0. | ||
|
|
||
| package memcache | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/kit/log" | ||
| "github.com/go-kit/kit/log/level" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "github.com/thanos-io/thanos/pkg/errutil" | ||
| "github.com/thanos-io/thanos/pkg/extprom" | ||
| ) | ||
|
|
||
| // Provider is a stateful cache for asynchronous memcached auto-discovery resolution. It provides a way to resolve | ||
| // addresses and obtain them. | ||
| type Provider struct { | ||
roystchiang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sync.RWMutex | ||
| resolver Resolver | ||
| clusterConfigs map[string]*clusterConfig | ||
| logger log.Logger | ||
|
|
||
| configVersion *extprom.TxGaugeVec | ||
| resolvedAddresses *extprom.TxGaugeVec | ||
| resolverFailuresCount prometheus.Counter | ||
| resolverLookupsCount prometheus.Counter | ||
| } | ||
|
|
||
| func NewProvider(logger log.Logger, reg prometheus.Registerer, dialTimeout time.Duration) *Provider { | ||
| p := &Provider{ | ||
| resolver: &memcachedAutoDiscovery{dialTimeout: dialTimeout}, | ||
| clusterConfigs: map[string]*clusterConfig{}, | ||
| configVersion: extprom.NewTxGaugeVec(reg, prometheus.GaugeOpts{ | ||
| Name: "auto_discovery_config_version", | ||
| Help: "The current auto discovery config version", | ||
| }, []string{"addr"}), | ||
| resolvedAddresses: extprom.NewTxGaugeVec(reg, prometheus.GaugeOpts{ | ||
| Name: "auto_discovery_resolved_addresses", | ||
| Help: "The number of memcached nodes found via auto discovery", | ||
| }, []string{"addr"}), | ||
| resolverLookupsCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{ | ||
| Name: "auto_discovery_total", | ||
| Help: "The number of memcache auto discovery attempts", | ||
| }), | ||
| resolverFailuresCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{ | ||
| Name: "auto_discovery_failures_total", | ||
| Help: "The number of memcache auto discovery failures", | ||
| }), | ||
| logger: logger, | ||
| } | ||
| return p | ||
| } | ||
|
|
||
| // Resolve stores a list of nodes auto-discovered from the provided addresses. | ||
| func (p *Provider) Resolve(ctx context.Context, addresses []string) error { | ||
| clusterConfigs := map[string]*clusterConfig{} | ||
| errs := errutil.MultiError{} | ||
|
|
||
| for _, address := range addresses { | ||
| clusterConfig, err := p.resolver.Resolve(ctx, address) | ||
| p.resolverLookupsCount.Inc() | ||
|
|
||
| if err != nil { | ||
| level.Warn(p.logger).Log( | ||
| "msg", "failed to perform auto-discovery for memcached", | ||
| "address", address, | ||
| ) | ||
| errs.Add(err) | ||
| p.resolverFailuresCount.Inc() | ||
|
|
||
| // Use cached values. | ||
| p.RLock() | ||
| clusterConfigs[address] = p.clusterConfigs[address] | ||
| p.RUnlock() | ||
| } else { | ||
| clusterConfigs[address] = clusterConfig | ||
| } | ||
| } | ||
|
|
||
| p.Lock() | ||
| defer p.Unlock() | ||
|
|
||
| p.resolvedAddresses.ResetTx() | ||
| p.configVersion.ResetTx() | ||
| for address, config := range clusterConfigs { | ||
| p.resolvedAddresses.WithLabelValues(address).Set(float64(len(config.nodes))) | ||
| p.configVersion.WithLabelValues(address).Set(float64(config.version)) | ||
| } | ||
| p.resolvedAddresses.Submit() | ||
| p.configVersion.Submit() | ||
|
|
||
| p.clusterConfigs = clusterConfigs | ||
|
|
||
| return errs.Err() | ||
| } | ||
|
|
||
| // Addresses returns the latest addresses present in the Provider. | ||
| func (p *Provider) Addresses() []string { | ||
| p.RLock() | ||
| defer p.RUnlock() | ||
|
|
||
| var result []string | ||
| for _, config := range p.clusterConfigs { | ||
roystchiang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for _, node := range config.nodes { | ||
| result = append(result, fmt.Sprintf("%s:%d", node.dns, node.port)) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
This file contains hidden or 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,81 @@ | ||
| // Copyright (c) The Thanos Authors. | ||
| // Licensed under the Apache License 2.0. | ||
|
|
||
| package memcache | ||
|
|
||
| import ( | ||
| "context" | ||
| "sort" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/go-kit/kit/log" | ||
| "github.com/thanos-io/thanos/pkg/testutil" | ||
| ) | ||
|
|
||
| func TestProviderUpdatesAddresses(t *testing.T) { | ||
| ctx := context.TODO() | ||
| clusters := []string{"memcached-cluster-1", "memcached-cluster-2"} | ||
| provider := NewProvider(log.NewNopLogger(), nil, 5*time.Second) | ||
| resolver := mockResolver{ | ||
| configs: map[string]*clusterConfig{ | ||
| "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}}}, | ||
| "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, | ||
| }, | ||
| } | ||
| provider.resolver = &resolver | ||
|
|
||
| testutil.Ok(t, provider.Resolve(ctx, clusters)) | ||
| addresses := provider.Addresses() | ||
| testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) | ||
|
|
||
| resolver.configs = map[string]*clusterConfig{ | ||
| "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}, {dns: "dns-3", ip: "ip-3", port: 11211}}}, | ||
| "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, | ||
| } | ||
|
|
||
| testutil.Ok(t, provider.Resolve(ctx, clusters)) | ||
| addresses = provider.Addresses() | ||
| sort.Strings(addresses) | ||
| testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080", "dns-3:11211"}, addresses) | ||
| } | ||
|
|
||
| func TestProviderDoesNotUpdateAddressIfFailed(t *testing.T) { | ||
| ctx := context.TODO() | ||
| clusters := []string{"memcached-cluster-1", "memcached-cluster-2"} | ||
| provider := NewProvider(log.NewNopLogger(), nil, 5*time.Second) | ||
| resolver := mockResolver{ | ||
| configs: map[string]*clusterConfig{ | ||
| "memcached-cluster-1": {nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}}}, | ||
| "memcached-cluster-2": {nodes: []node{{dns: "dns-2", ip: "ip-2", port: 8080}}}, | ||
| }, | ||
| } | ||
| provider.resolver = &resolver | ||
|
|
||
| testutil.Ok(t, provider.Resolve(ctx, clusters)) | ||
| addresses := provider.Addresses() | ||
| sort.Strings(addresses) | ||
| testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) | ||
|
|
||
| resolver.configs = nil | ||
| resolver.err = errors.New("oops") | ||
|
|
||
| testutil.NotOk(t, provider.Resolve(ctx, clusters)) | ||
| addresses = provider.Addresses() | ||
| sort.Strings(addresses) | ||
| testutil.Equals(t, []string{"dns-1:11211", "dns-2:8080"}, addresses) | ||
| } | ||
|
|
||
| type mockResolver struct { | ||
| configs map[string]*clusterConfig | ||
| err error | ||
| } | ||
|
|
||
| func (r *mockResolver) Resolve(_ context.Context, address string) (*clusterConfig, error) { | ||
| if r.err != nil { | ||
| return nil, r.err | ||
| } | ||
| return r.configs[address], nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.