-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common logic for sharding from bloom-gw and bloom-compactor i…
…nto a ring utils lib
- Loading branch information
1 parent
27240d7
commit 368f8f0
Showing
4 changed files
with
116 additions
and
63 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
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,85 @@ | ||
package ring | ||
|
||
import ( | ||
"github.com/grafana/dskit/ring" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
type TenantSharding interface { | ||
GetTenantSubRing(tenantID string) ring.ReadRing | ||
OwnsTenant(tenantID string) bool | ||
} | ||
|
||
type TenantShuffleSharding struct { | ||
r ring.ReadRing | ||
ringLifeCycler *ring.BasicLifecycler | ||
shardSizeForTenant func(tenantID string) int | ||
} | ||
|
||
func NewTenantShuffleSharding( | ||
r ring.ReadRing, | ||
ringLifeCycler *ring.BasicLifecycler, | ||
shardSizeForTenant func(tenantID string) int, | ||
) *TenantShuffleSharding { | ||
return &TenantShuffleSharding{ | ||
r: r, | ||
ringLifeCycler: ringLifeCycler, | ||
shardSizeForTenant: shardSizeForTenant, | ||
} | ||
} | ||
|
||
func (s *TenantShuffleSharding) GetTenantSubRing(tenantID string) ring.ReadRing { | ||
shardSize := s.shardSizeForTenant(tenantID) | ||
|
||
// A shard size of 0 means shuffle sharding is disabled for this specific user, | ||
if shardSize <= 0 { | ||
return s.r | ||
} | ||
|
||
return s.r.ShuffleShard(tenantID, shardSize) | ||
} | ||
|
||
func (s *TenantShuffleSharding) OwnsTenant(tenantID string) bool { | ||
subRing := s.GetTenantSubRing(tenantID) | ||
return subRing.HasInstance(s.ringLifeCycler.GetInstanceID()) | ||
} | ||
|
||
type FingerprintSharding interface { | ||
OwnsFingerprint(fp model.Fingerprint) (bool, error) | ||
} | ||
|
||
// FingerprintShuffleSharding is not thread-safe. | ||
type FingerprintShuffleSharding struct { | ||
r ring.ReadRing | ||
ringLifeCycler *ring.BasicLifecycler | ||
ringOp ring.Operation | ||
|
||
// Buffers for ring.Get() calls. | ||
bufDescs []ring.InstanceDesc | ||
bufHosts, bufZones []string | ||
} | ||
|
||
func NewFingerprintShuffleSharding( | ||
r ring.ReadRing, | ||
ringLifeCycler *ring.BasicLifecycler, | ||
ringOp ring.Operation, | ||
) *FingerprintShuffleSharding { | ||
s := FingerprintShuffleSharding{ | ||
r: r, | ||
ringLifeCycler: ringLifeCycler, | ||
ringOp: ringOp, | ||
} | ||
|
||
s.bufDescs, s.bufHosts, s.bufZones = ring.MakeBuffersForGet() | ||
|
||
return &s | ||
} | ||
|
||
func (s *FingerprintShuffleSharding) OwnsFingerprint(fp uint64) (bool, error) { | ||
rs, err := s.r.Get(uint32(fp), s.ringOp, s.bufDescs, s.bufHosts, s.bufZones) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return rs.Includes(s.ringLifeCycler.GetInstanceAddr()), nil | ||
} |