Skip to content

Commit

Permalink
Adds space quota for logs in bytes per second
Browse files Browse the repository at this point in the history
This commit updates the `/v3/space_quotas/` endpoints to allow
setting and retrieving of a new parameter (`log_limit_in_bytes_per_second`).
This will eventually permit the user to set log line production limits
in bytes per second, rather than lines per second.

Tracker Story ID: [#182353823]
Github Issue: cloudfoundry/capi-release#245

Signed-off-by: Carson Long <[email protected]>
Signed-off-by: Kenneth Lakin <[email protected]>
  • Loading branch information
klakin-pivotal authored and ctlong committed Jul 5, 2022
1 parent 08dda2e commit 0f07ed1
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 13 deletions.
5 changes: 5 additions & 0 deletions app/actions/space_quota_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.update(quota, message)
quota.instance_memory_limit = instance_memory_limit(message) if message.apps_limits_message.requested? :per_process_memory_in_mb
quota.app_instance_limit = app_instance_limit(message) if message.apps_limits_message.requested? :total_instances
quota.app_task_limit = app_task_limit(message) if message.apps_limits_message.requested? :per_app_tasks
quota.log_limit = log_limit(message) if message.apps_limits_message.requested? :log_limit_in_bytes_per_second

quota.total_services = total_services(message) if message.services_limits_message.requested? :total_service_instances
quota.total_service_keys = total_service_keys(message) if message.services_limits_message.requested? :total_service_keys
Expand Down Expand Up @@ -55,6 +56,10 @@ def self.app_task_limit(message)
default_if_nil(message.per_app_tasks, SpaceQuotaDefinition::UNLIMITED)
end

def self.log_limit(message)
default_if_nil(message.log_limit_in_bytes_per_second, SpaceQuotaDefinition::UNLIMITED)
end

def self.total_services(message)
default_if_nil(message.total_service_instances, SpaceQuotaDefinition::UNLIMITED)
end
Expand Down
3 changes: 3 additions & 0 deletions app/actions/space_quotas_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class SpaceQuotasCreate
class Error < ::StandardError
end

# rubocop:todo Metrics/CyclomaticComplexity
def create(message, organization:)
space_quota = nil

Expand All @@ -16,6 +17,7 @@ def create(message, organization:)
instance_memory_limit: message.per_process_memory_in_mb || SpaceQuotaDefinition::UNLIMITED,
app_instance_limit: message.total_instances || SpaceQuotaDefinition::UNLIMITED,
app_task_limit: message.per_app_tasks || SpaceQuotaDefinition::UNLIMITED,
log_limit: message.log_limit_in_bytes_per_second || QuotaDefinition::UNLIMITED,

# Services
total_services: message.total_service_instances || SpaceQuotaDefinition::DEFAULT_TOTAL_SERVICES,
Expand All @@ -35,6 +37,7 @@ def create(message, organization:)
rescue Sequel::ValidationFailed => e
validation_error!(e, message)
end
# rubocop:enable Metrics/CyclomaticComplexity

private

Expand Down
2 changes: 1 addition & 1 deletion app/messages/space_quota_update_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.key_requested?(key)
validate :services_validator, if: key_requested?(:services)
validate :routes_validator, if: key_requested?(:routes)

delegate :total_memory_in_mb, :per_process_memory_in_mb, :total_instances, :per_app_tasks, to: :apps_limits_message
delegate :total_memory_in_mb, :per_process_memory_in_mb, :total_instances, :per_app_tasks, :log_limit_in_bytes_per_second, to: :apps_limits_message
delegate :paid_services_allowed, :total_service_instances, :total_service_keys, to: :services_limits_message
delegate :total_routes, :total_reserved_ports, to: :routes_limits_message

Expand Down
7 changes: 5 additions & 2 deletions app/models/runtime/space_quota_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class SpaceQuotaDefinition < Sequel::Model

export_attributes :name, :organization_guid, :non_basic_services_allowed, :total_services,
:total_routes, :memory_limit, :instance_memory_limit, :app_instance_limit, :app_task_limit,
:total_service_keys, :total_reserved_route_ports
:total_service_keys, :total_reserved_route_ports, :log_limit
import_attributes :name, :organization_guid, :non_basic_services_allowed, :total_services,
:total_routes, :memory_limit, :instance_memory_limit, :app_instance_limit, :app_task_limit,
:total_service_keys, :total_reserved_route_ports
:total_service_keys, :total_reserved_route_ports, :log_limit

add_association_dependencies spaces: :nullify

# rubocop:todo Metrics/CyclomaticComplexity
def validate
validates_presence :name
validates_presence :non_basic_services_allowed
Expand All @@ -38,9 +39,11 @@ def validate
errors.add(:instance_memory_limit, :invalid_instance_memory_limit) if instance_memory_limit && instance_memory_limit < -1
errors.add(:app_instance_limit, :invalid_app_instance_limit) if app_instance_limit && app_instance_limit < UNLIMITED
errors.add(:app_task_limit, :invalid_app_task_limit) if app_task_limit && app_task_limit < UNLIMITED
errors.add(:log_limit, :invalid_log_limit) if log_limit && log_limit < UNLIMITED
errors.add(:total_service_keys, :invalid_total_service_keys) if total_service_keys && total_service_keys < UNLIMITED
validate_total_reserved_ports
end
# rubocop:enable Metrics/CyclomaticComplexity

def validate_change_organization(new_org)
raise CloudController::Errors::ApiError.new_from_details('OrganizationAlreadySet') unless organization.nil? || organization.guid == new_org.guid
Expand Down
1 change: 1 addition & 0 deletions app/presenters/v3/space_quota_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def to_hash
per_process_memory_in_mb: unlimited_to_nil(space_quota.instance_memory_limit),
total_instances: unlimited_to_nil(space_quota.app_instance_limit),
per_app_tasks: unlimited_to_nil(space_quota.app_task_limit),
log_limit_in_bytes_per_second: unlimited_to_nil(space_quota.log_limit),
},
services: {
paid_services_allowed: space_quota.non_basic_services_allowed,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sequel.migration do
change do
add_column :space_quota_definitions, :log_limit, :Bignum, null: false, default: -1
end
end
23 changes: 16 additions & 7 deletions spec/request/space_quotas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ module VCAP::CloudController
total_memory_in_mb: 5120,
per_process_memory_in_mb: 1024,
total_instances: nil,
per_app_tasks: 5
per_app_tasks: 5,
log_limit_in_bytes_per_second: 2000
},
services: {
paid_services_allowed: false,
Expand All @@ -112,7 +113,8 @@ module VCAP::CloudController
total_memory_in_mb: 5120,
per_process_memory_in_mb: 1024,
total_instances: nil,
per_app_tasks: 5
per_app_tasks: 5,
log_limit_in_bytes_per_second: 2000
},
services: {
paid_services_allowed: false,
Expand Down Expand Up @@ -208,6 +210,7 @@ module VCAP::CloudController
expect(last_response).to have_status_code(200)
expect(space_quota_to_update.reload.app_task_limit).to eq(9)
expect(space_quota_to_update.reload.memory_limit).to eq(-1)
expect(space_quota_to_update.reload.log_limit).to eq(-1)
expect(space_quota_to_update.reload.total_services).to eq(14)
expect(space_quota_to_update.reload.non_basic_services_allowed).to be_falsey
end
Expand All @@ -219,6 +222,7 @@ module VCAP::CloudController
expect(last_response).to have_status_code(200)
expect(space_quota_to_update.reload.app_task_limit).to eq(9)
expect(space_quota_to_update.reload.memory_limit).to eq(-1)
expect(space_quota_to_update.reload.log_limit).to eq(-1)
expect(space_quota_to_update.reload.total_services).to eq(14)
expect(space_quota_to_update.reload.non_basic_services_allowed).to be_falsey
end
Expand Down Expand Up @@ -406,7 +410,8 @@ module VCAP::CloudController
total_memory_in_mb: nil,
per_process_memory_in_mb: nil,
total_instances: nil,
per_app_tasks: nil
per_app_tasks: nil,
log_limit_in_bytes_per_second: nil
},
services: {
paid_services_allowed: true,
Expand Down Expand Up @@ -495,7 +500,8 @@ module VCAP::CloudController
total_memory_in_mb: nil,
per_process_memory_in_mb: nil,
total_instances: nil,
per_app_tasks: nil
per_app_tasks: nil,
log_limit_in_bytes_per_second: nil
},
services: {
paid_services_allowed: true,
Expand Down Expand Up @@ -555,7 +561,8 @@ module VCAP::CloudController
total_memory_in_mb: 5120,
per_process_memory_in_mb: 1024,
total_instances: 10,
per_app_tasks: 5
per_app_tasks: 5,
log_limit_in_bytes_per_second: 3000
},
services: {
paid_services_allowed: false,
Expand Down Expand Up @@ -589,7 +596,8 @@ module VCAP::CloudController
total_memory_in_mb: 5120,
per_process_memory_in_mb: 1024,
total_instances: 10,
per_app_tasks: 5
per_app_tasks: 5,
log_limit_in_bytes_per_second: 3000
},
services: {
paid_services_allowed: false,
Expand Down Expand Up @@ -968,7 +976,8 @@ def make_space_quota_json(space_quota, associated_spaces=space_quota.spaces)
total_memory_in_mb: 20480,
per_process_memory_in_mb: nil,
total_instances: nil,
per_app_tasks: 5
per_app_tasks: 5,
log_limit_in_bytes_per_second: nil
},
services: {
paid_services_allowed: true,
Expand Down
5 changes: 4 additions & 1 deletion spec/unit/actions/space_quota_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module VCAP::CloudController
total_memory_in_mb: 5120,
per_process_memory_in_mb: 1024,
total_instances: 8,
per_app_tasks: nil
per_app_tasks: nil,
log_limit_in_bytes_per_second: 2000,
},
services: {
paid_services_allowed: false,
Expand All @@ -50,6 +51,7 @@ module VCAP::CloudController
expect(updated_space_quota.instance_memory_limit).to eq(1024)
expect(updated_space_quota.app_instance_limit).to eq(8)
expect(updated_space_quota.app_task_limit).to eq(-1)
expect(updated_space_quota.log_limit).to eq(2000)

expect(updated_space_quota.total_services).to eq(10)
expect(updated_space_quota.total_service_keys).to eq(20)
Expand All @@ -63,6 +65,7 @@ module VCAP::CloudController
updated_space_quota = SpaceQuotaUpdate.update(space_quota, minimum_message)

expect(updated_space_quota.name).to eq('space_quota_name')
expect(updated_space_quota.log_limit).to eq(-1)
end

context 'when a model validation fails' do
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/actions/space_quotas_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module VCAP::CloudController
per_process_memory_in_mb: 6,
total_instances: 7,
per_app_tasks: 8,
log_limit_in_bytes_per_second: 2000,
},
services: {
paid_services_allowed: false,
Expand Down Expand Up @@ -68,6 +69,7 @@ module VCAP::CloudController
expect(space_quota.instance_memory_limit).to eq(-1)
expect(space_quota.app_instance_limit).to eq(-1)
expect(space_quota.app_task_limit).to eq(-1)
expect(space_quota.log_limit).to eq(-1)

expect(space_quota.total_services).to eq(-1)
expect(space_quota.total_service_keys).to eq(-1)
Expand All @@ -90,6 +92,7 @@ module VCAP::CloudController
expect(space_quota.instance_memory_limit).to eq(6)
expect(space_quota.app_instance_limit).to eq(7)
expect(space_quota.app_task_limit).to eq(8)
expect(space_quota.log_limit).to eq(2000)

expect(space_quota.total_services).to eq(9)
expect(space_quota.total_service_keys).to eq(10)
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/messages/space_quota_update_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module VCAP::CloudController
per_process_memory_in_mb: 1024,
total_instances: 2,
per_app_tasks: 4,
log_limit_in_bytes_per_second: 2000,
}
end

Expand Down Expand Up @@ -49,6 +50,7 @@ module VCAP::CloudController
expect(subject.per_process_memory_in_mb).to eq(1024)
expect(subject.total_instances).to eq(2)
expect(subject.per_app_tasks).to eq(4)
expect(subject.log_limit_in_bytes_per_second).to eq(2000)
expect(subject.paid_services_allowed).to be_truthy
expect(subject.total_service_instances).to eq(17)
expect(subject.total_service_keys).to eq(19)
Expand Down
15 changes: 13 additions & 2 deletions spec/unit/models/runtime/space_quota_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ module VCAP::CloudController
end
end

describe 'log_limit' do
it 'cannot be less than -1' do
space_quota_definition.log_limit = -2
expect(space_quota_definition).not_to be_valid
expect(space_quota_definition.errors.on(:log_limit)).to include(:invalid_log_limit)

space_quota_definition.log_limit = -1
expect(space_quota_definition).to be_valid
end
end

describe 'total_reserved_route_ports' do
let(:err_msg) do
'Total reserved ports must be -1, 0, or a positive integer, must ' \
Expand Down Expand Up @@ -163,13 +174,13 @@ module VCAP::CloudController
it do
is_expected.to export_attributes :name, :organization_guid, :non_basic_services_allowed, :total_services,
:total_routes, :memory_limit, :instance_memory_limit, :app_instance_limit, :app_task_limit,
:total_service_keys, :total_reserved_route_ports
:total_service_keys, :total_reserved_route_ports, :log_limit
end

it do
is_expected.to import_attributes :name, :organization_guid, :non_basic_services_allowed, :total_services,
:total_routes, :memory_limit, :instance_memory_limit, :app_instance_limit, :app_task_limit,
:total_service_keys, :total_reserved_route_ports
:total_service_keys, :total_reserved_route_ports, :log_limit
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/unit/presenters/v3/space_quota_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module VCAP::CloudController::Presenters::V3
instance_memory_limit: 3,
app_instance_limit: 4,
app_task_limit: 5,
log_limit: 2000,
non_basic_services_allowed: false,
total_services: 6,
total_service_keys: 7,
Expand All @@ -42,6 +43,7 @@ module VCAP::CloudController::Presenters::V3
expect(result[:apps][:per_process_memory_in_mb]).to eq(space_quota.instance_memory_limit)
expect(result[:apps][:total_instances]).to eq(space_quota.app_instance_limit)
expect(result[:apps][:per_app_tasks]).to eq(space_quota.app_task_limit)
expect(result[:apps][:log_limit_in_bytes_per_second]).to eq(space_quota.log_limit)
expect(result[:services][:paid_services_allowed]).to eq(space_quota.non_basic_services_allowed)
expect(result[:services][:total_service_instances]).to eq(space_quota.total_services)
expect(result[:services][:total_service_keys]).to eq(space_quota.total_service_keys)
Expand All @@ -67,6 +69,7 @@ module VCAP::CloudController::Presenters::V3
instance_memory_limit: -1,
app_instance_limit: -1,
app_task_limit: -1,
log_limit: -1,
total_services: -1,
total_service_keys: -1,
total_routes: -1,
Expand All @@ -79,6 +82,7 @@ module VCAP::CloudController::Presenters::V3
expect(result[:apps][:per_process_memory_in_mb]).to be_nil
expect(result[:apps][:total_instances]).to be_nil
expect(result[:apps][:per_app_tasks]).to be_nil
expect(result[:apps][:log_limit_in_bytes_per_second]).to be_nil
expect(result[:services][:total_service_instances]).to be_nil
expect(result[:services][:total_service_keys]).to be_nil
expect(result[:routes][:total_routes]).to be_nil
Expand Down

0 comments on commit 0f07ed1

Please sign in to comment.