Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Fields, Constant, Func For Ops Request Postgres (#758)
Signed-off-by: Emon46 <emon@appscode.com>
  • Loading branch information
Emon46 committed Jun 5, 2021
1 parent 722656b commit 98cd75f
Show file tree
Hide file tree
Showing 18 changed files with 1,408 additions and 17,794 deletions.
10 changes: 9 additions & 1 deletion apis/kubedb/v1alpha2/constants.go
Expand Up @@ -237,6 +237,15 @@ const (
PostgresKeyFileSecretSuffix = "key"
PostgresPEMSecretSuffix = "pem"
PostgresDefaultUsername = "postgres"
PostgresPgCoordinatorStatus = "Coordinator/Status"
// to pause the failover for postgres. this is helpful for ops request
PostgresPgCoordinatorStatusPause = "Pause"
// to resume the failover for postgres. this is helpful for ops request
PostgresPgCoordinatorStatusResume = "Resume"

// when we need to resume pg-coordinator as non transferable we are going to set this state.
// this is useful when we have set a node as primary and you don't want other node rather then this node to become primary.
PostgresPgCoordinatorStatusResumeNonTransferable = "NonTransferableResume"

// =========================== ProxySQL Constants ============================
LabelProxySQLName = ProxySQLKey + "/name"
Expand All @@ -249,7 +258,6 @@ const (
ProxySQLAdminPortName = "admin"
ProxySQLDataMountPath = "/var/lib/proxysql"
ProxySQLCustomConfigMountPath = "/etc/custom-config"

// =========================== Redis Constants ============================
RedisShardKey = RedisKey + "/shard"
RedisDatabasePortName = "db"
Expand Down
62 changes: 62 additions & 0 deletions apis/kubedb/v1alpha2/postgres_helpers.go
Expand Up @@ -18,6 +18,8 @@ package v1alpha2

import (
"fmt"
"math"
"strconv"
"time"

"kubedb.dev/apimachinery/apis"
Expand All @@ -27,6 +29,7 @@ import (

"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
appslister "k8s.io/client-go/listers/apps/v1"
Expand Down Expand Up @@ -299,3 +302,62 @@ func (p *Postgres) GetCertSecretName(alias PostgresCertificateAlias) string {
}
return p.CertificateName(alias)
}

// GetSharedBufferSizeForPostgres this func takes a input type int64 which is in bytes
// return the 25% of the input in Bytes, KiloBytes, MegaBytes, GigaBytes, or TeraBytes
func GetSharedBufferSizeForPostgres(resource *resource.Quantity) string {
// no more than 25% of main memory (RAM)
minSharedBuffer := int64(128 * 1024 * 1024)
ret := minSharedBuffer
if resource != nil {
ret = (resource.Value() / 100) * 25
}
// the shared buffer value can't be less then this
//128 MB is the minimum
if ret < minSharedBuffer {
ret = minSharedBuffer
}

sharedBuffer := ConvertBytesInMB(ret)
return sharedBuffer
}

func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
// this func take a float and return the int and fractional part separately
// math.modf(100.4) will return int part = 100 and fractional part = 0.40000000000000000
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return newVal
}

// ConvertBytesInMB this func takes a input type int64 which is in bytes
// return the input in Bytes, KiloBytes, MegaBytes, GigaBytes, or TeraBytes
func ConvertBytesInMB(value int64) string {
var suffixes [5]string
suffixes[0] = "B"
suffixes[1] = "KB"
suffixes[2] = "MB"
suffixes[3] = "GB"
suffixes[4] = "TB"

// here base is the type we are going to represent the value in string
// if base is 2 then we will represent the value in MB.
// if base is 0 then represent the value in B.
if value == 0 {
return "0B"
}
base := math.Log(float64(value)) / math.Log(1024)
getSize := Round(math.Pow(1024, base-math.Floor(base)), .5, 2)
getSuffix := suffixes[int(math.Floor(base))]

valueMB := strconv.FormatFloat(getSize, 'f', -1, 64) + string(getSuffix)
return valueMB
}
141 changes: 141 additions & 0 deletions apis/kubedb/v1alpha2/postgres_helpers_test.go
@@ -0,0 +1,141 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha2

import (
"testing"

"k8s.io/apimachinery/pkg/api/resource"
)

func TestRound(t *testing.T) {
type args struct {
val float64
roundOn float64
places int
}
tests := []struct {
name string
args args
wantNewVal float64
}{
{
name: "1st test",
args: args{
val: 1.666,
roundOn: .4,
places: 2,
},
wantNewVal: 1.67,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotNewVal := Round(tt.args.val, tt.args.roundOn, tt.args.places); gotNewVal != tt.wantNewVal {
t.Errorf("Round() = %v, want %v", gotNewVal, tt.wantNewVal)
}
})
}
}

func TestConvertBytesInMB(t *testing.T) {
type args struct {
value int64
}
tests := []struct {
name string
args args
want string
}{
{
name: "1st",
args: args{
value: 0,
},
want: "0B",
},
{
name: "2nd",
args: args{
value: 1,
},
want: "1B",
},
{
name: "3rd",
args: args{
value: 10245,
},
want: "10KB",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertBytesInMB(tt.args.value); got != tt.want {
t.Errorf("ConvertBytesInMB() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetSharedBufferSizeForPostgres(t *testing.T) {
type args struct {
resource *resource.Quantity
}
tests := []struct {
name string
args args
want string
}{
{
name: "1st",
args: args{
// 10GB
resource: resource.NewQuantity(int64(1024*1024*1024*10), resource.DecimalSI),
},
want: "2.5GB",
},
{
name: "2nd",
args: args{
resource: resource.NewQuantity(int64(1024*1024*1024), resource.DecimalSI),
},
want: "256MB",
},
{
name: "3rd",
args: args{
resource: resource.NewQuantity(int64(1024*1024), resource.DecimalSI),
},
want: "128MB",
},
{
name: "4th",
args: args{
resource: nil,
},
want: "128MB",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetSharedBufferSizeForPostgres(tt.args.resource); got != tt.want {
t.Errorf("GetSharedBufferSizeForPostgres() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 98cd75f

Please sign in to comment.