@@ -20,9 +20,11 @@ import (
2020 "fmt"
2121
2222 core "k8s.io/api/core/v1"
23+ "k8s.io/apimachinery/pkg/api/resource"
2324 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425 "k8s.io/apimachinery/pkg/util/intstr"
2526
27+ api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
2628 "github.com/presslabs/mysql-operator/pkg/options"
2729)
2830
@@ -76,49 +78,43 @@ func (cluster *MysqlCluster) SetDefaults(opt *options.Options) {
7678 // https://www.percona.com/blog/2018/03/26/mysql-8-0-innodb_dedicated_server-variable-optimizes-innodb/
7779
7880 // set innodb-buffer-pool-size if not set
79- if _ , ok := cluster .Spec .MysqlConf ["innodb-buffer-pool-size" ]; ! ok {
80- if mem := cluster .Spec .PodSpec .Resources .Requests .Memory (); mem != nil {
81- var bufferSize int64
82- if mem .Value () < gb {
83- // RAM < 1G => buffer size set to 128M
84- bufferSize = 128 * mb
85- } else if mem .Value () <= 4 * gb {
86- // RAM <= 4gb => buffer size set to RAM * 0.5
87- bufferSize = int64 (float64 (mem .Value ()) * 0.5 )
88- } else {
89- // RAM > 4gb => buffer size set to RAM * 0.75
90- bufferSize = int64 (float64 (mem .Value ()) * 0.75 )
91- }
81+ if mem := cluster .Spec .PodSpec .Resources .Requests .Memory (); mem != nil {
82+ bufferSize := humanizeSize (computeInnodbBufferPoolSize (mem ))
83+ setConfigIfNotSet (cluster .Spec .MysqlConf , "innodb-buffer-pool-size" , bufferSize )
84+ }
9285
93- cluster .Spec .MysqlConf ["innodb-buffer-pool-size" ] = humanizeSize (bufferSize )
94- }
86+ if mem := cluster .Spec .PodSpec .Resources .Requests .Memory (); mem != nil {
87+ logFileSize := humanizeSize (computeInnodbLogFileSize (mem ))
88+ setConfigIfNotSet (cluster .Spec .MysqlConf , "innodb-log-file-size" , logFileSize )
9589 }
9690
97- if _ , ok := cluster .Spec .MysqlConf ["innodb-log-file-size" ]; ! ok {
98- if mem := cluster .Spec .PodSpec .Resources .Requests .Memory (); mem != nil {
99- var logFileSize int64
100- if mem .Value () < gb {
101- // RAM < 1G
102- logFileSize = 48 * mb
103- } else if mem .Value () <= 4 * gb {
104- // RAM <= 4gb
105- logFileSize = 128 * mb
106- } else if mem .Value () <= 8 * gb {
107- // RAM <= 8gb
108- logFileSize = 512 * mb
109- } else if mem .Value () <= 16 * gb {
110- // RAM <= 16gb
111- logFileSize = 1 * gb
112- } else {
113- // RAM > 16gb
114- logFileSize = 2 * gb
91+ if pvc := cluster .Spec .VolumeSpec .PersistentVolumeClaim ; pvc != nil {
92+ if space := getRequestedStorage (pvc ); space != nil {
93+ binlogSpaceLimit := space .Value () / 2
94+ maxBinlogSize := min (binlogSpaceLimit / 4 , 1 * gb )
95+ if space .Value () < 2 * gb {
96+ binlogSpaceLimit = space .Value () / 3
97+ maxBinlogSize = min (binlogSpaceLimit / 3 , 1 * gb )
11598 }
116-
117- cluster .Spec .MysqlConf [ "innodb-log-file-size" ] = humanizeSize (logFileSize )
99+ setConfigIfNotSet ( cluster . Spec . MysqlConf , "max-binlog-size" , humanizeSize ( maxBinlogSize ))
100+ setConfigIfNotSet ( cluster .Spec .MysqlConf , "binlog-space-limit" , humanizeSize (binlogSpaceLimit ) )
118101 }
119102 }
120103}
121104
105+ func setConfigIfNotSet (conf api.MysqlConf , option string , value intstr.IntOrString ) {
106+ if _ , ok := conf [option ]; ! ok {
107+ conf [option ] = value
108+ }
109+ }
110+
111+ func getRequestedStorage (pvc * core.PersistentVolumeClaimSpec ) * resource.Quantity {
112+ if val , ok := pvc .Resources .Requests [core .ResourceStorage ]; ok {
113+ return & val
114+ }
115+ return nil
116+ }
117+
122118func humanizeSize (value int64 ) intstr.IntOrString {
123119 var unit string
124120
@@ -132,3 +128,51 @@ func humanizeSize(value int64) intstr.IntOrString {
132128
133129 return intstr .FromString (fmt .Sprintf ("%d%s" , value , unit ))
134130}
131+
132+ // computeInnodbLogFileSize returns a computed value, to configure MySQL, based on requested memory.
133+ func computeInnodbLogFileSize (mem * resource.Quantity ) int64 {
134+ var logFileSize int64
135+ if mem .Value () < gb {
136+ // RAM < 1G
137+ logFileSize = 48 * mb
138+ } else if mem .Value () <= 4 * gb {
139+ // RAM <= 4gb
140+ logFileSize = 128 * mb
141+ } else if mem .Value () <= 8 * gb {
142+ // RAM <= 8gb
143+ logFileSize = 512 * mb
144+ } else if mem .Value () <= 16 * gb {
145+ // RAM <= 16gb
146+ logFileSize = 1 * gb
147+ } else {
148+ // RAM > 16gb
149+ logFileSize = 2 * gb
150+ }
151+
152+ return logFileSize
153+ }
154+
155+ // computeInnodbBufferPoolSize returns a computed value, to configure MySQL, based on requested
156+ // memory.
157+ func computeInnodbBufferPoolSize (mem * resource.Quantity ) int64 {
158+ var bufferSize int64
159+ if mem .Value () < gb {
160+ // RAM < 1G => buffer size set to 128M
161+ bufferSize = 128 * mb
162+ } else if mem .Value () <= 4 * gb {
163+ // RAM <= 4gb => buffer size set to RAM * 0.5
164+ bufferSize = int64 (float64 (mem .Value ()) * 0.5 )
165+ } else {
166+ // RAM > 4gb => buffer size set to RAM * 0.75
167+ bufferSize = int64 (float64 (mem .Value ()) * 0.75 )
168+ }
169+
170+ return bufferSize
171+ }
172+
173+ func min (a , b int64 ) int64 {
174+ if a <= b {
175+ return a
176+ }
177+ return b
178+ }
0 commit comments