Table Of Contents

Previous topic

Account DB and Container DB

Next topic

Misc

This Page

Object

Object Auditor

class swift.obj.auditor.AuditorWorker(conf, logger, rcache, devices, zero_byte_only_at_fps=0)

Bases: object

Walk through file system to audit objects

audit_all_objects(mode='once', device_dirs=None)
create_recon_nested_dict(top_level_key, device_list, item)
failsafe_object_audit(location)

Entrypoint to object_audit, with a failsafe generic exception handler.

object_audit(location)

Audits the given object location.

Parameters:location – an audit location (from diskfile.object_audit_location_generator)
record_stats(obj_size)

Based on config’s object_size_stats will keep track of how many objects fall into the specified ranges. For example with the following:

object_size_stats = 10, 100, 1024

and your system has 3 objects of sizes: 5, 20, and 10000 bytes the log will look like: {“10”: 1, “100”: 1, “1024”: 0, “OVER”: 1}

class swift.obj.auditor.ObjectAuditor(conf, **options)

Bases: swift.common.daemon.Daemon

Audit objects.

audit_loop(parent, zbo_fps, override_devices=None, **kwargs)

Audit loop

clear_recon_cache(auditor_type)

Clear recon cache entries

fork_child(zero_byte_fps=False, **kwargs)

Child execution

run_audit(**kwargs)

Run the object audit

run_forever(*args, **kwargs)

Run the object audit until stopped.

run_once(*args, **kwargs)

Run the object audit once

Object Backend

Disk File Interface for the Swift Object Server

The DiskFile, DiskFileWriter and DiskFileReader classes combined define the on-disk abstraction layer for supporting the object server REST API interfaces (excluding REPLICATE). Other implementations wishing to provide an alternative backend for the object server must implement the three classes. An example alternative implementation can be found in the mem_server.py and mem_diskfile.py modules along size this one.

The DiskFileManager is a reference implemenation specific class and is not part of the backend API.

The remaining methods in this module are considered implementation specific and are also not considered part of the backend API.

class swift.obj.diskfile.AuditLocation(path, device, partition)

Bases: object

Represents an object location to be audited.

Other than being a bucket of data, the only useful thing this does is stringify to a filesystem path so the auditor’s logs look okay.

class swift.obj.diskfile.DiskFile(mgr, device_path, threadpool, partition, account=None, container=None, obj=None, _datadir=None)

Bases: object

Manage object files.

This specific implementation manages object files on a disk formatted with a POSIX-compliant file system that supports extended attributes as metadata on a file or directory.

Note

The arguments to the constructor are considered implementation specific. The API does not define the constructor arguments.

Parameters:
  • mgr – associated DiskFileManager instance
  • device_path – path to the target device or drive
  • threadpool – thread pool to use for blocking operations
  • partition – partition on the device in which the object lives
  • account – account name for the object
  • container – container name for the object
  • obj – object name for the object
account
container
content_length
create(*args, **kwds)

Context manager to create a file. We create a temporary file first, and then return a DiskFileWriter object to encapsulate the state.

Note

An implementation is not required to perform on-disk preallocations even if the parameter is specified. But if it does and it fails, it must raise a DiskFileNoSpace exception.

Parameters:size – optional initial size of file to explicitly allocate on disk
Raises DiskFileNoSpace:
 if a size is specified and allocation fails
delete(timestamp)

Delete the object.

This implementation creates a tombstone file using the given timestamp, and removes any older versions of the object file. Any file that has an older timestamp than timestamp will be deleted.

Note

An implementation is free to use or ignore the timestamp parameter.

Parameters:timestamp – timestamp to compare with each file
Raises DiskFileError:
 this implementation will raise the same errors as the create() method.
classmethod from_hash_dir(mgr, hash_dir_path, device_path, partition)
get_metadata()

Provide the metadata for a previously opened object as a dictionary.

Returns:object’s metadata dictionary
Raises DiskFileNotOpen:
 if the swift.obj.diskfile.DiskFile.open() method was not previously invoked
obj
open()

Open the object.

This implementation opens the data file representing the object, reads the associated metadata in the extended attributes, additionally combining metadata from fast-POST .meta files.

Note

An implementation is allowed to raise any of the following exceptions, but is only required to raise DiskFileNotExist when the object representation does not exist.

Raises:
  • DiskFileCollision – on name mis-match with metadata
  • DiskFileNotExist – if the object does not exist
  • DiskFileDeleted – if the object was previously deleted
  • DiskFileQuarantined – if while reading metadata of the file some data did pass cross checks
Returns:

itself for use as a context manager

read_metadata()

Return the metadata for an object without requiring the caller to open the object first.

Returns:metadata dictionary for an object
Raises DiskFileError:
 this implementation will raise the same errors as the open() method.
reader(keep_cache=False, _quarantine_hook=<function <lambda> at 0x5e539b0>)

Return a swift.common.swob.Response class compatible “app_iter” object as defined by swift.obj.diskfile.DiskFileReader.

For this implementation, the responsibility of closing the open file is passed to the swift.obj.diskfile.DiskFileReader object.

Parameters:
  • keep_cache – caller’s preference for keeping data read in the OS buffer cache
  • _quarantine_hook – 1-arg callable called when obj quarantined; the arg is the reason for quarantine. Default is to ignore it. Not needed by the REST layer.
Returns:

a swift.obj.diskfile.DiskFileReader object

timestamp
write_metadata(metadata)

Write a block of metadata to an object without requiring the caller to create the object first. Supports fast-POST behavior semantics.

Parameters:metadata – dictionary of metadata to be associated with the object
Raises DiskFileError:
 this implementation will raise the same errors as the create() method.
class swift.obj.diskfile.DiskFileManager(conf, logger)

Bases: object

Management class for devices, providing common place for shared parameters and methods not provided by the DiskFile class (which primarily services the object server REST API layer).

The get_diskfile() method is how this implementation creates a DiskFile object.

Note

This class is reference implementation specific and not part of the pluggable on-disk backend API.

Note

TODO(portante): Not sure what the right name to recommend here, as “manager” seemed generic enough, though suggestions are welcome.

Parameters:
  • conf – caller provided configuration object
  • logger – caller provided logger
construct_dev_path(device)

Construct the path to a device without checking if it is mounted.

Parameters:device – name of target device
Returns:full path to the device
get_dev_path(device, mount_check=None)

Return the path to a device, checking to see that it is a proper mount point based on a configuration parameter.

Parameters:
  • device – name of target device
  • mount_check – whether or not to check mountedness of device. Defaults to bool(self.mount_check).
Returns:

full path to the device, None if the path to the device is not a proper mount point.

get_diskfile(device, partition, account, container, obj, **kwargs)
get_diskfile_from_audit_location(audit_location)
get_diskfile_from_hash(device, partition, object_hash, **kwargs)

Returns a DiskFile instance for an object at the given object_hash. Just in case someone thinks of refactoring, be sure DiskFileDeleted is not raised, but the DiskFile instance representing the tombstoned object is returned instead.

Raises DiskFileNotExist:
 if the object does not exist
get_hashes(device, partition, suffix)
object_audit_location_generator(device_dirs=None)
pickle_async_update(device, account, container, obj, data, timestamp)
replication_lock(*args, **kwds)

A context manager that will lock on the device given, if configured to do so.

Raises ReplicationLockTimeout:
 If the lock on the device cannot be granted within the configured timeout.
yield_hashes(device, partition, suffixes=None)

Yields tuples of (full_path, hash_only, timestamp) for object information stored for the given device, partition, and (optionally) suffixes. If suffixes is None, all stored suffixes will be searched for object hashes. Note that if suffixes is not None but empty, such as [], then nothing will be yielded.

yield_suffixes(device, partition)

Yields tuples of (full_path, suffix_only) for suffixes stored on the given device and partition.

class swift.obj.diskfile.DiskFileReader(fp, data_file, obj_size, etag, threadpool, disk_chunk_size, keep_cache_size, device_path, logger, quarantine_hook, keep_cache=False)

Bases: object

Encapsulation of the WSGI read context for servicing GET REST API requests. Serves as the context manager object for the swift.obj.diskfile.DiskFile class’s swift.obj.diskfile.DiskFile.reader() method.

Note

The quarantining behavior of this method is considered implementation specific, and is not required of the API.

Note

The arguments to the constructor are considered implementation specific. The API does not define the constructor arguments.

Parameters:
  • fp – open file object pointer reference
  • data_file – on-disk data file name for the object
  • obj_size – verified on-disk size of the object
  • etag – expected metadata etag value for entire file
  • threadpool – thread pool to use for read operations
  • disk_chunk_size – size of reads from disk in bytes
  • keep_cache_size – maximum object size that will be kept in cache
  • device_path – on-disk device path, used when quarantining an obj
  • logger – logger caller wants this object to use
  • quarantine_hook – 1-arg callable called w/reason when quarantined
  • keep_cache – should resulting reads be kept in the buffer cache
app_iter_range(start, stop)

Returns an iterator over the data file for range (start, stop)

app_iter_ranges(ranges, content_type, boundary, size)

Returns an iterator over the data file for a set of ranges

close()

Close the open file handle if present.

For this specific implementation, this method will handle quarantining the file if necessary.

class swift.obj.diskfile.DiskFileWriter(name, datadir, fd, tmppath, bytes_per_sync, threadpool)

Bases: object

Encapsulation of the write context for servicing PUT REST API requests. Serves as the context manager object for the swift.obj.diskfile.DiskFile class’s swift.obj.diskfile.DiskFile.create() method.

Note

It is the responsibility of the swift.obj.diskfile.DiskFile.create() method context manager to close the open file descriptor.

Note

The arguments to the constructor are considered implementation specific. The API does not define the constructor arguments.

Parameters:
  • name – name of object from REST API
  • datadir – on-disk directory object will end up in on swift.obj.diskfile.DiskFileWriter.put()
  • fd – open file descriptor of temporary file to receive data
  • tmppath – full path name of the opened file descriptor
  • bytes_per_sync – number bytes written between sync calls
  • threadpool – internal thread pool to use for disk operations
put(metadata)

Finalize writing the file on disk.

For this implementation, this method is responsible for renaming the temporary file to the final name and directory location. This method should be called after the final call to swift.obj.diskfile.DiskFileWriter.write().

Parameters:metadata – dictionary of metadata to be associated with the object
write(chunk)

Write a chunk of data to disk. All invocations of this method must come before invoking the :func:

For this implementation, the data is written into a temporary file.

Parameters:chunk – the chunk of data to write as a string object
Returns:the total number of bytes written to an object
swift.obj.diskfile.get_hashes(partition_dir, recalculate=None, do_listdir=False, reclaim_age=604800)

Get a list of hashes for the suffix dir. do_listdir causes it to mistrust the hash cache for suffix existence at the (unexpectedly high) cost of a listdir. reclaim_age is just passed on to hash_suffix.

Parameters:
  • partition_dir – absolute path of partition to get hashes for
  • recalculate – list of suffixes which should be recalculated when got
  • do_listdir – force existence check for all hashes in the partition
  • reclaim_age – age at which to remove tombstones
Returns:

tuple of (number of suffix dirs hashed, dictionary of hashes)

swift.obj.diskfile.get_ondisk_files(files, datadir)

Given a simple list of files names, determine the files to use.

Params files:simple set of files as a python list
Params datadir:directory name files are from for convenience
Returns:a tuple of data, meta and ts (tombstone) files, in one of two states:
  • ts_file is not None, data_file is None, meta_file is None

    object is considered deleted

  • data_file is not None, ts_file is None

    object exists, and optionally has fast-POST metadata

swift.obj.diskfile.hash_cleanup_listdir(hsh_path, reclaim_age=604800)

List contents of a hash directory and clean up any old files.

Parameters:
  • hsh_path – object hash path
  • reclaim_age – age in seconds at which to remove tombstones
Returns:

list of files remaining in the directory, reverse sorted

swift.obj.diskfile.hash_suffix(path, reclaim_age)

Performs reclamation and returns an md5 of all (remaining) files.

Parameters:

reclaim_age – age in seconds at which to remove tombstones

Raises:
  • PathNotDir – if given path is not a valid directory
  • OSError – for non-ENOTDIR errors
swift.obj.diskfile.invalidate_hash(suffix_dir)

Invalidates the hash for a suffix_dir in the partition’s hashes file.

Parameters:suffix_dir – absolute path to suffix dir whose hash needs invalidating
swift.obj.diskfile.object_audit_location_generator(devices, mount_check=True, logger=None, device_dirs=None)

Given a devices path (e.g. “/srv/node”), yield an AuditLocation for all objects stored under that directory if device_dirs isn’t set. If device_dirs is set, only yield AuditLocation for the objects under the entries in device_dirs. The AuditLocation only knows the path to the hash directory, not to the .data file therein (if any). This is to avoid a double listdir(hash_dir); the DiskFile object will always do one, so we don’t.

Parameters:
  • devices – parent directory of the devices to be audited
  • mount_check – flag to check if a mount check should be performed on devices
  • logger – a logger object
Device_dirs :

a list of directories under devices to traverse

swift.obj.diskfile.quarantine_renamer(device_path, corrupted_file_path)

In the case that a file is corrupted, move it to a quarantined area to allow replication to fix it.

Params device_path:
 The path to the device the corrupted file is on.
Params corrupted_file_path:
 The path to the file you want quarantined.
Returns:path (str) of directory the file was moved to
Raises OSError:re-raises non errno.EEXIST / errno.ENOTEMPTY exceptions from rename
swift.obj.diskfile.read_metadata(fd)

Helper function to read the pickled metadata from an object file.

Parameters:fd – file descriptor or filename to load the metadata from
Returns:dictionary of metadata
swift.obj.diskfile.write_metadata(fd, metadata)

Helper function to write pickled metadata for an object file.

Parameters:
  • fd – file descriptor or filename to write the metadata
  • metadata – metadata to write

Object Replicator

class swift.obj.replicator.ObjectReplicator(conf)

Bases: swift.common.daemon.Daemon

Replicate objects.

Encapsulates most logic and data needed by the object replication process. Each call to .replicate() performs one replication pass. It’s up to the caller to do this in a loop.

check_ring()

Check to see if the ring has been updated

Returns:boolean indicating whether or not the ring has changed
collect_jobs()

Returns a sorted list of jobs (dictionaries) that specify the partitions, nodes, etc to be synced.

detect_lockups()

In testing, the pool.waitall() call very occasionally failed to return. This is an attempt to make sure the replicator finishes its replication pass in some eventuality.

heartbeat()

Loop that runs in the background during replication. It periodically logs progress.

kill_coros()

Utility function that kills all coroutines currently running.

replicate(override_devices=None, override_partitions=None)

Run a replication pass

rsync(node, job, suffixes)

Uses rsync to implement the sync method. This was the first sync method in Swift.

run_forever(*args, **kwargs)
run_once(*args, **kwargs)
ssync(node, job, suffixes)
stats_line()

Logs various stats for the currently running replication pass.

sync(node, job, suffixes)

Synchronize local suffix directories from a partition with a remote node.

Parameters:
  • node – the “dev” entry for the remote node to sync with
  • job – information about the partition being synced
  • suffixes – a list of suffixes which need to be pushed
Returns:

boolean indicating success or failure

update(job)

High-level method that replicates a single partition.

Parameters:job – a dict containing info about the partition to be replicated
update_deleted(job)

High-level method that replicates a single partition that doesn’t belong on this node.

Parameters:job – a dict containing info about the partition to be replicated
class swift.obj.ssync_sender.Sender(daemon, node, job, suffixes)

Bases: object

Sends REPLICATION requests to the object server.

These requests are eventually handled by ssync_receiver and full documentation about the process is there.

connect()

Establishes a connection and starts a REPLICATION request with the object server.

disconnect()

Closes down the connection to the object server once done with the REPLICATION request.

missing_check()

Handles the sender-side of the MISSING_CHECK step of a REPLICATION request.

Full documentation of this can be found at Receiver.missing_check().

readline()

Reads a line from the REPLICATION response body.

httplib has no readline and will block on read(x) until x is read, so we have to do the work ourselves. A bit of this is taken from Python’s httplib itself.

send_delete(url_path, timestamp)

Sends a DELETE subrequest with the given information.

send_put(url_path, df)

Sends a PUT subrequest for the url_path using the source df (DiskFile) and content_length.

updates()

Handles the sender-side of the UPDATES step of a REPLICATION request.

Full documentation of this can be found at Receiver.updates().

class swift.obj.ssync_receiver.Receiver(app, request)

Bases: object

Handles incoming REPLICATION requests to the object server.

These requests come from the object-replicator daemon that uses ssync_sender.

The number of concurrent REPLICATION requests is restricted by use of a replication_semaphore and can be configured with the object-server.conf [object-server] replication_concurrency setting.

A REPLICATION request is really just an HTTP conduit for sender/receiver replication communication. The overall REPLICATION request should always succeed, but it will contain multiple requests within its request and response bodies. This “hack” is done so that replication concurrency can be managed.

The general process inside a REPLICATION request is:

  1. Initialize the request: Basic request validation, mount check, acquire semaphore lock, etc..
  2. Missing check: Sender sends the hashes and timestamps of the object information it can send, receiver sends back the hashes it wants (doesn’t have or has an older timestamp).
  3. Updates: Sender sends the object information requested.
  4. Close down: Release semaphore lock, etc.
initialize_request()

Basic validation of request and mount check.

This function will be called before attempting to acquire a replication semaphore lock, so contains only quick checks.

missing_check()

Handles the receiver-side of the MISSING_CHECK step of a REPLICATION request.

Receives a list of hashes and timestamps of object information the sender can provide and responds with a list of hashes desired, either because they’re missing or have an older timestamp locally.

The process is generally:

  1. Sender sends :MISSING_CHECK: START and begins sending hash timestamp lines.
  2. Receiver gets :MISSING_CHECK: START and begins reading the hash timestamp lines, collecting the hashes of those it desires.
  3. Sender sends :MISSING_CHECK: END.
  4. Receiver gets :MISSING_CHECK: END, responds with :MISSING_CHECK: START, followed by the list of hashes it collected as being wanted (one per line), :MISSING_CHECK: END, and flushes any buffers.
  5. Sender gets :MISSING_CHECK: START and reads the list of hashes desired by the receiver until reading :MISSING_CHECK: END.

The collection and then response is so the sender doesn’t have to read while it writes to ensure network buffers don’t fill up and block everything.

updates()

Handles the UPDATES step of a REPLICATION request.

Receives a set of PUT and DELETE subrequests that will be routed to the object server itself for processing. These contain the information requested by the MISSING_CHECK step.

The PUT and DELETE subrequests are formatted pretty much exactly like regular HTTP requests, excepting the HTTP version on the first request line.

The process is generally:

  1. Sender sends :UPDATES: START and begins sending the PUT and DELETE subrequests.
  2. Receiver gets :UPDATES: START and begins routing the subrequests to the object server.
  3. Sender sends :UPDATES: END.
  4. Receiver gets :UPDATES: END and sends :UPDATES: START and :UPDATES: END (assuming no errors).
  5. Sender gets :UPDATES: START and :UPDATES: END.

If too many subrequests fail, as configured by replication_failure_threshold and replication_failure_ratio, the receiver will hang up the request early so as to not waste any more time.

At step 4, the receiver will send back an error if there were any failures (that didn’t cause a hangup due to the above thresholds) so the sender knows the whole was not entirely a success. This is so the sender knows if it can remove an out of place partition, for example.

Object Server

Object Server for Swift

class swift.obj.server.ObjectController(conf, logger=None)

Bases: object

Implements the WSGI application for the Swift Object Server.

DELETE(*a, **kw)

Handle HTTP DELETE requests for the Swift Object Server.

GET(*a, **kw)

Handle HTTP GET requests for the Swift Object Server.

HEAD(*a, **kw)

Handle HTTP HEAD requests for the Swift Object Server.

POST(*a, **kw)

Handle HTTP POST requests for the Swift Object Server.

PUT(*a, **kw)

Handle HTTP PUT requests for the Swift Object Server.

REPLICATE(*a, **kw)

Handle REPLICATE requests for the Swift Object Server. This is used by the object replicator to get hashes for directories.

REPLICATION(*a, **kw)
async_update(op, account, container, obj, host, partition, contdevice, headers_out, objdevice)

Sends or saves an async update.

Parameters:
  • op – operation performed (ex: ‘PUT’, or ‘DELETE’)
  • account – account name for the object
  • container – container name for the object
  • obj – object name
  • host – host that the container is on
  • partition – partition that the container is on
  • contdevice – device name that the container is on
  • headers_out – dictionary of headers to send in the container request
  • objdevice – device name that the object is in
container_update(op, account, container, obj, request, headers_out, objdevice)

Update the container when objects are updated.

Parameters:
  • op – operation performed (ex: ‘PUT’, or ‘DELETE’)
  • account – account name for the object
  • container – container name for the object
  • obj – object name
  • request – the original request object driving the update
  • headers_out – dictionary of headers to send in the container request(s)
  • objdevice – device name that the object is in
delete_at_update(op, delete_at, account, container, obj, request, objdevice)

Update the expiring objects container when objects are updated.

Parameters:
  • op – operation performed (ex: ‘PUT’, or ‘DELETE’)
  • delete_at – scheduled delete in UNIX seconds, int
  • account – account name for the object
  • container – container name for the object
  • obj – object name
  • request – the original request driving the update
  • objdevice – device name that the object is in
get_diskfile(device, partition, account, container, obj, **kwargs)

Utility method for instantiating a DiskFile object supporting a given REST API.

An implementation of the object server that wants to use a different DiskFile class would simply over-ride this method to provide that behavior.

setup(conf)

Implementation specific setup. This method is called at the very end by the constructor to allow a specific implementation to modify existing attributes or add its own attributes.

Parameters:conf – WSGI configuration parameter
swift.obj.server.app_factory(global_conf, **local_conf)

paste.deploy app factory for creating WSGI object server apps

swift.obj.server.global_conf_callback(preloaded_app_conf, global_conf)

Callback for swift.common.wsgi.run_wsgi during the global_conf creation so that we can add our replication_semaphore, used to limit the number of concurrent REPLICATION_REQUESTS across all workers.

Parameters:
  • preloaded_app_conf – The preloaded conf for the WSGI app. This conf instance will go away, so just read from it, don’t write.
  • global_conf – The global conf that will eventually be passed to the app_factory function later. This conf is created before the worker subprocesses are forked, so can be useful to set up semaphores, shared memory, etc.

Object Updater

class swift.obj.updater.ObjectUpdater(conf)

Bases: swift.common.daemon.Daemon

Update object information in container listings.

get_container_ring()

Get the container ring. Load it, if it hasn’t been yet.

object_sweep(device)

If there are async pendings on the device, walk each one and update.

Parameters:device – path to device
object_update(node, part, op, obj, headers)

Perform the object update to the container

Parameters:
  • node – node dictionary from the container ring
  • part – partition that holds the container
  • op – operation performed (ex: ‘POST’ or ‘DELETE’)
  • obj – object name being updated
  • headers – headers to send with the update
process_object_update(update_path, device)

Process the object information to be updated and update.

Parameters:
  • update_path – path to pickled object update file
  • device – path to device
run_forever(*args, **kwargs)

Run the updater continuously.

run_once(*args, **kwargs)

Run the updater once.

swift.obj.updater.random() → x in the interval [0, 1).