Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/dbv1/media_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type Id3Tags struct {
}

func mediaLink(cid string, trackId int32, userId int32, id3Tags *Id3Tags) (*MediaLink, error) {
first, rest := rendezvous.GlobalHasher.ReplicaSet3(cid)
rest = append(config.Cfg.StoreAllNodes, rest...)
first, rest := rendezvous.GlobalHasher.Select(cid)

timestamp := time.Now().Unix() * 1000
data := map[string]interface{}{
Expand Down
7 changes: 2 additions & 5 deletions api/dbv1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"api.audius.co/config"
"api.audius.co/rendezvous"
"api.audius.co/trashid"
"github.com/jackc/pgx/v5/pgtype"
Expand Down Expand Up @@ -64,8 +63,7 @@ func (q *Queries) UsersKeyed(ctx context.Context, arg GetUsersParams) (map[int32

if cid != "" {
// rendezvous for cid
first, rest := rendezvous.GlobalHasher.ReplicaSet3(cid)
rest = append(rest, config.Cfg.StoreAllNodes...)
first, rest := rendezvous.GlobalHasher.Select(cid)
coverPhoto = &RectangleImage{
X640: fmt.Sprintf("%s/content/%s/640x.jpg", first, cid),
X2000: fmt.Sprintf("%s/content/%s/2000x.jpg", first, cid),
Expand Down Expand Up @@ -126,8 +124,7 @@ func squareImageStruct(maybeCids ...pgtype.Text) *SquareImage {
}

// rendezvous for cid
first, rest := rendezvous.GlobalHasher.ReplicaSet3(cid)
rest = append(rest, config.Cfg.StoreAllNodes...)
first, rest := rendezvous.GlobalHasher.Select(cid)

return &SquareImage{
X150x150: fmt.Sprintf("%s/content/%s/150x150.jpg", first, cid),
Expand Down
2 changes: 1 addition & 1 deletion api/v1_rendezvous.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (app *ApiServer) v1Rendezvous(c *fiber.Ctx) error {
for i, node := range nodes {
endpoints[i] = node.Endpoint
}
first, rest := rendezvous.GlobalHasher.ReplicaSet3(cid)
first, rest := rendezvous.GlobalHasher.Select(cid)

return c.JSON(fiber.Map{
"first": first,
Expand Down
12 changes: 9 additions & 3 deletions rendezvous/rendezvous.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,22 @@ func (rh *RendezvousHasher) Rank(key string) []string {
return result
}

// Get a replica set of 3 nodes with random order
func (rh *RendezvousHasher) ReplicaSet3(key string) (string, []string) {
// Select returns a primary node and mirror nodes for the given key.
func (rh *RendezvousHasher) Select(key string) (string, []string) {
ranked := rh.Rank(key)
n := min(len(ranked), 3)
if n == 0 {
return "", []string{}
}

candidates := append([]string(nil), ranked[:n]...)
rand.Shuffle(n, func(i, j int) {
for _, h := range config.Cfg.StoreAllNodes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

if !slices.Contains(candidates, h) {
candidates = append(candidates, h)
}
}

rand.Shuffle(len(candidates), func(i, j int) {
candidates[i], candidates[j] = candidates[j], candidates[i]
})

Expand Down
4 changes: 2 additions & 2 deletions rendezvous/rendezvous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestReplicaSet3(t *testing.T) {
func TestSelect(t *testing.T) {
hosts := []string{
"https://host1.com",
"https://host2.com",
Expand All @@ -16,7 +16,7 @@ func TestReplicaSet3(t *testing.T) {
}

hasher := NewRendezvousHasher(hosts)
first, rest := hasher.ReplicaSet3("test-key")
first, rest := hasher.Select("test-key")
ranked := hasher.Rank("test-key")

assert.Equal(t, 2, len(rest))
Expand Down