From c653566f4aaf9a4e0cb9d2094b11e60b536335ad Mon Sep 17 00:00:00 2001 From: Christopher Bartz Date: Fri, 11 Aug 2017 12:07:24 +0200 Subject: [PATCH] headers_to_account_info include per policy stats This commit adds per-policy stats to proxy.controllers.base.headers_to_account_info Change-Id: I800266d15aabcc7b6e0234de3c9b965b5c15a623 Closes-Bug: #1675776 --- swift/proxy/controllers/base.py | 12 ++++++++++ test/unit/proxy/controllers/test_base.py | 28 ++++++++++++++++++++++-- test/unit/proxy/test_server.py | 8 +++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/swift/proxy/controllers/base.py b/swift/proxy/controllers/base.py index f7f6e7a20c..a7ae38462d 100644 --- a/swift/proxy/controllers/base.py +++ b/swift/proxy/controllers/base.py @@ -144,6 +144,18 @@ def headers_to_account_info(headers, status_int=HTTP_OK): 'container_count': headers.get('x-account-container-count'), 'total_object_count': headers.get('x-account-object-count'), 'bytes': headers.get('x-account-bytes-used'), + 'storage_policies': {policy.idx: { + 'container_count': int(headers.get( + 'x-account-storage-policy-{}-container-count'.format( + policy.name), 0)), + 'object_count': int(headers.get( + 'x-account-storage-policy-{}-object-count'.format( + policy.name), 0)), + 'bytes': int(headers.get( + 'x-account-storage-policy-{}-bytes-used'.format( + policy.name), 0))} + for policy in POLICIES + }, 'meta': meta, 'sysmeta': sysmeta, } diff --git a/test/unit/proxy/controllers/test_base.py b/test/unit/proxy/controllers/test_base.py index b241a5cec1..8351843d71 100644 --- a/test/unit/proxy/controllers/test_base.py +++ b/test/unit/proxy/controllers/test_base.py @@ -26,8 +26,8 @@ from swift.common import exceptions from swift.common.utils import split_path from swift.common.header_key_dict import HeaderKeyDict from swift.common.http import is_success -from swift.common.storage_policy import StoragePolicy -from test.unit import fake_http_connect, FakeRing, FakeMemcache +from swift.common.storage_policy import StoragePolicy, StoragePolicyCollection +from test.unit import fake_http_connect, FakeRing, FakeMemcache, PatchPolicies from swift.proxy import server as proxy_server from swift.common.request_helpers import ( get_sys_meta_prefix, get_object_transient_sysmeta @@ -615,6 +615,30 @@ class TestFuncs(unittest.TestCase): resp, headers_to_account_info(headers.items(), 200)) + def test_headers_to_account_info_storage_policies(self): + headers = { + 'x-account-storage-policy-zero-object-count': '13', + 'x-account-storage-policy-zero-container-count': '120', + 'x-account-storage-policy-zero-bytes-used': '1002', + 'x-account-storage-policy-one-object-count': '10', + 'x-account-storage-policy-one-container-count': '20', + } + spc = StoragePolicyCollection([StoragePolicy(0, 'zero', True), + StoragePolicy(1, 'one', False)]) + with PatchPolicies(spc): + resp = headers_to_account_info(headers.items(), 200) + self.assertEqual( + resp['storage_policies'][0]['object_count'], 13) + self.assertEqual( + resp['storage_policies'][0]['container_count'], 120) + self.assertEqual( + resp['storage_policies'][0]['bytes'], 1002) + self.assertEqual( + resp['storage_policies'][1]['object_count'], 10) + self.assertEqual( + resp['storage_policies'][1]['container_count'], 20) + self.assertEqual(resp['storage_policies'][1]['bytes'], 0) + def test_headers_to_object_info_missing(self): resp = headers_to_object_info({}, 404) self.assertEqual(resp['status'], 404) diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index 0b38658130..7f3e9c5be7 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -331,6 +331,10 @@ class TestController(unittest.TestCase): 'container_count': '12345', 'total_object_count': None, 'bytes': None, + 'storage_policies': {p.idx: { + 'container_count': 0, + 'object_count': 0, + 'bytes': 0} for p in POLICIES}, 'meta': {}, 'sysmeta': {}} self.assertEqual(container_info, @@ -358,6 +362,10 @@ class TestController(unittest.TestCase): 'container_count': None, # internally keep None 'total_object_count': None, 'bytes': None, + 'storage_policies': {p.idx: { + 'container_count': 0, + 'object_count': 0, + 'bytes': 0} for p in POLICIES}, 'meta': {}, 'sysmeta': {}} self.assertEqual(account_info,