Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SR states #24

Merged
merged 3 commits into from
Oct 17, 2023
Merged
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
267 changes: 178 additions & 89 deletions src/server/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,97 +10,164 @@ class ServerlessRuntime < OpenNebula::DocumentJSON

DOCUMENT_TYPE = 1337

FUNCTION_STATES = ['PENDING', 'RUNNING', 'UPDATING', 'ERROR'].freeze
FUNCTION_LCM_STATES = {
FUNCTION_STATES[0] => [
'LCM_INIT',
'BOOT',
'PROLOG',
'BOOT_UNKNOWN',
'BOOT_POWEROFF',
'BOOT_SUSPENDED',
'BOOT_STOPPED',
'BOOT_UNDEPLOY',
'PROLOG_UNDEPLOY',
'CLEANUP_RESUBMIT'
],
FUNCTION_STATES[1] => ['RUNNING'],
FUNCTION_STATES[3] => [
'FAILURE',
'UNKNOWN',
'BOOT_FAILURE',
'BOOT_MIGRATE_FAILURE',
'BOOT_UNDEPLOY_FAILURE',
'BOOT_STOPPED_FAILURE',
'PROLOG_FAILURE',
'PROLOG_MIGRATE_FAILURE',
'PROLOG_MIGRATE_POWEROFF_FAILURE',
'PROLOG_MIGRATE_SUSPEND_FAILURE',
'PROLOG_RESUME_FAILURE',
'PROLOG_UNDEPLOY_FAILURE',
'PROLOG_MIGRATE_UNKNOWN',
'PROLOG_MIGRATE_UNKNOWN_FAILURE'
]
}.freeze

SCHEMA_SPECIFICATION = {
:type => 'object',
:properties => {
:SERVERLESS_RUNTIME => {
:type => 'object',
:properties => {
:NAME => {
:type => 'string'
},
:ID => {
:type => 'integer'
},
:SERVICE_ID => {
:type => 'integer'
},
:FAAS => {
:type => 'object',
:properties => {
:CPU => {
:type => 'number'
},
:VCPU => {
:type => 'float'
:NAME => {
:type => 'string'
},
:MEMORY => {
:ID => {
:type => 'integer'
},
:DISK_SIZE => {
:SERVICE_ID => {
:type => 'integer'
},
:FLAVOUR => {
:type => 'string'
}
},
:required => ['FLAVOUR']
},
:DAAS => {
'oneOf' => [
{
:type => 'object',
:properties => {
:CPU => {
:type => 'number'
},
:VCPU => {
:type => 'integer'
},
:MEMORY => {
:type => 'integer'
},
:DISK_SIZE => {
:type => 'integer'
},
:FLAVOUR => {
:type => 'string'
}
:FAAS => {
:type => 'object',
:properties => {
:FLAVOUR => {
:type => 'string'
},
:CPU => {
:type => 'number'
},
:VCPU => {
:type => 'integer'
},
:MEMORY => {
:type => 'integer'
},
:DISK_SIZE => {
:type => 'integer'
},
:VM_ID => {
:type => 'integer'
},
:STATE => {
:type => 'string',
:enum => FUNCTION_STATES
},
:required => ['FLAVOUR']
:ENDPOINT => {
'oneOf' => [
{
:type => 'string'
},
{
:type => 'null'
}
]
}
},
{
:type => 'null'
:required => ['FLAVOUR']
},
:DAAS => {
'oneOf' => [
{
:type => 'object',
:properties => {
:FLAVOUR => {
:type => 'string'
},
:CPU => {
:type => 'number'
},
:VCPU => {
:type => 'integer'
},
:MEMORY => {
:type => 'integer'
},
:DISK_SIZE => {
:type => 'integer'
},
:VM_ID => {
:type => 'integer'
},
:STATE => {
:type => 'string',
:enum => FUNCTION_STATES
},
:ENDPOINT => {
'oneOf' => [
{
:type => 'string'
},
{
:type => 'null'
}
]
}
},
:required => ['FLAVOUR']
},
{
:type => 'null'
}
]
},
:SCHEDULING => {
:type => 'object',
:properties => {
:POLICY => {
:type => 'string'
},
:REQUIREMENTS => {
:type => 'string'
}
}
]
},
:SCHEDULING => {
:type => 'object',
:properties => {
:POLICY => {
:type => 'string'
},
:REQUIREMENTS => {
:type => 'string'
:DEVICE_INFO => {
:type => 'object',
:properties => {
:LATENCY_TO_PE => {
:type => 'integer'
},
:GEOGRAPHIC_LOCATION => {
:type => 'string'
}
}
}
}
},
:DEVICE_INFO => {
:type => 'object',
:properties => {
:LATENCY_TO_PE => {
:type => 'integer'
},
:GEOGRAPHIC_LOCATION => {
:type => 'string'
}
}
}
},
:required => ['FAAS']
:required => ['FAAS']
}
}
}
}.freeze

attr_accessor :cclient, :body

Expand Down Expand Up @@ -194,6 +261,22 @@ def delete
[204, '']
end

#
# Validates the Serverless Runtime specification using the distributed schema
#
# @param [Hash] specification a valid runtime specification parsed to a Hash
#
# @return [Array] [true,''] or [false, 'reason']
#
def self.validate(specification)
begin
JSON::Validator.validate!(SCHEMA_SPECIFICATION, specification)
[true, '']
rescue JSON::Schema::ValidationError => e
[false, "Invalid #{SR} specification: #{e.message}"]
end
end

#####################
# Inherited Functions
#####################
Expand Down Expand Up @@ -283,22 +366,6 @@ def self.service_sync(client, runtime_definition, service_id, timeout = 30)
end
end

#
# Validates the Serverless Runtime specification using the distributed schema
#
# @param [Hash] specification a valid runtime specification parsed to a Hash
#
# @return [Array] [true,''] or [false, 'reason']
#
def self.validate(specification)
begin
JSON::Validator.validate!(SCHEMA_SPECIFICATION, specification)
[true, '']
rescue JSON::Schema::ValidationError => e
[false, "Invalid #{SR} specification: #{e.message}"]
end
end

#
# Create oneflow service based on Serverless Runtime specification
#
Expand Down Expand Up @@ -396,7 +463,7 @@ def self.xaas_template(client, role)
t = '//TEMPLATE/'

xaas_template['VM_ID'] = vm_id
xaas_template['STATE'] = vm.state_str
xaas_template['STATE'] = map_vm_state(vm)
xaas_template['ENDPOINT'] = vm["#{t}NIC[NIC_ID=\"0\"]/IP"]

xaas_template['CPU'] = vm["#{t}CPU"].to_f
Expand Down Expand Up @@ -466,6 +533,28 @@ def self.vm_template_contents(specification, vm_template, conf_capacity)
xaas.join("\n")
end

#
# Maps an OpenNebula VM state to the accepted Function VM states
#
# @param [OpenNebula::VirtualMachine] vm Virtual Machine representing the Function
#
# @return [String] Serverless Runtime Function state
#
def self.map_vm_state(vm)
case vm.state_str
when 'INIT', 'PENDING', 'HOLD'
return FUNCTION_STATES[0]
when 'ACTIVE'
FUNCTION_LCM_STATES.each do |function_state, vm_states|
return function_state if vm_states.include?(vm.lcm_state_str)
end
when 'STOPPED', 'SUSPENDED', 'POWEROFF', 'UNDEPLOYED', 'CLONING'
return FUNCTION_STATES[2]
else
return FUNCTION_STATES[3]
end
end

end

end
4 changes: 2 additions & 2 deletions tests/lib/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
pp runtime

case runtime['SERVERLESS_RUNTIME']['FAAS']['STATE']
when 'ACTIVE'
when ProvisionEngine::ServerlessRuntime::FUNCTION_STATES[1]
verify_sr_spec(@conf[:specification], runtime)
break
when 'FAILED'
when ProvisionEngine::ServerlessRuntime::FUNCTION_STATES[3]
raise 'FaaS VM failed to deploy'
else
next
Expand Down