From 7d33c85a1a23e01926b589d28f630978e32607e3 Mon Sep 17 00:00:00 2001 From: Newnius Date: Sat, 11 Jul 2020 14:04:20 +0800 Subject: [PATCH] update api iterface --- src/allocator.go | 9 ++- src/configuration.go | 14 +--- src/main.go | 164 ++++++++++++++++---------------------- src/message.go | 11 +++ src/resource_pool.go | 12 +-- src/scheduler.go | 4 +- src/scheduler_FCFS.go | 10 +-- src/scheduler_capacity.go | 12 +-- src/scheduler_fair.go | 12 +-- src/scheduler_priority.go | 10 +-- 10 files changed, 106 insertions(+), 152 deletions(-) diff --git a/src/allocator.go b/src/allocator.go index 8bacc70..236d5c9 100644 --- a/src/allocator.go +++ b/src/allocator.go @@ -31,9 +31,12 @@ func (allocator *Allocator) init(conf Configuration) { } func (allocator *Allocator) updateStrategy(strategy string) bool { - allocator.allocationStrategy = strategy - log.Info("Allocator strategy switched to ", strategy) - return true + if strategy == "bestfit" || strategy == "ga" || strategy == "mixed" { + allocator.allocationStrategy = strategy + log.Info("Allocator strategy switched to ", strategy) + return true + } + return false } func (allocator *Allocator) allocate(nodes []NodeStatus, tasks []Task) Allocation { diff --git a/src/configuration.go b/src/configuration.go index 02ef524..6dfdf1b 100644 --- a/src/configuration.go +++ b/src/configuration.go @@ -117,19 +117,11 @@ func InstanceOfConfiguration() *Configuration { return configurationInstance } -func (config *Configuration) EnableMock() bool { +func (config *Configuration) SetMockEnabled(enabled bool) bool { config.mu.Lock() defer config.mu.Unlock() - config.mock = true - log.Info("configuration.mock = true") - return true -} - -func (config *Configuration) DisableMock() bool { - config.mu.Lock() - defer config.mu.Unlock() - config.mock = false - log.Info("configuration.mock = false") + config.mock = enabled + log.Info("configuration.mock = ", enabled) return true } diff --git a/src/main.go b/src/main.go index 25be703..17b6b39 100644 --- a/src/main.go +++ b/src/main.go @@ -250,20 +250,6 @@ func serverAPI(w http.ResponseWriter, r *http.Request) { w.Write(js) break - case "debug_enable": - log.Debug("enable schedule") - js, _ := json.Marshal(scheduler.Enable()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "debug_disable": - log.Debug("disable schedule") - js, _ := json.Marshal(scheduler.Disable()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - case "debug_scheduler_dump": log.Debug("debug_scheduler_dump") js, _ := json.Marshal(scheduler.DebugDump()) @@ -271,67 +257,6 @@ func serverAPI(w http.ResponseWriter, r *http.Request) { w.Write(js) break - case "debug_update_parallelism": - log.Debug("update_parallelism") - parallelism, _ := strconv.Atoi(r.URL.Query().Get("parallelism")) - js, _ := json.Marshal(scheduler.UpdateParallelism(parallelism)) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "debug_update_enable_share_ratio": - log.Debug("debug_update_enable_share_ratio") - - ratio := 0.75 - if t, err := strconv.ParseFloat(r.URL.Query().Get("ratio"), 32); err == nil { - ratio = t - } - js, _ := json.Marshal(InstanceOfConfiguration().SetShareRatio(ratio)) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "debug_update_enable_pre_schedule_ratio": - log.Debug("debug_update_enable_pre_schedule_ratio") - ratio := 0.95 - if t, err := strconv.ParseFloat(r.URL.Query().Get("ratio"), 32); err == nil { - ratio = t - } - js, _ := json.Marshal(InstanceOfConfiguration().SetPreScheduleRatio(ratio)) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "allocator_update_strategy": - log.Debug("allocator_update_strategy") - strategy := r.URL.Query().Get("strategy") - js, _ := json.Marshal(InstanceOfAllocator().updateStrategy(strategy)) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "pool_enable_batch": - log.Debug("pool_enable_batch") - js, _ := json.Marshal(InstanceOfResourcePool().EnableBatch()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "pool_disable_batch": - log.Debug("pool_disable_batch") - js, _ := json.Marshal(InstanceOfResourcePool().DisableBatch()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - - case "pool_set_batch_interval": - log.Debug("pool_set_batch_interval") - interval := str2int(r.URL.Query().Get("interval"), 1) - js, _ := json.Marshal(InstanceOfResourcePool().SetBatchInterval(interval)) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break - case "debug_pool_dump": log.Debug("debug_pool_dump") js, _ := json.Marshal(InstanceOfResourcePool().Dump()) @@ -339,31 +264,82 @@ func serverAPI(w http.ResponseWriter, r *http.Request) { w.Write(js) break - case "debug_enable_mock": - log.Debug("debug_enable_mock") - js, _ := json.Marshal(InstanceOfConfiguration().EnableMock()) + case "conf_list": + log.Debug("conf_list") + var msg MsgConfList + msg.Code = 0 + msg.Options = InstanceOfConfiguration().Dump() + js, _ := json.Marshal(msg) w.Header().Set("Content-Type", "application/json") w.Write(js) break - case "debug_disable_mock": - log.Debug("debug_disable_mock") - js, _ := json.Marshal(InstanceOfConfiguration().DisableMock()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break + case "conf_update": + log.Debug("conf_update") + option := r.URL.Query().Get("option") + value := r.URL.Query().Get("value") + ok := false + switch option { + /* pool.share */ + case "pool.share.enable_threshold": + if threshold, err := strconv.ParseFloat(value, 32); err == nil { + ok = InstanceOfConfiguration().SetShareRatio(threshold) + } + break - case "debug_conf_dump": - log.Debug("debug_conf_dump") - js, _ := json.Marshal(InstanceOfConfiguration().Dump()) - w.Header().Set("Content-Type", "application/json") - w.Write(js) - break + case "pool.share.max_utilization": + util, err := strconv.ParseFloat(value, 32) + if err == nil { + ok = InstanceOfConfiguration().SetShareMaxUtilization(util) + } + break - case "conf_set_share_max_utilization": - log.Debug("conf_set_share_max_utilization") - util, _ := strconv.ParseFloat(r.URL.Query().Get("util"), 32) - js, _ := json.Marshal(InstanceOfConfiguration().SetShareMaxUtilization(util)) + /* pool.pre_schedule */ + case "pool.pre_schedule.enable_threshold": + if threshold, err := strconv.ParseFloat(value, 32); err == nil { + ok = InstanceOfConfiguration().SetPreScheduleRatio(threshold) + } + break + + /* pool.batch */ + case "pool.batch.enabled": + ok = InstanceOfResourcePool().SetBatchEnabled(value == "true") + break + + case "pool.batch.interval": + interval := str2int(value, 1) + ok = InstanceOfResourcePool().SetBatchInterval(interval) + break + + /* scheduler.mock */ + case "scheduler.mock.enabled": + ok = InstanceOfConfiguration().SetMockEnabled(value == "true") + break + + /* scheduler.enabled */ + case "scheduler.enabled": + ok = scheduler.SetEnabled(value == "true") + break + + /* scheduler.parallelism */ + case "scheduler.parallelism": + parallelism, _ := strconv.Atoi(value) + ok = scheduler.UpdateParallelism(parallelism) + break + + /* allocator.strategy */ + case "allocator.strategy": + ok = InstanceOfAllocator().updateStrategy(value) + break + + } + var msg MsgConfUpdate + msg.Code = 0 + if !ok { + msg.Code = 1 + msg.Error = "Option not exist or invalid value" + } + js, _ := json.Marshal(msg) w.Header().Set("Content-Type", "application/json") w.Write(js) break diff --git a/src/message.go b/src/message.go index 113d31c..03ec65d 100644 --- a/src/message.go +++ b/src/message.go @@ -103,3 +103,14 @@ type MsgJobReqPredict struct { Error string `json:"error"` Labels map[string]float64 `json:"labels"` } + +type MsgConfUpdate struct { + Code int `json:"code"` + Error string `json:"error"` +} + +type MsgConfList struct { + Code int `json:"code"` + Error string `json:"error"` + Options map[string]interface{} `json:"options"` +} diff --git a/src/resource_pool.go b/src/resource_pool.go index 035b083..e4d43ad 100644 --- a/src/resource_pool.go +++ b/src/resource_pool.go @@ -1073,15 +1073,9 @@ func (pool *ResourcePool) releaseResource(job Job, agent NodeStatus) { } } -func (pool *ResourcePool) EnableBatch() bool { - pool.enableBatch = true - log.Info("enableBatch is set to true") - return true -} - -func (pool *ResourcePool) DisableBatch() bool { - pool.enableBatch = false - log.Info("enableBatch is set to false") +func (pool *ResourcePool) SetBatchEnabled(enabled bool) bool { + pool.enableBatch = enabled + log.Info("enableBatch is set to ", enabled) return true } diff --git a/src/scheduler.go b/src/scheduler.go index 1fd68d7..f64ea50 100644 --- a/src/scheduler.go +++ b/src/scheduler.go @@ -21,9 +21,7 @@ type Scheduler interface { Summary() MsgSummary - Enable() bool - - Disable() bool + SetEnabled(enabled bool) bool UpdateParallelism(parallelism int) bool diff --git a/src/scheduler_FCFS.go b/src/scheduler_FCFS.go index 9a023e4..57b9c44 100644 --- a/src/scheduler_FCFS.go +++ b/src/scheduler_FCFS.go @@ -167,13 +167,9 @@ func (scheduler *SchedulerFCFS) Summary() MsgSummary { return summary } -func (scheduler *SchedulerFCFS) Enable() bool { - scheduler.enabled = true - return true -} - -func (scheduler *SchedulerFCFS) Disable() bool { - scheduler.enabled = false +func (scheduler *SchedulerFCFS) SetEnabled(enabled bool) bool { + scheduler.enabled = enabled + log.Info("scheduler is set to ", enabled) return true } diff --git a/src/scheduler_capacity.go b/src/scheduler_capacity.go index 763eb86..7bbac40 100644 --- a/src/scheduler_capacity.go +++ b/src/scheduler_capacity.go @@ -381,15 +381,9 @@ func (scheduler *SchedulerCapacity) UpdateNextQueue() { log.Debug("updateNextQueue ->", next) } -func (scheduler *SchedulerCapacity) Enable() bool { - scheduler.enabled = true - log.Info("scheduler is enabled ", time.Now()) - return true -} - -func (scheduler *SchedulerCapacity) Disable() bool { - scheduler.enabled = false - log.Info("scheduler is disabled ", time.Now()) +func (scheduler *SchedulerCapacity) SetEnabled(enabled bool) bool { + scheduler.enabled = enabled + log.Info("scheduler is set to ", enabled) return true } diff --git a/src/scheduler_fair.go b/src/scheduler_fair.go index 87caddf..1485f53 100644 --- a/src/scheduler_fair.go +++ b/src/scheduler_fair.go @@ -796,15 +796,9 @@ func (scheduler *SchedulerFair) Summary() MsgSummary { return summary } -func (scheduler *SchedulerFair) Enable() bool { - scheduler.enabled = true - log.Info("scheduler is enabled ", time.Now()) - return true -} - -func (scheduler *SchedulerFair) Disable() bool { - scheduler.enabled = false - log.Info("scheduler is disabled ", time.Now()) +func (scheduler *SchedulerFair) SetEnabled(enabled bool) bool { + scheduler.enabled = enabled + log.Info("scheduler is set to ", enabled) return true } diff --git a/src/scheduler_priority.go b/src/scheduler_priority.go index 8cafd4d..4b38b57 100644 --- a/src/scheduler_priority.go +++ b/src/scheduler_priority.go @@ -311,13 +311,9 @@ func (scheduler *SchedulerPriority) Summary() MsgSummary { return summary } -func (scheduler *SchedulerPriority) Enable() bool { - scheduler.enabled = true - return true -} - -func (scheduler *SchedulerPriority) Disable() bool { - scheduler.enabled = false +func (scheduler *SchedulerPriority) SetEnabled(enabled bool) bool { + scheduler.enabled = enabled + log.Info("scheduler is set to ", enabled) return true }