-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
*: Allow exposing multiple label-sets #1284
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58dee8a
*: Allow exposing multiple label-sets
brancz 45e2334
pkg/store: Add more multi-label-sets matching tests
brancz a652743
Address comments
brancz ef37293
If LabelSet is empty, and Labels is not, use Labels as LabelSet
brancz fe1c7f1
pkg/store/proxy.go: Correctly handle no label-sets discovered
brancz b4b0082
pkg/query: Encapsulate LabelSet defaulting in spec.Metadata
brancz 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 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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -32,14 +33,14 @@ type StoreSpec interface { | |
// If metadata call fails we assume that store is no longer accessible and we should not use it. | ||
// NOTE: It is implementation responsibility to retry until context timeout, but a caller responsibility to manage | ||
// given store connection. | ||
Metadata(ctx context.Context, client storepb.StoreClient) (labels []storepb.Label, mint int64, maxt int64, err error) | ||
Metadata(ctx context.Context, client storepb.StoreClient) (labelSets []storepb.LabelSet, labels []storepb.Label, mint int64, maxt int64, err error) | ||
} | ||
|
||
type StoreStatus struct { | ||
Name string | ||
LastCheck time.Time | ||
LastError error | ||
Labels []storepb.Label | ||
LabelSets []storepb.LabelSet | ||
StoreType component.StoreAPI | ||
MinTime int64 | ||
MaxTime int64 | ||
|
@@ -62,12 +63,12 @@ func (s *grpcStoreSpec) Addr() string { | |
|
||
// Metadata method for gRPC store API tries to reach host Info method until context timeout. If we are unable to get metadata after | ||
// that time, we assume that the host is unhealthy and return error. | ||
func (s *grpcStoreSpec) Metadata(ctx context.Context, client storepb.StoreClient) (labels []storepb.Label, mint int64, maxt int64, err error) { | ||
func (s *grpcStoreSpec) Metadata(ctx context.Context, client storepb.StoreClient) (labelSets []storepb.LabelSet, labels []storepb.Label, mint int64, maxt int64, err error) { | ||
resp, err := client.Info(ctx, &storepb.InfoRequest{}, grpc.FailFast(false)) | ||
if err != nil { | ||
return nil, 0, 0, errors.Wrapf(err, "fetching store info from %s", s.addr) | ||
return nil, nil, 0, 0, errors.Wrapf(err, "fetching store info from %s", s.addr) | ||
} | ||
return resp.Labels, resp.MinTime, resp.MaxTime, nil | ||
return resp.LabelSets, resp.Labels, resp.MinTime, resp.MaxTime, nil | ||
} | ||
|
||
// StoreSet maintains a set of active stores. It is backed up by Store Specifications that are dynamically fetched on | ||
|
@@ -81,13 +82,13 @@ type StoreSet struct { | |
dialOpts []grpc.DialOption | ||
gRPCInfoCallTimeout time.Duration | ||
|
||
mtx sync.RWMutex | ||
storesStatusesMtx sync.RWMutex | ||
stores map[string]*storeRef | ||
storeNodeConnections prometheus.Gauge | ||
externalLabelStores map[string]int | ||
storeStatuses map[string]*StoreStatus | ||
unhealthyStoreTimeout time.Duration | ||
mtx sync.RWMutex | ||
storesStatusesMtx sync.RWMutex | ||
stores map[string]*storeRef | ||
storeNodeConnections prometheus.Gauge | ||
externalLabelOccurrencesInStores map[string]int | ||
storeStatuses map[string]*StoreStatus | ||
unhealthyStoreTimeout time.Duration | ||
} | ||
|
||
type storeSetNodeCollector struct { | ||
|
@@ -135,12 +136,12 @@ func NewStoreSet( | |
} | ||
|
||
ss := &StoreSet{ | ||
logger: log.With(logger, "component", "storeset"), | ||
storeSpecs: storeSpecs, | ||
dialOpts: dialOpts, | ||
storeNodeConnections: storeNodeConnections, | ||
gRPCInfoCallTimeout: 10 * time.Second, | ||
externalLabelStores: map[string]int{}, | ||
logger: log.With(logger, "component", "storeset"), | ||
storeSpecs: storeSpecs, | ||
dialOpts: dialOpts, | ||
storeNodeConnections: storeNodeConnections, | ||
gRPCInfoCallTimeout: 10 * time.Second, | ||
externalLabelOccurrencesInStores: map[string]int{}, | ||
stores: make(map[string]*storeRef), | ||
storeStatuses: make(map[string]*StoreStatus), | ||
unhealthyStoreTimeout: unhealthyStoreTimeout, | ||
|
@@ -162,27 +163,31 @@ type storeRef struct { | |
addr string | ||
|
||
// Meta (can change during runtime). | ||
labels []storepb.Label | ||
labelSets []storepb.LabelSet | ||
storeType component.StoreAPI | ||
minTime int64 | ||
maxTime int64 | ||
|
||
logger log.Logger | ||
} | ||
|
||
func (s *storeRef) Update(labels []storepb.Label, minTime int64, maxTime int64) { | ||
s.mtx.RLock() | ||
defer s.mtx.RUnlock() | ||
func (s *storeRef) Update(labelSets []storepb.LabelSet, labels []storepb.Label, minTime int64, maxTime int64) { | ||
if len(labelSets) == 0 && len(labels) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would hide it behind Metadata interface. |
||
labelSets = []storepb.LabelSet{{Labels: labels}} | ||
} | ||
|
||
s.labels = labels | ||
s.mtx.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
defer s.mtx.Unlock() | ||
|
||
s.labelSets = labelSets | ||
s.minTime = minTime | ||
s.maxTime = maxTime | ||
} | ||
|
||
func (s *storeRef) Labels() []storepb.Label { | ||
func (s *storeRef) LabelSets() []storepb.LabelSet { | ||
s.mtx.RLock() | ||
defer s.mtx.RUnlock() | ||
return s.labels | ||
return s.labelSets | ||
} | ||
|
||
func (s *storeRef) TimeRange() (int64, int64) { | ||
|
@@ -194,7 +199,7 @@ func (s *storeRef) TimeRange() (int64, int64) { | |
|
||
func (s *storeRef) String() string { | ||
mint, maxt := s.TimeRange() | ||
return fmt.Sprintf("Addr: %s Labels: %v Mint: %d Maxt: %d", s.addr, s.Labels(), mint, maxt) | ||
return fmt.Sprintf("Addr: %s LabelSets: %v Mint: %d Maxt: %d", s.addr, storepb.LabelSetsToString(s.LabelSets()), mint, maxt) | ||
} | ||
|
||
func (s *storeRef) Addr() string { | ||
|
@@ -211,10 +216,11 @@ func (s *StoreSet) Update(ctx context.Context) { | |
healthyStores := s.getHealthyStores(ctx) | ||
|
||
// Record the number of occurrences of external label combinations for current store slice. | ||
externalLabelStores := map[string]int{} | ||
externalLabelOccurrencesInStores := map[string]int{} | ||
for _, st := range healthyStores { | ||
externalLabelStores[externalLabelsFromStore(st)]++ | ||
externalLabelOccurrencesInStores[externalLabelsFromStore(st)]++ | ||
} | ||
level.Debug(s.logger).Log("msg", "updating healthy stores", "externalLabelOccurrencesInStores", fmt.Sprintf("%#+v", externalLabelOccurrencesInStores)) | ||
|
||
s.mtx.Lock() | ||
defer s.mtx.Unlock() | ||
|
@@ -238,10 +244,12 @@ func (s *StoreSet) Update(ctx context.Context) { | |
// No external labels means strictly store gateway or ruler and it is fine to have access to multiple instances of them. | ||
// | ||
// Sidecar will error out if it will be configured with empty external labels. | ||
if len(store.Labels()) > 0 && externalLabelStores[externalLabelsFromStore(store)] != 1 { | ||
externalLabels := externalLabelsFromStore(store) | ||
storesWithExternalLabels := externalLabelOccurrencesInStores[externalLabels] | ||
if len(store.LabelSets()) > 0 && storesWithExternalLabels != 1 { | ||
store.close() | ||
s.updateStoreStatus(store, errors.New(droppingStoreMessage)) | ||
level.Warn(s.logger).Log("msg", droppingStoreMessage, "address", addr) | ||
level.Warn(s.logger).Log("msg", droppingStoreMessage, "address", addr, "extLset", externalLabels, "duplicates", storesWithExternalLabels) | ||
continue | ||
} | ||
|
||
|
@@ -254,7 +262,7 @@ func (s *StoreSet) Update(ctx context.Context) { | |
s.updateStoreStatus(store, nil) | ||
level.Info(s.logger).Log("msg", "adding new store to query storeset", "address", addr) | ||
} | ||
s.externalLabelStores = externalLabelStores | ||
s.externalLabelOccurrencesInStores = externalLabelOccurrencesInStores | ||
s.storeNodeConnections.Set(float64(len(s.stores))) | ||
s.cleanUpStoreStatuses() | ||
} | ||
|
@@ -288,14 +296,14 @@ func (s *StoreSet) getHealthyStores(ctx context.Context) map[string]*storeRef { | |
store, ok := s.stores[addr] | ||
if ok { | ||
// Check existing store. Is it healthy? What are current metadata? | ||
labels, minTime, maxTime, err := spec.Metadata(ctx, store.StoreClient) | ||
labelSets, labels, minTime, maxTime, err := spec.Metadata(ctx, store.StoreClient) | ||
if err != nil { | ||
// Peer unhealthy. Do not include in healthy stores. | ||
s.updateStoreStatus(store, err) | ||
level.Warn(s.logger).Log("msg", "update of store node failed", "err", err, "address", addr) | ||
return | ||
} | ||
store.Update(labels, minTime, maxTime) | ||
store.Update(labelSets, labels, minTime, maxTime) | ||
} else { | ||
// New store or was unhealthy and was removed in the past - create new one. | ||
conn, err := grpc.DialContext(ctx, addr, s.dialOpts...) | ||
|
@@ -315,7 +323,7 @@ func (s *StoreSet) getHealthyStores(ctx context.Context) map[string]*storeRef { | |
return | ||
} | ||
store.storeType = component.FromProto(resp.StoreType) | ||
store.Update(resp.Labels, resp.MinTime, resp.MaxTime) | ||
store.Update(resp.LabelSets, resp.Labels, resp.MinTime, resp.MaxTime) | ||
} | ||
|
||
mtx.Lock() | ||
|
@@ -331,16 +339,21 @@ func (s *StoreSet) getHealthyStores(ctx context.Context) map[string]*storeRef { | |
} | ||
|
||
func externalLabelsFromStore(store *storeRef) string { | ||
tsdbLabels := labels.Labels{} | ||
for _, l := range store.labels { | ||
tsdbLabels = append(tsdbLabels, labels.Label{ | ||
Name: l.Name, | ||
Value: l.Value, | ||
}) | ||
tsdbLabelSetStrings := make([]string, 0, len(store.labelSets)) | ||
for _, ls := range store.labelSets { | ||
tsdbLabels := labels.Labels{} | ||
for _, l := range ls.Labels { | ||
tsdbLabels = append(tsdbLabels, labels.Label{ | ||
Name: l.Name, | ||
Value: l.Value, | ||
}) | ||
} | ||
sort.Sort(tsdbLabels) | ||
tsdbLabelSetStrings = append(tsdbLabelSetStrings, tsdbLabels.String()) | ||
} | ||
sort.Sort(tsdbLabels) | ||
sort.Strings(tsdbLabelSetStrings) | ||
|
||
return tsdbLabels.String() | ||
return strings.Join(tsdbLabelSetStrings, ",") | ||
} | ||
|
||
func (s *StoreSet) updateStoreStatus(store *storeRef, err error) { | ||
|
@@ -357,7 +370,7 @@ func (s *StoreSet) updateStoreStatus(store *storeRef, err error) { | |
status.LastCheck = time.Now() | ||
|
||
if err == nil { | ||
status.Labels = store.labels | ||
status.LabelSets = store.labelSets | ||
status.StoreType = store.storeType | ||
status.MinTime = store.minTime | ||
status.MaxTime = store.maxTime | ||
|
@@ -385,8 +398,8 @@ func (s *StoreSet) externalLabelOccurrences() map[string]int { | |
s.mtx.RLock() | ||
defer s.mtx.RUnlock() | ||
|
||
r := make(map[string]int, len(s.externalLabelStores)) | ||
for k, v := range s.externalLabelStores { | ||
r := make(map[string]int, len(s.externalLabelOccurrencesInStores)) | ||
for k, v := range s.externalLabelOccurrencesInStores { | ||
r[k] = v | ||
} | ||
|
||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really get why not just replacing with labelSets.
We can hide the compatibility logic internally, right?