Skip to content

Commit 0cc89aa

Browse files
committed
scheduler default対応
1 parent 7beeb7b commit 0cc89aa

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

pkg/scheduler/scheduler.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func SchedulerReady(cfg *types.SchedulerConfig, ssClient *scoreserver.Client, vm
1919
//ScoreServerのデータを取得し集計する
2020
pis, zps, err := AggregateInstance(pis, zps, ssClient, lg)
2121
if err != nil {
22-
lg.Error(err)
22+
lg.Error("Scheduler Aggregate Error: " + err.Error())
2323
return err
2424
}
2525
//ConfigよりInstanceの作成リストを削除リストを作る
@@ -28,13 +28,13 @@ func SchedulerReady(cfg *types.SchedulerConfig, ssClient *scoreserver.Client, vm
2828
//Instance削除リストから対象Instanceを削除する
2929
err = DeleteScheduler(diList, vmmsClient, lg)
3030
if err != nil {
31-
lg.Error(err)
31+
lg.Error("Scheduler DeleteScheduler: " + err.Error())
3232
return err
3333
}
3434
//Instance作成リストから対象ProblemのInstanceを作成する
3535
err = CreateScheduler(ciList, zps, vmmsClient, lg)
3636
if err != nil {
37-
lg.Error(err)
37+
lg.Error("Scheduler CreateScheduler: " + err.Error())
3838
return err
3939
}
4040
return nil
@@ -45,40 +45,38 @@ func InitSchedulerInfo(cfg *types.SchedulerConfig, lg *zap.Logger) (map[string]*
4545
var pis map[string]*types.ProblemInstance
4646
//Init pis
4747
for _, p := range cfg.Setting.Problems {
48-
pi := types.ProblemInstance{"", "", 0, 0, 0, 0, 0, p.KeepPool, nil, 0}
49-
pis[p.Name] = pi
48+
pis[p.Name] = &types.ProblemInstance{MachineImageName: "", ProblemID: "", NotReady: 0, Ready: 0, UnderChallenge: 0, UnderScoring: 0, Abandoned: 0, KeepPool: p.KeepPool, KIS: []types.KeepInstance{}, CurrentInstance: 0, DefaultInstance: p.DefaultInstance}
5049
}
5150
var zps []types.ZonePriority
5251
//Init zps
5352
for _, p := range cfg.Setting.Projects {
5453
for _, z := range p.Zones {
55-
zp := types.ZonePriority{p.Name, z.Name, z.Priority, z.MaxInstance, 0}
54+
zp := types.ZonePriority{ProjectName: p.Name, ZoneName: z.Name, Priority: z.Priority, MaxInstance: z.MaxInstance, CurrentInstance: 0}
5655
zps = append(zps, zp)
5756
}
5857
}
5958
return pis, zps
6059
}
6160

62-
func AggregateInstance(pis map[string]*types.ProblemInstance, zps []types.ZonePriority, scoreserverClient *scoreserver.scoreserverClien, lg *zap.Logger) (map[string]*types.ProblemInstance, []types.ZonePriority, error) {
61+
func AggregateInstance(pis map[string]*types.ProblemInstance, zps []types.ZonePriority, ssClient *scoreserver.Client, lg *zap.Logger) (map[string]*types.ProblemInstance, []types.ZonePriority, error) {
6362
lg.Info("Scheduler: Aggregate ScoreServer Info")
6463
//ScoreServerからデータを取得
65-
pes, err := scoreserverClient.ListProblemEnvironment()
64+
pes, err := ssClient.ListProblemEnvironment()
6665
if err != nil {
6766
return nil, nil, err
6867
}
6968

70-
for i, p := range pes {
71-
min := strings.Split(p.MachineImageName, "-")
69+
for _, p := range *pes {
70+
min := strings.Split(*p.MachineImageName, "-")
7271
pn := min[0]
73-
pis[pn].MachineImageName = p.MachineImageName
72+
pis[pn].MachineImageName = *p.MachineImageName
7473
pis[pn].ProblemID = p.ProblemID
75-
switch p.InnerStatus {
74+
switch *p.InnerStatus {
7675
case "NOT_READY":
7776
pis[pn].NotReady = pis[pn].NotReady + 1
7877
case "READY":
7978
pis[pn].Ready = pis[pn].Ready + 1
80-
ki := types.KeepInstance{p.InstanceName, p.ProjectName, p.ZoneName, p.CreatedAt}
81-
pis[pn].KIS = append(pis[pn].KIS, ki)
79+
pis[pn].KIS = append(pis[pn].KIS, types.KeepInstance{InstanceName: p.Name, ProjectName: p.ProjectName, ZoneName: p.ZoneName, CreatedAt: p.CreatedAt})
8280
case "UNDER_CHALLENGE":
8381
pis[pn].UnderChallenge = pis[pn].UnderChallenge + 1
8482
case "UNDER_SCORING":
@@ -114,18 +112,22 @@ func (a KIS) Less(i, j int) bool { return a[j].CreatedAt.After(a[i].CreatedAt) }
114112

115113
func SchedulingList(pis map[string]*types.ProblemInstance, lg *zap.Logger) ([]types.CreateInstance, []types.DeleteInstance) {
116114
lg.Info("Scheduler: Create Operation List")
117-
var ciList []types.CreateInstance
118-
var diList []types.DeleteInstance
115+
ciList := []types.CreateInstance{}
116+
diList := []types.DeleteInstance{}
119117
for pn, pi := range pis {
120118
sort.Sort(KIS(pi.KIS))
121119
//問題のReady+NotReady+Abandoned数がKeepInstanceを超えてはいけない。超えてたら削除対象。
120+
//default値以下のinstance数の場合はpoolを消さない
122121
for i := 0; pi.Ready+pi.NotReady+pi.Abandoned > pi.KeepPool; i++ {
123-
diList = append(diList, types.DeleteInstance{pn, pi.KIS[i].InstanceName, pi.KIS[i].ProjectName, pi.KIS[i].ZoneName})
122+
if pi.CurrentInstance < pi.DefaultInstance {
123+
break
124+
}
125+
diList = append(diList, types.DeleteInstance{ProblemName: pn, InstanceName: pi.KIS[i].InstanceName, ProjectName: pi.KIS[i].ProjectName, ZoneName: pi.KIS[i].ZoneName})
124126
pi.KeepPool++
125127
}
126128
//問題のReady+NotReady+Abandoned数がKeepPoolより少ない場合は作成対象にする
127129
for i := 0; pi.Ready+pi.NotReady+pi.Abandoned < pi.KeepPool; i++ {
128-
ciList = append(ciList, types.CreateInstance{pi.ProblemID, pi.MachineImageName, pi.KIS[i].ProjectName, pi.KIS[i].ZoneName})
130+
ciList = append(ciList, types.CreateInstance{ProblemName: pn, ProblemID: pi.ProblemID, MachineImageName: pi.MachineImageName, ProjectName: pi.KIS[i].ProjectName, ZoneName: pi.KIS[i].ZoneName})
129131
pi.NotReady++
130132
}
131133
}
@@ -173,7 +175,7 @@ func CreateScheduler(cis []types.CreateInstance, zps []types.ZonePriority, vmmsC
173175
if err != nil {
174176
break
175177
}
176-
lg.Info("CreateInstance: %s %s", cis[i].ProblemName, ci.InstanceName)
178+
lg.Info("CreateInstance: " + cis[i].ProblemName + " " + ci.InstanceName)
177179
//作れたら次のInstanceの処理に移る
178180
i++
179181
zp.CurrentInstance++

pkg/types/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type ProblemEnvironment struct {
8181
Host string `json:"host"`
8282
User string `json:"user"`
8383
Password string `json:"password"`
84-
ProblemID uuid.UUID `json:"problem_id"`
84+
ProblemID string `json:"problem_id"`
8585
CreatedAt time.Time `json:"created_at"`
8686
UpdatedAt time.Time `json:"updated_at"`
8787
ProjectName string `json:"project"`
@@ -142,6 +142,7 @@ type ProblemInstance struct {
142142
KeepPool int
143143
KIS []KeepInstance
144144
CurrentInstance int
145+
DefaultInstance int
145146
}
146147

147148
type KeepInstance struct {
@@ -164,6 +165,7 @@ type CreateInstance struct {
164165
ProblemID string
165166
MachineImageName string
166167
ProjectName string
168+
ZoneName string
167169
}
168170

169171
type DeleteInstance struct {

0 commit comments

Comments
 (0)