Skip to content

Commit

Permalink
Improve healthyInstancesInZone naming; simplify test
Browse files Browse the repository at this point in the history
Signed-off-by: JordanRushing <[email protected]>
  • Loading branch information
JordanRushing committed Jun 26, 2024
1 parent 3afb61c commit 51a6c2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 80 deletions.
6 changes: 3 additions & 3 deletions ring/lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func (i *Lifecycler) updateCounters(ringDesc *Desc) {
healthyInstancesCount := 0
instancesCount := 0
zones := map[string]int{}
healthyZones := map[string]int{}
healthyInstancesInZone := map[string]int{}

if ringDesc != nil {
now := time.Now()
Expand All @@ -935,7 +935,7 @@ func (i *Lifecycler) updateCounters(ringDesc *Desc) {
// Count the number of healthy instances for Write operation.
if ingester.IsHealthy(Write, i.cfg.RingConfig.HeartbeatTimeout, now) {
healthyInstancesCount++
healthyZones[ingester.Zone]++
healthyInstancesInZone[ingester.Zone]++
}
}
}
Expand All @@ -944,7 +944,7 @@ func (i *Lifecycler) updateCounters(ringDesc *Desc) {
i.countersLock.Lock()
i.healthyInstancesCount = healthyInstancesCount
i.instancesCount = instancesCount
i.healthyInstancesInZoneCount = healthyZones[i.cfg.Zone]
i.healthyInstancesInZoneCount = healthyInstancesInZone[i.cfg.Zone]
i.instancesInZoneCount = zones[i.cfg.Zone]
i.zonesCount = len(zones)
i.countersLock.Unlock()
Expand Down
83 changes: 6 additions & 77 deletions ring/lifecycler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestLifecycler_HealthyInstancesInZoneCount(t *testing.T) {
defer services.StopAndAwaitTerminated(ctx, lifecycler1) // nolint:errcheck

// Assert the first ingester joined the ring
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler1.HealthyInstancesInZoneCount() == 1
})

Expand All @@ -202,12 +202,12 @@ func TestLifecycler_HealthyInstancesInZoneCount(t *testing.T) {
defer services.StopAndAwaitTerminated(ctx, lifecycler2) // nolint:errcheck

// Assert the second ingester joined the ring
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler2.HealthyInstancesInZoneCount() == 2
})

// Assert the first ingester count is updated
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler1.HealthyInstancesInZoneCount() == 2
})

Expand All @@ -225,90 +225,19 @@ func TestLifecycler_HealthyInstancesInZoneCount(t *testing.T) {
defer services.StopAndAwaitTerminated(ctx, lifecycler3) // nolint:errcheck

// Assert the third ingester joined the ring
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler3.HealthyInstancesInZoneCount() == 1
})

// Assert the first ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler1.HealthyInstancesInZoneCount() == 2
})

// Assert the second ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler2.HealthyInstancesInZoneCount() == 2
})

// Add the fourth ingester to the ring in the same zone as the third ingester
lifecyclerConfig4 := testLifecyclerConfig(ringConfig, "ing4")
lifecyclerConfig4.HeartbeatPeriod = 100 * time.Millisecond
lifecyclerConfig4.JoinAfter = 100 * time.Millisecond
lifecyclerConfig4.Zone = "zone-b"

lifecycler4, err := NewLifecycler(lifecyclerConfig4, &nopFlushTransferer{}, "ingester", ringKey, true, log.NewNopLogger(), nil)
require.NoError(t, err)
assert.Equal(t, 0, lifecycler4.HealthyInstancesInZoneCount())

require.NoError(t, services.StartAndAwaitRunning(ctx, lifecycler4))
defer services.StopAndAwaitTerminated(ctx, lifecycler4) // nolint:errcheck

// Assert the fourth ingester joined the ring
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler4.HealthyInstancesInZoneCount() == 2
})

// Assert the first ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler1.HealthyInstancesInZoneCount() == 2
})

// Assert the second ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler2.HealthyInstancesInZoneCount() == 2
})

// Assert the third ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler3.HealthyInstancesInZoneCount() == 2
})

// Create another lifecycler for zone-c
lifecyclerConfig5 := testLifecyclerConfig(ringConfig, "ing5")
lifecyclerConfig5.HeartbeatPeriod = 100 * time.Millisecond
lifecyclerConfig5.JoinAfter = 100 * time.Millisecond
lifecyclerConfig5.Zone = "zone-c"

lifecycler5, err := NewLifecycler(lifecyclerConfig5, &nopFlushTransferer{}, "ingester", ringKey, true, log.NewNopLogger(), nil)
require.NoError(t, err)
assert.Equal(t, 0, lifecycler5.HealthyInstancesInZoneCount())

require.NoError(t, services.StartAndAwaitRunning(ctx, lifecycler5))
defer services.StopAndAwaitTerminated(ctx, lifecycler5) // nolint:errcheck

// Assert the fifth ingester joined the ring
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler5.HealthyInstancesInZoneCount() == 1
})

// Assert the first ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler1.HealthyInstancesInZoneCount() == 2
})

// Assert the second ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
test.Poll(t, time.Second, true, func() interface{} {
return lifecycler2.HealthyInstancesInZoneCount() == 2
})

// Assert the third ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler3.HealthyInstancesInZoneCount() == 2
})

// Assert the fourth ingester count is correct
test.Poll(t, 1000*time.Millisecond, true, func() interface{} {
return lifecycler4.HealthyInstancesInZoneCount() == 2
})
}

func TestLifecycler_InstancesInZoneCount(t *testing.T) {
Expand Down

0 comments on commit 51a6c2a

Please sign in to comment.