Ceph分布式存储实2.3 RADOS与LIBRADOS

2.3 RADOS与LIBRADOS


LIBRADOS模块是客户端用来访问RADOS对象存储设备的。Ceph存储集群提供了消息传递层协议,用于客户端与Ceph MonitorOSD交互,LIBRADOS以库形式为Ceph Client提供了这个功能,LIBRADOS就是操作RADOS对象存储的接口。所有Ceph客户端可以用LIBRADOSLIBRADOS里封装的相同功能和对象存储交互,LIBRBDLIBCEPHFS就利用了此功能。你可以用LIBRADOS直接和Ceph交互(如与Ceph兼容的应用程序、Ceph接口等)。下面是简单描述的步骤。

第1步:获取LIBRADOS。

第2步:配置集群句柄。

第3步:创建IO上下文。

第4步:关闭连接。

LIBRADOS架构图,如图2-5所示。

先根据配置文件调用librados创建一个rados,接下来为这个rados创建一个radosclient,radosclient包含3个主要模块(finisher、Messager、Objector)。再根据pool创建对应的ioctx,在ioctx中能够找到radosclient。再调用osdc对生成对应osd请求,与OSD进行通信响应请求。

下面分别介绍LIBRADOS的C语言、Java语言和Python语言示例。

1. LIBRADOS C语言示例

下面是LIBRADOS C语言示例。

#include <stdio.h>

#include <string.h>

#include <rados/librados.h>

int main (int argc, char argv**)

{

        /*声明集群句柄以及所需参数 */

        rados_t cluster;

        char cluster_name[] = “ceph”;        //集群名称

        char user_name[] = “client.admin”;   //指定访问集群的用户,这里用admin

        uint64_t flags;

        rados_ioctx_t io;                    //rados上下文句柄

        char *poolname = “data”;             //目标pool名

        char read_res[100];

        char xattr[] = “en_US”;

        /* 指定参数初始化句柄*/

        int err;

        err = rados_create2(&cluster, cluster_name, user_name, flags);

 

        if (err < 0) {

                fprintf(stderr, “%s: Couldn’t create the cluster handle! %s\n”, argv[0], strerror(-err));

                exit(EXIT_FAILURE);

        } else {

                printf(“\nCreated a cluster handle.\n”);

        }

        /* 读取配置文件用来配置句柄*/

        err = rados_conf_read_file(cluster, “/etc/ceph/ceph.conf”);

        if (err < 0) {

                fprintf(stderr, “%s: cannot read config file: %s\n”, argv[0], strerror(-err));

                exit(EXIT_FAILURE);

        } else {

                printf(“\nRead the config file.\n”);

        }

 

        /* 分解参数 */

        err = rados_conf_parse_argv(cluster, argc, argv);

        if (err < 0) {

                fprintf(stderr, “%s: cannot parse command line arguments: %s\n”, argv[0], strerror(-err));

                exit(EXIT_FAILURE);

        } else {

                printf(“\nRead the command line arguments.\n”);

        }

        /* 连接集群*/

        err = rados_connect(cluster);

        if (err < 0) {

                fprintf(stderr, “%s: cannot connect to cluster: %s\n”, argv[0], strerror(-err));

                exit(EXIT_FAILURE);

        } else {

                printf(“\nConnected to the cluster.\n”);

        }

       //创建rados句柄上下文

        err = rados_ioctx_create(cluster, poolname, &io);

        if (err < 0) {

                fprintf(stderr, “%s: cannot open rados pool %s: %s\n”, argv[0], poolname, strerror(-err));

                rados_shutdown(cluster);

                exit(EXIT_FAILURE);

        } else {

                printf(“\nCreated I/O context.\n”);

        }

 

        //写对象

        err = rados_write(io, “hw”, “Hello World!”, 12, 0);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot write object \”hw\” to pool %s: %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nWrote \”Hello World\” to object \”hw\”.\n”);

        }

       //设置对象属性

        err = rados_setxattr(io, “hw”, “lang”, xattr, 5);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot write xattr to pool %s: %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nWrote \”en_US\” to xattr \”lang\” for object \”hw\”.\n”);

        }

        rados_completion_t comp;

       //确认异步rados句柄成功创建

        err = rados_aio_create_completion(NULL, NULL, NULL, &comp);

        if (err < 0) {

                fprintf(stderr, “%s: Could not create aio completion: %s\n”, argv[0], strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nCreated AIO completion.\n”);

        }

 

        /* Next, read data using rados_aio_read. */

        //异步读对象

        err = rados_aio_read(io, “hw”, comp, read_res, 12, 0);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot read object. %s %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nRead object \”hw\”. The contents are:\n %s \n”, read_res);

        }

       //等待对象上的操作完成

        rados_wait_for_complete(comp);

 

        // 释放complete句柄

        rados_aio_release(comp);

        char xattr_res[100];

       //获取对象

        err = rados_getxattr(io, “hw”, “lang”, xattr_res, 5);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot read xattr. %s %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nRead xattr \”lang\” for object \”hw\”. The contents are:\n %s \n”, xattr_res);

        }

       //移除对象属性

        err = rados_rmxattr(io, “hw”, “lang”);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot remove xattr. %s %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nRemoved xattr \”lang\” for object \”hw\”.\n”);

        }

       //删除对象

        err = rados_remove(io, “hw”);

        if (err < 0) {

                fprintf(stderr, “%s: Cannot remove object. %s %s\n”, argv[0], poolname, strerror(-err));

                rados_ioctx_destroy(io);

                rados_shutdown(cluster);

                exit(1);

        } else {

                printf(“\nRemoved object \”hw\”.\n”);

        }

    rados_ioctx_destroy(io);                  //销毁io上下文

    rados_shutdown(cluster);                  //销毁句柄

}

2. LIBRADOS Java语言示例

下面是LIBRADOS Java语言示例。

import com.ceph.rados.Rados;

import com.ceph.rados.RadosException;

import java.io.File;

public class CephClient {

        public static void main (String args[]){

                try {

                        //获取句柄

                        Rados cluster = new Rados(“admin”);

                        System.out.println(“Created cluster handle.”);

 

                        File f = new File(“/etc/ceph/ceph.conf”);

                        //读取配置文件

                        cluster.confReadFile(f);

                        System.out.println(“Read the configuration file.”);

                       //连接集群

                        cluster.connect();

                        System.out.println(“Connected to the cluster.”);

 

                } catch (RadosException e) {

                        System.out.println(e.getMessage()+”:”+e.getReturnValue());

                }

        }

}

3. LIBRADOS Python语言示例

下面是LIBRADOS Python语言示例。

#!/usr/bin/python

#encoding=utf-8

import rados, sys

cluster = rados.Rados(conffile=’/etc/ceph/ceph.conf’)   #获取句柄

print “\n\nI/O Context and Object Operations”

print “=================================”

print “\nCreating a context for the ‘data’ pool”

if not cluster.pool_exists(‘data’):                  

raise RuntimeError(‘No data pool exists’)

ioctx = cluster.open_ioctx(‘data’)                  #获取pool的io上下文句柄

print “\nWriting object ‘hw’ with contents ‘Hello World!’ to pool ‘data’.”

ioctx.write(“hw”, “Hello World!”)                 #写入对象

print “Writing XATTR ‘lang’ with value ‘en_US’ to object ‘hw'”

ioctx.set_xattr(“hw”, “lang”, “en_US”)          #设置对象的属性

print “\nWriting object ‘bm’ with contents ‘Bonjour tout le monde!’ to pool ‘data’.”

ioctx.write(“bm”, “Bonjour tout le monde!”)

print “Writing XATTR ‘lang’ with value ‘fr_FR’ to object ‘bm'”

ioctx.set_xattr(“bm”, “lang”, “fr_FR”)

print “\nContents of object ‘hw’\n————————“

print ioctx.read(“hw”)                   #读取对象

print “\n\nGetting XATTR ‘lang’ from object ‘hw'”

print ioctx.get_xattr(“hw”, “lang”)        #读取对象属性

print “\nContents of object ‘bm’\n————————“

print ioctx.read(“bm”)

  print “\nClosing the connection.”

ioctx.close()                           #关闭io上下文

 

print “Shutting down the handle.”

cluster.shutdown()                      #销毁句柄

    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞