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 Sync To User performance (batch 1) #1896

Merged
merged 4 commits into from
Dec 22, 2020
Merged

Conversation

kitallis
Copy link
Contributor

@kitallis kitallis commented Dec 17, 2020

Story card: ch2086

Because

Whilst perf-testing block-syncs, we discovered various inefficiencies in our sync (GET) code-paths that get exacerbated when many users re-sync at the same time.

A full list of improvements and the rationale is here.

This addresses

This is a batch of fixes pulled from #1189 that should help in improving Facility sync performance.

There are ~4k facilities in production, fetching the block_region itself
takes 2 queries + 1 query where we fetch the facility for the page.

This means a grand total of potentially 8000 region-related queries get
fire just for inserting a block_region_id.

This preloads the block_regions for all facilities that need to be
shipped and that brings us back to 4 queries for a pagesize of 1000.
.includes(:facility_group)
.where.not(facility_group: nil)
end

private

# Memoize this call here so that we don't end up making thousands of calls to check user for each facility
def block_level_sync?
@is_block_level_sync_enabled = current_user&.block_level_sync? if @is_block_level_sync_enabled.nil?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to match the more idiomatic way of doing this type of nil-safe memoization?

def block_level_sync_enabled?
  return @block_level_sync_enabled if defined? @block_level_sync_enabled
  @block_level_sync_enabled = current_user&.block_level_sync?
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, wasn't aware of this, this seems a lot better, thanks!

@@ -49,6 +49,13 @@ class Facility < ApplicationRecord
foreign_key: "assigned_facility_id"

pg_search_scope :search_by_name, against: {name: "A", slug: "B"}, using: {tsearch: {prefix: true, any_word: true}}
scope :with_block_region_id, -> {
return all unless Flipper.enabled?(:regions_prep)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the feature flag check should go up into the sync code that is building up the query...having it done in the scope itself is hard to understand. It isn't clear why return all is valid if regions_prep is turned off...I think it would make more sense reading in context of the records_to_sync method.

@kitallis kitallis temporarily deployed to simple-review-pr-1896 December 18, 2020 06:11 Inactive
@prabhanshuguptagit prabhanshuguptagit marked this pull request as ready for review December 18, 2020 14:34
Copy link
Contributor

@harimohanraj89 harimohanraj89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we can't simply add another includes(:region) because we have to get the block region for each facility, not just its corresponding facility region.

Just ideating for a second, is there room to introduce a belongs_to :block_region on the facility model?

In any case, getting the block region is the awkward part. I suspect setting that up as an association would be even more awkward.

Looks good to me!

@kitallis
Copy link
Contributor Author

kitallis commented Dec 22, 2020

@harimohanraj89 I think it'll be equally awkward to setup the association, yep. We can tweak this around if we need something like this more and more. For now this is the only instance where preloading is necessary.

@prabhanshuguptagit prabhanshuguptagit temporarily deployed to simple-review-pr-1896 December 22, 2020 14:23 Inactive
@prabhanshuguptagit prabhanshuguptagit merged commit 518c477 into master Dec 22, 2020
@prabhanshuguptagit prabhanshuguptagit deleted the sync-perf-1 branch December 22, 2020 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants