From c6b6692a27d8be1d06c27f1b909e6cba58e32b2c Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 17 Jan 2024 17:07:04 +0800 Subject: [PATCH] scheduler refactor updated3 Former-commit-id: 651ce567abb9ad90e4d037eb9dca493d2fbf1d3a --- pkg/scheduler/database/cloudStorage.go | 22 +++++++++------ pkg/scheduler/database/storage.go | 6 ++-- pkg/scheduler/entity/entity.go | 13 +++++++++ .../strategies/replicationStrategy.go | 28 +++++++++++++++++-- 4 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 pkg/scheduler/entity/entity.go diff --git a/pkg/scheduler/database/cloudStorage.go b/pkg/scheduler/database/cloudStorage.go index 929ba159..d6e0e022 100644 --- a/pkg/scheduler/database/cloudStorage.go +++ b/pkg/scheduler/database/cloudStorage.go @@ -1,6 +1,9 @@ package database -import "gorm.io/gorm" +import ( + "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/entity" + "gorm.io/gorm" +) type CloudStorage struct { dbEngin *gorm.DB @@ -10,8 +13,8 @@ func NewCloudStorage(dbEngin *gorm.DB) *CloudStorage { return &CloudStorage{dbEngin: dbEngin} } -func (c *CloudStorage) GetProviderParams() ([]providerParams, error) { - var proParams []providerParams +func (c *CloudStorage) GetProviderParams() ([]entity.ProviderParams, error) { + var proParams []entity.ProviderParams sqlstr := "SELECT SUM(a.disk_avail) as disk_avail,SUM(a.mem_avail) as mem_avail,SUM(a.cpu_total * a.cpu_usable) as cpu_avail,participant_id from (SELECT * from sc_node_avail_info where created_time in (SELECT MAX(created_time) as time from sc_node_avail_info where deleted_flag = 0 GROUP BY participant_id,node_name)) a GROUP BY a.participant_id" c.dbEngin.Raw(sqlstr).Scan(&proParams) if len(proParams) == 0 { @@ -20,9 +23,12 @@ func (c *CloudStorage) GetProviderParams() ([]providerParams, error) { return proParams, nil } -type providerParams struct { - Disk_avail float64 - Mem_avail float64 - Cpu_avail float64 - Participant_id int64 +func (c *CloudStorage) FindAvailableParticipants() ([]entity.Participant, error) { + var parts []entity.Participant + sqlstr := "select id as participant_id, name as name from sc_participant_phy_info" + c.dbEngin.Raw(sqlstr).Scan(&parts) + if len(parts) == 0 { + return nil, nil + } + return parts, nil } diff --git a/pkg/scheduler/database/storage.go b/pkg/scheduler/database/storage.go index 7c293d77..d2371f59 100644 --- a/pkg/scheduler/database/storage.go +++ b/pkg/scheduler/database/storage.go @@ -1,8 +1,10 @@ package database +import "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/entity" + type Storage interface { - GetProviderParams() ([]providerParams, error) - FindAvailableCluster() + GetProviderParams() ([]entity.ProviderParams, error) + FindAvailableParticipants() ([]entity.Participant, error) } type NSIDSpecified interface { diff --git a/pkg/scheduler/entity/entity.go b/pkg/scheduler/entity/entity.go new file mode 100644 index 00000000..33e48dba --- /dev/null +++ b/pkg/scheduler/entity/entity.go @@ -0,0 +1,13 @@ +package entity + +type ProviderParams struct { + Disk_avail float64 + Mem_avail float64 + Cpu_avail float64 + Participant_id int64 +} + +type Participant struct { + Name string + Participant_id int64 +} diff --git a/pkg/scheduler/strategies/replicationStrategy.go b/pkg/scheduler/strategies/replicationStrategy.go index 569fad69..2a699c5a 100644 --- a/pkg/scheduler/strategies/replicationStrategy.go +++ b/pkg/scheduler/strategies/replicationStrategy.go @@ -1,12 +1,34 @@ package strategies +import ( + "github.com/pkg/errors" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/entity" +) + type ReplicationStrategy struct { + replicas int32 + participants []entity.Participant } -func (ps *ReplicationStrategy) findAvailableCLuster() error { - return nil +func NewReplicationStrategy(participants []entity.Participant, replicas int32) *ReplicationStrategy { + return &ReplicationStrategy{replicas: replicas, + participants: participants, + } } func (ps *ReplicationStrategy) Schedule() ([]*AssignedCluster, error) { - return nil, nil + if ps.replicas < 1 { + return nil, errors.New("replicas must be greater than 0") + } + + if ps.participants == nil { + return nil, errors.New("participantId must be set") + } + + var results []*AssignedCluster + for _, p := range ps.participants { + cluster := &AssignedCluster{ParticipantId: p.Participant_id, Name: p.Name, Replicas: ps.replicas} + results = append(results, cluster) + } + return results, nil }