|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/serverledge-faas/serverledge/internal/config" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +type HEFTlessPolicy struct{} |
| 13 | + |
| 14 | +func (policy *HEFTlessPolicy) Evaluate(r *Request, p *Progress) (OffloadingDecision, error) { |
| 15 | + |
| 16 | + completed := 0 |
| 17 | + |
| 18 | + if p == nil || !r.CanDoOffloading || len(p.ReadyToExecute) == 0 { |
| 19 | + return OffloadingDecision{Offload: false}, nil |
| 20 | + } |
| 21 | + |
| 22 | + for _, s := range p.Status { |
| 23 | + if s == Executed { |
| 24 | + completed++ |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if completed > 0 { |
| 29 | + placement, found := getCachedSolution(r) |
| 30 | + if found { |
| 31 | + log.Printf("Reusing cached placement\n") |
| 32 | + return computeDecisionFromPlacement(*placement, p, r), nil |
| 33 | + } else { |
| 34 | + // No rescheduling admitted |
| 35 | + return OffloadingDecision{Offload: false}, fmt.Errorf("HEFTless does not support re-scheduling during execution") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + params := prepareParameters(r, p) |
| 40 | + |
| 41 | + // Serialize to JSON |
| 42 | + jsonData, err := json.Marshal(params) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + // Create POST request |
| 48 | + optimizerHost := config.GetString(config.OFFLOADING_POLICY_OPTIMIZER_HOST, "localhost") |
| 49 | + optimizerPort := config.GetInt(config.OFFLOADING_POLICY_OPTIMIZER_PORT, 8080) |
| 50 | + url := fmt.Sprintf("http://%s:%d/heftless", optimizerHost, optimizerPort) |
| 51 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) |
| 52 | + if err != nil { |
| 53 | + return OffloadingDecision{Offload: false}, fmt.Errorf("creating request: %w", err) |
| 54 | + } |
| 55 | + req.Header.Set("Content-Type", "application/json") |
| 56 | + |
| 57 | + // Send the request |
| 58 | + resp, err := httpClient.Do(req) |
| 59 | + if err != nil { |
| 60 | + fmt.Println(err) |
| 61 | + return OffloadingDecision{Offload: false}, fmt.Errorf("sending request: %w", err) |
| 62 | + } |
| 63 | + defer resp.Body.Close() |
| 64 | + |
| 65 | + statusCode := resp.StatusCode |
| 66 | + if statusCode != 200 { |
| 67 | + return OffloadingDecision{Offload: false}, fmt.Errorf("scheduling failed with status code %d", statusCode) |
| 68 | + } |
| 69 | + |
| 70 | + // Read and print response |
| 71 | + var placement taskPlacement |
| 72 | + err = json.NewDecoder(resp.Body).Decode(&placement) |
| 73 | + if err != nil { |
| 74 | + fmt.Println(err) |
| 75 | + return OffloadingDecision{Offload: false}, fmt.Errorf("decoding response: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + cacheSolution(r, &placement, 9999) |
| 79 | + |
| 80 | + for k, v := range placement { |
| 81 | + fmt.Printf("Task: %s -> %s \n", k, v) |
| 82 | + } |
| 83 | + |
| 84 | + // parse results and make a decision |
| 85 | + return computeDecisionFromPlacement(placement, p, r), nil |
| 86 | +} |
0 commit comments