Skip to content

Commit

Permalink
handle pending blob copy operations
Browse files Browse the repository at this point in the history
subjects may create multiple copy blob operations to backfill the training data - do not attempt to copy one if we've scheduled one already
  • Loading branch information
camallen committed May 20, 2022
1 parent 925aff5 commit 72c89cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/services/storage/training_data_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Storage
class TrainingDataSync
COPY_OPERATION_SUCCESS_CODE = 'success'
COPY_OPERATION_OK_CODES = %w[success pending].freeze
PROD_CONTAINER_URL_PREFIX = 'https://panoptesuploads.blob.core.windows.net/public'
STAGING_CONTAINER_URL_PREFIX = 'https://panoptesuploadsstaging.blob.core.windows.net/public'

Expand All @@ -13,7 +13,7 @@ def initialize(src_image_url)
end

def run
return if image_url_blob_already_copied
return if image_url_blob_already_copied_or_pending

_copy_id, _copy_status = blob_service_client.copy_blob_from_uri(
Rails.env,
Expand All @@ -22,9 +22,12 @@ def run
)
end

def image_url_blob_already_copied
def image_url_blob_already_copied_or_pending
response = blob_service_client.get_blob_properties(Rails.env, blob_destination_path)
response.properties[:copy_status] == COPY_OPERATION_SUCCESS_CODE
# skip requesting a copy operation if the blob is already copied or pending / scheduled for copy
# failure / aborts will return false and schedule a new copy
# https://docs.microsoft.com/en-us/rest/api/storageservices/Copy-Blob?redirectedfrom=MSDN#working-with-a-pending-copy-operation-version-2012-02-12-and-newer
COPY_OPERATION_OK_CODES.include?(response.properties[:copy_status])
rescue Azure::Core::Http::HTTPError => _e
# treat all errors as a failure and attempt to re-copy
# we can refine this via the response status code
Expand Down
24 changes: 24 additions & 0 deletions spec/services/storage/training_data_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,29 @@
expect(blob_service_client_double).not_to have_received(:copy_blob_from_uri)
end
end

context 'when the blob is pending copy' do
let(:blob_copy_properties) do
{
blob_type: 'BlockBlob',
copy_id: '6569935c-a8a1-4f90-819a-e4258271e163',
copy_status: 'pending',
copy_source: src_image_url,
copy_progress: '0/551722',
copy_completion_time: nil,
copy_status_description: nil
}
end

before do
allow(blob_instance_double).to receive(:properties).and_return(blob_copy_properties)
allow(blob_service_client_double).to receive(:get_blob_properties).and_return(blob_instance_double)
end

it 'does not call the blob copy uri function' do
data_syncer.run
expect(blob_service_client_double).not_to have_received(:copy_blob_from_uri)
end
end
end
end

0 comments on commit 72c89cc

Please sign in to comment.