summaryrefslogtreecommitdiffstats
path: root/src/rgw/services/svc_rados.cc
blob: 4c587a985e8c1c369f16a7e42894bbde039535d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "svc_rados.h"

#include "include/rados/librados.hpp"
#include "common/errno.h"
#include "osd/osd_types.h"

#define dout_subsys ceph_subsys_rgw

int RGWS_RADOS::create_instance(const string& conf, RGWServiceInstanceRef *instance)
{
  instance->reset(new RGWSI_RADOS(this, cct));
  return 0;
}

static int init_ioctx(CephContext *cct, librados::Rados *rados, const rgw_pool& pool, librados::IoCtx& ioctx, bool create)
{
  int r = rados->ioctx_create(pool.name.c_str(), ioctx);
  if (r == -ENOENT && create) {
    r = rados->pool_create(pool.name.c_str());
    if (r == -ERANGE) {
      ldout(cct, 0)
        << __func__
        << " ERROR: librados::Rados::pool_create returned " << cpp_strerror(-r)
        << " (this can be due to a pool or placement group misconfiguration, e.g."
        << " pg_num < pgp_num or mon_max_pg_per_osd exceeded)"
        << dendl;
    }
    if (r < 0 && r != -EEXIST) {
      return r;
    }

    r = rados->ioctx_create(pool.name.c_str(), ioctx);
    if (r < 0) {
      return r;
    }

    r = ioctx.application_enable(pg_pool_t::APPLICATION_NAME_RGW, false);
    if (r < 0 && r != -EOPNOTSUPP) {
      return r;
    }
  } else if (r < 0) {
    return r;
  }
  if (!pool.ns.empty()) {
    ioctx.set_namespace(pool.ns);
  }
  return 0;
}

int RGWSI_RADOS::init(const string& conf, map<string, RGWServiceInstanceRef>& deps)
{
  auto handles = std::vector<librados::Rados>{static_cast<size_t>(cct->_conf->rgw_num_rados_handles)};

  for (auto& r : handles) {
    int ret = r.init_with_context(cct);
    if (ret < 0) {
      return ret;
    }
    ret = r.connect();
    if (ret < 0) {
      return ret;
    }
  }
  return 0;
}

librados::Rados* RGWSI_RADOS::get_rados_handle()
{
  if (rados.size() == 1) {
    return &rados[0];
  }
  handle_lock.get_read();
  pthread_t id = pthread_self();
  std::map<pthread_t, int>:: iterator it = rados_map.find(id);

  if (it != rados_map.end()) {
    handle_lock.put_read();
    return &rados[it->second];
  }
  handle_lock.put_read();
  handle_lock.get_write();
  const uint32_t handle = next_rados_handle;
  rados_map[id] = handle;
  if (++next_rados_handle == rados.size()) {
    next_rados_handle = 0;
  }
  handle_lock.put_write();
  return &rados[handle];
}

uint64_t RGWSI_RADOS::instance_id()
{
  return get_rados_handle()->get_instance_id();
}

int RGWSI_RADOS::open_pool_ctx(const rgw_pool& pool, librados::IoCtx& io_ctx)
{
  constexpr bool create = true; // create the pool if it doesn't exist
  return init_ioctx(cct, get_rados_handle(), pool, io_ctx, create);
}

void RGWSI_RADOS::Obj::init(const rgw_raw_obj& obj)
{
  ref.oid = obj.oid;
  ref.key = obj.loc;
  ref.pool = obj.pool;
}

int RGWSI_RADOS::Obj::open()
{
  int r = rados_svc->open_pool_ctx(ref.pool, ref.ioctx);
  if (r < 0)
    return r;

  ref.ioctx.locator_set_key(ref.key);

  return 0;
}

int RGWSI_RADOS::Obj::operate(librados::ObjectWriteOperation *op)
{
  return ref.ioctx.operate(ref.oid, op);
}

int RGWSI_RADOS::Obj::operate(librados::ObjectReadOperation *op, bufferlist *pbl)
{
  return ref.ioctx.operate(ref.oid, op, pbl);
}

int RGWSI_RADOS::Obj::aio_operate(librados::AioCompletion *c, librados::ObjectWriteOperation *op)
{
  return ref.ioctx.aio_operate(ref.oid, c, op);
}