Skip to content

Commit

Permalink
Bugfix: Allow scale-down when at one-shard-per-node
Browse files Browse the repository at this point in the history
  • Loading branch information
otrosien committed Nov 7, 2024
1 parent dcb643d commit 1ef093a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
6 changes: 2 additions & 4 deletions operator/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,8 @@ func shardToNodeRatio(shards, nodes int32) float64 {
}

func calculateNodesWithSameShardToNodeRatio(currentDesiredNodeReplicas, currentTotalShards, newTotalShards int32) int32 {
currentShardToNodeRatio := shardToNodeRatio(currentTotalShards, currentDesiredNodeReplicas)
if currentShardToNodeRatio <= 1 {
return currentDesiredNodeReplicas
}
// reconcile shardToNodeRatio to not become below 1
currentShardToNodeRatio := math.Max(shardToNodeRatio(currentTotalShards, currentDesiredNodeReplicas), 1)
return int32(math.Ceil(float64(newTotalShards) / float64(currentShardToNodeRatio)))
}

Expand Down
33 changes: 30 additions & 3 deletions operator/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,31 @@ func TestScaleDownByRemovingIndexReplica(t *testing.T) {
require.Equal(t, DOWN, actual.ScalingDirection, actual.Description)
}

func TestScaleDownByRemovingIndexReplicaWhenHavingOneShardPerNodeBeforeScaleDown(t *testing.T) {
eds := edsTestFixture(30)
eds.Spec.Scaling.MinReplicas = 5
eds.Spec.Scaling.MaxReplicas = 30
eds.Spec.Scaling.MinShardsPerNode = 1
eds.Spec.Scaling.MaxShardsPerNode = 6
eds.Spec.Scaling.MaxIndexReplicas = 4
eds.Spec.Scaling.MinIndexReplicas = 2

esNodes := make([]ESNode, 0)

// scale down by removing an index replica
esIndices := map[string]ESIndex{
"ad1": {Replicas: 4, Primaries: 6, Index: "ad1"},
}
scalingHint := DOWN

as := systemUnderTest(eds, nil, nil)

actual := as.calculateScalingOperation(esIndices, esNodes, scalingHint)
require.Equal(t, int32(24), *actual.NodeReplicas, actual.Description)
require.Equal(t, int32(3), actual.IndexReplicas[0].Replicas, actual.Description)
require.Equal(t, DOWN, actual.ScalingDirection, actual.Description)
}

func TestAtMaxIndexReplicas(t *testing.T) {
eds := edsTestFixture(4)
esNodes := make([]ESNode, 0)
Expand Down Expand Up @@ -354,14 +379,16 @@ func TestScaleUpCausedByShardToNodeRatioLessThanOne(t *testing.T) {
"ad1": {Replicas: 1, Primaries: 5, Index: "ad1"},
}
// calculated ShardToNode ratio is 10/11 ~ 0.9
// this should get reconciled to ShardToNode ratio of 1.
eds.Spec.Scaling.MinShardsPerNode = 1
// scaling independent of desired scaling direction
scalingHint := UP

as := systemUnderTest(eds, nil, nil)

actual := as.calculateScalingOperation(esIndices, esNodes, scalingHint)
require.Equal(t, int32(11), *actual.NodeReplicas, actual.Description)
require.Contains(t, actual.IndexReplicas, ESIndex{Index: "ad1", Primaries: 5, Replicas: 2})
require.Equal(t, int32(15), *actual.NodeReplicas, actual.Description)
require.Equal(t, 1, len(actual.IndexReplicas), actual.Description)
require.Equal(t, UP, actual.ScalingDirection, actual.Description)
}
Expand Down Expand Up @@ -610,8 +637,8 @@ func TestCalculateDecreaseNodes(t *testing.T) {
}

func TestCalculateNodesWithSameShardToNodeRatio(t *testing.T) {
require.Equal(t, 16, int(calculateNodesWithSameShardToNodeRatio(16, 4, 4)))
require.Equal(t, 16, int(calculateNodesWithSameShardToNodeRatio(16, 4, 6)))
require.Equal(t, 4, int(calculateNodesWithSameShardToNodeRatio(16, 4, 4)))
require.Equal(t, 6, int(calculateNodesWithSameShardToNodeRatio(16, 4, 6)))
require.Equal(t, 12, int(calculateNodesWithSameShardToNodeRatio(16, 32, 24)))
require.Equal(t, 17, int(calculateNodesWithSameShardToNodeRatio(17, 32, 32)))
}
Expand Down

0 comments on commit 1ef093a

Please sign in to comment.