diff --git a/swift/common/utils.py b/swift/common/utils.py index 2c8bd1d622..a71a9128ae 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -776,7 +776,7 @@ def readconf(conf, section_name=None, log_name=None, defaults=None): return conf -def write_pickle(obj, dest, tmp): +def write_pickle(obj, dest, tmp, pickle_protocol=0): """ Ensure that a pickle file gets written to disk. The file is first written to a tmp location, ensure it is synced to disk, then @@ -785,10 +785,11 @@ def write_pickle(obj, dest, tmp): :param obj: python object to be pickled :param dest: path of final destination file :param tmp: path to tmp to use + :param pickle_protocol: protocol to pickle the obj with, defaults to 0 """ - fd, tmppath = mkstemp(dir=tmp) + fd, tmppath = mkstemp(dir=tmp, suffix='.tmp') with os.fdopen(fd, 'wb') as fo: - pickle.dump(obj, fo) + pickle.dump(obj, fo, pickle_protocol) fo.flush() os.fsync(fd) renamer(tmppath, dest) diff --git a/swift/obj/replicator.py b/swift/obj/replicator.py index 01c0da056f..f58bace4b8 100644 --- a/swift/obj/replicator.py +++ b/swift/obj/replicator.py @@ -30,7 +30,7 @@ from eventlet.support.greenlets import GreenletExit from swift.common.ring import Ring from swift.common.utils import whataremyips, unlink_older_than, lock_path, \ - renamer, compute_eta, get_logger + compute_eta, get_logger, write_pickle from swift.common.bufferedhttp import http_connect from swift.common.daemon import Daemon @@ -105,9 +105,7 @@ def invalidate_hash(suffix_dir): except Exception: return hashes[suffix] = None - with open(hashes_file + '.tmp', 'wb') as fp: - pickle.dump(hashes, fp, PICKLE_PROTOCOL) - renamer(hashes_file + '.tmp', hashes_file) + write_pickle(hashes, hashes_file, partition_dir, PICKLE_PROTOCOL) def get_hashes(partition_dir, recalculate=[], do_listdir=False, @@ -157,9 +155,7 @@ def get_hashes(partition_dir, recalculate=[], do_listdir=False, modified = True sleep() if modified: - with open(hashes_file + '.tmp', 'wb') as fp: - pickle.dump(hashes, fp, PICKLE_PROTOCOL) - renamer(hashes_file + '.tmp', hashes_file) + write_pickle(hashes, hashes_file, partition_dir, PICKLE_PROTOCOL) return hashed, hashes