Files
ubicloud/migrate/20250117_vm_host_slice.rb
Maciek Sarnowicz b7d5bf9cb6 Migrations for VmHostSlice.
We'll put VMs in systemd slices. This change adds the migrations to
enable that.

Each slice reserves a subset of host's cpus & part of its memory. Then,
VMs can be allocated into the slice. `allowed_cpus` column maps directly
to the `AllowedCPUs` property of the corresponding systemd 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
`type` column. We will use `shared` slices to host burstable instances.
2025-01-13 00:23:07 -08:00

33 lines
1.3 KiB
Ruby

# frozen_string_literal: true
Sequel.migration do
change do
create_enum(:vm_host_slice_type, %w[dedicated shared])
create_table(:vm_host_slice) do
column :id, :uuid, primary_key: true
column :name, :text, null: false
column :enabled, :bool, null: false, default: false
column :type, :vm_host_slice_type, default: "dedicated", 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 :created_at, :timestamptz, null: false, default: Sequel.lit("now()")
foreign_key :vm_host_id, :vm_host, type: :uuid
# 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