From 36f18cc42d87205a39e046e2a591ab341a6c153f Mon Sep 17 00:00:00 2001 From: Anisur Rahman Date: Fri, 26 Apr 2024 19:13:19 +0600 Subject: [PATCH 1/4] Implement method for estimating database disk usage Signed-off-by: Anisur Rahman --- mssql/utils.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 mssql/utils.go diff --git a/mssql/utils.go b/mssql/utils.go new file mode 100644 index 000000000..be79a7ca6 --- /dev/null +++ b/mssql/utils.go @@ -0,0 +1,52 @@ +package mssql + +import ( + "context" + "fmt" + "k8s.io/klog/v2" + "strings" + "time" + "xorm.io/xorm" +) + +var ( + diskUsageSafetyFactor = 0.10 // Add 10% + diskUsageDefaultMi = 1024 // 1024 Mi +) + +func (xc *XormClient) GetEstimateDatabaseDiskUsage(dbName string, ctx context.Context) (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer cancel() + + // Create a session from the mssqlClient + session := xc.NewSession().Context(ctx) + defer func(session *xorm.Session) { + err := session.Close() + if err != nil { + klog.Error(err) + } + }(session) + + query := fmt.Sprintf(`USE %s; SELECT + (SELECT SUM(used_log_space_in_bytes) FROM sys.dm_db_log_space_usage) + + (SELECT SUM(allocated_extent_page_count)*8*1024 FROM sys.dm_db_file_space_usage) AS total_size`, quoteName(dbName)) + + var result struct { + TotalSize int64 `xorm:"total_size"` + } + _, err := session.SQL(query).Get(&result) + if err != nil { + return "", fmt.Errorf("failed to execute query: %v", err) + } + + totalDiskUsageInBytesWithExtra := result.TotalSize + int64(diskUsageSafetyFactor*float64(result.TotalSize)) + totalDiskUsageInMib := totalDiskUsageInBytesWithExtra / (1024 * 1024) + if totalDiskUsageInMib < int64(diskUsageDefaultMi) { + totalDiskUsageInMib = int64(diskUsageDefaultMi) + } + return fmt.Sprintf("%dMi", totalDiskUsageInMib), nil +} + +func quoteName(name string) string { + return "[" + strings.ReplaceAll(name, "]", "]]") + "]" +} From 65c9fe232de52501deec79842e7c822242ede1e6 Mon Sep 17 00:00:00 2001 From: Anisur Rahman Date: Fri, 26 Apr 2024 19:32:35 +0600 Subject: [PATCH 2/4] Fix linter Signed-off-by: Anisur Rahman --- mssql/utils.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mssql/utils.go b/mssql/utils.go index be79a7ca6..533fb4a5d 100644 --- a/mssql/utils.go +++ b/mssql/utils.go @@ -1,11 +1,28 @@ +/* +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 mssql import ( "context" "fmt" - "k8s.io/klog/v2" "strings" "time" + + "k8s.io/klog/v2" "xorm.io/xorm" ) @@ -14,7 +31,7 @@ var ( diskUsageDefaultMi = 1024 // 1024 Mi ) -func (xc *XormClient) GetEstimateDatabaseDiskUsage(dbName string, ctx context.Context) (string, error) { +func (xc *XormClient) GetEstimateDatabaseDiskUsage(dbName string) (string, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() From c13ebbd83fa4006f4881984cd37c8c57660cfe22 Mon Sep 17 00:00:00 2001 From: Anisur Rahman Date: Tue, 30 Apr 2024 11:49:20 +0600 Subject: [PATCH 3/4] Add `FetchNonSystemDatabases` method Signed-off-by: Anisur Rahman --- mssql/utils.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/mssql/utils.go b/mssql/utils.go index 533fb4a5d..67548817e 100644 --- a/mssql/utils.go +++ b/mssql/utils.go @@ -19,10 +19,8 @@ package mssql import ( "context" "fmt" - "strings" - "time" - "k8s.io/klog/v2" + "strings" "xorm.io/xorm" ) @@ -31,11 +29,7 @@ var ( diskUsageDefaultMi = 1024 // 1024 Mi ) -func (xc *XormClient) GetEstimateDatabaseDiskUsage(dbName string) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) - defer cancel() - - // Create a session from the mssqlClient +func (xc *XormClient) GetEstimateDatabaseDiskUsage(ctx context.Context, dbName string) (string, error) { session := xc.NewSession().Context(ctx) defer func(session *xorm.Session) { err := session.Close() @@ -67,3 +61,26 @@ func (xc *XormClient) GetEstimateDatabaseDiskUsage(dbName string) (string, error func quoteName(name string) string { return "[" + strings.ReplaceAll(name, "]", "]]") + "]" } + +func (xc *XormClient) FetchNonSystemDatabases(ctx context.Context) ([]string, error) { + // Create a session from the mssqlClient + session := xc.NewSession().Context(ctx) + defer func(session *xorm.Session) { + err := session.Close() + if err != nil { + klog.Error(err) + } + }(session) + + query := "SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb','kubedb_system')" + rows, err := session.Query(query) + if err != nil { + return nil, fmt.Errorf("failed to execute query: %v", err) + } + + var databases []string + for _, row := range rows { + databases = append(databases, string(row["name"])) + } + return databases, nil +} From 57aee0b95b502d7a5b7648d3cf3f8a4b29a3f980 Mon Sep 17 00:00:00 2001 From: Anisur Rahman Date: Tue, 30 Apr 2024 11:54:03 +0600 Subject: [PATCH 4/4] fix linter Signed-off-by: Anisur Rahman --- mssql/utils.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mssql/utils.go b/mssql/utils.go index 67548817e..1c787b138 100644 --- a/mssql/utils.go +++ b/mssql/utils.go @@ -19,8 +19,9 @@ package mssql import ( "context" "fmt" - "k8s.io/klog/v2" "strings" + + "k8s.io/klog/v2" "xorm.io/xorm" )