Skip to content

Commit

Permalink
fix: merged resource limit is affected by remaining in the resource g…
Browse files Browse the repository at this point in the history
…roup. (#2696)

### TL;DR

Fixed and refactored resource limit and remaining calculations in useResourceLimitAndRemaining hook.

This PR resolves the issue where the merged resource limit in useResourceLimitAndRemaining is affected by the remaining in the resource group.

Now, the maximum value is determined as the minimum value of the following:

- pre-container configuration (e.g., maxCPUCoresPerContainer)
- keypair resource limit
- group resource limit

The useResourceLimitAndRemaining should consider the domain resource limit and the total slot size of the resource group. This will be handled by another PR.

### What changed?

- Updated ResourceLimits type to use ResourceSlotName as key
- Commented out resourceGroupResourceSize calculations for cpu and mem
- Added keypair and group limits for accelerators
- Updated type assertions in remaining resource calculations

### How to test?

1. Test resource allocation functionality in the UI
2. Verify that resource limits are correctly applied for different resource types (CPU, memory, accelerators)
3. Check that remaining resources are calculated accurately
4. Ensure that the changes don't introduce any regressions in resource management
  • Loading branch information
yomybaby committed Sep 9, 2024
1 parent bacb099 commit 40b6062
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions react/src/hooks/useResourceLimitAndRemaining.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSuspendedBackendaiClient } from '.';
import { Image } from '../components/ImageEnvironmentSelectFormItems';
import { AUTOMATIC_DEFAULT_SHMEM } from '../components/ResourceAllocationFormItems';
import { addNumberWithUnits, iSizeToSize } from '../helper';
import { useResourceSlots } from '../hooks/backendai';
import { ResourceSlotName, useResourceSlots } from '../hooks/backendai';
import { useSuspenseTanQuery } from './reactQueryAlias';
import _ from 'lodash';
import { useMemo } from 'react';
Expand Down Expand Up @@ -47,9 +47,7 @@ export interface MergedResourceLimits {
}

type ResourceLimits = {
cpu: string | 'Infinity' | 'NaN';
mem: string | 'Infinity' | 'NaN';
'cuda.device': string | 'Infinity' | 'NaN';
[key in ResourceSlotName]?: string | 'Infinity' | 'NaN';
};
type ResourceUsing = ResourceLimits;
type ResourceRemaining = ResourceLimits;
Expand Down Expand Up @@ -237,7 +235,7 @@ export const useResourceLimitAndRemaining = ({
: baiClient._config.maxCPUCoresPerContainer,
limitParser(checkPresetInfo?.keypair_limits.cpu),
limitParser(checkPresetInfo?.group_limits.cpu),
resourceGroupResourceSize?.cpu,
// resourceGroupResourceSize?.cpu,
]),
},
mem:
Expand Down Expand Up @@ -273,20 +271,10 @@ export const useResourceLimitAndRemaining = ({
'g',
)?.number,
// scaling group all mem (using + remaining), string type
resourceGroupResourceSize?.mem &&
iSizeToSize(resourceGroupResourceSize?.mem + '', 'g')?.number,
// resourceGroupResourceSize?.mem &&
// iSizeToSize(resourceGroupResourceSize?.mem + '', 'g')?.number,
]) + 'g',
},
// shmem:
// resourceSlots?.mem === undefined
// ? undefined
// : {
// min: _.max([
// _.find(currentImage?.resource_limits, (i) => i?.key === 'shmem')
// ?.min,
// '64m',
// ]),
// },
accelerators: _.reduce(
acceleratorSlots,
(result, value, key) => {
Expand All @@ -306,8 +294,12 @@ export const useResourceLimitAndRemaining = ({
),
max: _.min([
perContainerLimit || 8,
limitParser(
checkPresetInfo?.keypair_limits[key as ResourceSlotName],
),
limitParser(checkPresetInfo?.group_limits[key as ResourceSlotName]),
// scaling group all cpu (using + remaining), string type
resourceGroupResourceSize.accelerators[key],
// resourceGroupResourceSize.accelerators[key],
]),
};
return result;
Expand All @@ -321,12 +313,15 @@ export const useResourceLimitAndRemaining = ({
(result, value, key) => {
result[key] =
_.min([
// @ts-ignore
_.toNumber(checkPresetInfo?.keypair_remaining[key]),
// @ts-ignore
_.toNumber(checkPresetInfo?.group_remaining[key]),
// @ts-ignore
_.toNumber(checkPresetInfo?.scaling_group_remaining[key]),
_.toNumber(
checkPresetInfo?.keypair_remaining[key as ResourceSlotName],
),
_.toNumber(
checkPresetInfo?.group_remaining[key as ResourceSlotName],
),
_.toNumber(
checkPresetInfo?.scaling_group_remaining[key as ResourceSlotName],
),
]) ?? Number.MAX_SAFE_INTEGER;
return result;
},
Expand Down

0 comments on commit 40b6062

Please sign in to comment.