Skip to content

Commit

Permalink
bugix parse mem
Browse files Browse the repository at this point in the history
  • Loading branch information
johankristianss committed Jan 7, 2024
1 parent 2142c2c commit 7dc5483
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
15 changes: 15 additions & 0 deletions pkg/client/colonies_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ func (client *ColoniesClient) AddExecutor(executor *core.Executor, prvKey string
return core.ConvertJSONToExecutor(respBodyString)
}

func (client *ColoniesClient) ReportAllocation(colonyName string, executorName string, alloc core.Allocations, prvKey string) error {
msg := rpc.CreateReportAllocationsMsg(colonyName, executorName, alloc)
jsonString, err := msg.ToJSON()
if err != nil {
return err
}

_, err = client.sendMessage(rpc.ReportAllocationsPayloadType, jsonString, prvKey, false, context.TODO())
if err != nil {
return err
}

return nil
}

func (client *ColoniesClient) GetExecutors(colonyName string, prvKey string) ([]*core.Executor, error) {
msg := rpc.CreateGetExecutorsMsg(colonyName)
jsonString, err := msg.ToJSON()
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/postgresql/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func (db *PQDatabase) AddProcess(process *core.Process) error {
return err
}

memory, err := parsers.ConvertMemoryToInt(process.FunctionSpec.Conditions.Memory)
memory, err := parsers.ConvertMemoryToBytes(process.FunctionSpec.Conditions.Memory)
if err != nil {
return err
}

storage, err := parsers.ConvertMemoryToInt(process.FunctionSpec.Conditions.Storage)
storage, err := parsers.ConvertMemoryToBytes(process.FunctionSpec.Conditions.Storage)
if err != nil {
return err
}

gpuMem, err := parsers.ConvertMemoryToInt(process.FunctionSpec.Conditions.GPU.Memory)
gpuMem, err := parsers.ConvertMemoryToBytes(process.FunctionSpec.Conditions.GPU.Memory)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/postgresql/processes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ func TestAddProcessConditions(t *testing.T) {
assert.Equal(t, processFromDB.FunctionSpec.Conditions.Processes, 2)
assert.Equal(t, processFromDB.FunctionSpec.Conditions.ProcessesPerNode, 1)
assert.Equal(t, processFromDB.FunctionSpec.Conditions.CPU, "1000m")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.Memory, "10000000Mi")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.Storage, "2000000000Mi")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.Memory, "9536Mi")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.Storage, "1907348Mi")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.WallTime, int64(70))
assert.Equal(t, processFromDB.FunctionSpec.Conditions.GPU.Name, "nvidia_2080ti")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.GPU.Count, 4)
assert.Equal(t, processFromDB.FunctionSpec.Conditions.GPU.Memory, "10000000Mi")
assert.Equal(t, processFromDB.FunctionSpec.Conditions.GPU.Memory, "9536Mi")
}

func TestSelectCandiate(t *testing.T) {
Expand Down
30 changes: 21 additions & 9 deletions pkg/parsers/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ func ConvertCPUToInt(cpu string) (int64, error) {
return value, nil
}

func ConvertMemoryToInt(mem string) (int64, error) {
func ConvertMemoryToBytes(mem string) (int64, error) {
unitMap := map[string]int64{
"Ki": 1024, "KiB": 1024,
"Mi": 1024 * 1024, "MiB": 1024 * 1024,
"Gi": 1024 * 1024 * 1024, "GiB": 1024 * 1024 * 1024,
"K": 1000, "KB": 1000,
"M": 1000 * 1000, "MB": 1000 * 1000,
"G": 1000 * 1000 * 1000, "GB": 1000 * 1000 * 1000,
"Ki": 1024,
"KiB": 1024,
"Mi": 1024 * 1024,
"MiB": 1024 * 1024,
"Gi": 1024 * 1024 * 1024,
"GiB": 1024 * 1024 * 1024,
"TiB": 1024 * 1024 * 1024 * 1024,
"K": 1000,
"KB": 1000,
"Kb": 1000,
"M": 1000 * 1000,
"MB": 1000 * 1000,
"Mb": 1000 * 1000,
"G": 1000 * 1000 * 1000,
"Gb": 1000 * 1000 * 1000,
"TB": 1000 * 1000 * 1000 * 1000,
"Tb": 1000 * 1000 * 1000 * 1000,
}

// Handling edge cases
Expand All @@ -56,7 +67,7 @@ func ConvertMemoryToInt(mem string) (int64, error) {
return 0, fmt.Errorf("no valid unit found in input")
}

mb := num * unitMap[unit] / 1000
mb := num * unitMap[unit]

return mb, nil
}
Expand All @@ -65,6 +76,7 @@ func ConvertCPUToString(cpu int64) string {
return strconv.FormatInt(cpu, 10) + "m"
}

// mem is in bytes
func ConvertMemoryToString(mem int64) string {
return strconv.FormatInt(mem, 10) + "Mi"
return strconv.FormatInt(mem/(1024*1024), 10) + "Mi"
}
2 changes: 1 addition & 1 deletion pkg/parsers/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestConvertMemoryToInt(t *testing.T) {
}

for _, tc := range cases {
result, err := ConvertMemoryToInt(tc.mem)
result, err := ConvertMemoryToBytes(tc.mem)
if tc.expected == -1 {
assert.Error(t, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/process_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (server *ColoniesServer) handleAssignProcessHTTPRequest(c *gin.Context, rec
if msg.AvailableMemory == "" {
memory = math.MaxInt64
} else {
memory, err = parsers.ConvertMemoryToInt(msg.AvailableMemory)
memory, err = parsers.ConvertMemoryToBytes(msg.AvailableMemory)
if server.handleHTTPError(c, err, http.StatusBadRequest) {
return
}
Expand Down

0 comments on commit 7dc5483

Please sign in to comment.