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

Cache hearings related fields selectively #15577

Merged
merged 23 commits into from
Nov 9, 2020
Merged

Conversation

rubaiyat22
Copy link

@rubaiyat22 rubaiyat22 commented Nov 5, 2020

May help with #15417
Resolves #15580

Description

  • cache all appeals in a determined order (descending order of Ids) which may prevent deadlock if two instances of job run at the same time
  • only cache some hearing related expensive fields for appeal if there exists at least one open ScheduleHearingTask

breakdown of appeals with open ScheduleHearingTask vs without (PROD)

as = Task.open.where(appeal_type: Appeal.name).pluck(:appeal_id).uniq
as.count
=> 56919
Task.open.where(appeal_id: as, type: ScheduleHearingTask.name, appeal_type: "Appeal").pluck(:appeal_id).uniq.count
=> 32620 # 57% -24299 difference

las = Task.open.where(appeal_type: LegacyAppeal.name).pluck(:appeal_id).uniq
las.count
=> 209664 
Task.open.where(appeal_id: las, type: ScheduleHearingTask.name, appeal_type: "LegacyAppeal").pluck(:appeal_id).uniq.count
=> 45036 21% -164,628 # we'd be avoiding making calls to BGS for 164,628 appeals

@va-bot
Copy link
Collaborator

va-bot commented Nov 5, 2020

1 Warning
⚠️ This PR appears to affect one or more integrations. Make sure to test code in UAT before merging. See these instructions for deploying a custom branch to UAT.

Generated by 🚫 Danger

@codeclimate
Copy link

codeclimate bot commented Nov 5, 2020

Code Climate has analyzed commit d94a72d and detected 0 issues on this pull request.

View more on Code Climate.

# Replicates LegacyAppeal#current_hearing_request_type
current_request = HearingDay::REQUEST_TYPES.key(changed_request)&.to_sym || original_request

veteran_file_number = LegacyAppeal.veteran_file_number_from_bfcorlid(vacols_case.bfcorlid)
Copy link
Author

Choose a reason for hiding this comment

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

LegacyAppeal.veteran_file_number can make requests to VACOLS so we can use the given vacols_case here to grab bfcorlid

app/services/cached_appeal_service.rb Show resolved Hide resolved
@rubaiyat22 rubaiyat22 changed the title Update Cache job runtime fixes Cache hearings related fields selectively Nov 6, 2020
Copy link
Contributor

@yoomlam yoomlam left a comment

Choose a reason for hiding this comment

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

First pass of suggestions. Will continue to ponder improving the overall flow.

app/services/cached_appeal_service.rb Outdated Show resolved Hide resolved
app/services/cached_appeal_service.rb Outdated Show resolved Hide resolved
Comment on lines 281 to 283
def appeal_ids_with_schedule_hearing_tasks
@appeal_ids_with_schedule_hearing_tasks ||= []
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if you can pass this around as a local variable (and as an argument to methods) rather than storing this instance variable as the state of this service.

Copy link
Author

Choose a reason for hiding this comment

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

yes i thought about that as well. i don't like having this as state of the service either but i was going back and forth because it requires passing this as parameter to multiple methods. I can definitely make those changes though

Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't mind converting it into a local variable, that will make the refactor easier.

Copy link
Contributor

@yoomlam yoomlam Nov 6, 2020

Choose a reason for hiding this comment

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

I also think there's a potential problem. Both cache_ama_appeals (for AMA) and cache_legacy_appeal_postgres_data/cache_legacy_appeal_vacols_data (for Legacy) call append_appeals_with_open_schedule_hearing_tasks, which only stores the appeal_id without the appeal_type. This means that AMA and Legacy appeal ids are clashing with each other. If cache_ama_appeals is called first, there will be AMA appeal ids in appeal_ids_with_open_sched_hearing_task when either of the Legacy methods are called, so there will be extra (AMA) appeal ids that are not relevant to Legacy appeals.

Should add a test for this to make sure that AMA appeal ids are not included in the cache update when the Legacy methods are called.

Copy link
Author

Choose a reason for hiding this comment

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

I converted both appeal_ids_with_schedule_hearing_tasks and vacols_id_to_appeal_id_mapping into local variables so this should not be an issue anymore

app/services/cached_appeal_service.rb Outdated Show resolved Hide resolved
app/services/cached_appeal_service.rb Show resolved Hide resolved
app/services/cached_appeal_service.rb Outdated Show resolved Hide resolved
Rubaiyat Rashid and others added 2 commits November 6, 2020 11:44
Copy link
Contributor

@yoomlam yoomlam left a comment

Choose a reason for hiding this comment

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

1 consideration but LTGM!

Comment on lines 297 to 300
# vacols_id => appeal_id mapping
def vacols_id_to_appeal_id_mapping(legacy_appeals)
legacy_appeals.pluck(:vacols_id, :id).to_h
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider deleting this method since it is only called once and is only 1 line of code.

@rubaiyat22 rubaiyat22 added Ready-to-Merge This PR is ready to be merged and will be picked up by va-bot to automatically merge to master and removed Ready-to-Merge This PR is ready to be merged and will be picked up by va-bot to automatically merge to master labels Nov 9, 2020
@va-bot va-bot merged commit 36f8bab into master Nov 9, 2020
@va-bot va-bot deleted the rubaiyat/cache-job-runtime branch November 9, 2020 22:55
rubaiyat22 pushed a commit that referenced this pull request Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready-to-Merge This PR is ready to be merged and will be picked up by va-bot to automatically merge to master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only cache hearings related attributes if we need it
3 participants