Skip to content

Commit a8a921f

Browse files
greglangeTarmac
authored and
Tarmac
committed
Update get_hashes in objrep to use utils.write_pickle
2 parents 7cedff5 + 80c1cd4 commit a8a921f

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

swift/common/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ def readconf(conf, section_name=None, log_name=None, defaults=None):
776776
return conf
777777

778778

779-
def write_pickle(obj, dest, tmp):
779+
def write_pickle(obj, dest, tmp, pickle_protocol=0):
780780
"""
781781
Ensure that a pickle file gets written to disk. The file
782782
is first written to a tmp location, ensure it is synced to disk, then
@@ -785,10 +785,11 @@ def write_pickle(obj, dest, tmp):
785785
:param obj: python object to be pickled
786786
:param dest: path of final destination file
787787
:param tmp: path to tmp to use
788+
:param pickle_protocol: protocol to pickle the obj with, defaults to 0
788789
"""
789-
fd, tmppath = mkstemp(dir=tmp)
790+
fd, tmppath = mkstemp(dir=tmp, suffix='.tmp')
790791
with os.fdopen(fd, 'wb') as fo:
791-
pickle.dump(obj, fo)
792+
pickle.dump(obj, fo, pickle_protocol)
792793
fo.flush()
793794
os.fsync(fd)
794795
renamer(tmppath, dest)

swift/obj/replicator.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from swift.common.ring import Ring
3232
from swift.common.utils import whataremyips, unlink_older_than, lock_path, \
33-
renamer, compute_eta, get_logger
33+
compute_eta, get_logger, write_pickle
3434
from swift.common.bufferedhttp import http_connect
3535
from swift.common.daemon import Daemon
3636

@@ -105,9 +105,7 @@ def invalidate_hash(suffix_dir):
105105
except Exception:
106106
return
107107
hashes[suffix] = None
108-
with open(hashes_file + '.tmp', 'wb') as fp:
109-
pickle.dump(hashes, fp, PICKLE_PROTOCOL)
110-
renamer(hashes_file + '.tmp', hashes_file)
108+
write_pickle(hashes, hashes_file, partition_dir, PICKLE_PROTOCOL)
111109

112110

113111
def get_hashes(partition_dir, recalculate=[], do_listdir=False,
@@ -157,9 +155,7 @@ def get_hashes(partition_dir, recalculate=[], do_listdir=False,
157155
modified = True
158156
sleep()
159157
if modified:
160-
with open(hashes_file + '.tmp', 'wb') as fp:
161-
pickle.dump(hashes, fp, PICKLE_PROTOCOL)
162-
renamer(hashes_file + '.tmp', hashes_file)
158+
write_pickle(hashes, hashes_file, partition_dir, PICKLE_PROTOCOL)
163159
return hashed, hashes
164160

165161

0 commit comments

Comments
 (0)