Merge pull request #2461 from coreos-inc/oci-blob-fix

Remove transaction around OCI blobs
This commit is contained in:
josephschorr 2017-03-23 15:04:57 -04:00 committed by GitHub
commit 71e27496db

View file

@ -1,8 +1,11 @@
import logging
from peewee import IntegrityError from peewee import IntegrityError
from data.model import db_transaction from data.model import db_transaction
from data.database import Blob, BlobPlacementLocation, BlobPlacement from data.database import Blob, BlobPlacementLocation, BlobPlacement
logger = logging.getLogger(__name__)
def _ensure_sha256_header(digest): def _ensure_sha256_header(digest):
if digest.startswith('sha256:'): if digest.startswith('sha256:'):
@ -17,21 +20,27 @@ def get_blob(digest):
def get_or_create_blob(digest, size, media_type_name, locations): def get_or_create_blob(digest, size, media_type_name, locations):
""" Try to find a blob by its digest or create it. """ """ Try to find a blob by its digest or create it. """
with db_transaction():
try:
blob = get_blob(digest)
except Blob.DoesNotExist:
blob = Blob.create(digest=_ensure_sha256_header(digest),
media_type_id=Blob.media_type.get_id(media_type_name),
size=size)
for location_name in locations:
location_id = BlobPlacement.location.get_id(location_name)
try:
BlobPlacement.create(blob=blob, location=location_id)
except IntegrityError:
pass
return blob # Get or create the blog entry for the digest.
try:
blob = get_blob(digest)
logger.debug('Retrieved blob with digest %s', digest)
except Blob.DoesNotExist:
blob = Blob.create(digest=_ensure_sha256_header(digest),
media_type_id=Blob.media_type.get_id(media_type_name),
size=size)
logger.debug('Created blob with digest %s', digest)
# Add the locations to the blob.
for location_name in locations:
location_id = BlobPlacement.location.get_id(location_name)
try:
BlobPlacement.create(blob=blob, location=location_id)
except IntegrityError:
logger.debug('Location %s already existing for blob %s', location_name, blob.id)
pass
return blob
def get_blob_locations(digest): def get_blob_locations(digest):