From b010ba13ebe90826c89f8fa2a5227eb03c69e205 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Wed, 27 Sep 2017 16:58:21 +0200 Subject: [PATCH] Retry container creation in swift Because of cache management container creation in Swift is somewhat racy, as we create containers lazily. Work around that issue by retrying creation a few times. Change-Id: I4c3671cdb23df1597c2b5c9c581ad7225847d64d Closes-Bug: #1719525 --- zaqar/storage/swift/utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/zaqar/storage/swift/utils.py b/zaqar/storage/swift/utils.py index c48f004b4..23ea5443a 100644 --- a/zaqar/storage/swift/utils.py +++ b/zaqar/storage/swift/utils.py @@ -42,8 +42,22 @@ def _put_or_create_container(client, *args, **kwargs): client.put_object(*args, **kwargs) except swiftclient.ClientException as e: if e.http_status == 404: - client.put_container(args[0]) - client.put_object(*args, **kwargs) + # Because of lazy creation, the container may be used by different + # clients and cause cache problem. Retrying object creation a few + # times should fix this. + for i in range(5): + client.put_container(args[0]) + try: + client.put_object(*args, **kwargs) + except swiftclient.ClientException as ex: + if ex.http_status != 404: + raise + else: + break + else: + # If we got there, we ignored the 5th exception, so the + # exception context will be set. + raise else: raise