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

Reduce queries to DB when collecting routing info #3813

Merged
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
27 changes: 18 additions & 9 deletions lib/cloud_controller/diego/protocol/routing_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ def initialize(process)
end

def routing_info
http_info_obj = http_info
tcp_info_obj = tcp_info
process_eager = ProcessModel.eager(route_mappings: { route: %i[domain route_binding] }).where(id: process.id).all

return {} if process_eager.empty?

http_info_obj = http_info(process_eager)
tcp_info_obj = tcp_info(process_eager)

route_info = {}
route_info['http_routes'] = http_info_obj if http_info_obj.present?
route_info['tcp_routes'] = tcp_info_obj if tcp_info_obj.present?
route_info['internal_routes'] = internal_routes
route_info['internal_routes'] = internal_routes(process_eager)
route_info
rescue RoutingApi::RoutingApiDisabled
raise CloudController::Errors::ApiError.new_from_details('RoutingApiDisabled')
Expand All @@ -27,8 +31,8 @@ def routing_info

private

def http_info
route_mappings = process.route_mappings.reject do |route_mapping|
def http_info(process_eager)
route_mappings = process_eager[0].route_mappings.reject do |route_mapping|
route_mapping.route.internal? || route_mapping.route.tcp?
end

Expand All @@ -43,8 +47,8 @@ def http_info
end
end

def tcp_info
route_mappings = process.route_mappings.select do |route_mapping|
def tcp_info(process_eager)
route_mappings = process_eager[0].route_mappings.select do |route_mapping|
r = route_mapping.route
r.tcp? && !r.internal?
end
Expand All @@ -58,8 +62,13 @@ def tcp_info
end
end

def internal_routes
process.routes.select(&:internal?).map do |r|
def internal_routes(process_eager)
route_mappings = process_eager[0].route_mappings.select do |route_mapping|
route_mapping.route.internal?
end

route_mappings.map do |route_mapping|
r = route_mapping.route
{ 'hostname' => "#{r.host}.#{r.domain.name}" }
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/lib/cloud_controller/diego/protocol/routing_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ class Protocol
end
end
end

context 'when process disappeared' do
before do
process.destroy
end

it 'return an empty object' do
expect(ri).to be_empty
end
end
end
end
end
Expand Down