This commit is contained in:
Sebastian Seedorf
2020-12-07 20:54:25 +01:00
parent 72b9985e3e
commit afbdcec28a
2 changed files with 184 additions and 13 deletions

View File

@@ -1,5 +1,52 @@
export class SampleClass {
add(a: number, b: number): number {
return a+b;
export interface VM {
cpu: number,
memory: number,
network: number,
}
export interface ServerCapacity {
limitCpu: number,
limitMemory: number,
limitNetwork: number,
}
export type Server = Set<VM>;
export class ServerConsolidation {
doesVmFitOnServer(vm: VM, capacity: ServerCapacity): boolean {
return vm.cpu <= capacity.limitCpu &&
vm.memory <= capacity.limitMemory &&
vm.network <= capacity.limitNetwork;
}
reduceCapacity(vm: VM, capacity: ServerCapacity): void {
capacity.limitCpu -= vm.cpu;
capacity.limitMemory -= vm.memory;
capacity.limitNetwork -= vm.network;
}
fit(vms: VM[], capacity: ServerCapacity): Server[] {
const servers: Server[] = [];
const capacitiesLeft: ServerCapacity[] = [];
for (const vm of vms) {
let found = false;
for (const [idx, server] of servers.entries()) {
const capacity = capacitiesLeft[idx];
if (this.doesVmFitOnServer(vm, capacity)) {
found = true;
server.add(vm);
this.reduceCapacity(vm, capacity);
break;
}
}
if (!found) {
servers.push(new Set([vm]));
const newCapacity = {...capacity};
this.reduceCapacity(vm, newCapacity);
capacitiesLeft.push(newCapacity);
}
}
return servers;
}
}