Skip to content

Commit f46b03d

Browse files
authored
fix: fix cpu usage calculation (#542)
1 parent 788ad9a commit f46b03d

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

src/containers/Tenant/Diagnostics/TenantOverview/MetricsCards/MetricsCards.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface TenantMetrics {
2727
memoryUsed?: number;
2828
memoryLimit?: number;
2929
cpuUsed?: number;
30-
cpuLimit?: number;
30+
cpuUsage?: number;
3131
storageUsed?: number;
3232
storageLimit?: number;
3333
}
@@ -51,11 +51,10 @@ export function MetricsCards({
5151
}: MetricsCardsProps) {
5252
const location = useLocation();
5353

54-
const {memoryUsed, memoryLimit, cpuUsed, cpuLimit, storageUsed, storageLimit} = metrics || {};
54+
const {memoryUsed, memoryLimit, cpuUsed, cpuUsage, storageUsed, storageLimit} = metrics || {};
5555

5656
const {metricsTab} = useTypedSelector((state) => state.tenant);
5757

58-
const cpuUsage = calculateUsage(cpuUsed, cpuLimit);
5958
const storageUsage = calculateUsage(storageUsed, storageLimit);
6059
const memoryUsage = calculateUsage(memoryUsed, memoryLimit);
6160

src/containers/Tenant/Diagnostics/TenantOverview/TenantOverview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ export function TenantOverview({tenantName, additionalTenantProps}: TenantOvervi
7070

7171
const tenantType = mapDatabaseTypeToDBName(Type);
7272

73-
const {cpu, storage, memory, cpuLimit, storageLimit, memoryLimit} =
73+
const {cpu, storage, memory, cpuUsage, storageLimit, memoryLimit} =
7474
calculateTenantMetrics(tenant);
7575

7676
const calculatedMetrics: TenantMetrics = {
7777
memoryUsed: memory,
7878
memoryLimit,
7979
cpuUsed: cpu,
80-
cpuLimit,
80+
cpuUsage,
8181
storageUsed: storage,
8282
storageLimit,
8383
};

src/store/reducers/tenants/utils.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,30 @@ const getTenantBackend = (tenant: TTenant) => {
2222
};
2323

2424
export const calculateTenantMetrics = (tenant?: TTenant) => {
25-
const {
26-
CoresUsed,
27-
MemoryUsed,
28-
StorageAllocatedSize,
29-
CoresLimit,
30-
MemoryLimit,
31-
StorageLimit,
32-
Metrics = {},
33-
} = tenant || {};
25+
const {CoresUsed, MemoryUsed, StorageAllocatedSize, MemoryLimit, StorageLimit, PoolStats} =
26+
tenant || {};
3427

35-
const cpuFromCores = isNumeric(CoresUsed) ? Number(CoresUsed) * 1_000_000 : undefined;
36-
const cpuFromMetrics = isNumeric(Metrics.CPU) ? Number(Metrics.CPU) : undefined;
28+
const systemPoolUsage = PoolStats?.find(({Name}) => Name === 'System')?.Usage;
29+
const userPoolUsage = PoolStats?.find(({Name}) => Name === 'User')?.Usage;
3730

38-
const cpu = cpuFromCores ?? cpuFromMetrics ?? undefined;
39-
40-
const rawMemory = MemoryUsed ?? Metrics.Memory;
41-
42-
const memory = isNumeric(rawMemory) ? Number(rawMemory) : undefined;
43-
44-
// Blob storage - actual database size
31+
const cpu = isNumeric(CoresUsed) ? Number(CoresUsed) * 1_000_000 : undefined;
32+
const memory = isNumeric(MemoryUsed) ? Number(MemoryUsed) : undefined;
4533
const storage = isNumeric(StorageAllocatedSize) ? Number(StorageAllocatedSize) : undefined;
46-
const cpuLimit = isNumeric(CoresLimit) ? Number(CoresLimit) : undefined;
34+
35+
// We use system pool usage and user pool usage to calculate cpu usage because
36+
// only these pools directly indicate resources available to perform user queries
37+
const cpuUsage =
38+
isNumeric(systemPoolUsage) || isNumeric(userPoolUsage)
39+
? Math.max(Number(systemPoolUsage), Number(userPoolUsage)) * 100
40+
: undefined;
4741
const memoryLimit = isNumeric(MemoryLimit) ? Number(MemoryLimit) : undefined;
4842
const storageLimit = isNumeric(StorageLimit) ? Number(StorageLimit) : undefined;
4943

5044
return {
5145
cpu,
5246
memory,
5347
storage,
54-
cpuLimit,
48+
cpuUsage,
5549
memoryLimit,
5650
storageLimit,
5751
};

src/types/api/tenant.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,22 @@ export interface TTenant {
4242
ResourceId?: string;
4343
Tablets?: TTabletStateInfo[];
4444
/** uint64 */
45-
StorageAllocatedSize?: string;
45+
StorageAllocatedSize?: string; // Actual database size
4646
/** uint64 */
4747
StorageMinAvailableSize?: string;
4848
Nodes?: TSystemStateInfo[];
4949
/** uint64 */
50-
MemoryUsed?: string;
50+
MemoryUsed?: string; // Actual memory consumption
5151
/** uint64 */
5252
MemoryLimit?: string;
5353
/** double */
54-
CoresUsed?: number;
54+
CoresUsed?: number; // Actual cpu consumption
5555
/** uint64 */
5656
StorageGroups?: string;
5757

5858
MonitoringEndpoint?: string; // additional
5959
ControlPlane?: ControlPlane; // additional
6060

61-
CoresLimit?: string; // TODO: check correctness in backend protos when fully supported
6261
StorageLimit?: string; // TODO: check correctness in backend protos when fully supported
6362
}
6463

@@ -69,15 +68,15 @@ interface THiveDomainStatsStateCount {
6968

7069
export interface TMetrics {
7170
/** uint64 */
72-
CPU?: string;
71+
CPU?: string; // Logical cpu consumption
7372
/** uint64 */
74-
Memory?: string;
73+
Memory?: string; // Logical memory consumption
7574
/** uint64 */
7675
Network?: string;
7776
/** uint64 */
7877
Counter?: string;
7978
/** uint64 */
80-
Storage?: string;
79+
Storage?: string; // Logical database size
8180
/** uint64 */
8281
ReadThroughput?: string;
8382
/** uint64 */

0 commit comments

Comments
 (0)