Skip to content

Commit

Permalink
Write out ring.gz's in a safer fashion
Browse files Browse the repository at this point in the history
Write the ring file to a temporary location first and then move it into
place. This should help prevent issues like reading a partial ring file
while a new instances is being written out.

Change-Id: I28357a2f038a51cb9d823822b92f736dff033a9e
  • Loading branch information
Florian Hines committed Mar 26, 2014
1 parent 86668aa commit c6cebb6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions swift/common/ring/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from io import BufferedReader
from hashlib import md5
from itertools import chain
from tempfile import NamedTemporaryFile

from swift.common.utils import hash_path, validate_configuration, json
from swift.common.ring.utils import tiers_for_dev
Expand Down Expand Up @@ -108,12 +109,18 @@ def save(self, filename):
#
# This only works on Python 2.7; on 2.6, we always get the
# current time in the gzip output.
tempf = NamedTemporaryFile(dir=".", prefix=filename, delete=False)
try:
gz_file = GzipFile(filename, 'wb', mtime=1300507380.0)
gz_file = GzipFile(filename, mode='wb', fileobj=tempf,
mtime=1300507380.0)
except TypeError:
gz_file = GzipFile(filename, 'wb')
gz_file = GzipFile(filename, mode='wb', fileobj=tempf)
self.serialize_v1(gz_file)
gz_file.close()
tempf.flush()
os.fsync(tempf.fileno())
tempf.close()
os.rename(tempf.name, filename)

def to_dict(self):
return {'devs': self.devs,
Expand Down

0 comments on commit c6cebb6

Please sign in to comment.