Add timeout to requests.get() in kolla/cmd/build.py

When kolla-build is running, if there are network issues or if the
source's location (like http://tarballs.openstack.org) fails to
respond due to high number of concurrent requests, kolla-build just
hangs/blocks indefinitely.

This patch set resolves this issue by adding a timeout of 120
seconds for requests.get() in kolla/cmd/build.py, adds a unit test
for it in kolla/tests/test_build.py and also the "timeout" argument
in kolla/common/config.py.

Change-Id: I7c8745a20b9bd1c3f5d6a55c72a794f16fd7e513
Closes-Bug: #1548614
This commit is contained in:
Vikram Hosakote 2016-02-23 21:04:04 +00:00
parent c81097df8b
commit 320c1f9272
3 changed files with 32 additions and 1 deletions

View File

@ -164,7 +164,14 @@ class WorkerThread(threading.Thread):
if source.get('type') == 'url': if source.get('type') == 'url':
LOG.debug("%s:Getting archive from %s", image['name'], LOG.debug("%s:Getting archive from %s", image['name'],
source['source']) source['source'])
r = requests.get(source['source']) try:
r = requests.get(source['source'], timeout=self.conf.timeout)
except requests_exc.Timeout:
LOG.exception('Request timed out while getting archive'
' from %s', source['source'])
image['status'] = "error"
image['logs'] = str()
return
if r.status_code == 200: if r.status_code == 200:
with open(dest_archive, 'wb') as f: with open(dest_archive, 'wb') as f:

View File

@ -131,6 +131,8 @@ _CLI_OPTS = [
cfg.BoolOpt('template-only', default=False, cfg.BoolOpt('template-only', default=False,
deprecated_group='kolla-build', deprecated_group='kolla-build',
help=("Don't build images. Generate Dockerfile only")), help=("Don't build images. Generate Dockerfile only")),
cfg.IntOpt('timeout', default=120,
help='Time in seconds after which any operation times out'),
] ]
_BASE_OPTS = [ _BASE_OPTS = [

View File

@ -14,6 +14,7 @@ import fixtures
import itertools import itertools
import mock import mock
import os import os
import requests
from kolla.cmd import build from kolla.cmd import build
from kolla.tests import base from kolla.tests import base
@ -106,6 +107,27 @@ class WorkerThreadTest(base.TestCase):
nocache=False, rm=True, pull=True, forcerm=True, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args) buildargs=build_args)
@mock.patch('docker.Client')
@mock.patch('requests.get')
def test_requests_get_timeout(self, mock_get, mock_client):
worker = build.WorkerThread(mock.Mock(),
mock.Mock(),
self.conf)
self.image['source'] = {
'source': 'http://fake/source',
'type': 'url',
'name': 'fake-image-base'
}
mock_get.side_effect = requests.exceptions.Timeout
get_result = worker.process_source(self.image,
self.image['source'])
self.assertIsNone(get_result)
self.assertEqual(self.image['status'], 'error')
self.assertEqual(self.image['logs'], str())
mock_get.assert_called_once_with(self.image['source']['source'],
timeout=120)
class KollaWorkerTest(base.TestCase): class KollaWorkerTest(base.TestCase):