Try to ensure we'll have a truly random postfix

While it is very improbable, it seemed fairly straight forward to add
a small bit of code to ensure the temporary file name is random and
won't clash with another host, pid or thread potentially working on
the same object.

BUG: XXXXXX (https://bugzilla.redhat.com/show_bug.cgi?id=XXXXXX)

Change-Id: I862021b725efbfe58b98754c4470aef4882eb8f7
Signed-off-by: Peter Portante <peter.portante@redhat.com>
Reviewed-on: http://review.gluster.org/5671
Reviewed-by: Luis Pabon <lpabon@redhat.com>
Tested-by: Luis Pabon <lpabon@redhat.com>
This commit is contained in:
Peter Portante 2013-08-20 19:13:42 -04:00 committed by Luis Pabon
parent 4735980723
commit 01444e6b6d

View File

@ -17,10 +17,16 @@ import os
import stat import stat
import fcntl import fcntl
import errno import errno
import random try:
from random import SystemRandom
random = SystemRandom()
except ImportError:
import random
import logging import logging
from socket import gethostname
from hashlib import md5 from hashlib import md5
from eventlet import sleep from eventlet import sleep
from greenlet import getcurrent
from contextlib import contextmanager from contextlib import contextmanager
from swift.common.utils import TRUE_VALUES, drop_buffer_cache, ThreadPool from swift.common.utils import TRUE_VALUES, drop_buffer_cache, ThreadPool
from swift.common.exceptions import DiskFileNotExist, DiskFileError, \ from swift.common.exceptions import DiskFileNotExist, DiskFileError, \
@ -53,6 +59,9 @@ DISALLOWED_HEADERS = set('content-length content-type deleted etag'.split())
MAX_RENAME_ATTEMPTS = 10 MAX_RENAME_ATTEMPTS = 10
MAX_OPEN_ATTEMPTS = 10 MAX_OPEN_ATTEMPTS = 10
_cur_pid = str(os.getpid())
_cur_host = str(gethostname())
def _random_sleep(): def _random_sleep():
sleep(random.uniform(0.5, 0.15)) sleep(random.uniform(0.5, 0.15))
@ -623,9 +632,11 @@ class DiskFile(SwiftDiskFile):
# Assume the full directory path exists to the file already, and # Assume the full directory path exists to the file already, and
# construct the proper name for the temporary file. # construct the proper name for the temporary file.
attempts = 1 attempts = 1
cur_thread = str(getcurrent())
while True: while True:
tmpfile = '.' + self._obj + '.' + md5(self._obj + postfix = md5(self._obj + _cur_host + _cur_pid + cur_thread
str(random.random())).hexdigest() + str(random.random())).hexdigest()
tmpfile = '.' + self._obj + '.' + postfix
tmppath = os.path.join(self.put_datadir, tmpfile) tmppath = os.path.join(self.put_datadir, tmpfile)
try: try:
fd = do_open(tmppath, fd = do_open(tmppath,