-
Notifications
You must be signed in to change notification settings - Fork 85
/
calculator.go
54 lines (43 loc) · 1.45 KB
/
calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package disk
import (
"context"
"fmt"
"github.com/filecoin-project/bacalhau/pkg/executor"
"github.com/filecoin-project/bacalhau/pkg/model"
)
type DiskUsageCalculatorParams struct {
Executors executor.ExecutorProvider
}
type DiskUsageCalculator struct {
executors executor.ExecutorProvider
}
func NewDiskUsageCalculator(params DiskUsageCalculatorParams) *DiskUsageCalculator {
return &DiskUsageCalculator{
executors: params.Executors,
}
}
func (c *DiskUsageCalculator) Calculate(
ctx context.Context, job model.Job, parsedUsage model.ResourceUsageData) (model.ResourceUsageData, error) {
requirements := model.ResourceUsageData{}
e, err := c.executors.Get(ctx, job.Spec.Engine)
if err != nil {
return model.ResourceUsageData{}, fmt.Errorf("error getting job disk space requirements: %w", err)
}
var totalDiskRequirements uint64 = 0
for _, input := range job.Spec.Inputs {
volumeSize, err := e.GetVolumeSize(ctx, input)
if err != nil {
return model.ResourceUsageData{}, fmt.Errorf("error getting job disk space requirements: %w", err)
}
totalDiskRequirements += volumeSize
}
// TODO: think about the fact that each shard might be different sizes
// this is probably good enough for now
totalShards := job.Spec.ExecutionPlan.TotalShards
if totalShards == 0 {
totalShards = 1
}
// update the job requirements disk space with what we calculated
requirements.Disk = totalDiskRequirements / uint64(totalShards)
return requirements, nil
}