Skip to content

Commit

Permalink
Merge pull request #1793 from cloudfoundry/performance-get-deployments
Browse files Browse the repository at this point in the history
Optimize performance of /deployments endpoint

[#152124892](https://www.pivotaltracker.com/story/show/152124892)
  • Loading branch information
GarfieldIsAPhilosopher authored Oct 20, 2017
2 parents f6b71dc + 8d194ea commit db15c93
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ def initialize(config)
deployments = @deployment_manager.all_by_name_asc
.select { |deployment| @permission_authorizer.is_granted?(deployment, :read, token_scopes) }
.map do |deployment|
cloud_config = if deployment.cloud_config.nil?
cloud_config = if deployment.cloud_config_id.nil?
'none'
elsif deployment.cloud_config == latest_cloud_config
elsif deployment.cloud_config_id == latest_cloud_config.id
'latest'
else
'outdated'
end

{
'name' => deployment.name,
'releases' => deployment.release_versions.map do |rv|
'releases' => deployment.release_versions.sort { |a,b| [a.release.name, a.version] <=> [b.release.name, b.version] } .map do |rv|
{
'name' => rv.release.name,
'version' => rv.version.to_s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def find_by_name(name)
end

def all_by_name_asc
Bosh::Director::Models::Deployment.order_by(Sequel.asc(:name)).all
Bosh::Director::Models::Deployment
.eager(:teams, :stemcells, release_versions: :release)
.order_by(Sequel.asc(:name))
.all
end

def create_deployment(username, manifest_text, cloud_config, runtime_configs, deployment, options = {}, context_id = '')
Expand Down
4 changes: 2 additions & 2 deletions src/bosh-director/lib/bosh/director/models/deployment.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module Bosh::Director::Models
class Deployment < Sequel::Model(Bosh::Director::Config.db)
many_to_many :stemcells
many_to_many :stemcells, order: [Sequel.asc(:name), Sequel.asc(:version)]
many_to_many :release_versions
one_to_many :job_instances, :class => "Bosh::Director::Models::Instance"
one_to_many :instances
one_to_many :properties, :class => "Bosh::Director::Models::DeploymentProperty"
one_to_many :problems, :class => "Bosh::Director::Models::DeploymentProblem"
many_to_one :cloud_config
many_to_many :runtime_configs
many_to_many :teams
many_to_many :teams, order: Sequel.asc(:name)
one_to_many :variable_sets, :class => 'Bosh::Director::Models::VariableSet'

def validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ def manifest_with_errand(deployment_name='errand')
end

describe 'listing deployments' do
let(:cloud_config) { Models::CloudConfig.make }
let(:deployment) { Models::Deployment.make(name: 'b', cloud_config_id: cloud_config.id) }

before { basic_authorize 'reader', 'reader' }

it 'lists deployment info in deployment name order' do
Expand Down Expand Up @@ -662,6 +665,37 @@ def manifest_with_errand(deployment_name='errand')
}
])
end

it 'orders the associations' do
release2 = Models::Release.make(name: 'r2')
release1 = Models::Release.make(name: 'r1')

deployment.add_release_version(Models::ReleaseVersion.make(version: '3', release_id: release1.id))
deployment.add_release_version(Models::ReleaseVersion.make(version: '2', release_id: release1.id))
deployment.add_release_version(Models::ReleaseVersion.make(version: '1', release_id: release2.id))

deployment.add_team(Models::Team.make(name: 'team2'))
deployment.add_team(Models::Team.make(name: 'team3'))
deployment.add_team(Models::Team.make(name: 'team1'))

deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell2', version: '4'))
deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell1', version: '1'))
deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell2', version: '3'))
deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell3', version: '2'))

get '/', {}, {}
expect(last_response.status).to eq(200)

body = JSON.parse(last_response.body)

expect(body.first['releases']).to eq([{'name' => 'r1', 'version' => '2'}, {'name' => 'r1', 'version' => '3'}, {'name' => 'r2', 'version' => '1'}])
expect(body.first['stemcells']).to eq([
{'name' => 'stemcell1', 'version' => '1'},
{'name' => 'stemcell2', 'version' => '3'},
{'name' => 'stemcell2', 'version' => '4'},
{'name' => 'stemcell3', 'version' => '2'}])
expect(body.first['teams']).to eq(%w(team1 team2 team3))
end
end

describe 'getting deployment info' do
Expand Down
30 changes: 30 additions & 0 deletions src/bosh-director/spec/unit/api/deployment_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,35 @@ module Bosh::Director
expect(subject.find_by_name(deployment.name)).to eq deployment
end
end

describe '#all_by_name_asc' do

before do
cloud_config = Models::CloudConfig.make
release = Models::Release.make
deployment = Models::Deployment.make(name: 'b', cloud_config_id: cloud_config.id)
release_version = Models::ReleaseVersion.make(release_id: release.id)
deployment.add_release_version(release_version)
end

it 'eagerly loads :stemcells, :release_versions, :teams' do
allow(Bosh::Director::Config.db).to receive(:execute).and_call_original

deployments = subject.all_by_name_asc

deployments.first.stemcells
deployments.first.release_versions.map(&:release)
deployments.first.teams

expect(Bosh::Director::Config.db).to have_received(:execute).exactly(5).times
end

it 'lists all deployments in alphabetic order' do
Models::Deployment.make(name: 'c')
Models::Deployment.make(name: 'a')

expect(subject.all_by_name_asc.map(&:name)).to eq(['a', 'b', 'c'])
end
end
end
end

0 comments on commit db15c93

Please sign in to comment.