Skip to content

Commit

Permalink
fix(obcluster): fix get cluster usage api; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI committed Mar 8, 2024
1 parent 97bc109 commit f63fc9d
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 29 deletions.
59 changes: 33 additions & 26 deletions internal/dashboard/business/oceanbase/obcluster_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
httpErr "github.com/oceanbase/ob-operator/pkg/errors"
"github.com/oceanbase/ob-operator/pkg/k8s/client"
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/connector"
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/model"
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/operation"
)

Expand Down Expand Up @@ -72,41 +73,47 @@ func GetOBClusterEssentialParameters(ctx context.Context, nn *param.K8sObjectIde
essentials := &response.OBClusterEssentialParameters{
MinPoolMemory: minPoolMemory.Value(),
}
zoneMapping := make(map[string]response.OBZoneAvaiableResource)
gvservers, err := manager.ListGVServers()
if err != nil {
return nil, httpErr.NewInternal(err.Error())
}
serverUsages := make([]*response.OBServerAvailableResource, 0, len(gvservers))
serverUsages, zoneMapping := getServerUsages(gvservers)
essentials.OBServerResources = serverUsages
essentials.OBZoneResourceMap = zoneMapping
return essentials, nil
}

func getServerUsages(gvservers []model.GVOBServer) ([]response.OBServerAvailableResource, map[string]*response.OBZoneAvaiableResource) {
zoneMapping := make(map[string]*response.OBZoneAvaiableResource)
serverUsages := make([]response.OBServerAvailableResource, 0, len(gvservers))
for _, gvserver := range gvservers {
zoneResource := &response.OBZoneAvaiableResource{
OBZone: gvserver.Zone,
AvailableCPU: gvserver.CPUCapacity - gvserver.CPUAssigned,
AvailableMemory: gvserver.MemCapacity - gvserver.MemAssigned,
AvailableLogDisk: gvserver.LogDiskCapacity - gvserver.LogDiskAssigned,
AvailableDataDisk: gvserver.DataDiskCapacity - gvserver.DataDiskAllocated,
}
if _, ok := zoneMapping[gvserver.Zone]; !ok {
zoneMapping[gvserver.Zone] = response.OBZoneAvaiableResource{
OBZone: gvserver.Zone,
AvailableCPU: gvserver.CPUCapacity - gvserver.CPUAssigned,
AvailableMemory: gvserver.MemCapacity - gvserver.MemAssigned,
AvailableLogDisk: gvserver.LogDiskCapacity - gvserver.LogDiskAssigned,
AvailableDataDisk: gvserver.DataDiskCapacity - gvserver.DataDiskAllocated,
}
zoneMapping[gvserver.Zone] = zoneResource
}
serverUsage := &response.OBServerAvailableResource{
OBServerIP: gvserver.ServerIP,
OBZoneAvaiableResource: response.OBZoneAvaiableResource{
OBZone: gvserver.Zone,
AvailableCPU: gvserver.CPUCapacity - gvserver.CPUAssigned,
AvailableMemory: gvserver.MemCapacity - gvserver.MemAssigned,
AvailableLogDisk: gvserver.LogDiskCapacity - gvserver.LogDiskAssigned,
AvailableDataDisk: gvserver.DataDiskCapacity - gvserver.DataDiskAllocated,
},
serverUsage := response.OBServerAvailableResource{
OBServerIP: gvserver.ServerIP,
OBZoneAvaiableResource: *zoneResource,
}
if zoneMapping[gvserver.Zone].AvailableCPU > serverUsage.AvailableCPU ||
zoneMapping[gvserver.Zone].AvailableMemory > serverUsage.AvailableMemory ||
zoneMapping[gvserver.Zone].AvailableLogDisk > serverUsage.AvailableLogDisk ||
zoneMapping[gvserver.Zone].AvailableDataDisk > serverUsage.AvailableDataDisk {
zoneMapping[gvserver.Zone] = serverUsage.OBZoneAvaiableResource
if zoneMapping[gvserver.Zone].AvailableCPU > serverUsage.AvailableCPU {
zoneMapping[gvserver.Zone].AvailableCPU = serverUsage.AvailableCPU
}
if zoneMapping[gvserver.Zone].AvailableMemory > serverUsage.AvailableMemory {
zoneMapping[gvserver.Zone].AvailableMemory = serverUsage.AvailableMemory
}
if zoneMapping[gvserver.Zone].AvailableLogDisk > serverUsage.AvailableLogDisk {
zoneMapping[gvserver.Zone].AvailableLogDisk = serverUsage.AvailableLogDisk
}
if zoneMapping[gvserver.Zone].AvailableDataDisk > serverUsage.AvailableDataDisk {
zoneMapping[gvserver.Zone].AvailableDataDisk = serverUsage.AvailableDataDisk
}
serverUsages = append(serverUsages, serverUsage)
}
essentials.OBServerResources = serverUsages
essentials.OBZoneResourceMap = zoneMapping
return essentials, nil
return serverUsages, zoneMapping
}
97 changes: 97 additions & 0 deletions internal/dashboard/business/oceanbase/obcluster_usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright (c) 2023 OceanBase
ob-operator is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package oceanbase

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/model"
)

func getMockedGVOBServers() []model.GVOBServer {
return []model.GVOBServer{{
ServerIP: "1",
Zone: "zone1",
CPUCapacity: 16,
CPUAssigned: 3,
MemCapacity: 16,
MemAssigned: 3,
MemoryLimit: 32,
LogDiskCapacity: 100,
LogDiskAssigned: 40,
DataDiskCapacity: 100,
DataDiskAllocated: 60,
}, {
ServerIP: "2",
Zone: "zone1",
CPUCapacity: 16,
CPUAssigned: 10, // max in zone1
MemCapacity: 16,
MemAssigned: 3,
MemoryLimit: 32,
LogDiskCapacity: 100,
LogDiskAssigned: 40,
DataDiskCapacity: 100,
DataDiskAllocated: 60,
}, {
ServerIP: "3",
Zone: "zone1",
CPUCapacity: 16,
CPUAssigned: 3,
MemCapacity: 16,
MemAssigned: 12, // max in zone1
MemoryLimit: 32,
LogDiskCapacity: 100,
LogDiskAssigned: 40,
DataDiskCapacity: 100,
DataDiskAllocated: 60,
}, {
ServerIP: "4",
Zone: "zone2",
CPUCapacity: 16,
CPUAssigned: 3,
MemCapacity: 16,
MemAssigned: 5,
MemoryLimit: 32,
LogDiskCapacity: 100,
LogDiskAssigned: 40,
DataDiskCapacity: 100,
DataDiskAllocated: 60,
}}
}

var _ = Describe("Test OBClsuter usage", func() {
It("Get observer usages", func() {
servers, zoneMapping := getServerUsages(getMockedGVOBServers())
Expect(servers).To(HaveLen(4))
Expect(servers[0].AvailableCPU).To(Equal(int64(13)))
Expect(servers[0].AvailableMemory).To(Equal(int64(13)))
Expect(servers[1].AvailableCPU).To(Equal(int64(6)))
Expect(servers[1].AvailableMemory).To(Equal(int64(13)))
Expect(servers[2].AvailableCPU).To(Equal(int64(13)))
Expect(servers[2].AvailableMemory).To(Equal(int64(4)))

Expect(zoneMapping).To(HaveLen(2))
Expect(zoneMapping).To(HaveKey("zone1"))
Expect(zoneMapping).To(HaveKey("zone2"))
Expect(zoneMapping["zone1"].AvailableCPU).To(Equal(int64(6)))
Expect(zoneMapping["zone1"].AvailableMemory).To(Equal(int64(4)))
Expect(zoneMapping["zone1"].AvailableDataDisk).To(Equal(int64(40)))
Expect(zoneMapping["zone1"].AvailableLogDisk).To(Equal(int64(60)))
Expect(zoneMapping["zone2"].AvailableCPU).To(Equal(int64(13)))
Expect(zoneMapping["zone2"].AvailableMemory).To(Equal(int64(11)))
Expect(zoneMapping["zone2"].AvailableDataDisk).To(Equal(int64(40)))
Expect(zoneMapping["zone2"].AvailableLogDisk).To(Equal(int64(60)))
})
})
6 changes: 3 additions & 3 deletions internal/dashboard/model/response/obcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ type NFSVolumeSpec struct {
}

type OBClusterEssentialParameters struct {
MinPoolMemory int64 `json:"minPoolMemory" example:"2147483648"`
OBServerResources []*OBServerAvailableResource `json:"obServerResources"`
OBZoneResourceMap map[string]OBZoneAvaiableResource `json:"obZoneResourceMap"`
MinPoolMemory int64 `json:"minPoolMemory" example:"2147483648"`
OBServerResources []OBServerAvailableResource `json:"obServerResources"`
OBZoneResourceMap map[string]*OBZoneAvaiableResource `json:"obZoneResourceMap"`
}

type OBServerAvailableResource struct {
Expand Down

0 comments on commit f63fc9d

Please sign in to comment.