-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
feat(blooms): Add bloom planner and bloom builder to backend
target
#13997
Merged
chaudum
merged 1 commit into
main
from
chaudum/bloom-planner-builder-simple-scalable-deployment
Sep 2, 2024
Merged
Changes from all commits
Commits
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
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,119 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/grafana/dskit/ring" | ||
"github.com/grafana/dskit/services" | ||
) | ||
|
||
const ( | ||
RingKeyOfLeader = 0xffff | ||
) | ||
|
||
type RingWatcher struct { | ||
services.Service | ||
id string | ||
ring *ring.Ring | ||
leader *ring.InstanceDesc | ||
lookupPeriod time.Duration | ||
logger log.Logger | ||
} | ||
|
||
// NewRingWatcher creates a service.Service that watches a ring for a leader instance. | ||
// The leader instance is the instance that owns the key `RingKeyOfLeader`. | ||
// It provides functions to get the leader's address, and to check whether a given instance in the ring is leader. | ||
// Bloom planner and bloom builder use this ring watcher to hook into index gateway ring when they are run as | ||
// part of the `backend` target of the Simple Scalable Deployment (SSD). | ||
// It should not be used for any other components outside of the bloombuild package. | ||
func NewRingWatcher(id string, ring *ring.Ring, lookupPeriod time.Duration, logger log.Logger) *RingWatcher { | ||
w := &RingWatcher{ | ||
id: id, | ||
ring: ring, | ||
lookupPeriod: lookupPeriod, | ||
logger: logger, | ||
} | ||
w.Service = services.NewBasicService(nil, w.updateLoop, nil) | ||
return w | ||
} | ||
|
||
func (w *RingWatcher) waitForInitialLeader(ctx context.Context) error { | ||
syncTicker := time.NewTicker(time.Second) | ||
defer syncTicker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-syncTicker.C: | ||
w.lookupAddresses() | ||
if w.leader != nil { | ||
return nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (w *RingWatcher) updateLoop(ctx context.Context) error { | ||
_ = w.waitForInitialLeader(ctx) | ||
|
||
syncTicker := time.NewTicker(w.lookupPeriod) | ||
defer syncTicker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-syncTicker.C: | ||
w.lookupAddresses() | ||
} | ||
} | ||
} | ||
|
||
func (w *RingWatcher) lookupAddresses() { | ||
bufDescs, bufHosts, bufZones := ring.MakeBuffersForGet() | ||
rs, err := w.ring.Get(RingKeyOfLeader, ring.WriteNoExtend, bufDescs, bufHosts, bufZones) | ||
if err != nil { | ||
level.Error(w.logger).Log("msg", "failed to get replicationset for key", "key", RingKeyOfLeader, "err", err) | ||
w.leader = nil | ||
return | ||
} | ||
|
||
for i := range rs.Instances { | ||
inst := rs.Instances[i] | ||
state, err := w.ring.GetInstanceState(inst.Id) | ||
if err != nil || state != ring.ACTIVE { | ||
return | ||
} | ||
tr, err := w.ring.GetTokenRangesForInstance(inst.Id) | ||
if err != nil && (len(tr) == 0 || tr.IncludesKey(RingKeyOfLeader)) { | ||
if w.leader == nil || w.leader.Id != inst.Id { | ||
level.Info(w.logger).Log("msg", "updated leader", "new_leader", inst) | ||
} | ||
w.leader = &inst | ||
return | ||
} | ||
} | ||
|
||
w.leader = nil | ||
} | ||
|
||
func (w *RingWatcher) IsLeader() bool { | ||
return w.IsInstanceLeader(w.id) | ||
} | ||
|
||
func (w *RingWatcher) IsInstanceLeader(instanceID string) bool { | ||
res := w.leader != nil && w.leader.Id == instanceID | ||
level.Debug(w.logger).Log("msg", "check if instance is leader", "inst", instanceID, "curr_leader", w.leader, "is_leader", res) | ||
return res | ||
} | ||
|
||
func (w *RingWatcher) GetLeaderAddress() (string, error) { | ||
if w.leader == nil { | ||
return "", ring.ErrEmptyRing | ||
} | ||
return w.leader.Addr, nil | ||
} |
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
Oops, something went wrong.
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 think this ticker could be replaced with a simpler
time.Sleep
at the beginning of the function if the ringWatcher is not null.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.
time.Sleep
,time.After
, andtime.NewTimer
do essentially the same, but since we already have aselect
loop, I think it's cleaner to avoidtime.Sleep
.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.
To me, a sleep is easier to understand as I don't need to think about which select will trigger earlier. But I don't have a strong opinion on this. Approved.