From 6dbac845d5096063d6b344cd0314d0b50f7c50b0 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Fri, 17 Aug 2012 19:44:21 +0800 Subject: [PATCH] Retuen a reasonable response to client. As we use the 'account_autocreate' for auto-create accounts, we send a request to HEAD an account which is just deleted(the account-reaper not reclaim it immediately), the proxy-server will return 'HTTP 500 Internal Server Error'. In my opinion, this is unreasonable, so I change the code in swift/proxy /server.py for returning a reasonable response. I modified the code in POST, GETorHEAD and account_info. At last, I modified some code in the unittest test/unit/proxy/test_server.py. Change-Id: Ib057b387c9da073d707ffae49ead54206a8fb7dd Fixes: Bug #1037889 --- swift/proxy/server.py | 14 ++++++++---- test/unit/proxy/test_server.py | 42 ++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/swift/proxy/server.py b/swift/proxy/server.py index 6061cc5f4b..c42632cc21 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -439,7 +439,9 @@ class Controller(object): self.app.account_ring, partition, 'PUT', path, [headers] * len(nodes)) if not is_success(resp.status_int): - raise Exception('Could not autocreate account %r' % path) + self.app.logger.warning('Could not autocreate account %r' % \ + path) + return None, None, None result_code = HTTP_OK if self.app.memcache and result_code in (HTTP_OK, HTTP_NOT_FOUND): if result_code == HTTP_OK: @@ -1808,8 +1810,9 @@ class AccountController(Controller): self.app.account_ring, partition, 'PUT', '/' + self.account_name, [headers] * len(nodes)) if not is_success(resp.status_int): - raise Exception('Could not autocreate account %r' % - self.account_name) + self.app.logger.warning('Could not autocreate account %r' % + self.account_name) + return resp resp = self.GETorHEAD_base(req, _('Account'), partition, nodes, req.path_info.rstrip('/'), len(nodes)) self.app.logger.timing_since('%s.timing' % (stats_type,), start_time) @@ -1876,8 +1879,9 @@ class AccountController(Controller): self.app.account_ring, account_partition, 'PUT', '/' + self.account_name, [headers] * len(accounts)) if not is_success(resp.status_int): - raise Exception('Could not autocreate account %r' % - self.account_name) + self.app.logger.warning('Could not autocreate account %r' % + self.account_name) + return resp self.app.logger.timing_since('POST.timing', start_time) return resp diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index 329b4597ef..6eddf9e152 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -450,13 +450,28 @@ class TestController(unittest.TestCase): proxy_server.http_connect = \ fake_http_connect(404, 404, 404, 503, 201, 503) exc = None - try: - partition, nodes, count = \ - self.controller.account_info(self.account, autocreate=True) - except Exception, err: - exc = err - self.assertEquals(str(exc), - "Could not autocreate account '/some_account'") + partition, nodes, count = \ + self.controller.account_info(self.account, autocreate=True) + self.check_account_info_return(partition, nodes, is_none=True) + self.assertEquals(None, count) + + self.memcache.store = {} + proxy_server.http_connect = \ + fake_http_connect(404, 404, 404, 403, 403, 403) + exc = None + partition, nodes, count = \ + self.controller.account_info(self.account, autocreate=True) + self.check_account_info_return(partition, nodes, is_none=True) + self.assertEquals(None, count) + + self.memcache.store = {} + proxy_server.http_connect = \ + fake_http_connect(404, 404, 404, 409, 409, 409) + exc = None + partition, nodes, count = \ + self.controller.account_info(self.account, autocreate=True) + self.check_account_info_return(partition, nodes, is_none=True) + self.assertEquals(None, count) def check_container_info_return(self, ret, is_none=False): if is_none: @@ -3812,6 +3827,11 @@ class TestAccountController(unittest.TestCase): controller.app.account_autocreate = True self.assert_status_map(controller.GET, (404, 404, 404, 201, 201, 201, 204), 204) + self.assert_status_map(controller.GET, + (404, 404, 404, 403, 403, 403, 403), 403) + self.assert_status_map(controller.GET, + (404, 404, 404, 409, 409, 409, 409), 409) + def test_HEAD(self): with save_globals(): @@ -3840,6 +3860,10 @@ class TestAccountController(unittest.TestCase): controller.app.account_autocreate = True self.assert_status_map(controller.HEAD, (404, 404, 404, 201, 201, 201, 204), 204) + self.assert_status_map(controller.HEAD, + (404, 404, 404, 403, 403, 403, 403), 403) + self.assert_status_map(controller.HEAD, + (404, 404, 404, 409, 409, 409, 409), 409) def test_POST_autocreate(self): with save_globals(): @@ -3850,6 +3874,10 @@ class TestAccountController(unittest.TestCase): controller.app.account_autocreate = True self.assert_status_map(controller.POST, (404, 404, 404, 201, 201, 201), 201) + self.assert_status_map(controller.POST, + (404, 404, 404, 403, 403, 403, 403), 403) + self.assert_status_map(controller.POST, + (404, 404, 404, 409, 409, 409, 409), 409) def test_connection_refused(self): self.app.account_ring.get_nodes('account')