Advertisement
TShiva

connect_and_write_to_ceph

Aug 30th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <rados/librados.h>
  5.  
  6. int main (int argc, const char **argv)
  7. {
  8.         //Объявляем основные хэндлеры для работы с Ceph-ом
  9.         rados_t cluster;
  10.         char cluster_name[] = "ceph";
  11.         char user_name[] = "client.admin";
  12.         uint64_t flags;
  13.  
  14.         //Инициируем хэндлер для работы с кластером имеющим имя "ceph" и клиентом по имени "client.admin",
  15.         int err;
  16.         err = rados_create2(&cluster, cluster_name, user_name, flags);
  17.  
  18.         if (err < 0) {
  19.                 fprintf(stderr, "%s: Couldn't create the cluster handle! %s\n", argv[0], strerror(-err));
  20.                 exit(EXIT_FAILURE);
  21.         } else {
  22.                 printf("\nCreated a cluster handle.\n");
  23.         }
  24.  
  25.         //считываем конфигурационный файл для настройки соединения с кластером
  26.         err = rados_conf_read_file(cluster, "/etc/ceph/ceph.conf");
  27.         if (err < 0) {
  28.                 fprintf(stderr, "%s: cannot read config file: %s\n", argv[0], strerror(-err));
  29.                 exit(EXIT_FAILURE);
  30.         } else {
  31.                 printf("\nRead the config file.\n");
  32.         }
  33.  
  34.         //Считываем командную строку с аргументами
  35.         err = rados_conf_parse_argv(cluster, argc, argv);
  36.         if (err < 0) {
  37.                 fprintf(stderr, "%s: cannot parse command line arguments: %s\n", argv[0], strerror(-err));
  38.                 exit(EXIT_FAILURE);
  39.         } else {
  40.                 printf("\nRead the command line arguments.\n");
  41.         }
  42.  
  43.         //Соединяемся с кластером
  44.         err = rados_connect(cluster);
  45.         if (err < 0) {
  46.                 fprintf(stderr, "%s: cannot connect to cluster: %s\n", argv[0], strerror(-err));
  47.                 exit(EXIT_FAILURE);
  48.         } else {
  49.                 printf("\nConnected to the cluster.\n");
  50.         }
  51.  
  52.        
  53.         rados_ioctx_t io;
  54.         char *poolname = "data";
  55.  
  56.         //Создаём объект контеста ввода/вывода
  57.         err = rados_ioctx_create(cluster, poolname, &io);
  58.         if (err < 0) {
  59.                 fprintf(stderr, "%s: cannot open rados pool %s: %s\n", argv[0], poolname, strerror(-err));
  60.                 rados_shutdown(cluster);
  61.                 exit(EXIT_FAILURE);
  62.         } else {
  63.                 printf("\nCreated I/O context.\n");
  64.         }
  65.  
  66.         //Записываем синхронно данные на кластер
  67.         err = rados_write(io, "hw", "Hello World!", 12, 0);
  68.         if (err < 0) {
  69.                 fprintf(stderr, "%s: Cannot write object \"hw\" to pool %s: %s\n", argv[0], poolname, strerror(-err));
  70.                 rados_ioctx_destroy(io);
  71.                 rados_shutdown(cluster);
  72.                 exit(1);
  73.         } else {
  74.                 printf("\nWrote \"Hello World\" to object \"hw\".\n");
  75.         }
  76.  
  77.         char xattr[] = "en_US";
  78.         //Устанавливаем атрибурт для объекта
  79.         err = rados_setxattr(io, "hw", "lang", xattr, 5);
  80.         if (err < 0) {
  81.                 fprintf(stderr, "%s: Cannot write xattr to pool %s: %s\n", argv[0], poolname, strerror(-err));
  82.                 rados_ioctx_destroy(io);
  83.                 rados_shutdown(cluster);
  84.                 exit(1);
  85.         } else {
  86.                 printf("\nWrote \"en_US\" to xattr \"lang\" for object \"hw\".\n");
  87.         }
  88.  
  89.  
  90.  
  91.         //Создаём объект для асинхронного считывания с кластера
  92.         rados_completion_t comp;
  93.         err = rados_aio_create_completion(NULL, NULL, NULL, &comp);
  94.         if (err < 0) {
  95.                 fprintf(stderr, "%s: Could not create aio completion: %s\n", argv[0], strerror(-err));
  96.                 rados_ioctx_destroy(io);
  97.                 rados_shutdown(cluster);
  98.                 exit(1);
  99.         } else {
  100.                 printf("\nCreated AIO completion.\n");
  101.         }
  102.  
  103.         //Асинхронно считываем данные с кластера
  104.         char read_res[100];
  105.         err = rados_aio_read(io, "hw", comp, read_res, 12, 0);
  106.         if (err < 0) {
  107.                 fprintf(stderr, "%s: Cannot read object. %s %s\n", argv[0], poolname, strerror(-err));
  108.                 rados_ioctx_destroy(io);
  109.                 rados_shutdown(cluster);
  110.                 exit(1);
  111.         } else {
  112.                 printf("\nRead object \"hw\". The contents are:\n %s \n", read_res);
  113.         }
  114.  
  115.         // Дожидаемся завершения операции
  116.         rados_aio_wait_for_complete(comp);
  117.  
  118.         //Освобождаем дескриптор асинхронного ввода-вывода, чтобы избежать утечек памяти
  119.         rados_aio_release(comp);
  120.  
  121.         // Получаем атрибут объекта
  122.         char xattr_res[100];
  123.         err = rados_getxattr(io, "hw", "lang", xattr_res, 5);
  124.         if (err < 0) {
  125.                 fprintf(stderr, "%s: Cannot read xattr. %s %s\n", argv[0], poolname, strerror(-err));
  126.                 rados_ioctx_destroy(io);
  127.                 rados_shutdown(cluster);
  128.                 exit(1);
  129.         } else {
  130.                 printf("\nRead xattr \"lang\" for object \"hw\". The contents are:\n %s \n", xattr_res);
  131.         }
  132.         //удаляем атрибут объекта
  133.         err = rados_rmxattr(io, "hw", "lang");
  134.         if (err < 0) {
  135.                 fprintf(stderr, "%s: Cannot remove xattr. %s %s\n", argv[0], poolname, strerror(-err));
  136.                 rados_ioctx_destroy(io);
  137.                 rados_shutdown(cluster);
  138.                 exit(1);
  139.         } else {
  140.                 printf("\nRemoved xattr \"lang\" for object \"hw\".\n");
  141.         }
  142.         //Удаляем объект
  143.         err = rados_remove(io, "hw");
  144.         if (err < 0) {
  145.                 fprintf(stderr, "%s: Cannot remove object. %s %s\n", argv[0], poolname, strerror(-err));
  146.                 rados_ioctx_destroy(io);
  147.                 rados_shutdown(cluster);
  148.                 exit(1);
  149.         } else {
  150.                 printf("\nRemoved object \"hw\".\n");
  151.         }
  152.     // Удаляем контекст ввода/вывода
  153.     rados_ioctx_destroy(io);
  154.     rados_shutdown(cluster);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement