Skip to content

Commit 11ffb30

Browse files
authored
[Feature] Extract CalculateMemoryReservation function into ArangoDeployment Group Spec (#1472)
1 parent 638425b commit 11ffb30

File tree

7 files changed

+114
-31
lines changed

7 files changed

+114
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- (Documentation) Use OpenAPI-compatible type names in docs
1717
- (Improvement) Use agency cache lock in metrics exporter
1818
- (Maintenance) Remove `scale_down_candidate` annotation
19+
- (Maintenance) Extract CalculateMemoryReservation function into ArangoDeployment Group Spec
1920

2021
## [1.2.34](https://github.com/arangodb/kube-arangodb/tree/1.2.34) (2023-10-16)
2122
- (Bugfix) Fix make manifests-crd-file command

docs/api/ArangoDeployment.V1.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
180180

181181
### .spec.agents.memoryReservation: integer
182182

183-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
184-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
185-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
183+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
184+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
185+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
186186

187187
Links:
188188
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)
@@ -1024,9 +1024,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
10241024

10251025
### .spec.coordinators.memoryReservation: integer
10261026

1027-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
1028-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
1029-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
1027+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
1028+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
1029+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
10301030

10311031
Links:
10321032
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)
@@ -1756,9 +1756,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
17561756

17571757
### .spec.dbservers.memoryReservation: integer
17581758

1759-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
1760-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
1761-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
1759+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
1760+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
1761+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
17621762

17631763
Links:
17641764
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)
@@ -2972,9 +2972,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
29722972

29732973
### .spec.single.memoryReservation: integer
29742974

2975-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
2976-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
2977-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
2975+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
2976+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
2977+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
29782978

29792979
Links:
29802980
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)
@@ -3869,9 +3869,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
38693869

38703870
### .spec.syncmasters.memoryReservation: integer
38713871

3872-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
3873-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
3874-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
3872+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
3873+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
3874+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
38753875

38763876
Links:
38773877
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)
@@ -4595,9 +4595,9 @@ MaxCount specifies a maximum for the count of servers. If set, a specification i
45954595

45964596
### .spec.syncworkers.memoryReservation: integer
45974597

4598-
MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
4599-
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
4600-
Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
4598+
MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
4599+
If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
4600+
Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
46014601

46024602
Links:
46034603
* [Docs of the ArangoDB Envs](https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/)

pkg/apis/deployment/v1/server_group_spec.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ type ServerGroupSpec struct {
9999
// +doc/default: true
100100
// +doc/link: Docs of the ArangoDB Envs|https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/
101101
OverrideDetectedTotalMemory *bool `json:"overrideDetectedTotalMemory,omitempty"`
102-
// MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
103-
// If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
104-
// Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
102+
// MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
103+
// If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
104+
// Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
105105
// +doc/default: 0
106106
// +doc/link: Docs of the ArangoDB Envs|https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/
107107
MemoryReservation *int64 `json:"memoryReservation,omitempty"`
@@ -796,11 +796,23 @@ func (s *ServerGroupSpec) GetExporterPort() uint16 {
796796
func (s *ServerGroupSpec) GetMemoryReservation() int64 {
797797
if s != nil {
798798
if v := s.MemoryReservation; v != nil {
799-
if q := *v; q >= 0 && q <= 50 {
799+
if q := *v; q < 0 {
800+
return 0
801+
} else if q > 50 {
802+
return 50
803+
} else {
800804
return q
801805
}
802806
}
803807
}
804808

805809
return 0
806810
}
811+
812+
func (s *ServerGroupSpec) CalculateMemoryReservation(memory int64) int64 {
813+
if r := s.GetMemoryReservation(); r > 0 {
814+
return int64((float64(memory)) * (float64(100-r) / 100))
815+
}
816+
817+
return memory
818+
}

pkg/apis/deployment/v1/server_group_spec_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,34 @@ func TestServerGroupSpecValidateArgs(t *testing.T) {
130130
assert.Error(t, ServerGroupSpec{Count: util.NewType[int](1), Args: []string{"--master.endpoint=http://something"}}.WithDefaults(ServerGroupSyncMasters, true, DeploymentModeCluster).Validate(ServerGroupSyncMasters, true, DeploymentModeCluster, EnvironmentDevelopment))
131131
assert.Error(t, ServerGroupSpec{Count: util.NewType[int](1), Args: []string{"--mq.type=strange"}}.WithDefaults(ServerGroupSyncMasters, true, DeploymentModeCluster).Validate(ServerGroupSyncMasters, true, DeploymentModeCluster, EnvironmentDevelopment))
132132
}
133+
134+
func TestServerGroupSpecMemoryReservation(t *testing.T) {
135+
// If group is nil
136+
assert.EqualValues(t, 1024, (*ServerGroupSpec)(nil).CalculateMemoryReservation(1024))
137+
138+
// If reservation is nil
139+
assert.EqualValues(t, 1024, (&ServerGroupSpec{}).CalculateMemoryReservation(1024))
140+
141+
// If reservation is 0
142+
assert.EqualValues(t, 1024, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](0)}).CalculateMemoryReservation(1024))
143+
144+
// If reservation is -1
145+
assert.EqualValues(t, 1024, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](-1)}).CalculateMemoryReservation(1024))
146+
147+
// If reservation is 10
148+
assert.EqualValues(t, 921, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](10)}).CalculateMemoryReservation(1024))
149+
150+
// If reservation is 25
151+
assert.EqualValues(t, 768, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](25)}).CalculateMemoryReservation(1024))
152+
153+
// If reservation is 50
154+
assert.EqualValues(t, 512, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](50)}).CalculateMemoryReservation(1024))
155+
156+
// If reservation is 51
157+
assert.EqualValues(t, 512, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](51)}).CalculateMemoryReservation(1024))
158+
159+
// If reservation is int max
160+
assert.EqualValues(t, 576460752303423488, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](50)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
161+
assert.EqualValues(t, 864691128455135232, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](25)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
162+
assert.EqualValues(t, 1037629354146162304, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](10)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
163+
}

pkg/apis/deployment/v2alpha1/server_group_spec.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ type ServerGroupSpec struct {
9999
// +doc/default: true
100100
// +doc/link: Docs of the ArangoDB Envs|https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/
101101
OverrideDetectedTotalMemory *bool `json:"overrideDetectedTotalMemory,omitempty"`
102-
// MemoryReservation determines system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
103-
// If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by specified value in percents.
104-
// Accepted Range <0, 50>. If value is outside accepted range, it is adjusted to the closest value.
102+
// MemoryReservation determines the system reservation of memory while calculating `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` value.
103+
// If this field is set, `ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY` is reduced by a specified value in percent.
104+
// Accepted Range <0, 50>. If the value is outside the accepted range, it is adjusted to the closest value.
105105
// +doc/default: 0
106106
// +doc/link: Docs of the ArangoDB Envs|https://docs.arangodb.com/devel/components/arangodb-server/environment-variables/
107107
MemoryReservation *int64 `json:"memoryReservation,omitempty"`
@@ -796,11 +796,23 @@ func (s *ServerGroupSpec) GetExporterPort() uint16 {
796796
func (s *ServerGroupSpec) GetMemoryReservation() int64 {
797797
if s != nil {
798798
if v := s.MemoryReservation; v != nil {
799-
if q := *v; q >= 0 && q <= 50 {
799+
if q := *v; q < 0 {
800+
return 0
801+
} else if q > 50 {
802+
return 50
803+
} else {
800804
return q
801805
}
802806
}
803807
}
804808

805809
return 0
806810
}
811+
812+
func (s *ServerGroupSpec) CalculateMemoryReservation(memory int64) int64 {
813+
if r := s.GetMemoryReservation(); r > 0 {
814+
return int64((float64(memory)) * (float64(100-r) / 100))
815+
}
816+
817+
return memory
818+
}

pkg/apis/deployment/v2alpha1/server_group_spec_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,34 @@ func TestServerGroupSpecValidateArgs(t *testing.T) {
130130
assert.Error(t, ServerGroupSpec{Count: util.NewType[int](1), Args: []string{"--master.endpoint=http://something"}}.WithDefaults(ServerGroupSyncMasters, true, DeploymentModeCluster).Validate(ServerGroupSyncMasters, true, DeploymentModeCluster, EnvironmentDevelopment))
131131
assert.Error(t, ServerGroupSpec{Count: util.NewType[int](1), Args: []string{"--mq.type=strange"}}.WithDefaults(ServerGroupSyncMasters, true, DeploymentModeCluster).Validate(ServerGroupSyncMasters, true, DeploymentModeCluster, EnvironmentDevelopment))
132132
}
133+
134+
func TestServerGroupSpecMemoryReservation(t *testing.T) {
135+
// If group is nil
136+
assert.EqualValues(t, 1024, (*ServerGroupSpec)(nil).CalculateMemoryReservation(1024))
137+
138+
// If reservation is nil
139+
assert.EqualValues(t, 1024, (&ServerGroupSpec{}).CalculateMemoryReservation(1024))
140+
141+
// If reservation is 0
142+
assert.EqualValues(t, 1024, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](0)}).CalculateMemoryReservation(1024))
143+
144+
// If reservation is -1
145+
assert.EqualValues(t, 1024, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](-1)}).CalculateMemoryReservation(1024))
146+
147+
// If reservation is 10
148+
assert.EqualValues(t, 921, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](10)}).CalculateMemoryReservation(1024))
149+
150+
// If reservation is 25
151+
assert.EqualValues(t, 768, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](25)}).CalculateMemoryReservation(1024))
152+
153+
// If reservation is 50
154+
assert.EqualValues(t, 512, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](50)}).CalculateMemoryReservation(1024))
155+
156+
// If reservation is 51
157+
assert.EqualValues(t, 512, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](51)}).CalculateMemoryReservation(1024))
158+
159+
// If reservation is int max
160+
assert.EqualValues(t, 576460752303423488, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](50)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
161+
assert.EqualValues(t, 864691128455135232, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](25)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
162+
assert.EqualValues(t, 1037629354146162304, (&ServerGroupSpec{MemoryReservation: util.NewType[int64](10)}).CalculateMemoryReservation(0xFFFFFFFFFFFFFFF))
163+
}

pkg/deployment/resources/pod_creator_arangod.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ func (a *ArangoDContainer) GetEnvs() ([]core.EnvVar, []core.EnvFromSource) {
213213
if resources.Limits != nil {
214214
if a.groupSpec.GetOverrideDetectedTotalMemory() {
215215
if limits, ok := resources.Limits[core.ResourceMemory]; ok {
216-
value := limits.Value()
217-
218-
if memoryReservation := a.groupSpec.GetMemoryReservation(); memoryReservation > 0 {
219-
value = (value / 100) * (100 - memoryReservation)
220-
}
216+
value := a.groupSpec.CalculateMemoryReservation(limits.Value())
221217

222218
envs.Add(true, core.EnvVar{
223219
Name: ArangoDBOverrideDetectedTotalMemoryEnv,

0 commit comments

Comments
 (0)