From 1ca2d8addeb5b98cbb1d611cecbd7a79c8e02155 Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Fri, 8 Oct 2021 11:05:30 +0200 Subject: [PATCH] fix: make cache and max-sql-memory configurable for cockroachdb (#2483) --- .../database/kinds/databases/managed/adapt.go | 2 ++ .../kinds/databases/managed/desired.go | 2 ++ .../databases/managed/statefulset/adapt.go | 29 ++++++++++++++++-- .../managed/statefulset/adapt_test.go | 30 +++++++++++++++---- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/operator/database/kinds/databases/managed/adapt.go b/operator/database/kinds/databases/managed/adapt.go index 8066a61ece..6deabf5eec 100644 --- a/operator/database/kinds/databases/managed/adapt.go +++ b/operator/database/kinds/databases/managed/adapt.go @@ -134,6 +134,8 @@ func Adapter( desiredKind.Spec.NodeSelector, desiredKind.Spec.Tolerations, desiredKind.Spec.Resources, + desiredKind.Spec.Cache, + desiredKind.Spec.MaxSQLMemory, ) if err != nil { return nil, nil, nil, nil, nil, false, err diff --git a/operator/database/kinds/databases/managed/desired.go b/operator/database/kinds/databases/managed/desired.go index eac6b44747..8ff892b449 100644 --- a/operator/database/kinds/databases/managed/desired.go +++ b/operator/database/kinds/databases/managed/desired.go @@ -26,6 +26,8 @@ type Spec struct { ClusterDns string `yaml:"clusterDNS,omitempty"` Backups map[string]*tree.Tree `yaml:"backups,omitempty"` Resources *k8s.Resources `yaml:"resources,omitempty"` + MaxSQLMemory string `yaml:"maxSqlMemory,omitempty"` + Cache string `yaml:"cache,omitempty"` } func parseDesiredV0(desiredTree *tree.Tree) (*DesiredV0, error) { diff --git a/operator/database/kinds/databases/managed/statefulset/adapt.go b/operator/database/kinds/databases/managed/statefulset/adapt.go index d3cdaac8c9..5a91592c17 100644 --- a/operator/database/kinds/databases/managed/statefulset/adapt.go +++ b/operator/database/kinds/databases/managed/statefulset/adapt.go @@ -63,6 +63,8 @@ func AdaptFunc( nodeSelector map[string]string, tolerations []corev1.Toleration, resourcesSFS *k8s.Resources, + cache string, + maxSqlMemory string, ) ( resources.QueryFunc, resources.DestroyFunc, @@ -149,6 +151,8 @@ func AdaptFunc( name, int(dbPort), replicaCount, + getCache(cache), + getMaxSqlMemory(maxSqlMemory), ), }, Resources: getResources(resourcesSFS), @@ -286,7 +290,14 @@ func AdaptFunc( return wrapedQuery, wrapedDestroy, ensureInit, checkDBReady, getAllDBs, err } -func getJoinExec(namespace string, name string, dbPort int, replicaCount int) string { +func getJoinExec( + namespace string, + name string, + dbPort int, + replicaCount int, + cache string, + maxSqlMemory string, +) string { joinList := make([]string, 0) for i := 0; i < replicaCount; i++ { joinList = append(joinList, fmt.Sprintf("%s-%d.%s.%s:%d", name, i, name, namespace, dbPort)) @@ -294,7 +305,21 @@ func getJoinExec(namespace string, name string, dbPort int, replicaCount int) st joinListStr := strings.Join(joinList, ",") locality := "zone=" + namespace - return "exec /cockroach/cockroach start --logtostderr --certs-dir " + certPath + " --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join " + joinListStr + " --locality " + locality + " --cache 25% --max-sql-memory 25%" + return "exec /cockroach/cockroach start --logtostderr --certs-dir " + certPath + " --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join " + joinListStr + " --locality " + locality + " --cache " + cache + " --max-sql-memory " + maxSqlMemory +} + +func getCache(cache string) string { + if cache != "" { + return cache + } + return "10%" +} + +func getMaxSqlMemory(maxSqlMemory string) string { + if maxSqlMemory != "" { + return maxSqlMemory + } + return "10%" } func getResources(resourcesSFS *k8s.Resources) corev1.ResourceRequirements { diff --git a/operator/database/kinds/databases/managed/statefulset/adapt_test.go b/operator/database/kinds/databases/managed/statefulset/adapt_test.go index 99cbcbe0b8..e9f145efcc 100644 --- a/operator/database/kinds/databases/managed/statefulset/adapt_test.go +++ b/operator/database/kinds/databases/managed/statefulset/adapt_test.go @@ -22,9 +22,11 @@ func TestStatefulset_JoinExec0(t *testing.T) { name := "test" dbPort := 26257 replicaCount := 0 + cache := "" + maxSqlMemory := "" - equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join --locality zone=testNs --cache 25% --max-sql-memory 25%" - assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount)) + equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join --locality zone=testNs --cache --max-sql-memory " + assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount, cache, maxSqlMemory)) } func TestStatefulset_JoinExec1(t *testing.T) { @@ -32,9 +34,11 @@ func TestStatefulset_JoinExec1(t *testing.T) { name := "test2" dbPort := 26257 replicaCount := 1 + cache := "15%" + maxSqlMemory := "15%" - equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join test2-0.test2.testNs2:26257 --locality zone=testNs2 --cache 25% --max-sql-memory 25%" - assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount)) + equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join test2-0.test2.testNs2:26257 --locality zone=testNs2 --cache 15% --max-sql-memory 15%" + assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount, cache, maxSqlMemory)) } func TestStatefulset_JoinExec2(t *testing.T) { @@ -42,9 +46,11 @@ func TestStatefulset_JoinExec2(t *testing.T) { name := "test" dbPort := 23 replicaCount := 2 + cache := "20%" + maxSqlMemory := "20%" - equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join test-0.test.testNs:23,test-1.test.testNs:23 --locality zone=testNs --cache 25% --max-sql-memory 25%" - assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount)) + equals := "exec /cockroach/cockroach start --logtostderr --certs-dir /cockroach/cockroach-certs --advertise-host $(hostname -f) --http-addr 0.0.0.0 --join test-0.test.testNs:23,test-1.test.testNs:23 --locality zone=testNs --cache 20% --max-sql-memory 20%" + assert.Equal(t, equals, getJoinExec(namespace, name, dbPort, replicaCount, cache, maxSqlMemory)) } func TestStatefulset_Resources0(t *testing.T) { @@ -150,6 +156,8 @@ func TestStatefulset_Adapt1(t *testing.T) { nodeSelector := map[string]string{} tolerations := []corev1.Toleration{} resourcesSFS := &k8s.Resources{} + cache := "" + maxSqlMemory := "" quantity, err := resource.ParseQuantity(storageCapacity) assert.NoError(t, err) @@ -228,6 +236,8 @@ func TestStatefulset_Adapt1(t *testing.T) { name, int(dbPort), replicaCount, + getCache(cache), + getMaxSqlMemory(maxSqlMemory), ), }, Resources: getResources(resourcesSFS), @@ -299,6 +309,8 @@ func TestStatefulset_Adapt1(t *testing.T) { nodeSelector, tolerations, resourcesSFS, + cache, + maxSqlMemory, ) assert.NoError(t, err) @@ -346,6 +358,8 @@ func TestStatefulset_Adapt2(t *testing.T) { nodeSelector := map[string]string{} tolerations := []corev1.Toleration{} resourcesSFS := &k8s.Resources{} + cache := "20%" + maxSqlMemory := "20%" quantity, err := resource.ParseQuantity(storageCapacity) assert.NoError(t, err) @@ -424,6 +438,8 @@ func TestStatefulset_Adapt2(t *testing.T) { name, int(dbPort), replicaCount, + getCache(cache), + getMaxSqlMemory(maxSqlMemory), ), }, Resources: getResources(resourcesSFS), @@ -495,6 +511,8 @@ func TestStatefulset_Adapt2(t *testing.T) { nodeSelector, tolerations, resourcesSFS, + cache, + maxSqlMemory, ) assert.NoError(t, err)