moved the txn_id read into a utils helper

This commit is contained in:
Clay Gerrard
2011-01-05 11:15:21 -06:00
parent 6c1bf02a62
commit 25bf6ebfc3
6 changed files with 43 additions and 18 deletions

View File

@@ -29,7 +29,7 @@ import simplejson
from swift.common.db import AccountBroker
from swift.common.utils import get_logger, get_param, hash_path, \
normalize_timestamp, split_path, storage_directory
normalize_timestamp, split_path, storage_directory, get_txn_id
from swift.common.constraints import ACCOUNT_LISTING_LIMIT, \
check_mount, check_float, check_utf8
from swift.common.db_replicator import ReplicatorRpc
@@ -86,7 +86,7 @@ class AccountController(object):
return Response(status='507 %s is not mounted' % drive)
broker = self._get_account_broker(drive, part, account)
if container: # put account container
if 'x-swift-txn-id' in req.headers:
if get_txn_id(req, None) is None:
broker.pending_timeout = 3
if req.headers.get('x-account-override-deleted', 'no').lower() != \
'yes' and broker.is_deleted():
@@ -296,7 +296,7 @@ class AccountController(object):
def __call__(self, env, start_response):
start_time = time.time()
req = Request(env)
self.logger.txn_id = req.headers.get('x-swift-txn-id', None)
self.logger.txn_id = get_txn_id(req, None)
if not check_utf8(req.path_info):
res = HTTPPreconditionFailed(body='Invalid UTF8')
else:
@@ -319,7 +319,7 @@ class AccountController(object):
time.strftime('%d/%b/%Y:%H:%M:%S +0000', time.gmtime()),
req.method, req.path,
res.status.split()[0], res.content_length or '-',
req.headers.get('x-swift-txn-id', '-'),
get_txn_id(req, '-'),
req.referer or '-', req.user_agent or '-',
trans_time,
additional_info)

View File

@@ -107,6 +107,16 @@ def get_param(req, name, default=None):
value.decode('utf8') # Ensure UTF8ness
return value
def get_txn_id(req, default=None):
"""
Get the transaction id from a request
:param req: Webob request object
:param default: value to return if no transaction id is found
"""
return req.headers.get('x-swift-txn-id',
req.headers.get('x-cf-trans-id', default))
def fallocate(fd, size):
"""

View File

@@ -31,7 +31,7 @@ from webob.exc import HTTPAccepted, HTTPBadRequest, HTTPConflict, \
from swift.common.db import ContainerBroker
from swift.common.utils import get_logger, get_param, hash_path, \
normalize_timestamp, storage_directory, split_path
normalize_timestamp, storage_directory, split_path, get_txn_id
from swift.common.constraints import CONTAINER_LISTING_LIMIT, \
check_mount, check_float, check_utf8
from swift.common.bufferedhttp import http_connect
@@ -95,7 +95,7 @@ class ContainerController(object):
'x-delete-timestamp': info['delete_timestamp'],
'x-object-count': info['object_count'],
'x-bytes-used': info['bytes_used'],
'x-swift-txn-id': req.headers.get('x-swift-txn-id', '-')}
'x-swift-txn-id': get_txn_id(req, '-')}
if req.headers.get('x-account-override-deleted', 'no').lower() == \
'yes':
account_headers['x-account-override-deleted'] = 'yes'
@@ -384,7 +384,7 @@ class ContainerController(object):
def __call__(self, env, start_response):
start_time = time.time()
req = Request(env)
self.logger.txn_id = req.headers.get('x-swift-txn-id', None)
self.logger.txn_id = get_txn_id(req, None)
if not check_utf8(req.path_info):
res = HTTPPreconditionFailed(body='Invalid UTF8')
else:
@@ -404,7 +404,7 @@ class ContainerController(object):
time.gmtime()),
req.method, req.path,
res.status.split()[0], res.content_length or '-',
req.headers.get('x-swift-txn-id', '-'),
get_txn_id(req, '-'),
req.referer or '-', req.user_agent or '-',
trans_time)
if req.method.upper() == 'REPLICATE':

View File

@@ -37,7 +37,7 @@ from eventlet import sleep, Timeout
from swift.common.utils import mkdirs, normalize_timestamp, \
storage_directory, hash_path, renamer, fallocate, \
split_path, drop_buffer_cache, get_logger, write_pickle
split_path, drop_buffer_cache, get_logger, write_pickle, get_txn_id
from swift.common.bufferedhttp import http_connect
from swift.common.constraints import check_object_creation, check_mount, \
check_float, check_utf8
@@ -409,7 +409,7 @@ class ObjectController(object):
'x-content-type': file.metadata['Content-Type'],
'x-timestamp': file.metadata['X-Timestamp'],
'x-etag': file.metadata['ETag'],
'x-swift-txn-id': request.headers.get('x-swift-txn-id', '-')},
'x-swift-txn-id': get_txn_id(request, '-')},
device)
resp = HTTPCreated(request=request, etag=etag)
return resp
@@ -531,7 +531,7 @@ class ObjectController(object):
file.unlinkold(metadata['X-Timestamp'])
self.container_update('DELETE', account, container, obj,
request.headers, {'x-timestamp': metadata['X-Timestamp'],
'x-swift-txn-id': request.headers.get('x-swift-txn-id', '-')},
'x-swift-txn-id': get_txn_id(request, '-')},
device)
resp = response_class(request=request)
return resp
@@ -562,7 +562,7 @@ class ObjectController(object):
"""WSGI Application entry point for the Swift Object Server."""
start_time = time.time()
req = Request(env)
self.logger.txn_id = req.headers.get('x-swift-txn-id', None)
self.logger.txn_id = get_txn_id(req, None)
if not check_utf8(req.path_info):
res = HTTPPreconditionFailed(body='Invalid UTF8')
else:
@@ -583,7 +583,7 @@ class ObjectController(object):
time.gmtime()),
req.method, req.path, res.status.split()[0],
res.content_length or '-', req.referer or '-',
req.headers.get('x-swift-txn-id', '-'),
get_txn_id(req, '-'),
req.user_agent or '-',
trans_time)
if req.method == 'REPLICATE':

View File

@@ -41,7 +41,7 @@ from webob import Request, Response
from swift.common.ring import Ring
from swift.common.utils import get_logger, normalize_timestamp, split_path, \
cache_from_env
cache_from_env, get_txn_id
from swift.common.bufferedhttp import http_connect
from swift.common.constraints import check_metadata, check_object_creation, \
check_utf8, CONTAINER_LISTING_LIMIT, MAX_ACCOUNT_NAME_LENGTH, \
@@ -1362,7 +1362,7 @@ class ContainerController(Controller):
container_partition, containers = self.app.container_ring.get_nodes(
self.account_name, self.container_name)
headers = {'X-Timestamp': normalize_timestamp(time.time()),
'x-swift-txn-id': self.trans_id}
'x-swift-txn-id': self.trans_id}
statuses = []
reasons = []
bodies = []
@@ -1707,8 +1707,9 @@ class BaseApplication(object):
return HTTPPreconditionFailed(request=req, body='Bad URL')
controller = controller(self, **path_parts)
controller.trans_id = req.headers.get('x-swift-txn-id', '-')
self.logger.txn_id = req.headers.get('x-swift-txn-id', None)
txn_id = get_txn_id(req, None)
controller.trans_id = txn_id or '-'
self.logger.txn_id = txn_id
try:
handler = getattr(controller, req.method)
if not getattr(handler, 'publicly_accessible'):
@@ -1786,7 +1787,7 @@ class Application(BaseApplication):
getattr(req, 'bytes_transferred', 0) or '-',
getattr(response, 'bytes_transferred', 0) or '-',
req.headers.get('etag', '-'),
req.headers.get('x-swift-txn-id', '-'),
get_txn_id(req, '-'),
logged_headers or '-',
trans_time,
)))

View File

@@ -28,6 +28,7 @@ from StringIO import StringIO
from functools import partial
from tempfile import NamedTemporaryFile
from webob import Request
from eventlet import sleep
from swift.common import utils
@@ -80,6 +81,19 @@ class TestUtils(unittest.TestCase):
def setUp(self):
utils.HASH_PATH_SUFFIX = 'endcap'
def test_get_txn_id(self):
req = Request.blank('')
req.headers['X-Swift-Txn-Id'] = 'tx12345'
self.assertEquals(utils.get_txn_id(req), 'tx12345')
environ = {'HTTP_X_CF_TRANS_ID': 'tx67890'}
req = Request.blank('', environ=environ)
self.assertEquals(utils.get_txn_id(req), 'tx67890')
req = Request.blank('')
self.assertEquals(utils.get_txn_id(req), None)
self.assertEquals(utils.get_txn_id(req, '-'), '-')
req.headers['X-Cf-Trans-Id'] = 'tx13579'
self.assertEquals(utils.get_txn_id(req, default='test'), 'tx13579')
def test_normalize_timestamp(self):
""" Test swift.common.utils.normalize_timestamp """
self.assertEquals(utils.normalize_timestamp('1253327593.48174'),