Librados (Python)
The rados module is a thin Python wrapper for librados. The source isavailable in /src/pybind/rados.py. You may also install it as a package.
Installation
To install Python libraries for Ceph, see Getting librados for Python.
Getting Started
You can create your own Ceph client using Python. The following tutorial willshow you how to import the Ceph Python module, connect to a Ceph cluster, andperform object operations as a client.admin user.
Note
To use the Ceph Python bindings, you must have access to arunning Ceph cluster. To set one up quickly, see Getting Started.
First, create a Python source file for your Ceph client.
- :linenos:
- sudo vim client.py
Import the Module
To use the rados.py module, import it into your source file.
|
|
Configure a Cluster Handle
Before connecting to the Ceph Storage Cluster, create a cluster handle. Bydefault, the cluster handle assumes a cluster named ceph (i.e., the defaultfor deployment tools, and our Getting Started guides too), and aclient.admin user name. You may change these defaults to suit your needs.
To connect to the Ceph Storage Cluster, your application needs to know where tofind the Ceph Monitor. Provide this information to your application byspecifying the path to your Ceph configuration file, which contains the locationof the initial Ceph monitors.
|
|
Ensure that the conffile argument provides the path and file name of yourCeph configuration file. You may use the sys module to avoid hard-coding theCeph configuration path and file name.
Your Python client also requires a client keyring. For this example, we use theclient.admin key by default. If you would like to specify the keyring whencreating the cluster handle, you may use the conf argument. Alternatively,you may specify the keyring path in your Ceph configuration file. For example,you may add something like the following line to you Ceph configuration file:
- keyring = /path/to/ceph.client.admin.keyring
For additional details on modifying your configuration via Python, see Configuration.
Connect to the Cluster
Once you have a cluster handle configured, you may connect to the cluster.With a connection to the cluster, you may execute methods that returninformation about the cluster.
|
|
By default, Ceph authentication is on. Your application will need to knowthe location of the keyring. The python-ceph module doesn’t have the defaultlocation, so you need to specify the keyring path. The easiest way to specifythe keyring is to add it to the Ceph configuration file. The following Cephconfiguration file example uses the client.admin keyring you generated withceph-deploy.
- [global]
- ...
- keyring=/path/to/keyring/ceph.client.admin.keyring
Manage Pools
When connected to the cluster, the Rados API allows you to manage pools. Youcan list pools, check for the existence of a pool, create a pool and delete apool.
|
|
Input/Output Context
Reading from and writing to the Ceph Storage Cluster requires an input/outputcontext (ioctx). You can create an ioctx with the open_ioctx() method of theRados class. The ioctx_name parameter is the name of the pool you wishto use.
|
|
Once you have an I/O context, you can read/write objects, extended attributes,and perform a number of other operations. After you complete operations, ensurethat you close the connection. For example:
|
|
Writing, Reading and Removing Objects
Once you create an I/O context, you can write objects to the cluster. If youwrite to an object that doesn’t exist, Ceph creates it. If you write to anobject that exists, Ceph overwrites it (except when you specify a range, andthen it only overwrites the range). You may read objects (and object ranges)from the cluster. You may also remove objects from the cluster. For example:
|
|
Writing and Reading XATTRS
Once you create an object, you can write extended attributes (XATTRs) tothe object and read XATTRs from the object. For example:
|
|
Listing Objects
If you want to examine the list of objects in a pool, you mayretrieve the list of objects and iterate over them with the object iterator.For example:
|
|
The Object class provides a file-like interface to an object, allowingyou to read and write content and extended attributes. Object operations usingthe I/O context provide additional functionality and asynchronous capabilities.
Cluster Handle API
The Rados class provides an interface into the Ceph Storage Daemon.
Configuration
The Rados class provides methods for getting and setting configurationvalues, reading the Ceph configuration file, and parsing arguments. Youdo not need to be connected to the Ceph Storage Cluster to invoke the followingmethods. See Storage Cluster Configuration for details on settings.
Parse known arguments from args, and remove; returnedargs contain only those unknown to ceph
Get the version number of the librados C library.
Returns:a tuple of (major,minor,extra) components of thelibrados version
Connection Management
Once you configure your cluster handle, you may connect to the cluster, checkthe cluster fsid, retrieve cluster statistics, and disconnect (shutdown)from the cluster. You may also assert that the cluster handle is in a particularstate (e.g., “configuring”, “connecting”, etc.).
Connect to the cluster. Use shutdown() to release resources.
Disconnects from the cluster. Call this explicitly when aRados.connect()ed object is no longer used.
Get the fsid of the cluster as a hexadecimal string.
Raises :ErrorReturns:str - cluster fsid
Read usage info about the cluster
This tells you total space, space used, space available, and numberof objects. These are not updated immediately when data is written,they are eventually consistent.
Returns:dict - contains the following keys:
- kb (int) - total space
- kb_used (int) - space used
- kb_avail (int) - free space available
- num_objects (int) - number of objects
- Rados.requirestate(*args_)
- Rados.require_state(self, *args)
Checks if the Rados object is in a special state
Raises :RadosStateError
Pool Operations
To use pool operation methods, you must connect to the Ceph Storage Clusterfirst. You may list the available pools, create a pool, check to see if a poolexists, and delete a pool.
Gets a list of pool names.
Returns:list - of pool names.
Input/Output Context API
To write data to and read data from the Ceph Object Store, you must createan Input/Output context (ioctx). The Rados class provides a open_ioctx()_method. The remaining ioctx operations involve invoking methods of the_Ioctx and other classes.
Checks if the rados.Ioctx object state is ‘open’
Raises :IoctxStateError
Get pool usage statistics
Returns:dict - contains the following keys:
- num_bytes (int) - size of pool in bytes
- num_kb (int) - size of pool in kbytes
- num_objects (int) - number of objects in the pool
- num_object_clones (int) - number of object clones
- num_object_copies (int) - number of object copies
- num_objects_missing_on_primary (int) - number of objets
- missing on primary
- num_objects_unfound (int) - number of unfound objects
- num_objects_degraded (int) - number of degraded objects
- num_rd (int) - bytes read
- num_rd_kb (int) - kbytes read
- num_wr (int) - bytes written
- num_wr_kb (int) - kbytes written
Attempt to change an io context’s associated auid “owner.”
Requires that you have write permission on both the current and newauid.
Raises :Error
Return the version of the last object read or written to.
This exposes the internal version number of the last object read orwritten via this io context
Returns:version of the last object used
Close a rados.Ioctx object.
This just tells librados that you no longer need to use the io context.It may not be freed immediately if there are pending asynchronousrequests on it, but you should not use an io context again aftercalling this function on it.
Object Operations
The Ceph Storage Cluster stores data as objects. You can read and write objectssynchronously or asynchronously. You can read and write from offsets. An objecthas a name (or key) and data.
- Ioctx.aiowrite(_object_name, to_write, offset=0, oncomplete=None, onsafe=None)
- Ioctx.aio_write(self, object_name, to_write, offset=0, oncomplete=None, onsafe=None)
Write data to an object asynchronously
Queues the write and returns.
Parameters:
- object_name (str) – name of the object
- to_write (bytes) – data to write
- offset (int) – byte offset in the object to begin writing at
- oncomplete (completion) – what to do when the write is safe and complete in memoryon all replicas
- onsafe (completion) – what to do when the write is safe and complete on storageon all replicasRaises :ErrorReturns:completion object
- Ioctx.aiowrite_full(_object_name, to_write, oncomplete=None, onsafe=None)
- Ioctx.aio_write_full(self, object_name, to_write, oncomplete=None, onsafe=None)
Asychronously write an entire object
The object is filled with the provided data. If the object exists,it is atomically truncated and then written.Queues the write and returns.
Parameters:
- object_name (str) – name of the object
- to_write (str) – data to write
- oncomplete (completion) – what to do when the write is safe and complete in memoryon all replicas
- onsafe (completion) – what to do when the write is safe and complete on storageon all replicasRaises :ErrorReturns:completion object
- Ioctx.aioappend(_object_name, to_append, oncomplete=None, onsafe=None)
- Ioctx.aio_append(self, object_name, to_append, oncomplete=None, onsafe=None)
Asychronously append data to an object
Queues the write and returns.
Parameters:
- object_name (str) – name of the object
- to_append (str) – data to append
- offset (int) – byte offset in the object to begin writing at
- oncomplete (completion) – what to do when the write is safe and complete in memoryon all replicas
- onsafe (completion) – what to do when the write is safe and complete on storageon all replicasRaises :ErrorReturns:completion object
Block until all pending writes in an io context are safe
Raises :Error
- Ioctx.aioread(_object_name, length, offset, oncomplete)
- Ioctx.aio_read(self, object_name, length, offset, oncomplete)
Asychronously read data from an object
oncomplete will be called with the returned read value aswell as the completion:
oncomplete(completion, data_read)
Parameters:
- object_name (str) – name of the object to read from
- length (int) – the number of bytes to read
- offset (int) – byte offset in the object to begin reading from
- oncomplete (completion) – what to do when the read is completeRaises :ErrorReturns:completion object
Object Extended Attributes
You may set extended attributes (XATTRs) on an object. You can retrieve a listof objects or XATTRs and iterate over them.
Object Interface
From an I/O context, you can retrieve a list of objects from a pool and iterateover them. The object interface provide makes each object look like a file, andyou may perform synchronous operations on the objects. For asynchronousoperations, you should use the I/O context methods.
Get ObjectIterator on rados.Ioctx object.
Returns:ObjectIterator