Skip to content

Commit

Permalink
fix compilation errors in model_test.go; set nil check guard in Token…
Browse files Browse the repository at this point in the history
…sFor in model.go; pass model_test.go with race check
  • Loading branch information
francoposa committed Jan 14, 2025
1 parent a05aa99 commit 7667afc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
4 changes: 3 additions & 1 deletion ring/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (d *Desc) IsReady(now time.Time, heartbeatTimeout time.Duration) error {
// Returned tokens are guaranteed to be sorted.
func (d *Desc) TokensFor(id string) (myTokens, allTokens Tokens) {
allTokens = d.GetTokens()
myTokens = d.Ingesters[id].Tokens
if ing := d.GetIngester(id); ing != nil {
myTokens = ing.Tokens
}
return
}

Expand Down
74 changes: 37 additions & 37 deletions ring/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ func TestInstanceDesc_GetLastHeartbeatAt(t *testing.T) {

func normalizedSource() *Desc {
r := NewDesc()
r.Ingesters["first"] = InstanceDesc{
r.Ingesters["first"] = &InstanceDesc{
Tokens: []uint32{100, 200, 300},
}
r.Ingesters["second"] = InstanceDesc{}
r.Ingesters["second"] = &InstanceDesc{}
return r
}

func normalizedOutput() *Desc {
return &Desc{
Ingesters: map[string]InstanceDesc{
Ingesters: map[string]*InstanceDesc{
"first": {},
"second": {Tokens: []uint32{100, 200, 300}},
},
Expand All @@ -153,7 +153,7 @@ func TestDesc_Ready(t *testing.T) {
now := time.Now()

r := &Desc{
Ingesters: map[string]InstanceDesc{
Ingesters: map[string]*InstanceDesc{
"ing1": {
Tokens: []uint32{100, 200, 300},
State: InstanceState_ACTIVE,
Expand All @@ -179,7 +179,7 @@ func TestDesc_Ready(t *testing.T) {
}

r = &Desc{
Ingesters: map[string]InstanceDesc{
Ingesters: map[string]*InstanceDesc{
"ing1": {
State: InstanceState_ACTIVE,
Timestamp: now.Unix(),
Expand All @@ -191,7 +191,7 @@ func TestDesc_Ready(t *testing.T) {
t.Fatal("expected !ready (no tokens), but got no error")
}

r.Ingesters["some ingester"] = InstanceDesc{
r.Ingesters["some ingester"] = &InstanceDesc{
Tokens: []uint32{12345},
Timestamp: now.Unix(),
}
Expand All @@ -207,11 +207,11 @@ func TestDesc_getTokensByZone(t *testing.T) {
expected map[string][]uint32
}{
"empty ring": {
desc: &Desc{Ingesters: map[string]InstanceDesc{}},
desc: &Desc{Ingesters: map[string]*InstanceDesc{}},
expected: map[string][]uint32{},
},
"single zone": {
desc: &Desc{Ingesters: map[string]InstanceDesc{
desc: &Desc{Ingesters: map[string]*InstanceDesc{
"instance-1": {Addr: "127.0.0.1", Tokens: []uint32{1, 5}, Zone: ""},
"instance-2": {Addr: "127.0.0.1", Tokens: []uint32{2, 4}, Zone: ""},
"instance-3": {Addr: "127.0.0.1", Tokens: []uint32{3, 6}, Zone: ""},
Expand All @@ -221,7 +221,7 @@ func TestDesc_getTokensByZone(t *testing.T) {
},
},
"multiple zones": {
desc: &Desc{Ingesters: map[string]InstanceDesc{
desc: &Desc{Ingesters: map[string]*InstanceDesc{
"instance-1": {Addr: "127.0.0.1", Tokens: []uint32{1, 5}, Zone: "zone-1"},
"instance-2": {Addr: "127.0.0.1", Tokens: []uint32{2, 4}, Zone: "zone-1"},
"instance-3": {Addr: "127.0.0.1", Tokens: []uint32{3, 6}, Zone: "zone-2"},
Expand All @@ -247,12 +247,12 @@ func TestDesc_TokensFor(t *testing.T) {
expectedAll Tokens
}{
"empty ring": {
desc: &Desc{Ingesters: map[string]InstanceDesc{}},
desc: &Desc{Ingesters: map[string]*InstanceDesc{}},
expectedMine: Tokens(nil),
expectedAll: Tokens{},
},
"single zone": {
desc: &Desc{Ingesters: map[string]InstanceDesc{
desc: &Desc{Ingesters: map[string]*InstanceDesc{
"instance-1": {Addr: "127.0.0.1", Tokens: []uint32{1, 5}, Zone: ""},
"instance-2": {Addr: "127.0.0.1", Tokens: []uint32{2, 4}, Zone: ""},
"instance-3": {Addr: "127.0.0.1", Tokens: []uint32{3, 6}, Zone: ""},
Expand All @@ -261,7 +261,7 @@ func TestDesc_TokensFor(t *testing.T) {
expectedAll: Tokens{1, 2, 3, 4, 5, 6},
},
"multiple zones": {
desc: &Desc{Ingesters: map[string]InstanceDesc{
desc: &Desc{Ingesters: map[string]*InstanceDesc{
"instance-1": {Addr: "127.0.0.1", Tokens: []uint32{1, 5}, Zone: "zone-1"},
"instance-2": {Addr: "127.0.0.1", Tokens: []uint32{2, 4}, Zone: "zone-1"},
"instance-3": {Addr: "127.0.0.1", Tokens: []uint32{3, 6}, Zone: "zone-2"},
Expand Down Expand Up @@ -292,67 +292,67 @@ func TestDesc_RingsCompare(t *testing.T) {
},
"one nil, one empty ring": {
r1: nil,
r2: &Desc{Ingesters: map[string]InstanceDesc{}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{}},
expected: Equal,
},
"two empty rings": {
r1: &Desc{Ingesters: map[string]InstanceDesc{}},
r2: &Desc{Ingesters: map[string]InstanceDesc{}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{}},
expected: Equal,
},
"same single instance": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
expected: Equal,
},
"same single instance, different timestamp": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Timestamp: 123456}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Timestamp: 789012}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Timestamp: 123456}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Timestamp: 789012}}},
expected: EqualButStatesAndTimestamps,
},
"same single instance, different state": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_JOINING}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_JOINING}}},
expected: EqualButStatesAndTimestamps,
},
"same single instance, different registered timestamp": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE, RegisteredTimestamp: 1}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE, RegisteredTimestamp: 2}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE, RegisteredTimestamp: 1}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", State: InstanceState_ACTIVE, RegisteredTimestamp: 2}}},
expected: Different,
},
"same single instance, different read only flag": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", ReadOnly: true}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", ReadOnly: true}}},
expected: Different,
},
"same single instance, different read only timestamp": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", ReadOnlyUpdatedTimestamp: time.Time{}.Unix()}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", ReadOnlyUpdatedTimestamp: time.Now().Unix()}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", ReadOnlyUpdatedTimestamp: time.Time{}.Unix()}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", ReadOnlyUpdatedTimestamp: time.Now().Unix()}}},
expected: Different,
},
"instance in different zone": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Zone: "one"}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Zone: "two"}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Zone: "one"}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Zone: "two"}}},
expected: Different,
},
"same instance, different address": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr2"}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr2"}}},
expected: Different,
},
"more instances in one ring": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}, "ing2": {Addr: "ing2"}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}, "ing2": {Addr: "ing2"}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
expected: Different,
},
"different tokens": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 3}}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1"}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 3}}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1"}}},
expected: Different,
},
"different tokens 2": {
r1: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 3}}}},
r2: &Desc{Ingesters: map[string]InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 4}}}},
r1: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 3}}}},
r2: &Desc{Ingesters: map[string]*InstanceDesc{"ing1": {Addr: "addr1", Tokens: []uint32{1, 2, 4}}}},
expected: Different,
},
}
Expand Down

0 comments on commit 7667afc

Please sign in to comment.