Skip to content

azure: fix race condition in PutContent() - #2088

Merged
dmcgowan merged 1 commit into
distribution:masterfrom
ahmetb:pr-upstream-azure-race-fix
Dec 6, 2016
Merged

dmcgowan merged 1 commit into
distribution:masterfrom
ahmetb:pr-upstream-azure-race-fix

Conversation

@ahmetb

@ahmetb ahmetb commented Nov 30, 2016

Copy link
Copy Markdown
Contributor

See #2077 for the background.

The PR #1438 which was not reviewed by azure folks basically introduced
a race condition around uploads to the same blob by multiple clients
concurrently as it used the "writer" class introduced in #1438 which does
chunked upload of blobs using "AppendBlob" type, which was not atomic.

This patch fixes PutContent() to use the atomic upload operation that works
for uploads smaller than 64 MB and works on "BlockBlob" types only. To be
backwards compatible, we query the type of the blob first and if it is not
a "BlockBlob" we delete the blob first before doing an atomic PUT. This
creates a small inconsistency/race window "only once". Once the blob is made
"BlockBlob", it is overwritten with a single PUT atomically next time.

Tested with existing code side by side, both registries with and without this
patch work fine without breaking each other. So this should be good from a
backwards/forward compatibility perspective, with a cost of doing an extra
HEAD checking the blob type.

Fixes #2077.

Signed-off-by: Ahmet Alp Balkan [email protected]
cc: @yuwaMSFT

See #2077 for background.

The PR #1438 which was not reviewed by azure folks basically introduced
a race condition around uploads to the same blob by multiple clients
concurrently as it used the "writer" type for PutContent(), introduced in #1438.
This does chunked upload of blobs using "AppendBlob" type, which was not atomic.

Usage of "writer" type and thus AppendBlobs on metadata files is currently not
concurrency-safe and generally, they are not the right type of blob for the job.

This patch fixes PutContent() to use the atomic upload operation that works
for uploads smaller than 64 MB and creates blobs with "BlockBlob" type. To be
backwards compatible, we query the type of the blob first and if it is not
a "BlockBlob" we delete the blob first before doing an atomic PUT. This
creates a small inconsistency/race window "only once". Once the blob is made
"BlockBlob", it is overwritten with a single PUT atomicallly next time.

Therefore, going forward, PutContent() will be producing BlockBlobs and it
will silently migrate the AppendBlobs introduced in #1438 to BlockBlobs with
this patch.

Tested with existing code side by side, both registries with and without this
patch work fine without breaking each other. So this should be good from a
backwards/forward compatiblity perspective, with a cost of doing an extra
HEAD checking the blob type.

Fixes #2077.

Signed-off-by: Ahmet Alp Balkan <[email protected]>
@codecov-io

Copy link
Copy Markdown

Current coverage is 51.07% (diff: 0.00%)

Merging #2088 into master will decrease coverage by 8.76%

@@             master      #2088   diff @@
==========================================
  Files           128        128           
  Lines         11399      11401      +2   
  Methods           0          0           
  Messages          0          0           
  Branches          0          0           
==========================================
- Hits           6821       5823    -998   
- Misses         3713       4827   +1114   
+ Partials        865        751    -114   

Powered by Codecov. Last update c599955...78d0660

@dmcgowan

Copy link
Copy Markdown
Collaborator

@ahmetalpbalkan can you link to the Azure documentation for this, I want to better understand the AppendBlob vs BlockBlob.

@ahmetb

ahmetb commented Nov 30, 2016 •

Copy link
Copy Markdown
Contributor Author

@dmcgowan https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/understanding-block-blobs--append-blobs--and-page-blobs

Problem here was:

  1. all azure blobs uploaded from the registry used to be BlockBlobs prior to Adds new StorageDriver.FileWriter interface #1438, which made everything AppendBlobs.
    • this makes sense for layers, it simplified the code a great deal.
  2. it does not make sense to have small metadata blobs as AppendBlobs because
    • you don't append to them ever
    • it's not allowed to overwrite AppendBlobs without deleting them first
    • you can atomically overwrite the BlockBlob type in a single PUT call, without deleting it first.

// handle the case when the request was a HEAD and service error could not
// be parsed, such as "storage: service returned without a response body
// (404 The specified blob does not exist.)"
if strings.Contains(fmt.Sprintf("%v", err), "404 The specified blob does not exist") {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

NOTE: we can remove this new conditional once we bring the following patch here by vendoring the latest azure-sdk-for-go Azure/azure-sdk-for-go#480

@ahmetb

ahmetb commented Dec 2, 2016

Copy link
Copy Markdown
Contributor Author

@dmcgowan what do you think? we've patched this locally and serves us well so far.

@dmcgowan

dmcgowan commented Dec 5, 2016

Copy link
Copy Markdown
Collaborator

LGTM

@dmcgowan dmcgowan added this to the Registry/2.7 milestone Dec 5, 2016
@dmcgowan

dmcgowan commented Dec 5, 2016

Copy link
Copy Markdown
Collaborator

@ahmetalpbalkan possible to get one other Azure folk to LGTM this?

@ahmetb

ahmetb commented Dec 5, 2016

Copy link
Copy Markdown
Contributor Author

We actually reviewed this internally and already started deploying. 😄

cc: @yuwaMSFT can you please LGTM?

@yuwaMSFT2

Copy link
Copy Markdown
Contributor

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

azure: concurrent writes result in 404s or corrupted blobs

4 participants