Skip to content

Commit

Permalink
feat: select with cluster id
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Aug 12, 2021
1 parent 0465aba commit a6f0558
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 384 deletions.
17 changes: 11 additions & 6 deletions manager/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

const (
CDNNamespace = "cdn"
SchedulerNamespace = "scheduler"
CDNNamespace = "cdn"
SchedulerNamespace = "scheduler"
SchedulersNamespace = "schedulers"
)

type Cache struct {
Expand Down Expand Up @@ -41,10 +42,14 @@ func MakeCacheKey(namespace string, id string) string {
return fmt.Sprintf("manager:%s:%s", namespace, id)
}

func MakeCDNCacheKey(hostname, clusterID string) string {
return MakeCacheKey(CDNNamespace, fmt.Sprintf("%s-%s", hostname, clusterID))
func MakeCDNCacheKey(hostname string, clusterID uint) string {
return MakeCacheKey(CDNNamespace, fmt.Sprintf("%s-%d", hostname, clusterID))
}

func MakeSchedulerCacheKey(hostname, clusterID string) string {
return MakeCacheKey(SchedulerNamespace, fmt.Sprintf("%s-%s", hostname, clusterID))
func MakeSchedulerCacheKey(hostname string, clusterID uint) string {
return MakeCacheKey(SchedulerNamespace, fmt.Sprintf("%s-%d", hostname, clusterID))
}

func MakeSchedulersCacheKey(hostname string) string {
return MakeCacheKey(SchedulersNamespace, hostname)
}
2 changes: 1 addition & 1 deletion manager/model/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type CDN struct {
Port int32 `gorm:"column:port;not null;comment:grpc service listening port" json:"port"`
DownloadPort int32 `gorm:"column:download_port;not null;comment:download service listening port" json:"download_port"`
Status string `gorm:"column:status;type:varchar(256);default:'inactive';comment:service status" json:"status"`
CDNClusterID *uint `gorm:"index:uk_cdn,unique;not null;comment:cdn cluster id"`
CDNClusterID uint `gorm:"index:uk_cdn,unique;not null;comment:cdn cluster id"`
CDNCluster CDNCluster `json:"-"`
}
2 changes: 1 addition & 1 deletion manager/model/cdn_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type CDNCluster struct {
Config JSONMap `gorm:"column:config;not null;comment:configuration" json:"config"`
SchedulerClusters []SchedulerCluster `gorm:"many2many:cdn_cluster_scheduler_cluster;" json:"-"`
CDNs []CDN `json:"-"`
SecurityGroupID *uint `gorm:"comment:security group id"`
SecurityGroupID uint `gorm:"comment:security group id"`
SecurityGroup SecurityGroup `json:"-"`
}
2 changes: 1 addition & 1 deletion manager/model/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ type Scheduler struct {
IP string `gorm:"column:ip;type:varchar(256);not null;comment:ip address" json:"ip"`
Port int32 `gorm:"column:port;not null;comment:grpc service listening port" json:"port"`
Status string `gorm:"column:status;type:varchar(256);default:'inactive';comment:service status" json:"status"`
SchedulerClusterID *uint `gorm:"index:uk_scheduler,unique;not null;comment:scheduler cluster id"`
SchedulerClusterID uint `gorm:"index:uk_scheduler,unique;not null;comment:scheduler cluster id"`
SchedulerCluster SchedulerCluster `json:"-"`
}
2 changes: 1 addition & 1 deletion manager/model/scheduler_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ type SchedulerCluster struct {
IsDefault bool `gorm:"column:is_default;not null;default:false;comment:default scheduler" json:"is_default"`
CDNClusters []CDNCluster `gorm:"many2many:cdn_cluster_scheduler_cluster;" json:"-"`
Schedulers []Scheduler `json:"-"`
SecurityGroupID *uint `gorm:"comment:security group id"`
SecurityGroupID uint `gorm:"comment:security group id"`
SecurityGroup SecurityGroup `json:"-"`
}
4 changes: 2 additions & 2 deletions manager/service/preheat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (s *rest) CreatePreheat(json types.CreatePreheatRequest) (*types.Preheat, e

scheduler := model.Scheduler{}
if err := s.db.First(&scheduler, model.Scheduler{
SchedulerClusterID: &schedulerCluster.ID,
SchedulerClusterID: schedulerCluster.ID,
Status: model.SchedulerStatusActive,
}).Error; err != nil {
return nil, err
Expand All @@ -32,7 +32,7 @@ func (s *rest) CreatePreheat(json types.CreatePreheatRequest) (*types.Preheat, e
for _, schedulerCluster := range schedulerClusters {
scheduler := model.Scheduler{}
if err := s.db.First(&scheduler, model.Scheduler{
SchedulerClusterID: &schedulerCluster.ID,
SchedulerClusterID: schedulerCluster.ID,
Status: model.SchedulerStatusActive,
}).Error; err != nil {
continue
Expand Down
50 changes: 27 additions & 23 deletions manager/service/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

func (s *rest) CreateScheduler(json types.CreateSchedulerRequest) (*model.Scheduler, error) {
scheduler := model.Scheduler{
HostName: json.HostName,
VIPs: json.VIPs,
IDC: json.IDC,
Location: json.Location,
NetConfig: json.NetConfig,
IP: json.IP,
Port: json.Port,
HostName: json.HostName,
VIPs: json.VIPs,
IDC: json.IDC,
Location: json.Location,
NetConfig: json.NetConfig,
IP: json.IP,
Port: json.Port,
SchedulerClusterID: json.SchedulerClusterID,
}

if err := s.db.Create(&scheduler).Error; err != nil {
Expand All @@ -34,12 +35,13 @@ func (s *rest) DestroyScheduler(id uint) error {
func (s *rest) UpdateScheduler(id uint, json types.UpdateSchedulerRequest) (*model.Scheduler, error) {
scheduler := model.Scheduler{}
if err := s.db.First(&scheduler, id).Updates(model.Scheduler{
VIPs: json.VIPs,
IDC: json.IDC,
Location: json.Location,
NetConfig: json.NetConfig,
IP: json.IP,
Port: json.Port,
VIPs: json.VIPs,
IDC: json.IDC,
Location: json.Location,
NetConfig: json.NetConfig,
IP: json.IP,
Port: json.Port,
SchedulerClusterID: json.SchedulerClusterID,
}).Error; err != nil {
return nil, err
}
Expand All @@ -59,11 +61,12 @@ func (s *rest) GetScheduler(id uint) (*model.Scheduler, error) {
func (s *rest) GetSchedulers(q types.GetSchedulersQuery) (*[]model.Scheduler, error) {
schedulers := []model.Scheduler{}
if err := s.db.Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.Scheduler{
HostName: q.HostName,
IDC: q.IDC,
Location: q.Location,
IP: q.IP,
Status: q.Status,
HostName: q.HostName,
IDC: q.IDC,
Location: q.Location,
IP: q.IP,
Status: q.Status,
SchedulerClusterID: q.SchedulerClusterID,
}).Find(&schedulers).Error; err != nil {
return nil, err
}
Expand All @@ -74,11 +77,12 @@ func (s *rest) GetSchedulers(q types.GetSchedulersQuery) (*[]model.Scheduler, er
func (s *rest) SchedulerTotalCount(q types.GetSchedulersQuery) (int64, error) {
var count int64
if err := s.db.Model(&model.Scheduler{}).Where(&model.Scheduler{
HostName: q.HostName,
IDC: q.IDC,
Location: q.Location,
IP: q.IP,
Status: q.Status,
HostName: q.HostName,
IDC: q.IDC,
Location: q.Location,
IP: q.IP,
Status: q.Status,
SchedulerClusterID: q.SchedulerClusterID,
}).Count(&count).Error; err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit a6f0558

Please # to comment.