Updates to support Havana interim version 1.9.1.

The code changes are basically:

  * Apply refactoring in the DiskFile class to use the new DiskWriter
    abstraction

    * Move and rename our diskfile module to match upstream

  * ThreadPools allow us to remove the tpool usage around fsync

  * Update the Ring subclass to support the get_part() method

  * Update to use the 1.9.1 proxy server unit tests

    * Move the DebugLogger class to test.unit

  * Rebuild the Rings to use the new layout

    * Remove backup ring builder files

  * Update spec files to 1.9.1, and tox to use swift 1.9.1

  * Updated version to 1.9.0-0

Change-Id: Ica12cac8b351627d67500723f1dbd8a54d45f7c8
Signed-off-by: Peter Portante <peter.portante@redhat.com>
Signed-off-by: Luis Pabon <lpabon@redhat.com>
Reviewed-on: http://review.gluster.org/5331
This commit is contained in:
Peter Portante 2013-07-15 16:52:46 -04:00 committed by Luis Pabon
parent 54bb5bec7a
commit 9d4e67e741
27 changed files with 1886 additions and 1248 deletions

View File

@ -44,6 +44,6 @@ class PkgInfo(object):
### ###
### Change the Package version here ### Change the Package version here
### ###
_pkginfo = PkgInfo('1.8.0', '7', 'glusterfs-openstack-swift', False) _pkginfo = PkgInfo('1.9.0', '0', 'glusterfs-openstack-swift', False)
__version__ = _pkginfo.pretty_version __version__ = _pkginfo.pretty_version
__canonical_version__ = _pkginfo.canonical_version __canonical_version__ = _pkginfo.canonical_version

View File

@ -24,7 +24,8 @@ from gluster.swift.common.DiskDir import DiskAccount
class AccountController(server.AccountController): class AccountController(server.AccountController):
def _get_account_broker(self, drive, part, account):
def _get_account_broker(self, drive, part, account, **kwargs):
""" """
Overriden to provide the GlusterFS specific broker that talks to Overriden to provide the GlusterFS specific broker that talks to
Gluster for the information related to servicing a given request Gluster for the information related to servicing a given request
@ -35,7 +36,7 @@ class AccountController(server.AccountController):
:param account: account name :param account: account name
:returns: DiskDir object :returns: DiskDir object
""" """
return DiskAccount(self.root, drive, account, self.logger) return DiskAccount(self.root, drive, account, self.logger, **kwargs)
def app_factory(global_conf, **local_conf): def app_factory(global_conf, **local_conf):

View File

@ -150,7 +150,8 @@ class DiskCommon(object):
""" """
Common fields and methods shared between DiskDir and DiskAccount classes. Common fields and methods shared between DiskDir and DiskAccount classes.
""" """
def __init__(self, root, drive, account, logger): def __init__(self, root, drive, account, logger, pending_timeout=None,
stale_reads_ok=False):
# WARNING: The following four fields are referenced as fields by our # WARNING: The following four fields are referenced as fields by our
# callers outside of this module, do not remove. # callers outside of this module, do not remove.
# Create a dummy db_file in Glusterfs.RUN_DIR # Create a dummy db_file in Glusterfs.RUN_DIR
@ -161,8 +162,8 @@ class DiskCommon(object):
file(_db_file, 'w+') file(_db_file, 'w+')
self.db_file = _db_file self.db_file = _db_file
self.metadata = {} self.metadata = {}
self.pending_timeout = 0 self.pending_timeout = pending_timeout or 10
self.stale_reads_ok = False self.stale_reads_ok = stale_reads_ok
# The following fields are common # The following fields are common
self.root = root self.root = root
assert logger is not None assert logger is not None
@ -287,8 +288,8 @@ class DiskDir(DiskCommon):
""" """
def __init__(self, path, drive, account, container, logger, def __init__(self, path, drive, account, container, logger,
uid=DEFAULT_UID, gid=DEFAULT_GID): uid=DEFAULT_UID, gid=DEFAULT_GID, **kwargs):
super(DiskDir, self).__init__(path, drive, account, logger) super(DiskDir, self).__init__(path, drive, account, logger, **kwargs)
self.uid = int(uid) self.uid = int(uid)
self.gid = int(gid) self.gid = int(gid)
@ -530,8 +531,9 @@ class DiskAccount(DiskCommon):
.update_metadata() .update_metadata()
""" """
def __init__(self, root, drive, account, logger): def __init__(self, root, drive, account, logger, **kwargs):
super(DiskAccount, self).__init__(root, drive, account, logger) super(DiskAccount, self).__init__(root, drive, account, logger,
**kwargs)
# Since accounts should always exist (given an account maps to a # Since accounts should always exist (given an account maps to a
# gluster volume directly, and the mount has already been checked at # gluster volume directly, and the mount has already been checked at

View File

@ -44,7 +44,3 @@ class AlreadyExistsAsDir(GlusterfsException):
class AlreadyExistsAsFile(GlusterfsException): class AlreadyExistsAsFile(GlusterfsException):
pass pass
class DiskFileNoSpace(GlusterfsException):
pass

View File

@ -19,7 +19,6 @@ import errno
import stat import stat
import random import random
import os.path as os_path # noqa import os.path as os_path # noqa
from eventlet import tpool
from eventlet import sleep from eventlet import sleep
from gluster.swift.common.exceptions import FileOrDirNotFoundError, \ from gluster.swift.common.exceptions import FileOrDirNotFoundError, \
NotDirectoryError, GlusterFileSystemOSError, GlusterFileSystemIOError NotDirectoryError, GlusterFileSystemOSError, GlusterFileSystemIOError
@ -243,7 +242,7 @@ def do_rename(old_path, new_path):
def do_fsync(fd): def do_fsync(fd):
try: try:
tpool.execute(os.fsync, fd) os.fsync(fd)
except OSError as err: except OSError as err:
raise GlusterFileSystemOSError( raise GlusterFileSystemOSError(
err.errno, '%s, os.fsync("%s")' % (err.strerror, fd)) err.errno, '%s, os.fsync("%s")' % (err.strerror, fd))

View File

@ -91,6 +91,29 @@ class Ring(ring.Ring):
""" """
return self._get_part_nodes(part) return self._get_part_nodes(part)
def get_part(self, account, container=None, obj=None):
"""
Get the partition for an account/container/object.
:param account: account name
:param container: container name
:param obj: object name
:returns: the partition number
"""
if account.startswith(reseller_prefix):
account = account.replace(reseller_prefix, '', 1)
# Save the account name in the table
# This makes part be the index of the location of the account
# in the list
try:
part = self.account_list.index(account)
except ValueError:
self.account_list.append(account)
part = self.account_list.index(account)
return part
def get_nodes(self, account, container=None, obj=None): def get_nodes(self, account, container=None, obj=None):
""" """
Get the partition and nodes for an account/container/object. Get the partition and nodes for an account/container/object.
@ -117,18 +140,7 @@ class Ring(ring.Ring):
hardware description hardware description
====== =============================================================== ====== ===============================================================
""" """
if account.startswith(reseller_prefix): part = self.get_part(account, container, obj)
account = account.replace(reseller_prefix, '', 1)
# Save the account name in the table
# This makes part be the index of the location of the account
# in the list
try:
part = self.account_list.index(account)
except ValueError:
self.account_list.append(account)
part = self.account_list.index(account)
return part, self._get_part_nodes(part) return part, self._get_part_nodes(part)
def get_more_nodes(self, part): def get_more_nodes(self, part):
@ -141,4 +153,4 @@ class Ring(ring.Ring):
See :func:`get_nodes` for a description of the node dicts. See :func:`get_nodes` for a description of the node dicts.
Should never be called in the swift UFO environment, so yield nothing Should never be called in the swift UFO environment, so yield nothing
""" """
yield self.false_node return []

View File

@ -33,7 +33,7 @@ class ContainerController(server.ContainerController):
directly). directly).
""" """
def _get_container_broker(self, drive, part, account, container): def _get_container_broker(self, drive, part, account, container, **kwargs):
""" """
Overriden to provide the GlusterFS specific broker that talks to Overriden to provide the GlusterFS specific broker that talks to
Gluster for the information related to servicing a given request Gluster for the information related to servicing a given request
@ -45,7 +45,8 @@ class ContainerController(server.ContainerController):
:param container: container name :param container: container name
:returns: DiskDir object, a duck-type of DatabaseBroker :returns: DiskDir object, a duck-type of DatabaseBroker
""" """
return DiskDir(self.root, drive, account, container, self.logger) return DiskDir(self.root, drive, account, container, self.logger,
**kwargs)
def account_update(self, req, account, container, broker): def account_update(self, req, account, container, broker):
""" """

View File

@ -22,11 +22,12 @@ import logging
from hashlib import md5 from hashlib import md5
from eventlet import sleep from eventlet import sleep
from contextlib import contextmanager from contextlib import contextmanager
from swift.common.utils import TRUE_VALUES, fallocate 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, \
DiskFileNoSpace, DiskFileDeviceUnavailable
from gluster.swift.common.exceptions import GlusterFileSystemOSError, \ from gluster.swift.common.exceptions import GlusterFileSystemOSError
DiskFileNoSpace from gluster.swift.common.Glusterfs import mount
from gluster.swift.common.fs_utils import do_fstat, do_open, do_close, \ from gluster.swift.common.fs_utils import do_fstat, do_open, do_close, \
do_unlink, do_chown, os_path, do_fsync, do_fchown, do_stat do_unlink, do_chown, os_path, do_fsync, do_fchown, do_stat
from gluster.swift.common.utils import read_metadata, write_metadata, \ from gluster.swift.common.utils import read_metadata, write_metadata, \
@ -37,13 +38,15 @@ from gluster.swift.common.utils import X_CONTENT_LENGTH, X_CONTENT_TYPE, \
FILE_TYPE, DEFAULT_UID, DEFAULT_GID, DIR_NON_OBJECT, DIR_OBJECT FILE_TYPE, DEFAULT_UID, DEFAULT_GID, DIR_NON_OBJECT, DIR_OBJECT
from ConfigParser import ConfigParser, NoSectionError, NoOptionError from ConfigParser import ConfigParser, NoSectionError, NoOptionError
from swift.obj.server import DiskFile from swift.obj.diskfile import DiskFile as SwiftDiskFile
from swift.obj.diskfile import DiskWriter as SwiftDiskWriter
# FIXME: Hopefully we'll be able to move to Python 2.7+ where O_CLOEXEC will # FIXME: Hopefully we'll be able to move to Python 2.7+ where O_CLOEXEC will
# be back ported. See http://www.python.org/dev/peps/pep-0433/ # be back ported. See http://www.python.org/dev/peps/pep-0433/
O_CLOEXEC = 02000000 O_CLOEXEC = 02000000
DEFAULT_DISK_CHUNK_SIZE = 65536 DEFAULT_DISK_CHUNK_SIZE = 65536
DEFAULT_BYTES_PER_SYNC = (512 * 1024 * 1024)
# keep these lower-case # keep these lower-case
DISALLOWED_HEADERS = set('content-length content-type deleted etag'.split()) DISALLOWED_HEADERS = set('content-length content-type deleted etag'.split())
@ -235,16 +238,10 @@ if _fs_conf.read(os.path.join('/etc/swift', 'fs.conf')):
in TRUE_VALUES in TRUE_VALUES
except (NoSectionError, NoOptionError): except (NoSectionError, NoOptionError):
_relaxed_writes = False _relaxed_writes = False
try:
_preallocate = _fs_conf.get('DEFAULT', 'preallocate', "no") \
in TRUE_VALUES
except (NoSectionError, NoOptionError):
_preallocate = False
else: else:
_mkdir_locking = False _mkdir_locking = False
_use_put_mount = False _use_put_mount = False
_relaxed_writes = False _relaxed_writes = False
_preallocate = False
if _mkdir_locking: if _mkdir_locking:
make_directory = _make_directory_locked make_directory = _make_directory_locked
@ -275,7 +272,153 @@ def _adjust_metadata(metadata):
return metadata return metadata
class Gluster_DiskFile(DiskFile): class DiskWriter(SwiftDiskWriter):
"""
Encapsulation of the write context for servicing PUT REST API
requests. Serves as the context manager object for DiskFile's writer()
method.
We just override the put() method for Gluster.
"""
def put(self, metadata, extension='.data'):
"""
Finalize writing the file on disk, and renames it from the temp file
to the real location. This should be called after the data has been
written to the temp file.
:param metadata: dictionary of metadata to be written
:param extension: extension to be used when making the file
"""
# Our caller will use '.data' here; we just ignore it since we map the
# URL directly to the file system.
assert self.tmppath is not None
metadata = _adjust_metadata(metadata)
df = self.disk_file
if dir_is_object(metadata):
if not df.data_file:
# Does not exist, create it
data_file = os.path.join(df._obj_path, df._obj)
_, df.metadata = self.threadpool.force_run_in_thread(
df._create_dir_object, data_file, metadata)
df.data_file = os.path.join(df._container_path, data_file)
elif not df.is_dir:
# Exists, but as a file
raise DiskFileError('DiskFile.put(): directory creation failed'
' since the target, %s, already exists as'
' a file' % df.data_file)
return
if df._is_dir:
# A pre-existing directory already exists on the file
# system, perhaps gratuitously created when another
# object was created, or created externally to Swift
# REST API servicing (UFO use case).
raise DiskFileError('DiskFile.put(): file creation failed since'
' the target, %s, already exists as a'
' directory' % df.data_file)
def finalize_put():
# Write out metadata before fsync() to ensure it is also forced to
# disk.
write_metadata(self.fd, metadata)
if not _relaxed_writes:
# We call fsync() before calling drop_cache() to lower the
# amount of redundant work the drop cache code will perform on
# the pages (now that after fsync the pages will be all
# clean).
do_fsync(self.fd)
# From the Department of the Redundancy Department, make sure
# we call drop_cache() after fsync() to avoid redundant work
# (pages all clean).
drop_buffer_cache(self.fd, 0, self.upload_size)
# At this point we know that the object's full directory path
# exists, so we can just rename it directly without using Swift's
# swift.common.utils.renamer(), which makes the directory path and
# adds extra stat() calls.
data_file = os.path.join(df.put_datadir, df._obj)
while True:
try:
os.rename(self.tmppath, data_file)
except OSError as err:
if err.errno in (errno.ENOENT, errno.EIO):
# FIXME: Why either of these two error conditions is
# happening is unknown at this point. This might be a
# FUSE issue of some sort or a possible race
# condition. So let's sleep on it, and double check
# the environment after a good nap.
_random_sleep()
# Tease out why this error occurred. The man page for
# rename reads:
# "The link named by tmppath does not exist; or, a
# directory component in data_file does not exist;
# or, tmppath or data_file is an empty string."
assert len(self.tmppath) > 0 and len(data_file) > 0
tpstats = do_stat(self.tmppath)
tfstats = do_fstat(self.fd)
assert tfstats
if not tpstats or tfstats.st_ino != tpstats.st_ino:
# Temporary file name conflict
raise DiskFileError(
'DiskFile.put(): temporary file, %s, was'
' already renamed (targeted for %s)' % (
self.tmppath, data_file))
else:
# Data file target name now has a bad path!
dfstats = do_stat(self.put_datadir)
if not dfstats:
raise DiskFileError(
'DiskFile.put(): path to object, %s, no'
' longer exists (targeted for %s)' % (
df.put_datadir,
data_file))
else:
is_dir = stat.S_ISDIR(dfstats.st_mode)
if not is_dir:
raise DiskFileError(
'DiskFile.put(): path to object, %s,'
' no longer a directory (targeted for'
' %s)' % (df.put_datadir,
data_file))
else:
# Let's retry since everything looks okay
logging.warn(
"DiskFile.put(): os.rename('%s','%s')"
" initially failed (%s) but a"
" stat('%s') following that succeeded:"
" %r" % (
self.tmppath, data_file,
str(err), df.put_datadir,
dfstats))
continue
else:
raise GlusterFileSystemOSError(
err.errno, "%s, os.rename('%s', '%s')" % (
err.strerror, self.tmppath, data_file))
else:
# Success!
break
# Close here so the calling context does not have to perform this
# in a thread.
do_close(self.fd)
self.threadpool.force_run_in_thread(finalize_put)
# Avoid the unlink() system call as part of the mkstemp context
# cleanup
self.tmppath = None
df.metadata = metadata
df._filter_metadata()
# Mark that it actually exists now
df.data_file = os.path.join(df.datadir, df._obj)
class DiskFile(SwiftDiskFile):
""" """
Manage object files on disk. Manage object files on disk.
@ -294,6 +437,12 @@ class Gluster_DiskFile(DiskFile):
:param logger: logger object for writing out log file messages :param logger: logger object for writing out log file messages
:param keep_data_fp: if True, don't close the fp, otherwise close it :param keep_data_fp: if True, don't close the fp, otherwise close it
:param disk_chunk_Size: size of chunks on file reads :param disk_chunk_Size: size of chunks on file reads
:param bytes_per_sync: number of bytes between fdatasync calls
:param iter_hook: called when __iter__ returns a chunk
:param threadpool: thread pool in which to do blocking operations
:param obj_dir: ignored
:param mount_check: check the target device is a mount point and not on the
root volume
:param uid: user ID disk object should assume (file or directory) :param uid: user ID disk object should assume (file or directory)
:param gid: group ID disk object should assume (file or directory) :param gid: group ID disk object should assume (file or directory)
""" """
@ -301,9 +450,16 @@ class Gluster_DiskFile(DiskFile):
def __init__(self, path, device, partition, account, container, obj, def __init__(self, path, device, partition, account, container, obj,
logger, keep_data_fp=False, logger, keep_data_fp=False,
disk_chunk_size=DEFAULT_DISK_CHUNK_SIZE, disk_chunk_size=DEFAULT_DISK_CHUNK_SIZE,
uid=DEFAULT_UID, gid=DEFAULT_GID, iter_hook=None): bytes_per_sync=DEFAULT_BYTES_PER_SYNC, iter_hook=None,
threadpool=None, obj_dir='objects', mount_check=False,
disallowed_metadata_keys=None, uid=DEFAULT_UID,
gid=DEFAULT_GID):
if mount_check and not mount(path, device):
raise DiskFileDeviceUnavailable()
self.disk_chunk_size = disk_chunk_size self.disk_chunk_size = disk_chunk_size
self.bytes_per_sync = bytes_per_sync
self.iter_hook = iter_hook self.iter_hook = iter_hook
self.threadpool = threadpool or ThreadPool(nthreads=0)
obj = obj.strip(os.path.sep) obj = obj.strip(os.path.sep)
if os.path.sep in obj: if os.path.sep in obj:
@ -326,7 +482,6 @@ class Gluster_DiskFile(DiskFile):
else: else:
self.put_datadir = self.datadir self.put_datadir = self.datadir
self._is_dir = False self._is_dir = False
self.tmppath = None
self.logger = logger self.logger = logger
self.metadata = {} self.metadata = {}
self.meta_file = None self.meta_file = None
@ -365,7 +520,7 @@ class Gluster_DiskFile(DiskFile):
create_object_metadata(data_file) create_object_metadata(data_file)
self.metadata = read_metadata(data_file) self.metadata = read_metadata(data_file)
self.filter_metadata() self._filter_metadata()
if not self._is_dir and keep_data_fp: if not self._is_dir and keep_data_fp:
# The caller has an assumption that the "fp" field of this # The caller has an assumption that the "fp" field of this
@ -390,22 +545,20 @@ class Gluster_DiskFile(DiskFile):
do_close(self.fp) do_close(self.fp)
self.fp = None self.fp = None
def is_deleted(self): def _filter_metadata(self):
""" if X_TYPE in self.metadata:
Check if the file is deleted. self.metadata.pop(X_TYPE)
if X_OBJECT_TYPE in self.metadata:
:returns: True if the file doesn't exist or has been flagged as self.metadata.pop(X_OBJECT_TYPE)
deleted.
"""
return not self.data_file
def _create_dir_object(self, dir_path, metadata=None): def _create_dir_object(self, dir_path, metadata=None):
""" """
Create a directory object at the specified path. No check is made to Create a directory object at the specified path. No check is made to
see if the directory object already exists, that is left to the see if the directory object already exists, that is left to the caller
caller (this avoids a potentially duplicate stat() system call). (this avoids a potentially duplicate stat() system call).
The "dir_path" must be relative to its container, self._container_path. The "dir_path" must be relative to its container,
self._container_path.
The "metadata" object is an optional set of metadata to apply to the The "metadata" object is an optional set of metadata to apply to the
newly created directory object. If not present, no initial metadata is newly created directory object. If not present, no initial metadata is
@ -455,233 +608,8 @@ class Gluster_DiskFile(DiskFile):
child = stack.pop() if stack else None child = stack.pop() if stack else None
return True, newmd return True, newmd
def put_metadata(self, metadata, tombstone=False):
"""
Short hand for putting metadata to .meta and .ts files.
:param metadata: dictionary of metadata to be written
:param tombstone: whether or not we are writing a tombstone
"""
if tombstone:
# We don't write tombstone files. So do nothing.
return
assert self.data_file is not None, \
"put_metadata: no file to put metadata into"
metadata = _adjust_metadata(metadata)
write_metadata(self.data_file, metadata)
self.metadata = metadata
self.filter_metadata()
def put(self, fd, metadata, extension='.data'):
"""
Finalize writing the file on disk, and renames it from the temp file
to the real location. This should be called after the data has been
written to the temp file.
:param fd: file descriptor of the temp file
:param metadata: dictionary of metadata to be written
:param extension: extension to be used when making the file
"""
# Our caller will use '.data' here; we just ignore it since we map the
# URL directly to the file system.
metadata = _adjust_metadata(metadata)
if dir_is_object(metadata):
if not self.data_file:
# Does not exist, create it
data_file = os.path.join(self._obj_path, self._obj)
_, self.metadata = self._create_dir_object(data_file, metadata)
self.data_file = os.path.join(self._container_path, data_file)
elif not self.is_dir:
# Exists, but as a file
raise DiskFileError('DiskFile.put(): directory creation failed'
' since the target, %s, already exists as'
' a file' % self.data_file)
return
if self._is_dir:
# A pre-existing directory already exists on the file
# system, perhaps gratuitously created when another
# object was created, or created externally to Swift
# REST API servicing (UFO use case).
raise DiskFileError('DiskFile.put(): file creation failed since'
' the target, %s, already exists as a'
' directory' % self.data_file)
# Write out metadata before fsync() to ensure it is also forced to
# disk.
write_metadata(fd, metadata)
if not _relaxed_writes:
do_fsync(fd)
if X_CONTENT_LENGTH in metadata:
# Don't bother doing this before fsync in case the OS gets any
# ideas to issue partial writes.
fsize = int(metadata[X_CONTENT_LENGTH])
self.drop_cache(fd, 0, fsize)
# At this point we know that the object's full directory path exists,
# so we can just rename it directly without using Swift's
# swift.common.utils.renamer(), which makes the directory path and
# adds extra stat() calls.
data_file = os.path.join(self.put_datadir, self._obj)
while True:
try:
os.rename(self.tmppath, data_file)
except OSError as err:
if err.errno in (errno.ENOENT, errno.EIO):
# FIXME: Why either of these two error conditions is
# happening is unknown at this point. This might be a FUSE
# issue of some sort or a possible race condition. So
# let's sleep on it, and double check the environment
# after a good nap.
_random_sleep()
# Tease out why this error occurred. The man page for
# rename reads:
# "The link named by tmppath does not exist; or, a
# directory component in data_file does not exist;
# or, tmppath or data_file is an empty string."
assert len(self.tmppath) > 0 and len(data_file) > 0
tpstats = do_stat(self.tmppath)
tfstats = do_fstat(fd)
assert tfstats
if not tpstats or tfstats.st_ino != tpstats.st_ino:
# Temporary file name conflict
raise DiskFileError('DiskFile.put(): temporary file,'
' %s, was already renamed'
' (targeted for %s)' % (
self.tmppath, data_file))
else:
# Data file target name now has a bad path!
dfstats = do_stat(self.put_datadir)
if not dfstats:
raise DiskFileError('DiskFile.put(): path to'
' object, %s, no longer exists'
' (targeted for %s)' % (
self.put_datadir,
data_file))
else:
is_dir = stat.S_ISDIR(dfstats.st_mode)
if not is_dir:
raise DiskFileError('DiskFile.put(): path to'
' object, %s, no longer a'
' directory (targeted for'
' %s)' % (self.put_datadir,
data_file))
else:
# Let's retry since everything looks okay
logging.warn("DiskFile.put(): os.rename('%s',"
"'%s') initially failed (%s) but"
" a stat('%s') following that"
" succeeded: %r" % (
self.tmppath, data_file,
str(err), self.put_datadir,
dfstats))
continue
else:
raise GlusterFileSystemOSError(
err.errno, "%s, os.rename('%s', '%s')" % (
err.strerror, self.tmppath, data_file))
else:
# Success!
break
# Avoid the unlink() system call as part of the mkstemp context cleanup
self.tmppath = None
self.metadata = metadata
self.filter_metadata()
# Mark that it actually exists now
self.data_file = os.path.join(self.datadir, self._obj)
def unlinkold(self, timestamp):
"""
Remove any older versions of the object file. Any file that has an
older timestamp than timestamp will be deleted.
:param timestamp: timestamp to compare with each file
"""
if not self.metadata or self.metadata[X_TIMESTAMP] >= timestamp:
return
assert self.data_file, \
"Have metadata, %r, but no data_file" % self.metadata
if self._is_dir:
# Marker, or object, directory.
#
# Delete from the filesystem only if it contains
# no objects. If it does contain objects, then just
# remove the object metadata tag which will make this directory a
# fake-filesystem-only directory and will be deleted
# when the container or parent directory is deleted.
metadata = read_metadata(self.data_file)
if dir_is_object(metadata):
metadata[X_OBJECT_TYPE] = DIR_NON_OBJECT
write_metadata(self.data_file, metadata)
rmobjdir(self.data_file)
else:
# Delete file object
do_unlink(self.data_file)
# Garbage collection of non-object directories.
# Now that we deleted the file, determine
# if the current directory and any parent
# directory may be deleted.
dirname = os.path.dirname(self.data_file)
while dirname and dirname != self._container_path:
# Try to remove any directories that are not
# objects.
if not rmobjdir(dirname):
# If a directory with objects has been
# found, we can stop garabe collection
break
else:
dirname = os.path.dirname(dirname)
self.metadata = {}
self.data_file = None
def get_data_file_size(self):
"""
Returns the os_path.getsize for the file. Raises an exception if this
file does not match the Content-Length stored in the metadata, or if
self.data_file does not exist.
:returns: file size as an int
:raises DiskFileError: on file size mismatch.
:raises DiskFileNotExist: on file not existing (including deleted)
"""
#Marker directory.
if self._is_dir:
return 0
try:
file_size = 0
if self.data_file:
file_size = os_path.getsize(self.data_file)
if X_CONTENT_LENGTH in self.metadata:
metadata_size = int(self.metadata[X_CONTENT_LENGTH])
if file_size != metadata_size:
self.metadata[X_CONTENT_LENGTH] = file_size
write_metadata(self.data_file, self.metadata)
return file_size
except OSError as err:
if err.errno != errno.ENOENT:
raise
raise DiskFileNotExist('Data File does not exist.')
def filter_metadata(self):
if X_TYPE in self.metadata:
self.metadata.pop(X_TYPE)
if X_OBJECT_TYPE in self.metadata:
self.metadata.pop(X_OBJECT_TYPE)
@contextmanager @contextmanager
def mkstemp(self, size=None): def writer(self, size=None):
""" """
Contextmanager to make a temporary file, optionally of a specified Contextmanager to make a temporary file, optionally of a specified
initial size. initial size.
@ -706,9 +634,7 @@ class Gluster_DiskFile(DiskFile):
except GlusterFileSystemOSError as gerr: except GlusterFileSystemOSError as gerr:
if gerr.errno == errno.ENOSPC: if gerr.errno == errno.ENOSPC:
# Raise DiskFileNoSpace to be handled by upper layers # Raise DiskFileNoSpace to be handled by upper layers
excp = DiskFileNoSpace() raise DiskFileNoSpace()
excp.drive = os.path.basename(self.device_path)
raise excp
if gerr.errno == errno.EEXIST: if gerr.errno == errno.EEXIST:
# Retry with a different random number. # Retry with a different random number.
continue continue
@ -750,30 +676,117 @@ class Gluster_DiskFile(DiskFile):
' create a temporary file without running' ' create a temporary file without running'
' into a name conflict after 1,000 attempts' ' into a name conflict after 1,000 attempts'
' for: %s' % (data_file,)) ' for: %s' % (data_file,))
dw = None
self.tmppath = tmppath
try: try:
# Ensure it is properly owned before we make it available. # Ensure it is properly owned before we make it available.
do_fchown(fd, self.uid, self.gid) do_fchown(fd, self.uid, self.gid)
if _preallocate and size: # NOTE: we do not perform the fallocate() call at all. We ignore
# For XFS, fallocate() turns off speculative pre-allocation # it completely.
# until a write is issued either to the last block of the file dw = DiskWriter(self, fd, tmppath, self.threadpool)
# before the EOF or beyond the EOF. This means that we are yield dw
# less likely to fragment free space with pre-allocated
# extents that get truncated back to the known file size.
# However, this call also turns holes into allocated but
# unwritten extents, so that allocation occurs before the
# write, not during XFS writeback. This effectively defeats
# any allocation optimizations the filesystem can make at
# writeback time.
fallocate(fd, size)
yield fd
finally: finally:
try: try:
do_close(fd) if dw.fd:
do_close(dw.fd)
except OSError: except OSError:
pass pass
if self.tmppath: if dw.tmppath:
tmppath, self.tmppath = self.tmppath, None do_unlink(dw.tmppath)
do_unlink(tmppath)
def put_metadata(self, metadata, tombstone=False):
"""
Short hand for putting metadata to .meta and .ts files.
:param metadata: dictionary of metadata to be written
:param tombstone: whether or not we are writing a tombstone
"""
if tombstone:
# We don't write tombstone files. So do nothing.
return
assert self.data_file is not None, \
"put_metadata: no file to put metadata into"
metadata = _adjust_metadata(metadata)
self.threadpool.run_in_thread(write_metadata, self.data_file, metadata)
self.metadata = metadata
self._filter_metadata()
def unlinkold(self, timestamp):
"""
Remove any older versions of the object file. Any file that has an
older timestamp than timestamp will be deleted.
:param timestamp: timestamp to compare with each file
"""
if not self.metadata or self.metadata[X_TIMESTAMP] >= timestamp:
return
assert self.data_file, \
"Have metadata, %r, but no data_file" % self.metadata
def _unlinkold():
if self._is_dir:
# Marker, or object, directory.
#
# Delete from the filesystem only if it contains no objects.
# If it does contain objects, then just remove the object
# metadata tag which will make this directory a
# fake-filesystem-only directory and will be deleted when the
# container or parent directory is deleted.
metadata = read_metadata(self.data_file)
if dir_is_object(metadata):
metadata[X_OBJECT_TYPE] = DIR_NON_OBJECT
write_metadata(self.data_file, metadata)
rmobjdir(self.data_file)
else:
# Delete file object
do_unlink(self.data_file)
# Garbage collection of non-object directories. Now that we
# deleted the file, determine if the current directory and any
# parent directory may be deleted.
dirname = os.path.dirname(self.data_file)
while dirname and dirname != self._container_path:
# Try to remove any directories that are not objects.
if not rmobjdir(dirname):
# If a directory with objects has been found, we can stop
# garabe collection
break
else:
dirname = os.path.dirname(dirname)
self.threadpool.run_in_thread(_unlinkold)
self.metadata = {}
self.data_file = None
def get_data_file_size(self):
"""
Returns the os_path.getsize for the file. Raises an exception if this
file does not match the Content-Length stored in the metadata, or if
self.data_file does not exist.
:returns: file size as an int
:raises DiskFileError: on file size mismatch.
:raises DiskFileNotExist: on file not existing (including deleted)
"""
#Marker directory.
if self._is_dir:
return 0
try:
file_size = 0
if self.data_file:
def _old_getsize():
file_size = os_path.getsize(self.data_file)
if X_CONTENT_LENGTH in self.metadata:
metadata_size = int(self.metadata[X_CONTENT_LENGTH])
if file_size != metadata_size:
# FIXME - bit rot detection?
self.metadata[X_CONTENT_LENGTH] = file_size
write_metadata(self.data_file, self.metadata)
return file_size
file_size = self.threadpool.run_in_thread(_old_getsize)
return file_size
except OSError as err:
if err.errno != errno.ENOENT:
raise
raise DiskFileNotExist('Data File does not exist.')

View File

@ -13,20 +13,15 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
""" Object Server for Gluster Swift UFO """ """ Object Server for Gluster for Swift """
# Simply importing this monkey patches the constraint handling to fit our # Simply importing this monkey patches the constraint handling to fit our
# needs # needs
from swift.obj import server
import gluster.swift.common.utils # noqa
import gluster.swift.common.constraints # noqa import gluster.swift.common.constraints # noqa
from swift.common.utils import public, timing_stats
from gluster.swift.common.DiskFile import Gluster_DiskFile
from gluster.swift.common.exceptions import DiskFileNoSpace
from swift.common.swob import HTTPInsufficientStorage
# Monkey patch the object server module to use Gluster's DiskFile definition from swift.obj import server
server.DiskFile = Gluster_DiskFile
from gluster.swift.obj.diskfile import DiskFile
class ObjectController(server.ObjectController): class ObjectController(server.ObjectController):
@ -37,6 +32,18 @@ class ObjectController(server.ObjectController):
operations directly). operations directly).
""" """
def _diskfile(self, device, partition, account, container, obj, **kwargs):
"""Utility method for instantiating a DiskFile."""
kwargs.setdefault('mount_check', self.mount_check)
kwargs.setdefault('bytes_per_sync', self.bytes_per_sync)
kwargs.setdefault('disk_chunk_size', self.disk_chunk_size)
kwargs.setdefault('threadpool', self.threadpools[device])
kwargs.setdefault('obj_dir', server.DATADIR)
kwargs.setdefault('disallowed_metadata_keys',
server.DISALLOWED_HEADERS)
return DiskFile(self.devices, device, partition, account,
container, obj, self.logger, **kwargs)
def container_update(self, op, account, container, obj, request, def container_update(self, op, account, container, obj, request,
headers_out, objdevice): headers_out, objdevice):
""" """
@ -56,15 +63,6 @@ class ObjectController(server.ObjectController):
""" """
return return
@public
@timing_stats()
def PUT(self, request):
try:
return server.ObjectController.PUT(self, request)
except DiskFileNoSpace as err:
drive = err.drive
return HTTPInsufficientStorage(drive=drive, request=request)
def app_factory(global_conf, **local_conf): def app_factory(global_conf, **local_conf):
"""paste.deploy app factory for creating WSGI object server apps""" """paste.deploy app factory for creating WSGI object server apps"""

View File

@ -39,11 +39,11 @@ BuildRequires: python-setuptools
Requires : memcached Requires : memcached
Requires : openssl Requires : openssl
Requires : python Requires : python
Requires : openstack-swift >= 1.8.0 Requires : openstack-swift >= 1.9.1
Requires : openstack-swift-account >= 1.8.0 Requires : openstack-swift-account >= 1.9.1
Requires : openstack-swift-container >= 1.8.0 Requires : openstack-swift-container >= 1.9.1
Requires : openstack-swift-object >= 1.8.0 Requires : openstack-swift-object >= 1.9.1
Requires : openstack-swift-proxy >= 1.8.0 Requires : openstack-swift-proxy >= 1.9.1
Obsoletes: glusterfs-swift-plugin Obsoletes: glusterfs-swift-plugin
Obsoletes: glusterfs-swift Obsoletes: glusterfs-swift
Obsoletes: glusterfs-ufo Obsoletes: glusterfs-ufo

View File

@ -1,10 +1,9 @@
""" Swift tests """ """ Swift tests """
import sys
import os import os
import copy import copy
import logging
import errno import errno
import logging
from sys import exc_info from sys import exc_info
from contextlib import contextmanager from contextlib import contextmanager
from collections import defaultdict from collections import defaultdict
@ -13,13 +12,98 @@ from eventlet.green import socket
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
from test import get_config from test import get_config
from ConfigParser import MissingSectionHeaderError from swift.common.utils import config_true_value
from StringIO import StringIO
from swift.common.utils import readconf, config_true_value
from logging import Handler
from hashlib import md5 from hashlib import md5
from eventlet import sleep, spawn, Timeout from eventlet import sleep, Timeout
import logging.handlers import logging.handlers
from httplib import HTTPException
class DebugLogger(object):
"""A simple stdout logger for eventlet wsgi."""
def write(self, *args):
print args
class FakeRing(object):
def __init__(self, replicas=3, max_more_nodes=0):
# 9 total nodes (6 more past the initial 3) is the cap, no matter if
# this is set higher, or R^2 for R replicas
self.replicas = replicas
self.max_more_nodes = max_more_nodes
self.devs = {}
def set_replicas(self, replicas):
self.replicas = replicas
self.devs = {}
@property
def replica_count(self):
return self.replicas
def get_part(self, account, container=None, obj=None):
return 1
def get_nodes(self, account, container=None, obj=None):
devs = []
for x in xrange(self.replicas):
devs.append(self.devs.get(x))
if devs[x] is None:
self.devs[x] = devs[x] = \
{'ip': '10.0.0.%s' % x,
'port': 1000 + x,
'device': 'sd' + (chr(ord('a') + x)),
'zone': x % 3,
'region': x % 2,
'id': x}
return 1, devs
def get_part_nodes(self, part):
return self.get_nodes('blah')[1]
def get_more_nodes(self, part):
# replicas^2 is the true cap
for x in xrange(self.replicas, min(self.replicas + self.max_more_nodes,
self.replicas * self.replicas)):
yield {'ip': '10.0.0.%s' % x,
'port': 1000 + x,
'device': 'sda',
'zone': x % 3,
'region': x % 2,
'id': x}
class FakeMemcache(object):
def __init__(self):
self.store = {}
def get(self, key):
return self.store.get(key)
def keys(self):
return self.store.keys()
def set(self, key, value, time=0):
self.store[key] = value
return True
def incr(self, key, time=0):
self.store[key] = self.store.setdefault(key, 0) + 1
return self.store[key]
@contextmanager
def soft_lock(self, key, timeout=0, retries=5):
yield True
def delete(self, key):
try:
del self.store[key]
except Exception:
pass
return True
def readuntil2crlfs(fd): def readuntil2crlfs(fd):
@ -28,6 +112,8 @@ def readuntil2crlfs(fd):
crlfs = 0 crlfs = 0
while crlfs < 2: while crlfs < 2:
c = fd.read(1) c = fd.read(1)
if not c:
raise ValueError("didn't get two CRLFs; just got %r" % rv)
rv = rv + c rv = rv + c
if c == '\r' and lc != '\n': if c == '\r' and lc != '\n':
crlfs = 0 crlfs = 0
@ -137,6 +223,7 @@ class FakeLogger(object):
def exception(self, *args, **kwargs): def exception(self, *args, **kwargs):
self.log_dict['exception'].append((args, kwargs, str(exc_info()[1]))) self.log_dict['exception'].append((args, kwargs, str(exc_info()[1])))
print 'FakeLogger Exception: %s' % self.log_dict
# mock out the StatsD logging methods: # mock out the StatsD logging methods:
increment = _store_in('increment') increment = _store_in('increment')
@ -279,7 +366,7 @@ def fake_http_connect(*code_iter, **kwargs):
class FakeConn(object): class FakeConn(object):
def __init__(self, status, etag=None, body='', timestamp='1', def __init__(self, status, etag=None, body='', timestamp='1',
expect_status=None): expect_status=None, headers=None):
self.status = status self.status = status
if expect_status is None: if expect_status is None:
self.expect_status = self.status self.expect_status = self.status
@ -292,6 +379,7 @@ def fake_http_connect(*code_iter, **kwargs):
self.received = 0 self.received = 0
self.etag = etag self.etag = etag
self.body = body self.body = body
self.headers = headers or {}
self.timestamp = timestamp self.timestamp = timestamp
def getresponse(self): def getresponse(self):
@ -323,9 +411,12 @@ def fake_http_connect(*code_iter, **kwargs):
'x-timestamp': self.timestamp, 'x-timestamp': self.timestamp,
'last-modified': self.timestamp, 'last-modified': self.timestamp,
'x-object-meta-test': 'testing', 'x-object-meta-test': 'testing',
'x-delete-at': '9876543210',
'etag': etag, 'etag': etag,
'x-works': 'yes', 'x-works': 'yes'}
'x-account-container-count': kwargs.get('count', 12345)} if self.status // 100 == 2:
headers['x-account-container-count'] = \
kwargs.get('count', 12345)
if not self.timestamp: if not self.timestamp:
del headers['x-timestamp'] del headers['x-timestamp']
try: try:
@ -335,8 +426,7 @@ def fake_http_connect(*code_iter, **kwargs):
pass pass
if 'slow' in kwargs: if 'slow' in kwargs:
headers['content-length'] = '4' headers['content-length'] = '4'
if 'headers' in kwargs: headers.update(self.headers)
headers.update(kwargs['headers'])
return headers.items() return headers.items()
def read(self, amt=None): def read(self, amt=None):
@ -360,6 +450,11 @@ def fake_http_connect(*code_iter, **kwargs):
timestamps_iter = iter(kwargs.get('timestamps') or ['1'] * len(code_iter)) timestamps_iter = iter(kwargs.get('timestamps') or ['1'] * len(code_iter))
etag_iter = iter(kwargs.get('etags') or [None] * len(code_iter)) etag_iter = iter(kwargs.get('etags') or [None] * len(code_iter))
if isinstance(kwargs.get('headers'), list):
headers_iter = iter(kwargs['headers'])
else:
headers_iter = iter([kwargs.get('headers', {})] * len(code_iter))
x = kwargs.get('missing_container', [False] * len(code_iter)) x = kwargs.get('missing_container', [False] * len(code_iter))
if not isinstance(x, (tuple, list)): if not isinstance(x, (tuple, list)):
x = [x] * len(code_iter) x = [x] * len(code_iter)
@ -384,6 +479,7 @@ def fake_http_connect(*code_iter, **kwargs):
else: else:
expect_status = status expect_status = status
etag = etag_iter.next() etag = etag_iter.next()
headers = headers_iter.next()
timestamp = timestamps_iter.next() timestamp = timestamps_iter.next()
if status <= 0: if status <= 0:
@ -393,6 +489,6 @@ def fake_http_connect(*code_iter, **kwargs):
else: else:
body = body_iter.next() body = body_iter.next()
return FakeConn(status, etag, body=body, timestamp=timestamp, return FakeConn(status, etag, body=body, timestamp=timestamp,
expect_status=expect_status) expect_status=expect_status, headers=headers)
return connect return connect

View File

@ -1,3 +1,3 @@
The unit tests expect certain ring data built using the following command: The unit tests expect certain ring data built using the following command:
../../../../bin/gluster-swift-gen-builders test iops ../../../../bin/gluster-swift-gen-builders test iops

Binary file not shown.

Binary file not shown.

View File

@ -272,8 +272,8 @@ class TestDiskCommon(unittest.TestCase):
self.fake_accounts[0], self.fake_logger) self.fake_accounts[0], self.fake_logger)
assert dc.metadata == {} assert dc.metadata == {}
assert dc.db_file == dd._db_file assert dc.db_file == dd._db_file
assert dc.pending_timeout == 0 assert dc.pending_timeout == 10
assert dc.stale_reads_ok == False assert dc.stale_reads_ok is False
assert dc.root == self.td assert dc.root == self.td
assert dc.logger == self.fake_logger assert dc.logger == self.fake_logger
assert dc.account == self.fake_accounts[0] assert dc.account == self.fake_accounts[0]
@ -290,8 +290,8 @@ class TestDiskCommon(unittest.TestCase):
dc._dir_exists_read_metadata() dc._dir_exists_read_metadata()
assert dc.metadata == fake_md, repr(dc.metadata) assert dc.metadata == fake_md, repr(dc.metadata)
assert dc.db_file == dd._db_file assert dc.db_file == dd._db_file
assert dc.pending_timeout == 0 assert dc.pending_timeout == 10
assert dc.stale_reads_ok == False assert dc.stale_reads_ok is False
assert dc.root == self.td assert dc.root == self.td
assert dc.logger == self.fake_logger assert dc.logger == self.fake_logger
assert dc.account == self.fake_accounts[0] assert dc.account == self.fake_accounts[0]
@ -303,8 +303,8 @@ class TestDiskCommon(unittest.TestCase):
dc._dir_exists_read_metadata() dc._dir_exists_read_metadata()
assert dc.metadata == {} assert dc.metadata == {}
assert dc.db_file == dd._db_file assert dc.db_file == dd._db_file
assert dc.pending_timeout == 0 assert dc.pending_timeout == 10
assert dc.stale_reads_ok == False assert dc.stale_reads_ok is False
assert dc.root == self.td assert dc.root == self.td
assert dc.logger == self.fake_logger assert dc.logger == self.fake_logger
assert dc.account == "dne0" assert dc.account == "dne0"

View File

@ -51,6 +51,10 @@ class TestRing(unittest.TestCase):
for node in self.ring.get_more_nodes(0): for node in self.ring.get_more_nodes(0):
assert node['device'] == 'volume_not_in_ring' assert node['device'] == 'volume_not_in_ring'
def test_second_device_part(self):
part = self.ring.get_part('iops')
assert part == 0
def test_second_device_with_reseller_prefix(self): def test_second_device_with_reseller_prefix(self):
part, node = self.ring.get_nodes('AUTH_iops') part, node = self.ring.get_nodes('AUTH_iops')
assert node[0]['device'] == 'iops' assert node[0]['device'] == 'iops'

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
""" Tests for gluster.swift.common.DiskFile """ """ Tests for gluster.swift.obj.diskfile """
import os import os
import stat import stat
@ -26,20 +26,20 @@ from mock import patch
from hashlib import md5 from hashlib import md5
import gluster.swift.common.utils import gluster.swift.common.utils
import gluster.swift.common.DiskFile import gluster.swift.obj.diskfile
from swift.common.utils import normalize_timestamp from swift.common.utils import normalize_timestamp
from gluster.swift.common.DiskFile import Gluster_DiskFile from gluster.swift.obj.diskfile import DiskFile
from swift.common.exceptions import DiskFileNotExist, DiskFileError from swift.common.exceptions import DiskFileNotExist, DiskFileError, \
DiskFileNoSpace
from gluster.swift.common.utils import DEFAULT_UID, DEFAULT_GID, X_TYPE, \ from gluster.swift.common.utils import DEFAULT_UID, DEFAULT_GID, X_TYPE, \
X_OBJECT_TYPE, DIR_OBJECT X_OBJECT_TYPE, DIR_OBJECT
from test_utils import _initxattr, _destroyxattr from test.unit.common.test_utils import _initxattr, _destroyxattr
from test.unit import FakeLogger from test.unit import FakeLogger
from gluster.swift.common.exceptions import *
_metadata = {} _metadata = {}
def _mock_read_metadata(filename): def _mock_read_metadata(filename):
global _metadata
if filename in _metadata: if filename in _metadata:
md = _metadata[filename] md = _metadata[filename]
else: else:
@ -47,9 +47,11 @@ def _mock_read_metadata(filename):
return md return md
def _mock_write_metadata(filename, metadata): def _mock_write_metadata(filename, metadata):
global _metadata
_metadata[filename] = metadata _metadata[filename] = metadata
def _mock_clear_metadata(): def _mock_clear_metadata():
global _metadata
_metadata = {} _metadata = {}
@ -58,7 +60,7 @@ class MockException(Exception):
def _mock_rmobjdir(p): def _mock_rmobjdir(p):
raise MockException("gluster.swift.common.DiskFile.rmobjdir() called") raise MockException("gluster.swift.obj.diskfile.rmobjdir() called")
def _mock_do_fsync(fd): def _mock_do_fsync(fd):
return return
@ -72,36 +74,35 @@ def _mock_renamer(a, b):
class TestDiskFile(unittest.TestCase): class TestDiskFile(unittest.TestCase):
""" Tests for gluster.swift.common.DiskFile """ """ Tests for gluster.swift.obj.diskfile """
def setUp(self): def setUp(self):
self.lg = FakeLogger() self.lg = FakeLogger()
_initxattr() _initxattr()
_mock_clear_metadata() _mock_clear_metadata()
self._saved_df_wm = gluster.swift.common.DiskFile.write_metadata self._saved_df_wm = gluster.swift.obj.diskfile.write_metadata
self._saved_df_rm = gluster.swift.common.DiskFile.read_metadata self._saved_df_rm = gluster.swift.obj.diskfile.read_metadata
gluster.swift.common.DiskFile.write_metadata = _mock_write_metadata gluster.swift.obj.diskfile.write_metadata = _mock_write_metadata
gluster.swift.common.DiskFile.read_metadata = _mock_read_metadata gluster.swift.obj.diskfile.read_metadata = _mock_read_metadata
self._saved_ut_wm = gluster.swift.common.utils.write_metadata self._saved_ut_wm = gluster.swift.common.utils.write_metadata
self._saved_ut_rm = gluster.swift.common.utils.read_metadata self._saved_ut_rm = gluster.swift.common.utils.read_metadata
gluster.swift.common.utils.write_metadata = _mock_write_metadata gluster.swift.common.utils.write_metadata = _mock_write_metadata
gluster.swift.common.utils.read_metadata = _mock_read_metadata gluster.swift.common.utils.read_metadata = _mock_read_metadata
self._saved_do_fsync = gluster.swift.common.DiskFile.do_fsync self._saved_do_fsync = gluster.swift.obj.diskfile.do_fsync
gluster.swift.common.DiskFile.do_fsync = _mock_do_fsync gluster.swift.obj.diskfile.do_fsync = _mock_do_fsync
def tearDown(self): def tearDown(self):
self.lg = None self.lg = None
_destroyxattr() _destroyxattr()
gluster.swift.common.DiskFile.write_metadata = self._saved_df_wm gluster.swift.obj.diskfile.write_metadata = self._saved_df_wm
gluster.swift.common.DiskFile.read_metadata = self._saved_df_rm gluster.swift.obj.diskfile.read_metadata = self._saved_df_rm
gluster.swift.common.utils.write_metadata = self._saved_ut_wm gluster.swift.common.utils.write_metadata = self._saved_ut_wm
gluster.swift.common.utils.read_metadata = self._saved_ut_rm gluster.swift.common.utils.read_metadata = self._saved_ut_rm
gluster.swift.common.DiskFile.do_fsync = self._saved_do_fsync gluster.swift.obj.diskfile.do_fsync = self._saved_do_fsync
def test_constructor_no_slash(self): def test_constructor_no_slash(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf._obj_path == "" assert gdf._obj_path == ""
assert gdf.name == "bar" assert gdf.name == "bar"
@ -126,8 +127,8 @@ class TestDiskFile(unittest.TestCase):
def test_constructor_leadtrail_slash(self): def test_constructor_leadtrail_slash(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "/b/a/z/",
"/b/a/z/", self.lg) self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf._obj_path == "b/a" assert gdf._obj_path == "b/a"
assert gdf.name == "bar/b/a" assert gdf.name == "bar/b/a"
@ -152,8 +153,7 @@ class TestDiskFile(unittest.TestCase):
'ETag': etag, 'ETag': etag,
'X-Timestamp': ts, 'X-Timestamp': ts,
'Content-Type': 'application/octet-stream'} 'Content-Type': 'application/octet-stream'}
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
assert not gdf._is_dir assert not gdf._is_dir
@ -181,8 +181,7 @@ class TestDiskFile(unittest.TestCase):
exp_md = ini_md.copy() exp_md = ini_md.copy()
del exp_md['X-Type'] del exp_md['X-Type']
del exp_md['X-Object-Type'] del exp_md['X-Object-Type']
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
assert not gdf._is_dir assert not gdf._is_dir
@ -205,8 +204,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
assert not gdf._is_dir assert not gdf._is_dir
@ -232,8 +230,8 @@ class TestDiskFile(unittest.TestCase):
exp_md = ini_md.copy() exp_md = ini_md.copy()
del exp_md['X-Type'] del exp_md['X-Type']
del exp_md['X-Object-Type'] del exp_md['X-Object-Type']
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "d", self.lg,
"d", self.lg, keep_data_fp=True) keep_data_fp=True)
assert gdf._obj == "d" assert gdf._obj == "d"
assert gdf.data_file == the_dir assert gdf.data_file == the_dir
assert gdf._is_dir assert gdf._is_dir
@ -250,8 +248,8 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg,
"z", self.lg, keep_data_fp=True) keep_data_fp=True)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
assert not gdf._is_dir assert not gdf._is_dir
@ -261,20 +259,19 @@ class TestDiskFile(unittest.TestCase):
def test_constructor_chunk_size(self): def test_constructor_chunk_size(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg,
"z", self.lg, disk_chunk_size=8192) disk_chunk_size=8192)
assert gdf.disk_chunk_size == 8192 assert gdf.disk_chunk_size == 8192
def test_constructor_iter_hook(self): def test_constructor_iter_hook(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg,
"z", self.lg, iter_hook='hook') iter_hook='hook')
assert gdf.iter_hook == 'hook' assert gdf.iter_hook == 'hook'
def test_close(self): def test_close(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
# Should be a no-op, as by default is_dir is False, but fp is None # Should be a no-op, as by default is_dir is False, but fp is None
gdf.close() gdf.close()
@ -285,22 +282,21 @@ class TestDiskFile(unittest.TestCase):
assert gdf.fp == "123" assert gdf.fp == "123"
gdf._is_dir = False gdf._is_dir = False
saved_dc = gluster.swift.common.DiskFile.do_close saved_dc = gluster.swift.obj.diskfile.do_close
self.called = False self.called = False
def our_do_close(fp): def our_do_close(fp):
self.called = True self.called = True
gluster.swift.common.DiskFile.do_close = our_do_close gluster.swift.obj.diskfile.do_close = our_do_close
try: try:
gdf.close() gdf.close()
assert self.called assert self.called
assert gdf.fp is None assert gdf.fp is None
finally: finally:
gluster.swift.common.DiskFile.do_close = saved_dc gluster.swift.obj.diskfile.do_close = saved_dc
def test_is_deleted(self): def test_is_deleted(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf.is_deleted() assert gdf.is_deleted()
gdf.data_file = "/tmp/foo/bar" gdf.data_file = "/tmp/foo/bar"
assert not gdf.is_deleted() assert not gdf.is_deleted()
@ -311,8 +307,8 @@ class TestDiskFile(unittest.TestCase):
the_dir = "dir" the_dir = "dir"
try: try:
os.makedirs(the_cont) os.makedirs(the_cont)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
os.path.join(the_dir, "z"), self.lg) os.path.join(the_dir, "z"), self.lg)
# Not created, dir object path is different, just checking # Not created, dir object path is different, just checking
assert gdf._obj == "z" assert gdf._obj == "z"
gdf._create_dir_object(the_dir) gdf._create_dir_object(the_dir)
@ -328,8 +324,8 @@ class TestDiskFile(unittest.TestCase):
the_dir = "dir" the_dir = "dir"
try: try:
os.makedirs(the_cont) os.makedirs(the_cont)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
os.path.join(the_dir, "z"), self.lg) os.path.join(the_dir, "z"), self.lg)
# Not created, dir object path is different, just checking # Not created, dir object path is different, just checking
assert gdf._obj == "z" assert gdf._obj == "z"
dir_md = {'Content-Type': 'application/directory', dir_md = {'Content-Type': 'application/directory',
@ -349,19 +345,18 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_dir, "wb") as fd: with open(the_dir, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir/z", self.lg)
"dir/z", self.lg)
# Not created, dir object path is different, just checking # Not created, dir object path is different, just checking
assert gdf._obj == "z" assert gdf._obj == "z"
def _mock_do_chown(p, u, g): def _mock_do_chown(p, u, g):
assert u == DEFAULT_UID assert u == DEFAULT_UID
assert g == DEFAULT_GID assert g == DEFAULT_GID
dc = gluster.swift.common.DiskFile.do_chown dc = gluster.swift.obj.diskfile.do_chown
gluster.swift.common.DiskFile.do_chown = _mock_do_chown gluster.swift.obj.diskfile.do_chown = _mock_do_chown
self.assertRaises(DiskFileError, self.assertRaises(DiskFileError,
gdf._create_dir_object, gdf._create_dir_object,
the_dir) the_dir)
gluster.swift.common.DiskFile.do_chown = dc gluster.swift.obj.diskfile.do_chown = dc
self.assertFalse(os.path.isdir(the_dir)) self.assertFalse(os.path.isdir(the_dir))
self.assertFalse(the_dir in _metadata) self.assertFalse(the_dir in _metadata)
finally: finally:
@ -375,19 +370,18 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_dir, "wb") as fd: with open(the_dir, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir/z", self.lg)
"dir/z", self.lg)
# Not created, dir object path is different, just checking # Not created, dir object path is different, just checking
assert gdf._obj == "z" assert gdf._obj == "z"
def _mock_do_chown(p, u, g): def _mock_do_chown(p, u, g):
assert u == DEFAULT_UID assert u == DEFAULT_UID
assert g == DEFAULT_GID assert g == DEFAULT_GID
dc = gluster.swift.common.DiskFile.do_chown dc = gluster.swift.obj.diskfile.do_chown
gluster.swift.common.DiskFile.do_chown = _mock_do_chown gluster.swift.obj.diskfile.do_chown = _mock_do_chown
self.assertRaises(DiskFileError, self.assertRaises(DiskFileError,
gdf._create_dir_object, gdf._create_dir_object,
the_dir) the_dir)
gluster.swift.common.DiskFile.do_chown = dc gluster.swift.obj.diskfile.do_chown = dc
self.assertFalse(os.path.isdir(the_dir)) self.assertFalse(os.path.isdir(the_dir))
self.assertFalse(the_dir in _metadata) self.assertFalse(the_dir in _metadata)
finally: finally:
@ -399,8 +393,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "z") the_dir = os.path.join(the_path, "z")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
md = { 'Content-Type': 'application/octet-stream', 'a': 'b' } md = { 'Content-Type': 'application/octet-stream', 'a': 'b' }
gdf.put_metadata(md.copy()) gdf.put_metadata(md.copy())
assert gdf.metadata == md, "gdf.metadata = %r, md = %r" % (gdf.metadata, md) assert gdf.metadata == md, "gdf.metadata = %r, md = %r" % (gdf.metadata, md)
@ -410,8 +403,7 @@ class TestDiskFile(unittest.TestCase):
def test_put_w_tombstone(self): def test_put_w_tombstone(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
assert gdf.metadata == {} assert gdf.metadata == {}
gdf.put_metadata({'x': '1'}, tombstone=True) gdf.put_metadata({'x': '1'}, tombstone=True)
@ -425,8 +417,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg)
"z", self.lg)
newmd = gdf.metadata.copy() newmd = gdf.metadata.copy()
newmd['X-Object-Meta-test'] = '1234' newmd['X-Object-Meta-test'] = '1234'
gdf.put_metadata(newmd) gdf.put_metadata(newmd)
@ -443,7 +434,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
newmd = gdf.metadata.copy() newmd = gdf.metadata.copy()
newmd['Content-Type'] = '' newmd['Content-Type'] = ''
@ -460,7 +451,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "dir") the_dir = os.path.join(the_path, "dir")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir", self.lg) "dir", self.lg)
newmd = gdf.metadata.copy() newmd = gdf.metadata.copy()
newmd['X-Object-Meta-test'] = '1234' newmd['X-Object-Meta-test'] = '1234'
@ -476,7 +467,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "dir") the_dir = os.path.join(the_path, "dir")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir", self.lg) "dir", self.lg)
newmd = gdf.metadata.copy() newmd = gdf.metadata.copy()
newmd['X-Object-Meta-test'] = '1234' newmd['X-Object-Meta-test'] = '1234'
@ -492,14 +483,15 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_cont, "dir") the_dir = os.path.join(the_cont, "dir")
try: try:
os.makedirs(the_cont) os.makedirs(the_cont)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir", self.lg) "dir", self.lg)
assert gdf.metadata == {} assert gdf.metadata == {}
newmd = { newmd = {
'ETag': 'etag', 'ETag': 'etag',
'X-Timestamp': 'ts', 'X-Timestamp': 'ts',
'Content-Type': 'application/directory'} 'Content-Type': 'application/directory'}
gdf.put(None, newmd, extension='.dir') with gdf.writer() as dw:
dw.put(newmd, extension='.dir')
assert gdf.data_file == the_dir assert gdf.data_file == the_dir
for key,val in newmd.items(): for key,val in newmd.items():
assert gdf.metadata[key] == val assert gdf.metadata[key] == val
@ -515,7 +507,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "dir") the_dir = os.path.join(the_path, "dir")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir", self.lg) "dir", self.lg)
origmd = gdf.metadata.copy() origmd = gdf.metadata.copy()
origfmd = _metadata[the_dir] origfmd = _metadata[the_dir]
@ -524,13 +516,14 @@ class TestDiskFile(unittest.TestCase):
# how this can happen normally. # how this can happen normally.
newmd['Content-Type'] = '' newmd['Content-Type'] = ''
newmd['X-Object-Meta-test'] = '1234' newmd['X-Object-Meta-test'] = '1234'
try: with gdf.writer() as dw:
gdf.put(None, newmd, extension='.data') try:
except DiskFileError: dw.put(newmd, extension='.data')
pass except DiskFileError:
else: pass
self.fail("Expected to encounter" else:
" 'already-exists-as-dir' exception") self.fail("Expected to encounter"
" 'already-exists-as-dir' exception")
assert gdf.metadata == origmd assert gdf.metadata == origmd
assert _metadata[the_dir] == origfmd assert _metadata[the_dir] == origfmd
finally: finally:
@ -541,7 +534,7 @@ class TestDiskFile(unittest.TestCase):
the_cont = os.path.join(td, "vol0", "bar") the_cont = os.path.join(td, "vol0", "bar")
try: try:
os.makedirs(the_cont) os.makedirs(the_cont)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf._obj_path == "" assert gdf._obj_path == ""
@ -560,11 +553,11 @@ class TestDiskFile(unittest.TestCase):
'Content-Length': '5', 'Content-Length': '5',
} }
with gdf.mkstemp() as fd: with gdf.writer() as dw:
assert gdf.tmppath is not None assert dw.tmppath is not None
tmppath = gdf.tmppath tmppath = dw.tmppath
os.write(fd, body) dw.write(body)
gdf.put(fd, metadata) dw.put(metadata)
assert gdf.data_file == os.path.join(td, "vol0", "bar", "z") assert gdf.data_file == os.path.join(td, "vol0", "bar", "z")
assert os.path.exists(gdf.data_file) assert os.path.exists(gdf.data_file)
@ -578,7 +571,7 @@ class TestDiskFile(unittest.TestCase):
the_cont = os.path.join(td, "vol0", "bar") the_cont = os.path.join(td, "vol0", "bar")
try: try:
os.makedirs(the_cont) os.makedirs(the_cont)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf._obj_path == "" assert gdf._obj_path == ""
@ -601,13 +594,14 @@ class TestDiskFile(unittest.TestCase):
with mock.patch("os.open", mock_open): with mock.patch("os.open", mock_open):
try: try:
with gdf.mkstemp() as fd: with gdf.writer() as dw:
assert gdf.tmppath is not None assert dw.tmppath is not None
tmppath = gdf.tmppath dw.write(body)
os.write(fd, body) dw.put(metadata)
gdf.put(fd, metadata)
except DiskFileNoSpace: except DiskFileNoSpace:
pass pass
else:
self.fail("Expected exception DiskFileNoSpace")
finally: finally:
shutil.rmtree(td) shutil.rmtree(td)
@ -616,7 +610,7 @@ class TestDiskFile(unittest.TestCase):
the_file = os.path.join(the_obj_path, "z") the_file = os.path.join(the_obj_path, "z")
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
try: try:
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
the_file, self.lg) the_file, self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf._obj_path == the_obj_path assert gdf._obj_path == the_obj_path
@ -635,11 +629,11 @@ class TestDiskFile(unittest.TestCase):
'Content-Length': '5', 'Content-Length': '5',
} }
with gdf.mkstemp() as fd: with gdf.writer() as dw:
assert gdf.tmppath is not None assert dw.tmppath is not None
tmppath = gdf.tmppath tmppath = dw.tmppath
os.write(fd, body) dw.write(body)
gdf.put(fd, metadata) dw.put(metadata)
assert gdf.data_file == os.path.join(td, "vol0", "bar", "b", "a", "z") assert gdf.data_file == os.path.join(td, "vol0", "bar", "b", "a", "z")
assert os.path.exists(gdf.data_file) assert os.path.exists(gdf.data_file)
@ -649,32 +643,32 @@ class TestDiskFile(unittest.TestCase):
def test_unlinkold_no_metadata(self): def test_unlinkold_no_metadata(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf.metadata == {} assert gdf.metadata == {}
_saved_rmobjdir = gluster.swift.common.DiskFile.rmobjdir _saved_rmobjdir = gluster.swift.obj.diskfile.rmobjdir
gluster.swift.common.DiskFile.rmobjdir = _mock_rmobjdir gluster.swift.obj.diskfile.rmobjdir = _mock_rmobjdir
try: try:
gdf.unlinkold(None) gdf.unlinkold(None)
except MockException as exp: except MockException as exp:
self.fail(str(exp)) self.fail(str(exp))
finally: finally:
gluster.swift.common.DiskFile.rmobjdir = _saved_rmobjdir gluster.swift.obj.diskfile.rmobjdir = _saved_rmobjdir
def test_unlinkold_same_timestamp(self): def test_unlinkold_same_timestamp(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf.metadata == {} assert gdf.metadata == {}
gdf.metadata['X-Timestamp'] = 1 gdf.metadata['X-Timestamp'] = 1
_saved_rmobjdir = gluster.swift.common.DiskFile.rmobjdir _saved_rmobjdir = gluster.swift.obj.diskfile.rmobjdir
gluster.swift.common.DiskFile.rmobjdir = _mock_rmobjdir gluster.swift.obj.diskfile.rmobjdir = _mock_rmobjdir
try: try:
gdf.unlinkold(1) gdf.unlinkold(1)
except MockException as exp: except MockException as exp:
self.fail(str(exp)) self.fail(str(exp))
finally: finally:
gluster.swift.common.DiskFile.rmobjdir = _saved_rmobjdir gluster.swift.obj.diskfile.rmobjdir = _saved_rmobjdir
def test_unlinkold_file(self): def test_unlinkold_file(self):
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
@ -684,7 +678,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -705,7 +699,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -729,7 +723,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -766,7 +760,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "d") the_dir = os.path.join(the_path, "d")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"d", self.lg, keep_data_fp=True) "d", self.lg, keep_data_fp=True)
assert gdf.data_file == the_dir assert gdf.data_file == the_dir
assert gdf._is_dir assert gdf._is_dir
@ -786,7 +780,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -795,7 +789,7 @@ class TestDiskFile(unittest.TestCase):
finally: finally:
shutil.rmtree(td) shutil.rmtree(td)
def test_get_data_file_size(self): def test_get_data_file_size_md_restored(self):
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
the_path = os.path.join(td, "vol0", "bar") the_path = os.path.join(td, "vol0", "bar")
the_file = os.path.join(the_path, "z") the_file = os.path.join(the_path, "z")
@ -803,7 +797,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -817,10 +811,10 @@ class TestDiskFile(unittest.TestCase):
def test_get_data_file_size_dne(self): def test_get_data_file_size_dne(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar",
"/b/a/z/", self.lg) "/b/a/z/", self.lg)
try: try:
s = gdf.get_data_file_size() gdf.get_data_file_size()
except DiskFileNotExist: except DiskFileNotExist:
pass pass
else: else:
@ -834,14 +828,14 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
assert not gdf._is_dir assert not gdf._is_dir
gdf.data_file = gdf.data_file + ".dne" gdf.data_file = gdf.data_file + ".dne"
try: try:
s = gdf.get_data_file_size() gdf.get_data_file_size()
except DiskFileNotExist: except DiskFileNotExist:
pass pass
else: else:
@ -857,7 +851,7 @@ class TestDiskFile(unittest.TestCase):
os.makedirs(the_path) os.makedirs(the_path)
with open(the_file, "wb") as fd: with open(the_file, "wb") as fd:
fd.write("1234") fd.write("1234")
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf._obj == "z" assert gdf._obj == "z"
assert gdf.data_file == the_file assert gdf.data_file == the_file
@ -871,7 +865,7 @@ class TestDiskFile(unittest.TestCase):
with patch("os.path.getsize", _mock_getsize_eaccess_err): with patch("os.path.getsize", _mock_getsize_eaccess_err):
try: try:
s = gdf.get_data_file_size() gdf.get_data_file_size()
except OSError as err: except OSError as err:
assert err.errno == errno.EACCES assert err.errno == errno.EACCES
else: else:
@ -887,7 +881,7 @@ class TestDiskFile(unittest.TestCase):
the_dir = os.path.join(the_path, "d") the_dir = os.path.join(the_path, "d")
try: try:
os.makedirs(the_dir) os.makedirs(the_dir)
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"d", self.lg, keep_data_fp=True) "d", self.lg, keep_data_fp=True)
assert gdf._obj == "d" assert gdf._obj == "d"
assert gdf.data_file == the_dir assert gdf.data_file == the_dir
@ -898,42 +892,42 @@ class TestDiskFile(unittest.TestCase):
def test_filter_metadata(self): def test_filter_metadata(self):
assert not os.path.exists("/tmp/foo") assert not os.path.exists("/tmp/foo")
gdf = Gluster_DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar", gdf = DiskFile("/tmp/foo", "vol0", "p57", "ufo47", "bar",
"z", self.lg) "z", self.lg)
assert gdf.metadata == {} assert gdf.metadata == {}
gdf.filter_metadata() gdf._filter_metadata()
assert gdf.metadata == {} assert gdf.metadata == {}
gdf.metadata[X_TYPE] = 'a' gdf.metadata[X_TYPE] = 'a'
gdf.metadata[X_OBJECT_TYPE] = 'b' gdf.metadata[X_OBJECT_TYPE] = 'b'
gdf.metadata['foobar'] = 'c' gdf.metadata['foobar'] = 'c'
gdf.filter_metadata() gdf._filter_metadata()
assert X_TYPE not in gdf.metadata assert X_TYPE not in gdf.metadata
assert X_OBJECT_TYPE not in gdf.metadata assert X_OBJECT_TYPE not in gdf.metadata
assert 'foobar' in gdf.metadata assert 'foobar' in gdf.metadata
def test_mkstemp(self): def test_writer(self):
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
the_path = os.path.join(td, "vol0", "bar")
the_dir = os.path.join(the_path, "dir")
try: try:
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir/z", self.lg) "dir/z", self.lg)
saved_tmppath = '' saved_tmppath = ''
with gdf.mkstemp() as fd: saved_fd = None
with gdf.writer() as dw:
assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir")
assert os.path.isdir(gdf.datadir) assert os.path.isdir(gdf.datadir)
saved_tmppath = gdf.tmppath saved_tmppath = dw.tmppath
assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.dirname(saved_tmppath) == gdf.datadir
assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.basename(saved_tmppath)[:3] == '.z.'
assert os.path.exists(saved_tmppath) assert os.path.exists(saved_tmppath)
os.write(fd, "123") dw.write("123")
saved_fd = dw.fd
# At the end of previous with block a close on fd is called. # At the end of previous with block a close on fd is called.
# Calling os.close on the same fd will raise an OSError # Calling os.close on the same fd will raise an OSError
# exception and we must catch it. # exception and we must catch it.
try: try:
os.close(fd) os.close(saved_fd)
except OSError as err: except OSError:
pass pass
else: else:
self.fail("Exception expected") self.fail("Exception expected")
@ -941,44 +935,40 @@ class TestDiskFile(unittest.TestCase):
finally: finally:
shutil.rmtree(td) shutil.rmtree(td)
def test_mkstemp_err_on_close(self): def test_writer_err_on_close(self):
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
the_path = os.path.join(td, "vol0", "bar")
the_dir = os.path.join(the_path, "dir")
try: try:
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir/z", self.lg) "dir/z", self.lg)
saved_tmppath = '' saved_tmppath = ''
with gdf.mkstemp() as fd: with gdf.writer() as dw:
assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir")
assert os.path.isdir(gdf.datadir) assert os.path.isdir(gdf.datadir)
saved_tmppath = gdf.tmppath saved_tmppath = dw.tmppath
assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.dirname(saved_tmppath) == gdf.datadir
assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.basename(saved_tmppath)[:3] == '.z.'
assert os.path.exists(saved_tmppath) assert os.path.exists(saved_tmppath)
os.write(fd, "123") dw.write("123")
# Closing the fd prematurely should not raise any exceptions. # Closing the fd prematurely should not raise any exceptions.
os.close(fd) os.close(dw.fd)
assert not os.path.exists(saved_tmppath) assert not os.path.exists(saved_tmppath)
finally: finally:
shutil.rmtree(td) shutil.rmtree(td)
def test_mkstemp_err_on_unlink(self): def test_writer_err_on_unlink(self):
td = tempfile.mkdtemp() td = tempfile.mkdtemp()
the_path = os.path.join(td, "vol0", "bar")
the_dir = os.path.join(the_path, "dir")
try: try:
gdf = Gluster_DiskFile(td, "vol0", "p57", "ufo47", "bar", gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar",
"dir/z", self.lg) "dir/z", self.lg)
saved_tmppath = '' saved_tmppath = ''
with gdf.mkstemp() as fd: with gdf.writer() as dw:
assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir")
assert os.path.isdir(gdf.datadir) assert os.path.isdir(gdf.datadir)
saved_tmppath = gdf.tmppath saved_tmppath = dw.tmppath
assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.dirname(saved_tmppath) == gdf.datadir
assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.basename(saved_tmppath)[:3] == '.z.'
assert os.path.exists(saved_tmppath) assert os.path.exists(saved_tmppath)
os.write(fd, "123") dw.write("123")
os.unlink(saved_tmppath) os.unlink(saved_tmppath)
assert not os.path.exists(saved_tmppath) assert not os.path.exists(saved_tmppath)
finally: finally:

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@ setenv = VIRTUAL_ENV={envdir}
NOSE_OPENSTACK_SHOW_ELAPSED=1 NOSE_OPENSTACK_SHOW_ELAPSED=1
NOSE_OPENSTACK_STDOUT=1 NOSE_OPENSTACK_STDOUT=1
deps = deps =
https://launchpad.net/swift/grizzly/1.8.0/+download/swift-1.8.0.tar.gz https://launchpad.net/swift/havana/1.9.1/+download/swift-1.9.1.tar.gz
-r{toxinidir}/tools/test-requires -r{toxinidir}/tools/test-requires
changedir = {toxinidir}/test/unit changedir = {toxinidir}/test/unit
commands = nosetests -v --exe --with-xunit --with-coverage --cover-package gluster --cover-erase --cover-xml --cover-html --cover-branches {posargs} commands = nosetests -v --exe --with-xunit --with-coverage --cover-package gluster --cover-erase --cover-xml --cover-html --cover-branches {posargs}