-
Notifications
You must be signed in to change notification settings - Fork 11
Publish to S3 via a debounced queue #1022
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
Open
rsaksida
wants to merge
3
commits into
master
Choose a base branch
from
feature/s3-bulk-uploads
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| require 'envelope_graph_sync' | ||
| require 'sync_pending_envelope_graphs_with_s3' | ||
|
|
||
| class SyncEnvelopeGraphsWithS3Job < ActiveJob::Base # rubocop:todo Style/Documentation | ||
| def perform(sync_id) | ||
| sync = EnvelopeGraphSync.find(sync_id) | ||
| last_activity_at = nil | ||
| last_activity_version_id = nil | ||
| pending_sync = false | ||
| wait_until = nil | ||
|
|
||
| sync.with_lock do | ||
| last_activity_at = sync.last_activity_at | ||
| last_activity_version_id = sync.last_activity_version_id | ||
| pending_sync = last_activity_version_id.present? && | ||
| (sync.last_synced_version_id.blank? || | ||
| sync.last_synced_version_id < last_activity_version_id) | ||
|
|
||
| return if sync.scheduled_for_at.blank? && !pending_sync | ||
|
|
||
| if sync.scheduled_for_at.present? | ||
| wait_until = last_activity_at + EnvelopeGraphSync.debounce_window | ||
|
|
||
| if wait_until > Time.current | ||
| sync.update!(scheduled_for_at: wait_until) | ||
| else | ||
| wait_until = nil | ||
| sync.update!(scheduled_for_at: nil) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| if wait_until | ||
| self.class.set(wait_until: wait_until).perform_later(sync.id) | ||
| return | ||
| end | ||
|
|
||
| return unless last_activity_at && last_activity_version_id && sync.mark_syncing! | ||
|
|
||
| begin | ||
| SyncPendingEnvelopeGraphsWithS3.new( | ||
| envelope_community: sync.envelope_community, | ||
| cutoff_version_id: last_activity_version_id, | ||
| sync: sync | ||
| ).call | ||
| rescue StandardError => e | ||
| sync.mark_sync_error!(e) | ||
| raise | ||
| ensure | ||
| sync.clear_syncing! | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # Tracks debounced S3 graph sync scheduling for an envelope community. | ||
| require 'sync_envelope_graphs_with_s3_job' | ||
|
|
||
| class EnvelopeGraphSync < ActiveRecord::Base | ||
| PUBLISH_LOCKED = 'Publishing is temporarily locked while S3 sync is in progress'.freeze | ||
|
|
||
| belongs_to :envelope_community | ||
|
|
||
| validates :envelope_community_id, uniqueness: true | ||
| validates :last_activity_at, presence: true | ||
|
|
||
| class << self | ||
| def debounce_window | ||
| (ENV['ENVELOPE_GRAPH_SYNC_DEBOUNCE_SECONDS'].presence || '60').to_i.seconds | ||
| end | ||
|
|
||
| def sync_lock_timeout | ||
| (ENV['ENVELOPE_GRAPH_SYNC_LOCK_TIMEOUT_SECONDS'].presence || '600').to_i.seconds | ||
| end | ||
|
|
||
| def syncing?(envelope_community) | ||
| sync = find_by(envelope_community: envelope_community) | ||
| return false unless sync | ||
|
|
||
| sync.syncing? | ||
| end | ||
|
|
||
| def record_activity!(envelope_community, version_id:) | ||
| return unless version_id | ||
|
|
||
| sync = find_or_create_by!(envelope_community: envelope_community) do |record| | ||
| record.last_activity_at = Time.current | ||
| record.last_synced_version_id = previous_version_id(envelope_community, version_id) | ||
| end | ||
|
|
||
| wait_until = nil | ||
|
|
||
| sync.with_lock do | ||
| now = Time.current | ||
| sync.last_activity_at = now | ||
| sync.last_activity_version_id = [ | ||
| sync.last_activity_version_id, | ||
| version_id | ||
| ].compact.max | ||
|
|
||
| if sync.scheduled_for_at.blank? || sync.scheduled_for_at <= now | ||
| wait_until = now + debounce_window | ||
| sync.scheduled_for_at = wait_until | ||
| end | ||
|
|
||
| sync.save! | ||
| end | ||
|
|
||
| if wait_until | ||
| SyncEnvelopeGraphsWithS3Job.set(wait_until: wait_until).perform_later(sync.id) | ||
| end | ||
|
|
||
| sync | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def previous_version_id(envelope_community, version_id) | ||
| EnvelopeVersion | ||
| .where(item_type: 'Envelope', envelope_community_id: envelope_community.id) | ||
| .where('id < ?', version_id) | ||
| .maximum(:id) | ||
| end | ||
| end | ||
|
|
||
| def syncing? | ||
| return false unless syncing | ||
|
|
||
| clear_stale_sync! if stale_sync? | ||
|
|
||
| syncing | ||
| end | ||
|
|
||
| def mark_syncing! | ||
| with_lock do | ||
| clear_stale_sync! if stale_sync? | ||
| return false if syncing | ||
|
|
||
| update!( | ||
| syncing: true, | ||
| syncing_started_at: Time.current, | ||
| last_sync_error: nil | ||
| ) | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| def clear_syncing! | ||
| update!( | ||
| syncing: false, | ||
| syncing_started_at: nil, | ||
| last_sync_finished_at: Time.current | ||
| ) | ||
| end | ||
|
|
||
| def mark_sync_error!(error) | ||
| update!(last_sync_error: "#{error.class}: #{error.message}") | ||
| end | ||
|
|
||
| def mark_synced_through!(version_id) | ||
| return unless version_id | ||
|
|
||
| with_lock do | ||
| update!( | ||
| last_synced_version_id: [ | ||
| last_synced_version_id, | ||
| version_id | ||
| ].compact.max | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def stale_sync? | ||
| syncing && syncing_started_at.present? && syncing_started_at <= self.class.sync_lock_timeout.ago | ||
| end | ||
|
|
||
| def clear_stale_sync! | ||
| return unless stale_sync? | ||
|
|
||
| MR.log_with_labels( | ||
| :warn, | ||
| 'Clearing stale envelope graph sync lock', | ||
| envelope_community: envelope_community.name, | ||
| syncing_started_at: syncing_started_at.iso8601, | ||
| timeout_seconds: self.class.sync_lock_timeout.to_i | ||
| ) | ||
|
|
||
| update!( | ||
| syncing: false, | ||
| syncing_started_at: nil, | ||
| last_sync_finished_at: Time.current, | ||
| last_sync_error: 'Stale sync lock cleared after timeout' | ||
| ) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| require 'argo_workflows_client' | ||
|
|
||
| class SubmitPartialGraphIndexWorkflow | ||
| TEMPLATE_NAME = 'update-index-graphs-input-file-elasticsearch'.freeze | ||
|
|
||
| def self.call(envelope_community:, manifest_key:) | ||
| new(envelope_community: envelope_community, manifest_key: manifest_key).call | ||
| end | ||
|
|
||
| attr_reader :envelope_community, :manifest_key | ||
|
|
||
| def initialize(envelope_community:, manifest_key:) | ||
| @envelope_community = envelope_community | ||
| @manifest_key = manifest_key | ||
| end | ||
|
|
||
| def call | ||
| workflow = client.submit_workflow( | ||
| template_name: TEMPLATE_NAME, | ||
| generate_name: "#{community_name.tr('_', '-')}-partial-graph-index-", | ||
| parameters: parameters | ||
| ) | ||
| workflow_name = workflow.dig(:metadata, :name) | ||
| raise 'Argo workflow submission did not return a workflow name' if workflow_name.blank? | ||
|
|
||
| workflow | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def client | ||
| @client ||= ArgoWorkflowsClient.new | ||
| end | ||
|
|
||
| def community_name | ||
| envelope_community.name | ||
| end | ||
|
|
||
| def task_image | ||
| ENV.fetch( | ||
| 'ARGO_WORKFLOWS_PARTIAL_GRAPH_INDEX_TASK_IMAGE', | ||
| ENV.fetch('ARGO_WORKFLOWS_TASK_IMAGE') | ||
| ) | ||
| end | ||
|
|
||
| def parameters | ||
| { | ||
| 'task-image' => task_image, | ||
| 'index-name' => ENV.fetch('ARGO_WORKFLOWS_PARTIAL_GRAPH_INDEX_NAME'), | ||
| 'input-bucket' => graphs_bucket, | ||
| 'input-file-key' => manifest_key, | ||
| 'source-bucket' => graphs_bucket, | ||
| 'prefix-path' => '', | ||
| 'aws-region' => ENV.fetch('AWS_REGION') | ||
| } | ||
| end | ||
|
|
||
| def graphs_bucket | ||
| ENV.fetch('ENVELOPE_GRAPHS_BUCKET') | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.