Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 167 additions & 45 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,190 @@ export function quantityToScalar(quantity: string): number | bigint {
if (!quantity) {
return 0;
}
if (/\s/.test(quantity)) {
throw new Error('Unknown quantity ' + quantity);
}
const suffix = findSuffix(quantity);
if (suffix === '') {
const num = Number(quantity).valueOf();
if (isNaN(num)) {
throw new Error('Unknown quantity ' + quantity);
}
return num;
if (suffix === '' || suffix === 'n' || suffix === 'u' || suffix === 'm') {
return quantityToNumber(quantity, suffix);
}
const multiplier = integerSuffixMultiplier(suffix);
if (multiplier === undefined) {
throw new Error(`Unknown suffix: ${suffix}`);
}
return scaleByIntegerSuffix(quantity, suffix, multiplier);
}

// k..E and Ki..Ei count whole base units. Scale the mantissa in base 10 with
// BigInt — significand * multiplier * 10^pow10 — and round a non-zero remainder
// away from zero, matching resource.Quantity.Value(). Number() is not used here:
// it cannot represent every integer past MAX_SAFE_INTEGER or every decimal
// fraction. n/u/m and a bare mantissa stay JS numbers. A power of ten past
// 10000 is rejected so a pathological exponent cannot allocate an unbounded
// integer; real suffixes never reach it. findSuffix leaves a scientific
// exponent in the mantissa (`1.5e3Ki`) and treats a bare `1e3` / `1.5e6` as
// having no suffix.

const MAX_DECIMAL_EXPONENT = BigInt(10000);

const THOUSAND = BigInt(1000);
const MILLION = BigInt(1000 * 1000);
const BILLION = BigInt(1000 * 1000 * 1000);
const KIBI = BigInt(1024);
const MEBI = BigInt(1024 * 1024);
const GIBI = BigInt(1024 * 1024 * 1024);

function integerSuffixMultiplier(suffix: string): bigint | undefined {
switch (suffix) {
case 'n':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
case 'u':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0;
case 'm':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0;
case 'k':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000);
return THOUSAND;
case 'M':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000);
return MILLION;
case 'G':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
return BILLION;
case 'T':
return (
BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000)
);
return BILLION * THOUSAND;
case 'P':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000)
);
return BILLION * MILLION;
case 'E':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000 * 1000)
);
return BILLION * BILLION;
case 'Ki':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024);
return KIBI;
case 'Mi':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024);
return MEBI;
case 'Gi':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024);
return GIBI;
case 'Ti':
return (
BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024)
);
return GIBI * KIBI;
case 'Pi':
return (
BigInt(quantity.substr(0, quantity.length - 2)) *
BigInt(1024 * 1024 * 1024) *
BigInt(1024 * 1024)
);
return GIBI * MEBI;
case 'Ei':
return (
BigInt(quantity.substr(0, quantity.length - 2)) *
BigInt(1024 * 1024 * 1024) *
BigInt(1024 * 1024 * 1024)
);
return GIBI * GIBI;
default:
return undefined;
}
}

function quantityToNumber(quantity: string, suffix: string): number {
const mantissa = suffix === '' ? quantity : quantity.substring(0, quantity.length - suffix.length);
const num = Number(mantissa).valueOf();
if (isNaN(num)) {
throw new Error('Unknown quantity ' + quantity);
}
switch (suffix) {
case 'n':
return num / 1_000_000_000.0;
case 'u':
return num / 1_000_000.0;
case 'm':
return num / 1000.0;
default:
throw new Error(`Unknown suffix: ${suffix}`);
return num;
}
}

interface FixedDecimal {
negative: boolean;
significand: bigint;
// ±significand * 10^pow10
pow10: bigint;
}

function scaleByIntegerSuffix(quantity: string, suffix: string, multiplier: bigint): bigint {
const mantissa = quantity.substring(0, quantity.length - suffix.length);
const parsed = parseFixedDecimal(mantissa);
if (parsed === undefined) {
throw new Error('Unknown quantity ' + quantity);
}
const scaled = multiplyAndScale(parsed.significand, multiplier, parsed.pow10);
if (scaled === undefined) {
throw new Error('Unknown quantity ' + quantity);
}
return parsed.negative ? -scaled : scaled;
}

function parseFixedDecimal(mantissa: string): FixedDecimal | undefined {
const match = /^([+-])?(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(mantissa);
if (match === null) {
return undefined;
}
const sign = match[1];
const intPart = match[2];
const fracPart = match[3];
const expPart = match[4];
if (intPart.length === 0 && (fracPart === undefined || fracPart.length === 0)) {
return undefined;
}
const digits = `${intPart}${fracPart ?? ''}`;
const significand = BigInt(digits.replace(/^0+(?=\d)/, ''));
const fracDigits = fracPart === undefined ? BigInt(0) : BigInt(fracPart.length);
const exponent = parseExponent(expPart);
if (exponent === undefined) {
return undefined;
}
return {
negative: sign === '-' && significand !== BigInt(0),
significand,
pow10: exponent - fracDigits,
};
}

function parseExponent(expPart: string | undefined): bigint | undefined {
if (expPart === undefined) {
return BigInt(0);
}
const digits = expPart.replace(/^[+-]/, '').replace(/^0+(?=\d)/, '');
// 10000 is five digits. Anything longer is past MAX_DECIMAL_EXPONENT, and
// constructing that BigInt would itself be the allocation we are avoiding.
if (digits.length > 5) {
return undefined;
}
const magnitude = BigInt(digits);
return expPart.charAt(0) === '-' ? -magnitude : magnitude;
}

function multiplyAndScale(significand: bigint, multiplier: bigint, pow10: bigint): bigint | undefined {
if (significand === BigInt(0)) {
return BigInt(0);
}
const magnitude = significand * multiplier;
if (pow10 >= BigInt(0)) {
if (pow10 > MAX_DECIMAL_EXPONENT) {
return undefined;
}
return magnitude * pow10n(pow10);
}
const denExp = -pow10;
// 10^denExp is greater than magnitude, so the quotient is 0 and the
// remainder is not. Round away from zero to 1 without building the power.
if (denExp >= BigInt(magnitude.toString().length)) {
return BigInt(1);
}
if (denExp > MAX_DECIMAL_EXPONENT) {
return undefined;
}
const denominator = pow10n(denExp);
const quotient = magnitude / denominator;
const remainder = magnitude % denominator;
return remainder === BigInt(0) ? quotient : quotient + BigInt(1);
}

function pow10n(exp: bigint): bigint {
let result = BigInt(1);
let base = BigInt(10);
let exponent = exp;
const zero = BigInt(0);
const two = BigInt(2);
while (exponent > zero) {
if (exponent % two !== zero) {
result *= base;
}
exponent /= two;
if (exponent > zero) {
base *= base;
}
}
return result;
}

export class ResourceStatus {
Expand Down
143 changes: 143 additions & 0 deletions src/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,100 @@ describe('Utils', () => {
});
});

it('should parse fractional quantities with integer suffixes', () => {
strictEqual(quantityToScalar('1.5Gi'), BigInt(1610612736));
strictEqual(quantityToScalar('0.5Mi'), BigInt(524288));
strictEqual(quantityToScalar('1.5Ki'), BigInt(1536));
strictEqual(quantityToScalar('0.1k'), BigInt(100));
strictEqual(quantityToScalar('2.5G'), BigInt(2500000000));

strictEqual(quantityToScalar('.5Gi'), BigInt(536870912));
strictEqual(quantityToScalar('2.Gi'), BigInt(2147483648));
strictEqual(quantityToScalar('0.125Ki'), BigInt(128));
strictEqual(quantityToScalar('0.0Gi'), BigInt(0));
strictEqual(quantityToScalar('+1.5Gi'), BigInt(1610612736));
strictEqual(quantityToScalar('-1.5Gi'), BigInt(-1610612736));
strictEqual(quantityToScalar('-3k'), BigInt(-3000));
strictEqual(quantityToScalar('-10Ki'), BigInt(-10240));
strictEqual(quantityToScalar('-0.0k'), BigInt(0));
strictEqual(quantityToScalar('1e20k'), BigInt('1' + '0'.repeat(23)));

// findSuffix keeps a trailing SI suffix and leaves the exponent on the mantissa.
// A bare exponent still has no suffix and stays a number.
strictEqual(quantityToScalar('1.5e6'), 1500000);
strictEqual(quantityToScalar('1e3k'), BigInt(1000000));
strictEqual(quantityToScalar('1.5e3Ki'), BigInt(1536000));
strictEqual(quantityToScalar('1.5e-1k'), BigInt(150));
strictEqual(quantityToScalar('1.e2k'), BigInt(100000));

strictEqual(quantityToScalar('1.5Ti'), BigInt(1649267441664));
strictEqual(quantityToScalar('0.5Pi'), BigInt(562949953421312));
strictEqual(quantityToScalar('1.5Ei'), BigInt('1729382256910270464'));
strictEqual(quantityToScalar('2.5T'), BigInt('2500000000000'));
strictEqual(quantityToScalar('0.5P'), BigInt('500000000000000'));
strictEqual(quantityToScalar('1.25E'), BigInt('1250000000000000000'));

// Past the range Number can represent. The float reading of the mantissa
// loses digits, so rounding that product is not the exact decimal value.
const floatThousands = BigInt(Math.round(Number('9007199254740993.1') * 1000));
strictEqual(floatThousands === BigInt('9007199254740993100'), false);
strictEqual(quantityToScalar('9007199254740993.1k'), BigInt('9007199254740993100'));
strictEqual(Number('1.0000000000000001'), 1);
strictEqual(quantityToScalar('1.0000000000000001E'), BigInt('1000000000000000100'));
strictEqual(quantityToScalar('1.0000000000000001P'), BigInt('1000000000000001'));
strictEqual(quantityToScalar('1.000000000000000000001Ei'), BigInt('1152921504606846977'));

// Quantity.Value rounds away from zero. 1.3Ki is 1331.2, 0.0001k is 0.1,
// 0.1Ti is 109951162777.6. Truncation toward zero would be 1 smaller.
strictEqual(quantityToScalar('1.3Ki'), BigInt(1332));
strictEqual(quantityToScalar('-1.3Ki'), BigInt(-1332));
strictEqual(quantityToScalar('0.0001k'), BigInt(1));
strictEqual(quantityToScalar('-0.0001k'), BigInt(-1));
strictEqual(quantityToScalar('0.1Ti'), BigInt('109951162778'));

throws(() => quantityToScalar('abcGi'), {
name: 'Error',
message: 'Unknown quantity abcGi',
});
throws(() => quantityToScalar('1.2.3Gi'), {
name: 'Error',
message: 'Unknown quantity 1.2.3Gi',
});
throws(() => quantityToScalar('.Gi'), {
name: 'Error',
message: 'Unknown quantity .Gi',
});
throws(() => quantityToScalar('--1Gi'), {
name: 'Error',
message: 'Unknown quantity --1Gi',
});
throws(() => quantityToScalar('1.2.3m'), {
name: 'Error',
message: 'Unknown quantity 1.2.3m',
});
throws(() => quantityToScalar(' 1.5Gi'), {
name: 'Error',
message: 'Unknown quantity 1.5Gi',
});
throws(() => quantityToScalar('1.5 Gi'), {
name: 'Error',
message: 'Unknown quantity 1.5 Gi',
});
throws(() => quantityToScalar('1e10001k'), {
name: 'Error',
message: 'Unknown quantity 1e10001k',
});
throws(() => quantityToScalar('1e100001k'), {
name: 'Error',
message: 'Unknown quantity 1e100001k',
});
const tinyHuge = `1.${'0'.repeat(10001)}1k`;
throws(() => quantityToScalar(tinyHuge), {
name: 'Error',
message: 'Unknown quantity ' + tinyHuge,
});
});

it('should get resources for pod', () => {
const pod = {
spec: {
Expand Down Expand Up @@ -130,10 +224,59 @@ describe('Utils', () => {
strictEqual(memResult.limit, BigInt(20480));
});

it('should total fractional memory requests and limits', () => {
const pod = {
spec: {
containers: [
{
name: 'app',
resources: {
requests: {
cpu: '100m',
memory: '1.5Gi',
},
limits: {
cpu: '200m',
memory: '0.5Gi',
},
},
} as V1Container,
{
name: 'sidecar',
resources: {
requests: {
cpu: '1.0',
memory: '0.5Gi',
},
limits: {
memory: '1.5Gi',
},
},
} as V1Container,
] as V1Container[],
},
} as V1Pod;

const cpuResult = totalCPU(pod);
strictEqual(cpuResult.request, 1.1);
strictEqual(cpuResult.limit, 0.2);

const memResult = totalMemory(pod);
strictEqual(memResult.request, BigInt(1610612736) + BigInt(536870912));
strictEqual(memResult.limit, BigInt(536870912) + BigInt(1610612736));
});

it('should find the suffix correctly', () => {
strictEqual(findSuffix('1234567'), '');
strictEqual(findSuffix('1234asdf'), 'asdf');
strictEqual(findSuffix('1.0'), '');
// A scientific exponent ends in a digit, so it is not a suffix.
// The SI suffix after one is. `e` alone is not a known suffix either.
strictEqual(findSuffix('1e3'), '');
strictEqual(findSuffix('1.5e6'), '');
strictEqual(findSuffix('1.5Gi'), 'Gi');
strictEqual(findSuffix('1.5e3Ki'), 'Ki');
strictEqual(findSuffix('10E'), 'E');
});

it('shoult extract the headers for ApiException correctly', () => {
Expand Down