Skip to content
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

Make upload more robust by ignoring spurious errors while polling the scan status. #480

Merged
merged 1 commit into from Apr 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions snapcraft/storeapi/_upload.py
Expand Up @@ -20,13 +20,13 @@
import time
import os

import requests
from concurrent.futures import ThreadPoolExecutor
from progressbar import (ProgressBar, Percentage, Bar, AnimatedMarker)
from requests_toolbelt import (MultipartEncoder, MultipartEncoderMonitor)

from .common import (
get_oauth_session,
is_scan_completed,
retry,
)
from .compat import open, quote_plus, urljoin
Expand Down Expand Up @@ -318,6 +318,30 @@ def get_post_files(metadata=None):
return files


def is_scan_completed(response):
"""Return True if the response indicates the scan process completed."""
if response is None:
# To cope with spurious connection failures lacking a proper response:
# either we'll retry and succeed or we fail for all retries and report
# an error.
return False
if response.ok:
return response.json().get('completed', False)
return False


def get_scan_status(session, url):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I would name these helper funcions with a leading underscore, to make it clear that they are called only from higher level functions.

Copy link
Author

Choose a reason for hiding this comment

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

There is a refactor in progress in another branch, I'd rather address that there ?

Copy link
Contributor

Choose a reason for hiding this comment

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

works for me.

try:
resp = session.get(url)
return resp
except (requests.ConnectionError, requests.HTTPError):
Copy link
Collaborator

Choose a reason for hiding this comment

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

What was HTTPError, will we loop forever if the resource does not exist?

Copy link
Author

Choose a reason for hiding this comment

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

What was HTTPError

I don't remember encountering it in this particular case but the code involved can trigger it. I can remove it if you prefer.

will we loop forever if the resource does not exist?

No. The caller retries a fixed number of times and will report the last error.

# Something went wrong and we couldn't acquire the status. Upper
# level (is_scan_completed) will deal with the None response
# meaning we don't know the status. This avoid a spurious
# connection error breaking an upload for a wrong reason.
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

According to [1], we should return an exception instead of None. That makes sense because the name of the exception will make it's meaning clearer than None.

[1] https://www.goodreads.com/book/show/23020812-effective-python

Copy link
Author

Choose a reason for hiding this comment

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

we should return an exception instead of None.

No, that's the bug. The caller (intermediate, hidden, out of our reach) can't catch it nor act properly. Returning None tells the caller: nah, not completed yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, @Retry doesn't let you handle the exception. The comments make this clear, so I'm ok with this.



def get_scan_data(session, status_url):
"""Return metadata about the state of the upload scan process."""
# initial retry after 5 seconds
Expand All @@ -328,7 +352,7 @@ def get_scan_data(session, status_url):
delay=SCAN_STATUS_POLL_DELAY,
backoff=1)
def get_status():
return session.get(status_url)
return get_scan_status(session, status_url)

response, aborted = get_status()

Expand Down
7 changes: 0 additions & 7 deletions snapcraft/storeapi/common.py
Expand Up @@ -67,13 +67,6 @@ def store_api_call(path, session=None, method='GET', data=None):
return result


def is_scan_completed(response):
"""Return True if the response indicates the scan process completed."""
if response.ok:
return response.json().get('completed', False)
return False


def retry(terminator=None, retries=3, delay=3, backoff=2, logger=None):
"""Decorate a function to automatically retry calling it on failure.

Expand Down
34 changes: 33 additions & 1 deletion snapcraft/tests/test_storeapi_upload.py
Expand Up @@ -17,13 +17,20 @@
import json
import os
import tempfile
import unittest

from mock import ANY, call, patch
from requests import Response
from requests import (
ConnectionError,
HTTPError,
Response,
)

from snapcraft import tests
from snapcraft.storeapi._upload import (
get_upload_url,
get_scan_status,
is_scan_completed,
upload_app,
upload_files,
upload,
Expand Down Expand Up @@ -650,3 +657,28 @@ def test_get_upload_url(self):
upload_url = "http://example.com/click-package-upload/app.dev/"
url = get_upload_url('app.dev')
self.assertEqual(url, upload_url)


class FakeSession(object):

def __init__(self, exc):
self.exc = exc

def get(self, url):
raise self.exc()


class ScanStatusTestCase(unittest.TestCase):

def test_is_scan_complete_for_none(self):
self.assertFalse(is_scan_completed(None))

def get_scan_status(self, exc):
raiser = FakeSession(exc)
return get_scan_status(raiser, 'foo')

def test_get_status_connection_error(self):
self.assertIsNone(self.get_scan_status(ConnectionError))

def test_get_status_http_error(self):
self.assertIsNone(self.get_scan_status(HTTPError))