Files
ubicloud/migrate/20250113_vm_host_slice.rb
Maciek Sarnowicz 7eb4f082da Add VmHostSlice migrations.
We'll put VMs in cgroup slices. This change adds the migrations to
enable that. A VmHostSlice is used to track resource usage of a cgroup
slice.

Each slice reserves a subset of host's cpus & part of its memory. Then,
VMs can be allocated into the slice.

`total_memory_gib` & `used_memory_gib` are used to make sure that we
will have enough memory in the host for the VMs that'll be added to the
slice in future. When a VmHostSlice record is allocated, it will reserve
`total_memory_gib` from the host.

Each slice can be either be `dedicated` or `shared`, as specified in the
`is_shared` column. We will use `shared` slices to host burstable instances.
2025-01-13 16:11:57 -08:00

32 lines
1.2 KiB
Ruby

# frozen_string_literal: true
Sequel.migration do
change do
create_table(:vm_host_slice) do
column :id, :uuid, primary_key: true
column :name, :text, null: false
column :enabled, :bool, default: false, null: false
column :is_shared, :bool, default: false, null: false
column :cores, Integer, null: false
column :total_cpu_percent, Integer, null: false
column :used_cpu_percent, Integer, null: false
column :total_memory_gib, Integer, null: false
column :used_memory_gib, Integer, null: false
column :family, :text, null: false
column :created_at, :timestamptz, null: false, default: Sequel::CURRENT_TIMESTAMP
foreign_key :vm_host_id, :vm_host, type: :uuid, null: false
# Enforce the CPU and memory calculation logic
constraint(:cores_not_negative) { cores >= 0 }
constraint(:used_cpu_not_negative) { used_cpu_percent >= 0 }
constraint(:cpu_allocation_limit) { used_cpu_percent <= total_cpu_percent }
constraint(:used_memory_not_negative) { used_memory_gib >= 0 }
constraint(:memory_allocation_limit) { used_memory_gib <= total_memory_gib }
end
alter_table(:vm) do
add_foreign_key :vm_host_slice_id, :vm_host_slice, type: :uuid
end
end
end