1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/compat.h"
8 #include <boost/algorithm/string.hpp>
10 #include <boost/format.hpp>
11 #include <boost/optional.hpp>
12 #include <boost/utility/in_place_factory.hpp>
14 #include "common/ceph_json.h"
15 #include "common/utf8.h"
17 #include "common/errno.h"
18 #include "common/Formatter.h"
19 #include "common/Throttle.h"
20 #include "common/Finisher.h"
22 #include "rgw_rados.h"
23 #include "rgw_cache.h"
25 #include "rgw_acl_s3.h" /* for dumping s3policy in debug log */
26 #include "rgw_metadata.h"
27 #include "rgw_bucket.h"
28 #include "rgw_rest_conn.h"
29 #include "rgw_cr_rados.h"
30 #include "rgw_cr_rest.h"
32 #include "cls/rgw/cls_rgw_ops.h"
33 #include "cls/rgw/cls_rgw_types.h"
34 #include "cls/rgw/cls_rgw_client.h"
35 #include "cls/rgw/cls_rgw_const.h"
36 #include "cls/refcount/cls_refcount_client.h"
37 #include "cls/version/cls_version_client.h"
38 #include "cls/log/cls_log_client.h"
39 #include "cls/statelog/cls_statelog_client.h"
40 #include "cls/timeindex/cls_timeindex_client.h"
41 #include "cls/lock/cls_lock_client.h"
42 #include "cls/user/cls_user_client.h"
43 #include "osd/osd_types.h"
45 #include "rgw_tools.h"
46 #include "rgw_coroutine.h"
47 #include "rgw_compression.h"
49 #undef fork // fails to compile RGWPeriod::fork() below
51 #include "common/Clock.h"
53 #include "include/rados/librados.hpp"
54 using namespace librados
;
62 #include "auth/Crypto.h" // get_random_bytes()
69 #include "rgw_object_expirer_core.h"
71 #include "rgw_data_sync.h"
72 #include "rgw_realm_watcher.h"
73 #include "rgw_reshard.h"
75 #include "compressor/Compressor.h"
77 #define dout_context g_ceph_context
78 #define dout_subsys ceph_subsys_rgw
82 static string notify_oid_prefix
= "notify";
83 static string
*notify_oids
= NULL
;
84 static string shadow_ns
= "shadow";
85 static string dir_oid_prefix
= ".dir.";
86 static string default_storage_pool_suffix
= "rgw.buckets.data";
87 static string default_bucket_index_pool_suffix
= "rgw.buckets.index";
88 static string default_storage_extra_pool_suffix
= "rgw.buckets.non-ec";
89 static string avail_pools
= ".pools.avail";
91 static string zone_info_oid_prefix
= "zone_info.";
92 static string zone_names_oid_prefix
= "zone_names.";
93 static string region_info_oid_prefix
= "region_info.";
94 static string zone_group_info_oid_prefix
= "zonegroup_info.";
95 static string realm_names_oid_prefix
= "realms_names.";
96 static string realm_info_oid_prefix
= "realms.";
97 static string default_region_info_oid
= "default.region";
98 static string default_zone_group_info_oid
= "default.zonegroup";
99 static string period_info_oid_prefix
= "periods.";
100 static string period_latest_epoch_info_oid
= ".latest_epoch";
101 static string region_map_oid
= "region_map";
102 static string zonegroup_map_oid
= "zonegroup_map";
103 static string log_lock_name
= "rgw_log_lock";
104 static string default_realm_info_oid
= "default.realm";
105 const string default_zonegroup_name
= "default";
106 const string default_zone_name
= "default";
107 static string zonegroup_names_oid_prefix
= "zonegroups_names.";
108 static RGWObjCategory main_category
= RGW_OBJ_CATEGORY_MAIN
;
109 #define RGW_USAGE_OBJ_PREFIX "usage."
110 #define FIRST_EPOCH 1
111 static string RGW_DEFAULT_ZONE_ROOT_POOL
= "rgw.root";
112 static string RGW_DEFAULT_ZONEGROUP_ROOT_POOL
= "rgw.root";
113 static string RGW_DEFAULT_REALM_ROOT_POOL
= "rgw.root";
114 static string RGW_DEFAULT_PERIOD_ROOT_POOL
= "rgw.root";
116 #define RGW_STATELOG_OBJ_PREFIX "statelog."
118 #define dout_subsys ceph_subsys_rgw
121 static bool rgw_get_obj_data_pool(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
,
122 const string
& placement_id
, const rgw_obj
& obj
, rgw_pool
*pool
)
124 if (!zone_params
.get_head_data_pool(placement_id
, obj
, pool
)) {
125 RGWZonePlacementInfo placement
;
126 if (!zone_params
.get_placement(zonegroup
.default_placement
, &placement
)) {
130 if (!obj
.in_extra_data
) {
131 *pool
= placement
.data_pool
;
133 *pool
= placement
.get_data_extra_pool();
140 static bool rgw_obj_to_raw(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
,
141 const string
& placement_id
, const rgw_obj
& obj
, rgw_raw_obj
*raw_obj
)
143 get_obj_bucket_and_oid_loc(obj
, raw_obj
->oid
, raw_obj
->loc
);
145 return rgw_get_obj_data_pool(zonegroup
, zone_params
, placement_id
, obj
, &raw_obj
->pool
);
148 rgw_raw_obj
rgw_obj_select::get_raw_obj(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
) const
152 rgw_obj_to_raw(zonegroup
, zone_params
, placement_rule
, obj
, &r
);
158 rgw_raw_obj
rgw_obj_select::get_raw_obj(RGWRados
*store
) const
162 store
->obj_to_raw(placement_rule
, obj
, &r
);
168 int rgw_init_ioctx(librados::Rados
*rados
, const rgw_pool
& pool
, IoCtx
& ioctx
, bool create
)
170 int r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
171 if (r
== -ENOENT
&& create
) {
172 r
= rados
->pool_create(pool
.name
.c_str());
173 if (r
< 0 && r
!= -EEXIST
) {
177 r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
182 r
= ioctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
183 if (r
< 0 && r
!= -EOPNOTSUPP
) {
189 if (!pool
.ns
.empty()) {
190 ioctx
.set_namespace(pool
.ns
);
196 void RGWObjectCtxImpl
<rgw_obj
, RGWObjState
>::invalidate(rgw_obj
& obj
) {
197 RWLock::WLocker
wl(lock
);
198 auto iter
= objs_state
.find(obj
);
199 if (iter
== objs_state
.end()) {
202 bool is_atomic
= iter
->second
.is_atomic
;
203 bool prefetch_data
= iter
->second
.prefetch_data
;
205 objs_state
.erase(iter
);
207 if (is_atomic
|| prefetch_data
) {
208 auto& s
= objs_state
[obj
];
209 s
.is_atomic
= is_atomic
;
210 s
.prefetch_data
= prefetch_data
;
215 void RGWObjectCtxImpl
<rgw_raw_obj
, RGWRawObjState
>::invalidate(rgw_raw_obj
& obj
) {
216 RWLock::WLocker
wl(lock
);
217 auto iter
= objs_state
.find(obj
);
218 if (iter
== objs_state
.end()) {
222 objs_state
.erase(iter
);
225 void RGWDefaultZoneGroupInfo::dump(Formatter
*f
) const {
226 encode_json("default_zonegroup", default_zonegroup
, f
);
229 void RGWDefaultZoneGroupInfo::decode_json(JSONObj
*obj
) {
231 JSONDecoder::decode_json("default_zonegroup", default_zonegroup
, obj
);
232 /* backward compatability with region */
233 if (default_zonegroup
.empty()) {
234 JSONDecoder::decode_json("default_region", default_zonegroup
, obj
);
238 rgw_pool
RGWZoneGroup::get_pool(CephContext
*cct_
)
240 if (cct_
->_conf
->rgw_zonegroup_root_pool
.empty()) {
241 return rgw_pool(RGW_DEFAULT_ZONEGROUP_ROOT_POOL
);
244 return rgw_pool(cct_
->_conf
->rgw_zonegroup_root_pool
);
247 int RGWZoneGroup::create_default(bool old_format
)
249 name
= default_zonegroup_name
;
252 RGWZoneGroupPlacementTarget placement_target
;
253 placement_target
.name
= "default-placement";
254 placement_targets
[placement_target
.name
] = placement_target
;
255 default_placement
= "default-placement";
257 RGWZoneParams
zone_params(default_zone_name
);
259 int r
= zone_params
.init(cct
, store
, false);
261 ldout(cct
, 0) << "create_default: error initializing zone params: " << cpp_strerror(-r
) << dendl
;
265 r
= zone_params
.create_default();
266 if (r
< 0 && r
!= -EEXIST
) {
267 ldout(cct
, 0) << "create_default: error in create_default zone params: " << cpp_strerror(-r
) << dendl
;
269 } else if (r
== -EEXIST
) {
270 ldout(cct
, 10) << "zone_params::create_default() returned -EEXIST, we raced with another default zone_params creation" << dendl
;
271 zone_params
.clear_id();
272 r
= zone_params
.init(cct
, store
);
274 ldout(cct
, 0) << "create_default: error in init existing zone params: " << cpp_strerror(-r
) << dendl
;
277 ldout(cct
, 20) << "zone_params::create_default() " << zone_params
.get_name() << " id " << zone_params
.get_id()
281 RGWZone
& default_zone
= zones
[zone_params
.get_id()];
282 default_zone
.name
= zone_params
.get_name();
283 default_zone
.id
= zone_params
.get_id();
284 master_zone
= default_zone
.id
;
287 if (r
< 0 && r
!= -EEXIST
) {
288 ldout(cct
, 0) << "error storing zone group info: " << cpp_strerror(-r
) << dendl
;
293 ldout(cct
, 10) << "create_default() returned -EEXIST, we raced with another zonegroup creation" << dendl
;
295 r
= init(cct
, store
);
305 post_process_params();
310 const string
RGWZoneGroup::get_default_oid(bool old_region_format
)
312 if (old_region_format
) {
313 if (cct
->_conf
->rgw_default_region_info_oid
.empty()) {
314 return default_region_info_oid
;
316 return cct
->_conf
->rgw_default_region_info_oid
;
319 string default_oid
= cct
->_conf
->rgw_default_zonegroup_info_oid
;
321 if (cct
->_conf
->rgw_default_zonegroup_info_oid
.empty()) {
322 default_oid
= default_zone_group_info_oid
;
325 default_oid
+= "." + realm_id
;
330 const string
& RGWZoneGroup::get_info_oid_prefix(bool old_region_format
)
332 if (old_region_format
) {
333 return region_info_oid_prefix
;
335 return zone_group_info_oid_prefix
;
338 const string
& RGWZoneGroup::get_names_oid_prefix()
340 return zonegroup_names_oid_prefix
;
343 const string
& RGWZoneGroup::get_predefined_name(CephContext
*cct
) {
344 return cct
->_conf
->rgw_zonegroup
;
347 int RGWZoneGroup::equals(const string
& other_zonegroup
) const
349 if (is_master
&& other_zonegroup
.empty())
352 return (id
== other_zonegroup
);
355 int RGWZoneGroup::add_zone(const RGWZoneParams
& zone_params
, bool *is_master
, bool *read_only
,
356 const list
<string
>& endpoints
, const string
*ptier_type
,
357 bool *psync_from_all
, list
<string
>& sync_from
, list
<string
>& sync_from_rm
)
359 auto& zone_id
= zone_params
.get_id();
360 auto& zone_name
= zone_params
.get_name();
362 // check for duplicate zone name on insert
363 if (!zones
.count(zone_id
)) {
364 for (const auto& zone
: zones
) {
365 if (zone
.second
.name
== zone_name
) {
366 ldout(cct
, 0) << "ERROR: found existing zone name " << zone_name
367 << " (" << zone
.first
<< ") in zonegroup " << get_name() << dendl
;
375 if (!master_zone
.empty() && master_zone
!= zone_params
.get_id()) {
376 ldout(cct
, 0) << "NOTICE: overriding master zone: " << master_zone
<< dendl
;
378 master_zone
= zone_params
.get_id();
379 } else if (master_zone
== zone_params
.get_id()) {
384 RGWZone
& zone
= zones
[zone_params
.get_id()];
385 zone
.name
= zone_params
.get_name();
386 zone
.id
= zone_params
.get_id();
387 if (!endpoints
.empty()) {
388 zone
.endpoints
= endpoints
;
391 zone
.read_only
= *read_only
;
394 zone
.tier_type
= *ptier_type
;
397 if (psync_from_all
) {
398 zone
.sync_from_all
= *psync_from_all
;
401 for (auto add
: sync_from
) {
402 zone
.sync_from
.insert(add
);
405 for (auto rm
: sync_from_rm
) {
406 zone
.sync_from
.erase(rm
);
409 post_process_params();
415 int RGWZoneGroup::rename_zone(const RGWZoneParams
& zone_params
)
417 RGWZone
& zone
= zones
[zone_params
.get_id()];
418 zone
.name
= zone_params
.get_name();
423 void RGWZoneGroup::post_process_params()
425 bool log_data
= zones
.size() > 1;
427 if (master_zone
.empty()) {
428 map
<string
, RGWZone
>::iterator iter
= zones
.begin();
429 if (iter
!= zones
.end()) {
430 master_zone
= iter
->first
;
434 for (map
<string
, RGWZone
>::iterator iter
= zones
.begin(); iter
!= zones
.end(); ++iter
) {
435 RGWZone
& zone
= iter
->second
;
436 zone
.log_data
= log_data
;
438 RGWZoneParams
zone_params(zone
.id
, zone
.name
);
439 int ret
= zone_params
.init(cct
, store
);
441 ldout(cct
, 0) << "WARNING: could not read zone params for zone id=" << zone
.id
<< " name=" << zone
.name
<< dendl
;
445 for (map
<string
, RGWZonePlacementInfo
>::iterator iter
= zone_params
.placement_pools
.begin();
446 iter
!= zone_params
.placement_pools
.end(); ++iter
) {
447 const string
& placement_name
= iter
->first
;
448 if (placement_targets
.find(placement_name
) == placement_targets
.end()) {
449 RGWZoneGroupPlacementTarget placement_target
;
450 placement_target
.name
= placement_name
;
451 placement_targets
[placement_name
] = placement_target
;
456 if (default_placement
.empty() && !placement_targets
.empty()) {
457 default_placement
= placement_targets
.begin()->first
;
461 int RGWZoneGroup::remove_zone(const std::string
& zone_id
)
463 map
<string
, RGWZone
>::iterator iter
= zones
.find(zone_id
);
464 if (iter
== zones
.end()) {
465 ldout(cct
, 0) << "zone id " << zone_id
<< " is not a part of zonegroup "
472 post_process_params();
477 int RGWZoneGroup::read_default_id(string
& default_id
, bool old_format
)
479 if (realm_id
.empty()) {
480 /* try using default realm */
482 int ret
= realm
.init(cct
, store
);
483 // no default realm exist
485 return read_id(default_zonegroup_name
, default_id
);
487 realm_id
= realm
.get_id();
490 return RGWSystemMetaObj::read_default_id(default_id
, old_format
);
493 int RGWZoneGroup::set_as_default(bool exclusive
)
495 if (realm_id
.empty()) {
496 /* try using default realm */
498 int ret
= realm
.init(cct
, store
);
500 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
503 realm_id
= realm
.get_id();
506 return RGWSystemMetaObj::set_as_default(exclusive
);
509 int RGWSystemMetaObj::init(CephContext
*_cct
, RGWRados
*_store
, bool setup_obj
, bool old_format
)
517 if (old_format
&& id
.empty()) {
524 name
= get_predefined_name(cct
);
527 r
= use_default(old_format
);
531 } else if (!old_format
) {
532 r
= read_id(name
, id
);
535 ldout(cct
, 0) << "error in read_id for object name: " << name
<< " : " << cpp_strerror(-r
) << dendl
;
542 return read_info(id
, old_format
);
545 int RGWSystemMetaObj::read_default(RGWDefaultSystemMetaObjInfo
& default_info
, const string
& oid
)
547 auto pool
= get_pool(cct
);
549 RGWObjectCtx
obj_ctx(store
);
550 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
555 bufferlist::iterator iter
= bl
.begin();
556 ::decode(default_info
, iter
);
557 } catch (buffer::error
& err
) {
558 ldout(cct
, 0) << "error decoding data from " << pool
<< ":" << oid
<< dendl
;
565 int RGWSystemMetaObj::read_default_id(string
& default_id
, bool old_format
)
567 RGWDefaultSystemMetaObjInfo default_info
;
569 int ret
= read_default(default_info
, get_default_oid(old_format
));
574 default_id
= default_info
.default_id
;
579 int RGWSystemMetaObj::use_default(bool old_format
)
581 return read_default_id(id
, old_format
);
584 int RGWSystemMetaObj::set_as_default(bool exclusive
)
586 string oid
= get_default_oid();
588 rgw_pool
pool(get_pool(cct
));
591 RGWDefaultSystemMetaObjInfo default_info
;
592 default_info
.default_id
= id
;
594 ::encode(default_info
, bl
);
596 int ret
= rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
597 exclusive
, NULL
, real_time(), NULL
);
604 int RGWSystemMetaObj::read_id(const string
& obj_name
, string
& object_id
)
606 rgw_pool
pool(get_pool(cct
));
609 string oid
= get_names_oid_prefix() + obj_name
;
611 RGWObjectCtx
obj_ctx(store
);
612 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
617 RGWNameToId nameToId
;
619 bufferlist::iterator iter
= bl
.begin();
620 ::decode(nameToId
, iter
);
621 } catch (buffer::error
& err
) {
622 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << oid
<< dendl
;
625 object_id
= nameToId
.obj_id
;
629 int RGWSystemMetaObj::delete_obj(bool old_format
)
631 rgw_pool
pool(get_pool(cct
));
633 /* check to see if obj is the default */
634 RGWDefaultSystemMetaObjInfo default_info
;
635 int ret
= read_default(default_info
, get_default_oid(old_format
));
636 if (ret
< 0 && ret
!= -ENOENT
)
638 if (default_info
.default_id
== id
|| (old_format
&& default_info
.default_id
== name
)) {
639 string oid
= get_default_oid(old_format
);
640 rgw_raw_obj
default_named_obj(pool
, oid
);
641 ret
= store
->delete_system_obj(default_named_obj
);
643 ldout(cct
, 0) << "Error delete default obj name " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
648 string oid
= get_names_oid_prefix() + name
;
649 rgw_raw_obj
object_name(pool
, oid
);
650 ret
= store
->delete_system_obj(object_name
);
652 ldout(cct
, 0) << "Error delete obj name " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
657 string oid
= get_info_oid_prefix(old_format
);
664 rgw_raw_obj
object_id(pool
, oid
);
665 ret
= store
->delete_system_obj(object_id
);
667 ldout(cct
, 0) << "Error delete object id " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
673 int RGWSystemMetaObj::store_name(bool exclusive
)
675 rgw_pool
pool(get_pool(cct
));
676 string oid
= get_names_oid_prefix() + name
;
678 RGWNameToId nameToId
;
679 nameToId
.obj_id
= id
;
682 ::encode(nameToId
, bl
);
683 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(), exclusive
, NULL
, real_time(), NULL
);
686 int RGWSystemMetaObj::rename(const string
& new_name
)
689 int ret
= read_id(new_name
, new_id
);
693 if (ret
< 0 && ret
!= -ENOENT
) {
694 ldout(cct
, 0) << "Error read_id " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
697 string old_name
= name
;
701 ldout(cct
, 0) << "Error storing new obj info " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
704 ret
= store_name(true);
706 ldout(cct
, 0) << "Error storing new name " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
709 /* delete old name */
710 rgw_pool
pool(get_pool(cct
));
711 string oid
= get_names_oid_prefix() + old_name
;
712 rgw_raw_obj
old_name_obj(pool
, oid
);
713 ret
= store
->delete_system_obj(old_name_obj
);
715 ldout(cct
, 0) << "Error delete old obj name " << old_name
<< ": " << cpp_strerror(-ret
) << dendl
;
722 int RGWSystemMetaObj::read_info(const string
& obj_id
, bool old_format
)
724 rgw_pool
pool(get_pool(cct
));
728 string oid
= get_info_oid_prefix(old_format
) + obj_id
;
730 RGWObjectCtx
obj_ctx(store
);
731 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
733 ldout(cct
, 0) << "failed reading obj info from " << pool
<< ":" << oid
<< ": " << cpp_strerror(-ret
) << dendl
;
738 bufferlist::iterator iter
= bl
.begin();
739 ::decode(*this, iter
);
740 } catch (buffer::error
& err
) {
741 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << oid
<< dendl
;
748 int RGWSystemMetaObj::read()
750 int ret
= read_id(name
, id
);
755 return read_info(id
);
758 int RGWSystemMetaObj::create(bool exclusive
)
762 /* check to see the name is not used */
763 ret
= read_id(name
, id
);
764 if (exclusive
&& ret
== 0) {
765 ldout(cct
, 10) << "ERROR: name " << name
<< " already in use for obj id " << id
<< dendl
;
767 } else if ( ret
< 0 && ret
!= -ENOENT
) {
768 ldout(cct
, 0) << "failed reading obj id " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
773 /* create unique id */
776 new_uuid
.generate_random();
777 new_uuid
.print(uuid_str
);
781 ret
= store_info(exclusive
);
783 ldout(cct
, 0) << "ERROR: storing info for " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
787 return store_name(exclusive
);
790 int RGWSystemMetaObj::store_info(bool exclusive
)
792 rgw_pool
pool(get_pool(cct
));
794 string oid
= get_info_oid_prefix() + id
;
798 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(), exclusive
, NULL
, real_time(), NULL
);
801 int RGWSystemMetaObj::write(bool exclusive
)
803 int ret
= store_info(exclusive
);
805 ldout(cct
, 20) << __func__
<< "(): store_info() returned ret=" << ret
<< dendl
;
808 ret
= store_name(exclusive
);
810 ldout(cct
, 20) << __func__
<< "(): store_name() returned ret=" << ret
<< dendl
;
817 const string
& RGWRealm::get_predefined_name(CephContext
*cct
) {
818 return cct
->_conf
->rgw_realm
;
821 int RGWRealm::create(bool exclusive
)
823 int ret
= RGWSystemMetaObj::create(exclusive
);
825 ldout(cct
, 0) << "ERROR creating new realm object " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
828 // create the control object for watch/notify
829 ret
= create_control(exclusive
);
831 ldout(cct
, 0) << "ERROR creating control for new realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
835 if (current_period
.empty()) {
836 /* create new period for the realm */
837 ret
= period
.init(cct
, store
, id
, name
, false);
841 ret
= period
.create(true);
843 ldout(cct
, 0) << "ERROR: creating new period for realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
847 period
= RGWPeriod(current_period
, 0);
848 int ret
= period
.init(cct
, store
, id
, name
);
850 ldout(cct
, 0) << "ERROR: failed to init period " << current_period
<< dendl
;
854 ret
= set_current_period(period
);
856 ldout(cct
, 0) << "ERROR: failed set current period " << current_period
<< dendl
;
859 // try to set as default. may race with another create, so pass exclusive=true
860 // so we don't override an existing default
861 ret
= set_as_default(true);
862 if (ret
< 0 && ret
!= -EEXIST
) {
863 ldout(cct
, 0) << "WARNING: failed to set realm as default realm, ret=" << ret
<< dendl
;
869 int RGWRealm::delete_obj()
871 int ret
= RGWSystemMetaObj::delete_obj();
875 return delete_control();
878 int RGWRealm::create_control(bool exclusive
)
880 auto pool
= rgw_pool
{get_pool(cct
)};
881 auto oid
= get_control_oid();
882 return rgw_put_system_obj(store
, pool
, oid
, nullptr, 0, exclusive
,
883 nullptr, real_time(), nullptr);
886 int RGWRealm::delete_control()
888 auto pool
= rgw_pool
{get_pool(cct
)};
889 auto obj
= rgw_raw_obj
{pool
, get_control_oid()};
890 return store
->delete_system_obj(obj
);
893 rgw_pool
RGWRealm::get_pool(CephContext
*cct
)
895 if (cct
->_conf
->rgw_realm_root_pool
.empty()) {
896 return rgw_pool(RGW_DEFAULT_REALM_ROOT_POOL
);
898 return rgw_pool(cct
->_conf
->rgw_realm_root_pool
);
901 const string
RGWRealm::get_default_oid(bool old_format
)
903 if (cct
->_conf
->rgw_default_realm_info_oid
.empty()) {
904 return default_realm_info_oid
;
906 return cct
->_conf
->rgw_default_realm_info_oid
;
909 const string
& RGWRealm::get_names_oid_prefix()
911 return realm_names_oid_prefix
;
914 const string
& RGWRealm::get_info_oid_prefix(bool old_format
)
916 return realm_info_oid_prefix
;
919 int RGWRealm::set_current_period(RGWPeriod
& period
)
921 // update realm epoch to match the period's
922 if (epoch
> period
.get_realm_epoch()) {
923 ldout(cct
, 0) << "ERROR: set_current_period with old realm epoch "
924 << period
.get_realm_epoch() << ", current epoch=" << epoch
<< dendl
;
927 if (epoch
== period
.get_realm_epoch() && current_period
!= period
.get_id()) {
928 ldout(cct
, 0) << "ERROR: set_current_period with same realm epoch "
929 << period
.get_realm_epoch() << ", but different period id "
930 << period
.get_id() << " != " << current_period
<< dendl
;
934 epoch
= period
.get_realm_epoch();
935 current_period
= period
.get_id();
939 ldout(cct
, 0) << "ERROR: period update: " << cpp_strerror(-ret
) << dendl
;
943 ret
= period
.reflect();
945 ldout(cct
, 0) << "ERROR: period.reflect(): " << cpp_strerror(-ret
) << dendl
;
952 string
RGWRealm::get_control_oid()
954 return get_info_oid_prefix() + id
+ ".control";
957 int RGWRealm::notify_zone(bufferlist
& bl
)
959 // open a context on the realm's pool
960 rgw_pool pool
{get_pool(cct
)};
962 int r
= rgw_init_ioctx(store
->get_rados_handle(), pool
, ctx
);
964 ldout(cct
, 0) << "Failed to open pool " << pool
<< dendl
;
967 // send a notify on the realm object
968 r
= ctx
.notify2(get_control_oid(), bl
, 0, nullptr);
970 ldout(cct
, 0) << "Realm notify failed with " << r
<< dendl
;
976 int RGWRealm::notify_new_period(const RGWPeriod
& period
)
979 // push the period to dependent zonegroups/zones
980 ::encode(RGWRealmNotify::ZonesNeedPeriod
, bl
);
981 ::encode(period
, bl
);
982 // reload the gateway with the new period
983 ::encode(RGWRealmNotify::Reload
, bl
);
985 return notify_zone(bl
);
988 std::string
RGWPeriodConfig::get_oid(const std::string
& realm_id
)
990 if (realm_id
.empty()) {
991 return "period_config.default";
993 return "period_config." + realm_id
;
996 rgw_pool
RGWPeriodConfig::get_pool(CephContext
*cct
)
998 const auto& pool_name
= cct
->_conf
->rgw_period_root_pool
;
999 if (pool_name
.empty()) {
1000 return {RGW_DEFAULT_PERIOD_ROOT_POOL
};
1005 int RGWPeriodConfig::read(RGWRados
*store
, const std::string
& realm_id
)
1007 RGWObjectCtx
obj_ctx(store
);
1008 const auto& pool
= get_pool(store
->ctx());
1009 const auto& oid
= get_oid(realm_id
);
1012 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, nullptr, nullptr);
1017 bufferlist::iterator iter
= bl
.begin();
1018 ::decode(*this, iter
);
1019 } catch (buffer::error
& err
) {
1025 int RGWPeriodConfig::write(RGWRados
*store
, const std::string
& realm_id
)
1027 const auto& pool
= get_pool(store
->ctx());
1028 const auto& oid
= get_oid(realm_id
);
1030 ::encode(*this, bl
);
1031 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1032 false, nullptr, real_time(), nullptr);
1035 int RGWPeriod::init(CephContext
*_cct
, RGWRados
*_store
, const string
& period_realm_id
,
1036 const string
& period_realm_name
, bool setup_obj
)
1040 realm_id
= period_realm_id
;
1041 realm_name
= period_realm_name
;
1046 return init(_cct
, _store
, setup_obj
);
1050 int RGWPeriod::init(CephContext
*_cct
, RGWRados
*_store
, bool setup_obj
)
1059 RGWRealm
realm(realm_id
, realm_name
);
1060 int ret
= realm
.init(cct
, store
);
1062 ldout(cct
, 0) << "RGWPeriod::init failed to init realm " << realm_name
<< " id " << realm_id
<< " : " <<
1063 cpp_strerror(-ret
) << dendl
;
1066 id
= realm
.get_current_period();
1067 realm_id
= realm
.get_id();
1071 int ret
= use_latest_epoch();
1073 ldout(cct
, 0) << "failed to use_latest_epoch period id " << id
<< " realm " << realm_name
<< " id " << realm_id
1074 << " : " << cpp_strerror(-ret
) << dendl
;
1083 int RGWPeriod::get_zonegroup(RGWZoneGroup
& zonegroup
, const string
& zonegroup_id
) {
1084 map
<string
, RGWZoneGroup
>::const_iterator iter
;
1085 if (!zonegroup_id
.empty()) {
1086 iter
= period_map
.zonegroups
.find(zonegroup_id
);
1088 iter
= period_map
.zonegroups
.find("default");
1090 if (iter
!= period_map
.zonegroups
.end()) {
1091 zonegroup
= iter
->second
;
1098 const string
& RGWPeriod::get_latest_epoch_oid()
1100 if (cct
->_conf
->rgw_period_latest_epoch_info_oid
.empty()) {
1101 return period_latest_epoch_info_oid
;
1103 return cct
->_conf
->rgw_period_latest_epoch_info_oid
;
1106 const string
& RGWPeriod::get_info_oid_prefix()
1108 return period_info_oid_prefix
;
1111 const string
RGWPeriod::get_period_oid_prefix()
1113 return get_info_oid_prefix() + id
;
1116 const string
RGWPeriod::get_period_oid()
1118 std::ostringstream oss
;
1119 oss
<< get_period_oid_prefix();
1120 // skip the epoch for the staging period
1121 if (id
!= get_staging_id(realm_id
))
1122 oss
<< "." << epoch
;
1126 int RGWPeriod::read_latest_epoch(RGWPeriodLatestEpochInfo
& info
,
1127 RGWObjVersionTracker
*objv
)
1129 string oid
= get_period_oid_prefix() + get_latest_epoch_oid();
1131 rgw_pool
pool(get_pool(cct
));
1133 RGWObjectCtx
obj_ctx(store
);
1134 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, objv
, nullptr);
1136 ldout(cct
, 1) << "error read_lastest_epoch " << pool
<< ":" << oid
<< dendl
;
1140 bufferlist::iterator iter
= bl
.begin();
1141 ::decode(info
, iter
);
1142 } catch (buffer::error
& err
) {
1143 ldout(cct
, 0) << "error decoding data from " << pool
<< ":" << oid
<< dendl
;
1150 int RGWPeriod::get_latest_epoch(epoch_t
& latest_epoch
)
1152 RGWPeriodLatestEpochInfo info
;
1154 int ret
= read_latest_epoch(info
);
1159 latest_epoch
= info
.epoch
;
1164 int RGWPeriod::use_latest_epoch()
1166 RGWPeriodLatestEpochInfo info
;
1167 int ret
= read_latest_epoch(info
);
1177 int RGWPeriod::set_latest_epoch(epoch_t epoch
, bool exclusive
,
1178 RGWObjVersionTracker
*objv
)
1180 string oid
= get_period_oid_prefix() + get_latest_epoch_oid();
1182 rgw_pool
pool(get_pool(cct
));
1185 RGWPeriodLatestEpochInfo info
;
1190 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1191 exclusive
, objv
, real_time(), nullptr);
1194 int RGWPeriod::update_latest_epoch(epoch_t epoch
)
1196 static constexpr int MAX_RETRIES
= 20;
1198 for (int i
= 0; i
< MAX_RETRIES
; i
++) {
1199 RGWPeriodLatestEpochInfo info
;
1200 RGWObjVersionTracker objv
;
1201 bool exclusive
= false;
1203 // read existing epoch
1204 int r
= read_latest_epoch(info
, &objv
);
1206 // use an exclusive create to set the epoch atomically
1208 ldout(cct
, 20) << "creating initial latest_epoch=" << epoch
1209 << " for period=" << id
<< dendl
;
1211 ldout(cct
, 0) << "ERROR: failed to read latest_epoch" << dendl
;
1213 } else if (epoch
<= info
.epoch
) {
1214 r
= -EEXIST
; // fail with EEXIST if epoch is not newer
1215 ldout(cct
, 1) << "found existing latest_epoch " << info
.epoch
1216 << " >= given epoch " << epoch
<< ", returning r=" << r
<< dendl
;
1219 ldout(cct
, 20) << "updating latest_epoch from " << info
.epoch
1220 << " -> " << epoch
<< " on period=" << id
<< dendl
;
1223 r
= set_latest_epoch(epoch
, exclusive
, &objv
);
1225 continue; // exclusive create raced with another update, retry
1226 } else if (r
== -ECANCELED
) {
1227 continue; // write raced with a conflicting version, retry
1230 ldout(cct
, 0) << "ERROR: failed to write latest_epoch" << dendl
;
1233 return 0; // return success
1236 return -ECANCELED
; // fail after max retries
1239 int RGWPeriod::delete_obj()
1241 rgw_pool
pool(get_pool(cct
));
1243 // delete the object for each period epoch
1244 for (epoch_t e
= 1; e
<= epoch
; e
++) {
1245 RGWPeriod p
{get_id(), e
};
1246 rgw_raw_obj oid
{pool
, p
.get_period_oid()};
1247 int ret
= store
->delete_system_obj(oid
);
1249 ldout(cct
, 0) << "WARNING: failed to delete period object " << oid
1250 << ": " << cpp_strerror(-ret
) << dendl
;
1254 // delete the .latest_epoch object
1255 rgw_raw_obj oid
{pool
, get_period_oid_prefix() + get_latest_epoch_oid()};
1256 int ret
= store
->delete_system_obj(oid
);
1258 ldout(cct
, 0) << "WARNING: failed to delete period object " << oid
1259 << ": " << cpp_strerror(-ret
) << dendl
;
1264 int RGWPeriod::read_info()
1266 rgw_pool
pool(get_pool(cct
));
1270 RGWObjectCtx
obj_ctx(store
);
1271 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, get_period_oid(), bl
, NULL
, NULL
);
1273 ldout(cct
, 0) << "failed reading obj info from " << pool
<< ":" << get_period_oid() << ": " << cpp_strerror(-ret
) << dendl
;
1278 bufferlist::iterator iter
= bl
.begin();
1279 ::decode(*this, iter
);
1280 } catch (buffer::error
& err
) {
1281 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << get_period_oid() << dendl
;
1288 int RGWPeriod::create(bool exclusive
)
1292 /* create unique id */
1295 new_uuid
.generate_random();
1296 new_uuid
.print(uuid_str
);
1299 epoch
= FIRST_EPOCH
;
1303 ret
= store_info(exclusive
);
1305 ldout(cct
, 0) << "ERROR: storing info for " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
1309 ret
= set_latest_epoch(epoch
);
1311 ldout(cct
, 0) << "ERROR: setting latest epoch " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
1317 int RGWPeriod::store_info(bool exclusive
)
1319 rgw_pool
pool(get_pool(cct
));
1321 string oid
= get_period_oid();
1323 ::encode(*this, bl
);
1325 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1326 exclusive
, NULL
, real_time(), NULL
);
1329 rgw_pool
RGWPeriod::get_pool(CephContext
*cct
)
1331 if (cct
->_conf
->rgw_period_root_pool
.empty()) {
1332 return rgw_pool(RGW_DEFAULT_PERIOD_ROOT_POOL
);
1334 return rgw_pool(cct
->_conf
->rgw_period_root_pool
);
1337 int RGWPeriod::add_zonegroup(const RGWZoneGroup
& zonegroup
)
1339 if (zonegroup
.realm_id
!= realm_id
) {
1342 int ret
= period_map
.update(zonegroup
, cct
);
1344 ldout(cct
, 0) << "ERROR: updating period map: " << cpp_strerror(-ret
) << dendl
;
1348 return store_info(false);
1351 int RGWPeriod::update()
1353 ldout(cct
, 20) << __func__
<< " realm " << realm_id
<< " period " << get_id() << dendl
;
1354 list
<string
> zonegroups
;
1355 int ret
= store
->list_zonegroups(zonegroups
);
1357 ldout(cct
, 0) << "ERROR: failed to list zonegroups: " << cpp_strerror(-ret
) << dendl
;
1361 // clear zone short ids of removed zones. period_map.update() will add the
1362 // remaining zones back
1363 period_map
.short_zone_ids
.clear();
1365 for (auto& iter
: zonegroups
) {
1366 RGWZoneGroup
zg(string(), iter
);
1367 ret
= zg
.init(cct
, store
);
1369 ldout(cct
, 0) << "WARNING: zg.init() failed: " << cpp_strerror(-ret
) << dendl
;
1373 if (zg
.realm_id
!= realm_id
) {
1374 ldout(cct
, 20) << "skipping zonegroup " << zg
.get_name() << " zone realm id " << zg
.realm_id
<< ", not on our realm " << realm_id
<< dendl
;
1378 if (zg
.master_zone
.empty()) {
1379 ldout(cct
, 0) << "ERROR: zonegroup " << zg
.get_name() << " should have a master zone " << dendl
;
1383 if (zg
.is_master_zonegroup()) {
1384 master_zonegroup
= zg
.get_id();
1385 master_zone
= zg
.master_zone
;
1388 int ret
= period_map
.update(zg
, cct
);
1394 ret
= period_config
.read(store
, realm_id
);
1395 if (ret
< 0 && ret
!= -ENOENT
) {
1396 ldout(cct
, 0) << "ERROR: failed to read period config: "
1397 << cpp_strerror(ret
) << dendl
;
1403 int RGWPeriod::reflect()
1405 for (auto& iter
: period_map
.zonegroups
) {
1406 RGWZoneGroup
& zg
= iter
.second
;
1407 zg
.reinit_instance(cct
, store
);
1408 int r
= zg
.write(false);
1410 ldout(cct
, 0) << "ERROR: failed to store zonegroup info for zonegroup=" << iter
.first
<< ": " << cpp_strerror(-r
) << dendl
;
1413 if (zg
.is_master_zonegroup()) {
1414 // set master as default if no default exists
1415 r
= zg
.set_as_default(true);
1417 ldout(cct
, 1) << "Set the period's master zonegroup " << zg
.get_id()
1418 << " as the default" << dendl
;
1423 int r
= period_config
.write(store
, realm_id
);
1425 ldout(cct
, 0) << "ERROR: failed to store period config: "
1426 << cpp_strerror(-r
) << dendl
;
1432 void RGWPeriod::fork()
1434 ldout(cct
, 20) << __func__
<< " realm " << realm_id
<< " period " << id
<< dendl
;
1435 predecessor_uuid
= id
;
1436 id
= get_staging_id(realm_id
);
1441 static int read_sync_status(RGWRados
*store
, rgw_meta_sync_status
*sync_status
)
1443 // initialize a sync status manager to read the status
1444 RGWMetaSyncStatusManager
mgr(store
, store
->get_async_rados());
1449 r
= mgr
.read_sync_status(sync_status
);
1454 int RGWPeriod::update_sync_status(const RGWPeriod
¤t_period
,
1455 std::ostream
& error_stream
,
1456 bool force_if_stale
)
1458 rgw_meta_sync_status status
;
1459 int r
= read_sync_status(store
, &status
);
1461 ldout(cct
, 0) << "period failed to read sync status: "
1462 << cpp_strerror(-r
) << dendl
;
1466 std::vector
<std::string
> markers
;
1468 const auto current_epoch
= current_period
.get_realm_epoch();
1469 if (current_epoch
!= status
.sync_info
.realm_epoch
) {
1470 // no sync status markers for the current period
1471 assert(current_epoch
> status
.sync_info
.realm_epoch
);
1472 const int behind
= current_epoch
- status
.sync_info
.realm_epoch
;
1473 if (!force_if_stale
&& current_epoch
> 1) {
1474 error_stream
<< "ERROR: This zone is " << behind
<< " period(s) behind "
1475 "the current master zone in metadata sync. If this zone is promoted "
1476 "to master, any metadata changes during that time are likely to "
1478 "Waiting for this zone to catch up on metadata sync (see "
1479 "'radosgw-admin sync status') is recommended.\n"
1480 "To promote this zone to master anyway, add the flag "
1481 "--yes-i-really-mean-it." << std::endl
;
1484 // empty sync status markers - other zones will skip this period during
1485 // incremental metadata sync
1486 markers
.resize(status
.sync_info
.num_shards
);
1488 markers
.reserve(status
.sync_info
.num_shards
);
1489 for (auto& i
: status
.sync_markers
) {
1490 auto& marker
= i
.second
;
1491 // filter out markers from other periods
1492 if (marker
.realm_epoch
!= current_epoch
) {
1493 marker
.marker
.clear();
1495 markers
.emplace_back(std::move(marker
.marker
));
1499 std::swap(sync_status
, markers
);
1503 int RGWPeriod::commit(RGWRealm
& realm
, const RGWPeriod
& current_period
,
1504 std::ostream
& error_stream
, bool force_if_stale
)
1506 ldout(cct
, 20) << __func__
<< " realm " << realm
.get_id() << " period " << current_period
.get_id() << dendl
;
1507 // gateway must be in the master zone to commit
1508 if (master_zone
!= store
->get_zone_params().get_id()) {
1509 error_stream
<< "Cannot commit period on zone "
1510 << store
->get_zone_params().get_id() << ", it must be sent to "
1511 "the period's master zone " << master_zone
<< '.' << std::endl
;
1514 // period predecessor must match current period
1515 if (predecessor_uuid
!= current_period
.get_id()) {
1516 error_stream
<< "Period predecessor " << predecessor_uuid
1517 << " does not match current period " << current_period
.get_id()
1518 << ". Use 'period pull' to get the latest period from the master, "
1519 "reapply your changes, and try again." << std::endl
;
1522 // realm epoch must be 1 greater than current period
1523 if (realm_epoch
!= current_period
.get_realm_epoch() + 1) {
1524 error_stream
<< "Period's realm epoch " << realm_epoch
1525 << " does not come directly after current realm epoch "
1526 << current_period
.get_realm_epoch() << ". Use 'realm pull' to get the "
1527 "latest realm and period from the master zone, reapply your changes, "
1528 "and try again." << std::endl
;
1531 // did the master zone change?
1532 if (master_zone
!= current_period
.get_master_zone()) {
1533 // store the current metadata sync status in the period
1534 int r
= update_sync_status(current_period
, error_stream
, force_if_stale
);
1536 ldout(cct
, 0) << "failed to update metadata sync status: "
1537 << cpp_strerror(-r
) << dendl
;
1540 // create an object with a new period id
1543 ldout(cct
, 0) << "failed to create new period: " << cpp_strerror(-r
) << dendl
;
1546 // set as current period
1547 r
= realm
.set_current_period(*this);
1549 ldout(cct
, 0) << "failed to update realm's current period: "
1550 << cpp_strerror(-r
) << dendl
;
1553 ldout(cct
, 4) << "Promoted to master zone and committed new period "
1555 realm
.notify_new_period(*this);
1558 // period must be based on current epoch
1559 if (epoch
!= current_period
.get_epoch()) {
1560 error_stream
<< "Period epoch " << epoch
<< " does not match "
1561 "predecessor epoch " << current_period
.get_epoch()
1562 << ". Use 'period pull' to get the latest epoch from the master zone, "
1563 "reapply your changes, and try again." << std::endl
;
1566 // set period as next epoch
1567 set_id(current_period
.get_id());
1568 set_epoch(current_period
.get_epoch() + 1);
1569 set_predecessor(current_period
.get_predecessor());
1570 realm_epoch
= current_period
.get_realm_epoch();
1571 // write the period to rados
1572 int r
= store_info(false);
1574 ldout(cct
, 0) << "failed to store period: " << cpp_strerror(-r
) << dendl
;
1577 // set as latest epoch
1578 r
= update_latest_epoch(epoch
);
1580 // already have this epoch (or a more recent one)
1584 ldout(cct
, 0) << "failed to set latest epoch: " << cpp_strerror(-r
) << dendl
;
1589 ldout(cct
, 0) << "failed to update local objects: " << cpp_strerror(-r
) << dendl
;
1592 ldout(cct
, 4) << "Committed new epoch " << epoch
1593 << " for period " << id
<< dendl
;
1594 realm
.notify_new_period(*this);
1598 int RGWZoneParams::create_default(bool old_format
)
1600 name
= default_zone_name
;
1615 int get_zones_pool_set(CephContext
* cct
,
1617 const list
<string
>& zones
,
1618 const string
& my_zone_id
,
1619 set
<rgw_pool
>& pool_names
)
1621 for(auto const& iter
: zones
) {
1622 RGWZoneParams
zone(iter
);
1623 int r
= zone
.init(cct
, store
);
1625 ldout(cct
, 0) << "Error: init zone " << iter
<< ":" << cpp_strerror(-r
) << dendl
;
1628 if (zone
.get_id() != my_zone_id
) {
1629 pool_names
.insert(zone
.domain_root
);
1630 pool_names
.insert(zone
.metadata_heap
);
1631 pool_names
.insert(zone
.control_pool
);
1632 pool_names
.insert(zone
.gc_pool
);
1633 pool_names
.insert(zone
.log_pool
);
1634 pool_names
.insert(zone
.intent_log_pool
);
1635 pool_names
.insert(zone
.usage_log_pool
);
1636 pool_names
.insert(zone
.user_keys_pool
);
1637 pool_names
.insert(zone
.user_email_pool
);
1638 pool_names
.insert(zone
.user_swift_pool
);
1639 pool_names
.insert(zone
.user_uid_pool
);
1640 pool_names
.insert(zone
.roles_pool
);
1641 pool_names
.insert(zone
.reshard_pool
);
1642 for(auto& iter
: zone
.placement_pools
) {
1643 pool_names
.insert(iter
.second
.index_pool
);
1644 pool_names
.insert(iter
.second
.data_pool
);
1645 pool_names
.insert(iter
.second
.data_extra_pool
);
1652 rgw_pool
fix_zone_pool_dup(set
<rgw_pool
> pools
,
1653 const string
& default_prefix
,
1654 const string
& default_suffix
,
1655 const rgw_pool
& suggested_pool
)
1657 string suggested_name
= suggested_pool
.to_str();
1659 string prefix
= default_prefix
;
1660 string suffix
= default_suffix
;
1662 if (!suggested_pool
.empty()) {
1663 prefix
= suggested_name
.substr(0, suggested_name
.find("."));
1664 suffix
= suggested_name
.substr(prefix
.length());
1667 rgw_pool
pool(prefix
+ suffix
);
1669 if (pools
.find(pool
) == pools
.end()) {
1673 pool
= prefix
+ "_" + std::to_string(std::rand()) + suffix
;
1674 if (pools
.find(pool
) == pools
.end()) {
1681 int RGWZoneParams::fix_pool_names()
1685 int r
= store
->list_zones(zones
);
1687 ldout(cct
, 10) << "WARNING: store->list_zones() returned r=" << r
<< dendl
;
1690 set
<rgw_pool
> pools
;
1691 r
= get_zones_pool_set(cct
, store
, zones
, id
, pools
);
1693 ldout(cct
, 0) << "Error: get_zones_pool_names" << r
<< dendl
;
1697 domain_root
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:root", domain_root
);
1698 if (!metadata_heap
.name
.empty()) {
1699 metadata_heap
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:heap", metadata_heap
);
1701 control_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.control", control_pool
);
1702 gc_pool
= fix_zone_pool_dup(pools
, name
,".rgw.log:gc", gc_pool
);
1703 lc_pool
= fix_zone_pool_dup(pools
, name
,".rgw.log:lc", lc_pool
);
1704 log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log", log_pool
);
1705 intent_log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:intent", intent_log_pool
);
1706 usage_log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:usage", usage_log_pool
);
1707 user_keys_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.keys", user_keys_pool
);
1708 user_email_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.email", user_email_pool
);
1709 user_swift_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.swift", user_swift_pool
);
1710 user_uid_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.uid", user_uid_pool
);
1711 roles_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:roles", roles_pool
);
1712 reshard_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:reshard", reshard_pool
);
1714 for(auto& iter
: placement_pools
) {
1715 iter
.second
.index_pool
= fix_zone_pool_dup(pools
, name
, "." + default_bucket_index_pool_suffix
,
1716 iter
.second
.index_pool
);
1717 iter
.second
.data_pool
= fix_zone_pool_dup(pools
, name
, "." + default_storage_pool_suffix
,
1718 iter
.second
.data_pool
);
1719 iter
.second
.data_extra_pool
= fix_zone_pool_dup(pools
, name
, "." + default_storage_extra_pool_suffix
,
1720 iter
.second
.data_extra_pool
);
1726 int RGWZoneParams::create(bool exclusive
)
1728 /* check for old pools config */
1729 rgw_raw_obj
obj(domain_root
, avail_pools
);
1730 int r
= store
->raw_obj_stat(obj
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1732 ldout(store
->ctx(), 10) << "couldn't find old data placement pools config, setting up new ones for the zone" << dendl
;
1733 /* a new system, let's set new placement info */
1734 RGWZonePlacementInfo default_placement
;
1735 default_placement
.index_pool
= name
+ "." + default_bucket_index_pool_suffix
;
1736 default_placement
.data_pool
= name
+ "." + default_storage_pool_suffix
;
1737 default_placement
.data_extra_pool
= name
+ "." + default_storage_extra_pool_suffix
;
1738 placement_pools
["default-placement"] = default_placement
;
1741 r
= fix_pool_names();
1743 ldout(cct
, 0) << "ERROR: fix_pool_names returned r=" << r
<< dendl
;
1747 r
= RGWSystemMetaObj::create(exclusive
);
1752 // try to set as default. may race with another create, so pass exclusive=true
1753 // so we don't override an existing default
1754 r
= set_as_default(true);
1755 if (r
< 0 && r
!= -EEXIST
) {
1756 ldout(cct
, 10) << "WARNING: failed to set zone as default, r=" << r
<< dendl
;
1762 rgw_pool
RGWZoneParams::get_pool(CephContext
*cct
)
1764 if (cct
->_conf
->rgw_zone_root_pool
.empty()) {
1765 return rgw_pool(RGW_DEFAULT_ZONE_ROOT_POOL
);
1768 return rgw_pool(cct
->_conf
->rgw_zone_root_pool
);
1771 const string
RGWZoneParams::get_default_oid(bool old_format
)
1774 return cct
->_conf
->rgw_default_zone_info_oid
;
1777 return cct
->_conf
->rgw_default_zone_info_oid
+ "." + realm_id
;
1780 const string
& RGWZoneParams::get_names_oid_prefix()
1782 return zone_names_oid_prefix
;
1785 const string
& RGWZoneParams::get_info_oid_prefix(bool old_format
)
1787 return zone_info_oid_prefix
;
1790 const string
& RGWZoneParams::get_predefined_name(CephContext
*cct
) {
1791 return cct
->_conf
->rgw_zone
;
1794 int RGWZoneParams::init(CephContext
*cct
, RGWRados
*store
, bool setup_obj
, bool old_format
)
1797 name
= cct
->_conf
->rgw_zone
;
1800 return RGWSystemMetaObj::init(cct
, store
, setup_obj
, old_format
);
1803 int RGWZoneParams::read_default_id(string
& default_id
, bool old_format
)
1805 if (realm_id
.empty()) {
1806 /* try using default realm */
1808 int ret
= realm
.init(cct
, store
);
1809 //no default realm exist
1811 return read_id(default_zone_name
, default_id
);
1813 realm_id
= realm
.get_id();
1816 return RGWSystemMetaObj::read_default_id(default_id
, old_format
);
1820 int RGWZoneParams::set_as_default(bool exclusive
)
1822 if (realm_id
.empty()) {
1823 /* try using default realm */
1825 int ret
= realm
.init(cct
, store
);
1827 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
1830 realm_id
= realm
.get_id();
1833 return RGWSystemMetaObj::set_as_default(exclusive
);
1836 const string
& RGWZoneParams::get_compression_type(const string
& placement_rule
) const
1838 static const std::string NONE
{"none"};
1839 auto p
= placement_pools
.find(placement_rule
);
1840 if (p
== placement_pools
.end()) {
1843 const auto& type
= p
->second
.compression_type
;
1844 return !type
.empty() ? type
: NONE
;
1847 void RGWPeriodMap::encode(bufferlist
& bl
) const {
1848 ENCODE_START(2, 1, bl
);
1850 ::encode(zonegroups
, bl
);
1851 ::encode(master_zonegroup
, bl
);
1852 ::encode(short_zone_ids
, bl
);
1856 void RGWPeriodMap::decode(bufferlist::iterator
& bl
) {
1857 DECODE_START(2, bl
);
1859 ::decode(zonegroups
, bl
);
1860 ::decode(master_zonegroup
, bl
);
1861 if (struct_v
>= 2) {
1862 ::decode(short_zone_ids
, bl
);
1866 zonegroups_by_api
.clear();
1867 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.begin();
1868 iter
!= zonegroups
.end(); ++iter
) {
1869 RGWZoneGroup
& zonegroup
= iter
->second
;
1870 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
1871 if (zonegroup
.is_master_zonegroup()) {
1872 master_zonegroup
= zonegroup
.get_id();
1877 // run an MD5 hash on the zone_id and return the first 32 bits
1878 static uint32_t gen_short_zone_id(const std::string zone_id
)
1880 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
1882 hash
.Update((const byte
*)zone_id
.c_str(), zone_id
.size());
1886 memcpy((char *)&short_id
, md5
, sizeof(short_id
));
1887 return std::max(short_id
, 1u);
1890 int RGWPeriodMap::update(const RGWZoneGroup
& zonegroup
, CephContext
*cct
)
1892 if (zonegroup
.is_master_zonegroup() && (!master_zonegroup
.empty() && zonegroup
.get_id() != master_zonegroup
)) {
1893 ldout(cct
,0) << "Error updating periodmap, multiple master zonegroups configured "<< dendl
;
1894 ldout(cct
,0) << "master zonegroup: " << master_zonegroup
<< " and " << zonegroup
.get_id() <<dendl
;
1897 map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.find(zonegroup
.get_id());
1898 if (iter
!= zonegroups
.end()) {
1899 RGWZoneGroup
& old_zonegroup
= iter
->second
;
1900 if (!old_zonegroup
.api_name
.empty()) {
1901 zonegroups_by_api
.erase(old_zonegroup
.api_name
);
1904 zonegroups
[zonegroup
.get_id()] = zonegroup
;
1906 if (!zonegroup
.api_name
.empty()) {
1907 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
1910 if (zonegroup
.is_master_zonegroup()) {
1911 master_zonegroup
= zonegroup
.get_id();
1912 } else if (master_zonegroup
== zonegroup
.get_id()) {
1913 master_zonegroup
= "";
1916 for (auto& i
: zonegroup
.zones
) {
1917 auto& zone
= i
.second
;
1918 if (short_zone_ids
.find(zone
.id
) != short_zone_ids
.end()) {
1921 // calculate the zone's short id
1922 uint32_t short_id
= gen_short_zone_id(zone
.id
);
1924 // search for an existing zone with the same short id
1925 for (auto& s
: short_zone_ids
) {
1926 if (s
.second
== short_id
) {
1927 ldout(cct
, 0) << "New zone '" << zone
.name
<< "' (" << zone
.id
1928 << ") generates the same short_zone_id " << short_id
1929 << " as existing zone id " << s
.first
<< dendl
;
1934 short_zone_ids
[zone
.id
] = short_id
;
1940 uint32_t RGWPeriodMap::get_zone_short_id(const string
& zone_id
) const
1942 auto i
= short_zone_ids
.find(zone_id
);
1943 if (i
== short_zone_ids
.end()) {
1949 int RGWZoneGroupMap::read(CephContext
*cct
, RGWRados
*store
)
1953 int ret
= period
.init(cct
, store
);
1955 cerr
<< "failed to read current period info: " << cpp_strerror(ret
);
1959 bucket_quota
= period
.get_config().bucket_quota
;
1960 user_quota
= period
.get_config().user_quota
;
1961 zonegroups
= period
.get_map().zonegroups
;
1962 zonegroups_by_api
= period
.get_map().zonegroups_by_api
;
1963 master_zonegroup
= period
.get_map().master_zonegroup
;
1968 void RGWRegionMap::encode(bufferlist
& bl
) const {
1969 ENCODE_START( 3, 1, bl
);
1970 ::encode(regions
, bl
);
1971 ::encode(master_region
, bl
);
1972 ::encode(bucket_quota
, bl
);
1973 ::encode(user_quota
, bl
);
1977 void RGWRegionMap::decode(bufferlist::iterator
& bl
) {
1978 DECODE_START(3, bl
);
1979 ::decode(regions
, bl
);
1980 ::decode(master_region
, bl
);
1982 ::decode(bucket_quota
, bl
);
1984 ::decode(user_quota
, bl
);
1988 void RGWZoneGroupMap::encode(bufferlist
& bl
) const {
1989 ENCODE_START( 3, 1, bl
);
1990 ::encode(zonegroups
, bl
);
1991 ::encode(master_zonegroup
, bl
);
1992 ::encode(bucket_quota
, bl
);
1993 ::encode(user_quota
, bl
);
1997 void RGWZoneGroupMap::decode(bufferlist::iterator
& bl
) {
1998 DECODE_START(3, bl
);
1999 ::decode(zonegroups
, bl
);
2000 ::decode(master_zonegroup
, bl
);
2002 ::decode(bucket_quota
, bl
);
2004 ::decode(user_quota
, bl
);
2007 zonegroups_by_api
.clear();
2008 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.begin();
2009 iter
!= zonegroups
.end(); ++iter
) {
2010 RGWZoneGroup
& zonegroup
= iter
->second
;
2011 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
2012 if (zonegroup
.is_master_zonegroup()) {
2013 master_zonegroup
= zonegroup
.get_name();
2018 void RGWObjVersionTracker::prepare_op_for_read(ObjectReadOperation
*op
)
2020 obj_version
*check_objv
= version_for_check();
2023 cls_version_check(*op
, *check_objv
, VER_COND_EQ
);
2026 cls_version_read(*op
, &read_version
);
2029 void RGWObjVersionTracker::prepare_op_for_write(ObjectWriteOperation
*op
)
2031 obj_version
*check_objv
= version_for_check();
2032 obj_version
*modify_version
= version_for_write();
2035 cls_version_check(*op
, *check_objv
, VER_COND_EQ
);
2038 if (modify_version
) {
2039 cls_version_set(*op
, *modify_version
);
2041 cls_version_inc(*op
);
2045 void RGWObjManifest::obj_iterator::operator++()
2047 if (manifest
->explicit_objs
) {
2050 if (explicit_iter
== manifest
->objs
.end()) {
2051 ofs
= manifest
->obj_size
;
2055 update_explicit_pos();
2061 uint64_t obj_size
= manifest
->get_obj_size();
2062 uint64_t head_size
= manifest
->get_head_size();
2064 if (ofs
== obj_size
) {
2068 if (manifest
->rules
.empty()) {
2072 /* are we still pointing at the head? */
2073 if (ofs
< head_size
) {
2074 rule_iter
= manifest
->rules
.begin();
2075 RGWObjManifestRule
*rule
= &rule_iter
->second
;
2076 ofs
= MIN(head_size
, obj_size
);
2079 stripe_size
= MIN(obj_size
- ofs
, rule
->stripe_max_size
);
2080 if (rule
->part_size
> 0) {
2081 stripe_size
= MIN(stripe_size
, rule
->part_size
);
2087 RGWObjManifestRule
*rule
= &rule_iter
->second
;
2089 stripe_ofs
+= rule
->stripe_max_size
;
2091 dout(20) << "RGWObjManifest::operator++(): rule->part_size=" << rule
->part_size
<< " rules.size()=" << manifest
->rules
.size() << dendl
;
2093 if (rule
->part_size
> 0) {
2094 /* multi part, multi stripes object */
2096 dout(20) << "RGWObjManifest::operator++(): stripe_ofs=" << stripe_ofs
<< " part_ofs=" << part_ofs
<< " rule->part_size=" << rule
->part_size
<< dendl
;
2098 if (stripe_ofs
>= part_ofs
+ rule
->part_size
) {
2099 /* moved to the next part */
2101 part_ofs
+= rule
->part_size
;
2102 stripe_ofs
= part_ofs
;
2104 bool last_rule
= (next_rule_iter
== manifest
->rules
.end());
2105 /* move to the next rule? */
2106 if (!last_rule
&& stripe_ofs
>= next_rule_iter
->second
.start_ofs
) {
2107 rule_iter
= next_rule_iter
;
2108 last_rule
= (next_rule_iter
== manifest
->rules
.end());
2112 cur_part_id
= rule_iter
->second
.start_part_num
;
2117 rule
= &rule_iter
->second
;
2120 stripe_size
= MIN(rule
->part_size
- (stripe_ofs
- part_ofs
), rule
->stripe_max_size
);
2123 cur_override_prefix
= rule
->override_prefix
;
2126 if (ofs
> obj_size
) {
2132 dout(20) << "RGWObjManifest::operator++(): result: ofs=" << ofs
<< " stripe_ofs=" << stripe_ofs
<< " part_ofs=" << part_ofs
<< " rule->part_size=" << rule
->part_size
<< dendl
;
2136 int RGWObjManifest::generator::create_begin(CephContext
*cct
, RGWObjManifest
*_m
, const string
& placement_rule
, rgw_bucket
& _b
, rgw_obj
& _obj
)
2140 manifest
->set_tail_placement(placement_rule
, _b
);
2141 manifest
->set_head(placement_rule
, _obj
, 0);
2144 if (manifest
->get_prefix().empty()) {
2146 gen_rand_alphanumeric(cct
, buf
, sizeof(buf
) - 1);
2148 string oid_prefix
= ".";
2149 oid_prefix
.append(buf
);
2150 oid_prefix
.append("_");
2152 manifest
->set_prefix(oid_prefix
);
2155 bool found
= manifest
->get_rule(0, &rule
);
2157 derr
<< "ERROR: manifest->get_rule() could not find rule" << dendl
;
2161 uint64_t head_size
= manifest
->get_head_size();
2163 if (head_size
> 0) {
2164 cur_stripe_size
= head_size
;
2166 cur_stripe_size
= rule
.stripe_max_size
;
2169 cur_part_id
= rule
.start_part_num
;
2171 manifest
->get_implicit_location(cur_part_id
, cur_stripe
, 0, NULL
, &cur_obj
);
2173 // Normal object which not generated through copy operation
2174 manifest
->set_tail_instance(_obj
.key
.instance
);
2176 manifest
->update_iterators();
2181 int RGWObjManifest::generator::create_next(uint64_t ofs
)
2183 if (ofs
< last_ofs
) /* only going forward */
2186 uint64_t max_head_size
= manifest
->get_max_head_size();
2188 if (ofs
< max_head_size
) {
2189 manifest
->set_head_size(ofs
);
2192 if (ofs
>= max_head_size
) {
2193 manifest
->set_head_size(max_head_size
);
2194 cur_stripe
= (ofs
- max_head_size
) / rule
.stripe_max_size
;
2195 cur_stripe_size
= rule
.stripe_max_size
;
2197 if (cur_part_id
== 0 && max_head_size
> 0) {
2203 manifest
->set_obj_size(ofs
);
2205 manifest
->get_implicit_location(cur_part_id
, cur_stripe
, ofs
, NULL
, &cur_obj
);
2207 manifest
->update_iterators();
2212 const RGWObjManifest::obj_iterator
& RGWObjManifest::obj_begin()
2217 const RGWObjManifest::obj_iterator
& RGWObjManifest::obj_end()
2222 RGWObjManifest::obj_iterator
RGWObjManifest::obj_find(uint64_t ofs
)
2224 if (ofs
> obj_size
) {
2227 RGWObjManifest::obj_iterator
iter(this);
2232 int RGWObjManifest::append(RGWObjManifest
& m
, RGWZoneGroup
& zonegroup
, RGWZoneParams
& zone_params
)
2234 if (explicit_objs
|| m
.explicit_objs
) {
2235 return append_explicit(m
, zonegroup
, zone_params
);
2238 if (rules
.empty()) {
2243 string override_prefix
;
2245 if (prefix
.empty()) {
2249 if (prefix
!= m
.prefix
) {
2250 override_prefix
= m
.prefix
;
2253 map
<uint64_t, RGWObjManifestRule
>::iterator miter
= m
.rules
.begin();
2254 if (miter
== m
.rules
.end()) {
2255 return append_explicit(m
, zonegroup
, zone_params
);
2258 for (; miter
!= m
.rules
.end(); ++miter
) {
2259 map
<uint64_t, RGWObjManifestRule
>::reverse_iterator last_rule
= rules
.rbegin();
2261 RGWObjManifestRule
& rule
= last_rule
->second
;
2263 if (rule
.part_size
== 0) {
2264 rule
.part_size
= obj_size
- rule
.start_ofs
;
2267 RGWObjManifestRule
& next_rule
= miter
->second
;
2268 if (!next_rule
.part_size
) {
2269 next_rule
.part_size
= m
.obj_size
- next_rule
.start_ofs
;
2272 string rule_prefix
= prefix
;
2273 if (!rule
.override_prefix
.empty()) {
2274 rule_prefix
= rule
.override_prefix
;
2277 string next_rule_prefix
= m
.prefix
;
2278 if (!next_rule
.override_prefix
.empty()) {
2279 next_rule_prefix
= next_rule
.override_prefix
;
2282 if (rule
.part_size
!= next_rule
.part_size
||
2283 rule
.stripe_max_size
!= next_rule
.stripe_max_size
||
2284 rule_prefix
!= next_rule_prefix
) {
2285 if (next_rule_prefix
!= prefix
) {
2286 append_rules(m
, miter
, &next_rule_prefix
);
2288 append_rules(m
, miter
, NULL
);
2293 uint64_t expected_part_num
= rule
.start_part_num
+ 1;
2294 if (rule
.part_size
> 0) {
2295 expected_part_num
= rule
.start_part_num
+ (obj_size
+ next_rule
.start_ofs
- rule
.start_ofs
) / rule
.part_size
;
2298 if (expected_part_num
!= next_rule
.start_part_num
) {
2299 append_rules(m
, miter
, NULL
);
2304 set_obj_size(obj_size
+ m
.obj_size
);
2309 int RGWObjManifest::append(RGWObjManifest
& m
, RGWRados
*store
)
2311 return append(m
, store
->get_zonegroup(), store
->get_zone_params());
2314 void RGWObjManifest::append_rules(RGWObjManifest
& m
, map
<uint64_t, RGWObjManifestRule
>::iterator
& miter
,
2315 string
*override_prefix
)
2317 for (; miter
!= m
.rules
.end(); ++miter
) {
2318 RGWObjManifestRule rule
= miter
->second
;
2319 rule
.start_ofs
+= obj_size
;
2320 if (override_prefix
)
2321 rule
.override_prefix
= *override_prefix
;
2322 rules
[rule
.start_ofs
] = rule
;
2326 void RGWObjManifest::convert_to_explicit(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
)
2328 if (explicit_objs
) {
2331 obj_iterator iter
= obj_begin();
2333 while (iter
!= obj_end()) {
2334 RGWObjManifestPart
& part
= objs
[iter
.get_stripe_ofs()];
2335 const rgw_obj_select
& os
= iter
.get_location();
2336 const rgw_raw_obj
& raw_loc
= os
.get_raw_obj(zonegroup
, zone_params
);
2339 uint64_t ofs
= iter
.get_stripe_ofs();
2344 rgw_raw_obj_to_obj(tail_placement
.bucket
, raw_loc
, &part
.loc
);
2347 uint64_t next_ofs
= iter
.get_stripe_ofs();
2349 part
.size
= next_ofs
- ofs
;
2352 explicit_objs
= true;
2357 int RGWObjManifest::append_explicit(RGWObjManifest
& m
, const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
)
2359 if (!explicit_objs
) {
2360 convert_to_explicit(zonegroup
, zone_params
);
2362 if (!m
.explicit_objs
) {
2363 m
.convert_to_explicit(zonegroup
, zone_params
);
2365 map
<uint64_t, RGWObjManifestPart
>::iterator iter
;
2366 uint64_t base
= obj_size
;
2367 for (iter
= m
.objs
.begin(); iter
!= m
.objs
.end(); ++iter
) {
2368 RGWObjManifestPart
& part
= iter
->second
;
2369 objs
[base
+ iter
->first
] = part
;
2371 obj_size
+= m
.obj_size
;
2376 bool RGWObjManifest::get_rule(uint64_t ofs
, RGWObjManifestRule
*rule
)
2378 if (rules
.empty()) {
2382 map
<uint64_t, RGWObjManifestRule
>::iterator iter
= rules
.upper_bound(ofs
);
2383 if (iter
!= rules
.begin()) {
2387 *rule
= iter
->second
;
2392 void RGWObjVersionTracker::generate_new_write_ver(CephContext
*cct
)
2394 write_version
.ver
= 1;
2397 write_version
.tag
.clear();
2398 append_rand_alpha(cct
, write_version
.tag
, write_version
.tag
, TAG_LEN
);
2401 int RGWPutObjProcessor::complete(size_t accounted_size
, const string
& etag
,
2402 real_time
*mtime
, real_time set_mtime
,
2403 map
<string
, bufferlist
>& attrs
, real_time delete_at
,
2404 const char *if_match
, const char *if_nomatch
, const string
*user_data
,
2405 rgw_zone_set
*zones_trace
)
2407 int r
= do_complete(accounted_size
, etag
, mtime
, set_mtime
, attrs
, delete_at
, if_match
, if_nomatch
, user_data
, zones_trace
);
2411 is_complete
= !canceled
;
2415 CephContext
*RGWPutObjProcessor::ctx()
2417 return store
->ctx();
2420 RGWPutObjProcessor_Aio::~RGWPutObjProcessor_Aio()
2427 set
<rgw_raw_obj
>::iterator iter
;
2428 bool need_to_remove_head
= false;
2429 rgw_raw_obj raw_head
;
2431 if (!head_obj
.empty()) {
2432 store
->obj_to_raw(bucket_info
.placement_rule
, head_obj
, &raw_head
);
2436 * We should delete the object in the "multipart" namespace to avoid race condition.
2437 * Such race condition is caused by the fact that the multipart object is the gatekeeper of a multipart
2438 * upload, when it is deleted, a second upload would start with the same suffix("2/"), therefore, objects
2439 * written by the second upload may be deleted by the first upload.
2440 * details is describled on #11749
2442 * The above comment still stands, but instead of searching for a specific object in the multipart
2443 * namespace, we just make sure that we remove the object that is marked as the head object after
2444 * we remove all the other raw objects. Note that we use different call to remove the head object,
2445 * as this one needs to go via the bucket index prepare/complete 2-phase commit scheme.
2447 for (iter
= written_objs
.begin(); iter
!= written_objs
.end(); ++iter
) {
2448 const rgw_raw_obj
& obj
= *iter
;
2449 if (!head_obj
.empty() && obj
== raw_head
) {
2450 ldout(store
->ctx(), 5) << "NOTE: we should not process the head object (" << obj
<< ") here" << dendl
;
2451 need_to_remove_head
= true;
2455 int r
= store
->delete_raw_obj(obj
);
2456 if (r
< 0 && r
!= -ENOENT
) {
2457 ldout(store
->ctx(), 5) << "WARNING: failed to remove obj (" << obj
<< "), leaked" << dendl
;
2461 if (need_to_remove_head
) {
2462 ldout(store
->ctx(), 5) << "NOTE: we are going to process the head obj (" << raw_head
<< ")" << dendl
;
2463 int r
= store
->delete_obj(obj_ctx
, bucket_info
, head_obj
, 0, 0);
2464 if (r
< 0 && r
!= -ENOENT
) {
2465 ldout(store
->ctx(), 0) << "WARNING: failed to remove obj (" << raw_head
<< "), leaked" << dendl
;
2470 int RGWPutObjProcessor_Aio::handle_obj_data(rgw_raw_obj
& obj
, bufferlist
& bl
, off_t ofs
, off_t abs_ofs
, void **phandle
, bool exclusive
)
2472 if ((uint64_t)abs_ofs
+ bl
.length() > obj_len
)
2473 obj_len
= abs_ofs
+ bl
.length();
2475 if (!(obj
== last_written_obj
)) {
2476 last_written_obj
= obj
;
2479 // For the first call pass -1 as the offset to
2481 return store
->aio_put_obj_data(NULL
, obj
, bl
, ((ofs
!= 0) ? ofs
: -1), exclusive
, phandle
);
2484 struct put_obj_aio_info
RGWPutObjProcessor_Aio::pop_pending()
2486 struct put_obj_aio_info info
;
2487 info
= pending
.front();
2488 pending
.pop_front();
2489 pending_size
-= info
.size
;
2493 int RGWPutObjProcessor_Aio::wait_pending_front()
2495 if (pending
.empty()) {
2498 struct put_obj_aio_info info
= pop_pending();
2499 int ret
= store
->aio_wait(info
.handle
);
2502 add_written_obj(info
.obj
);
2508 bool RGWPutObjProcessor_Aio::pending_has_completed()
2510 if (pending
.empty())
2513 struct put_obj_aio_info
& info
= pending
.front();
2514 return store
->aio_completed(info
.handle
);
2517 int RGWPutObjProcessor_Aio::drain_pending()
2520 while (!pending
.empty()) {
2521 int r
= wait_pending_front();
2528 int RGWPutObjProcessor_Aio::throttle_data(void *handle
, const rgw_raw_obj
& obj
, uint64_t size
, bool need_to_wait
)
2530 bool _wait
= need_to_wait
;
2533 struct put_obj_aio_info info
;
2534 info
.handle
= handle
;
2537 pending_size
+= size
;
2538 pending
.push_back(info
);
2540 size_t orig_size
= pending_size
;
2542 /* first drain complete IOs */
2543 while (pending_has_completed()) {
2544 int r
= wait_pending_front();
2551 /* resize window in case messages are draining too fast */
2552 if (orig_size
- pending_size
>= window_size
) {
2553 window_size
+= store
->ctx()->_conf
->rgw_max_chunk_size
;
2554 uint64_t max_window_size
= store
->ctx()->_conf
->rgw_put_obj_max_window_size
;
2555 if (window_size
> max_window_size
) {
2556 window_size
= max_window_size
;
2560 /* now throttle. Note that need_to_wait should only affect the first IO operation */
2561 if (pending_size
> window_size
|| _wait
) {
2562 int r
= wait_pending_front();
2569 int RGWPutObjProcessor_Atomic::write_data(bufferlist
& bl
, off_t ofs
, void **phandle
, rgw_raw_obj
*pobj
, bool exclusive
)
2571 if (ofs
>= next_part_ofs
) {
2572 int r
= prepare_next_part(ofs
);
2585 return RGWPutObjProcessor_Aio::handle_obj_data(cur_obj
, bl
, ofs
- cur_part_ofs
, ofs
, phandle
, exclusive
);
2588 int RGWPutObjProcessor_Aio::prepare(RGWRados
*store
, string
*oid_rand
)
2590 RGWPutObjProcessor::prepare(store
, oid_rand
);
2592 window_size
= store
->ctx()->_conf
->rgw_put_obj_min_window_size
;
2597 int RGWPutObjProcessor_Atomic::handle_data(bufferlist
& bl
, off_t ofs
, void **phandle
, rgw_raw_obj
*pobj
, bool *again
)
2600 uint64_t max_write_size
= MIN(max_chunk_size
, (uint64_t)next_part_ofs
- data_ofs
);
2602 pending_data_bl
.claim_append(bl
);
2603 if (pending_data_bl
.length() < max_write_size
) {
2608 pending_data_bl
.splice(0, max_write_size
, &bl
);
2610 /* do we have enough data pending accumulated that needs to be written? */
2611 *again
= (pending_data_bl
.length() >= max_chunk_size
);
2613 if (!data_ofs
&& !immutable_head()) {
2614 first_chunk
.claim(bl
);
2615 obj_len
= (uint64_t)first_chunk
.length();
2616 int r
= prepare_next_part(obj_len
);
2623 off_t write_ofs
= data_ofs
;
2624 data_ofs
= write_ofs
+ bl
.length();
2625 bool exclusive
= (!write_ofs
&& immutable_head()); /* immutable head object, need to verify nothing exists there
2626 we could be racing with another upload, to the same
2627 object and cleanup can be messy */
2628 int ret
= write_data(bl
, write_ofs
, phandle
, pobj
, exclusive
);
2629 if (ret
>= 0) { /* we might return, need to clear bl as it was already sent */
2636 int RGWPutObjProcessor_Atomic::prepare_init(RGWRados
*store
, string
*oid_rand
)
2638 RGWPutObjProcessor_Aio::prepare(store
, oid_rand
);
2640 int r
= store
->get_max_chunk_size(bucket_info
.placement_rule
, head_obj
, &max_chunk_size
);
2648 int RGWPutObjProcessor_Atomic::prepare(RGWRados
*store
, string
*oid_rand
)
2650 head_obj
.init(bucket
, obj_str
);
2652 int r
= prepare_init(store
, oid_rand
);
2657 if (!version_id
.empty()) {
2658 head_obj
.key
.set_instance(version_id
);
2659 } else if (versioned_object
) {
2660 store
->gen_rand_obj_instance_name(&head_obj
);
2663 manifest
.set_trivial_rule(max_chunk_size
, store
->ctx()->_conf
->rgw_obj_stripe_size
);
2665 r
= manifest_gen
.create_begin(store
->ctx(), &manifest
, bucket_info
.placement_rule
, head_obj
.bucket
, head_obj
);
2673 int RGWPutObjProcessor_Atomic::prepare_next_part(off_t ofs
) {
2675 int ret
= manifest_gen
.create_next(ofs
);
2677 lderr(store
->ctx()) << "ERROR: manifest_gen.create_next() returned ret=" << ret
<< dendl
;
2681 next_part_ofs
= ofs
+ manifest_gen
.cur_stripe_max_size();
2682 cur_obj
= manifest_gen
.get_cur_obj(store
);
2687 int RGWPutObjProcessor_Atomic::complete_parts()
2689 if (obj_len
> (uint64_t)cur_part_ofs
) {
2690 return prepare_next_part(obj_len
);
2695 int RGWPutObjProcessor_Atomic::complete_writing_data()
2697 if (!data_ofs
&& !immutable_head()) {
2698 /* only claim if pending_data_bl() is not empty. This is needed because we might be called twice
2699 * (e.g., when a retry due to race happens). So a second call to first_chunk.claim() would
2700 * clobber first_chunk
2702 if (pending_data_bl
.length() > 0) {
2703 first_chunk
.claim(pending_data_bl
);
2705 obj_len
= (uint64_t)first_chunk
.length();
2707 while (pending_data_bl
.length()) {
2708 void *handle
= nullptr;
2710 uint64_t max_write_size
= MIN(max_chunk_size
, (uint64_t)next_part_ofs
- data_ofs
);
2711 if (max_write_size
> pending_data_bl
.length()) {
2712 max_write_size
= pending_data_bl
.length();
2715 pending_data_bl
.splice(0, max_write_size
, &bl
);
2716 uint64_t write_len
= bl
.length();
2717 int r
= write_data(bl
, data_ofs
, &handle
, &obj
, false);
2719 ldout(store
->ctx(), 0) << "ERROR: write_data() returned " << r
<< dendl
;
2722 data_ofs
+= write_len
;
2723 r
= throttle_data(handle
, obj
, write_len
, false);
2725 ldout(store
->ctx(), 0) << "ERROR: throttle_data() returned " << r
<< dendl
;
2729 if (data_ofs
>= next_part_ofs
) {
2730 r
= prepare_next_part(data_ofs
);
2732 ldout(store
->ctx(), 0) << "ERROR: prepare_next_part() returned " << r
<< dendl
;
2737 int r
= complete_parts();
2742 r
= drain_pending();
2749 int RGWPutObjProcessor_Atomic::do_complete(size_t accounted_size
, const string
& etag
,
2750 real_time
*mtime
, real_time set_mtime
,
2751 map
<string
, bufferlist
>& attrs
,
2752 real_time delete_at
,
2753 const char *if_match
,
2754 const char *if_nomatch
, const string
*user_data
,
2755 rgw_zone_set
*zones_trace
) {
2756 int r
= complete_writing_data();
2760 obj_ctx
.obj
.set_atomic(head_obj
);
2762 RGWRados::Object
op_target(store
, bucket_info
, obj_ctx
, head_obj
);
2764 /* some object types shouldn't be versioned, e.g., multipart parts */
2765 op_target
.set_versioning_disabled(!versioned_object
);
2767 RGWRados::Object::Write
obj_op(&op_target
);
2769 obj_op
.meta
.data
= &first_chunk
;
2770 obj_op
.meta
.manifest
= &manifest
;
2771 obj_op
.meta
.ptag
= &unique_tag
; /* use req_id as operation tag */
2772 obj_op
.meta
.if_match
= if_match
;
2773 obj_op
.meta
.if_nomatch
= if_nomatch
;
2774 obj_op
.meta
.mtime
= mtime
;
2775 obj_op
.meta
.set_mtime
= set_mtime
;
2776 obj_op
.meta
.owner
= bucket_info
.owner
;
2777 obj_op
.meta
.flags
= PUT_OBJ_CREATE
;
2778 obj_op
.meta
.olh_epoch
= olh_epoch
;
2779 obj_op
.meta
.delete_at
= delete_at
;
2780 obj_op
.meta
.user_data
= user_data
;
2781 obj_op
.meta
.zones_trace
= zones_trace
;
2782 obj_op
.meta
.modify_tail
= true;
2784 r
= obj_op
.write_meta(obj_len
, accounted_size
, attrs
);
2789 canceled
= obj_op
.meta
.canceled
;
2794 const char* RGWRados::admin_commands
[4][3] = {
2796 "cache list name=filter,type=CephString,req=false",
2797 "cache list [filter_str]: list object cache, possibly matching substrings" },
2799 "cache inspect name=target,type=CephString,req=true",
2800 "cache inspect target: print cache element" },
2802 "cache erase name=target,type=CephString,req=true",
2803 "cache erase target: erase element from cache" },
2806 "cache zap: erase all elements from cache" }
2810 int RGWRados::watch(const string
& oid
, uint64_t *watch_handle
, librados::WatchCtx2
*ctx
) {
2811 int r
= control_pool_ctx
.watch2(oid
, watch_handle
, ctx
);
2817 int RGWRados::unwatch(uint64_t watch_handle
)
2819 int r
= control_pool_ctx
.unwatch2(watch_handle
);
2821 ldout(cct
, 0) << "ERROR: rados->unwatch2() returned r=" << r
<< dendl
;
2824 r
= rados
[0].watch_flush();
2826 ldout(cct
, 0) << "ERROR: rados->watch_flush() returned r=" << r
<< dendl
;
2832 void RGWRados::add_watcher(int i
)
2834 ldout(cct
, 20) << "add_watcher() i=" << i
<< dendl
;
2835 Mutex::Locker
l(watchers_lock
);
2836 watchers_set
.insert(i
);
2837 if (watchers_set
.size() == (size_t)num_watchers
) {
2838 ldout(cct
, 2) << "all " << num_watchers
<< " watchers are set, enabling cache" << dendl
;
2839 set_cache_enabled(true);
2843 void RGWRados::remove_watcher(int i
)
2845 ldout(cct
, 20) << "remove_watcher() i=" << i
<< dendl
;
2846 Mutex::Locker
l(watchers_lock
);
2847 size_t orig_size
= watchers_set
.size();
2848 watchers_set
.erase(i
);
2849 if (orig_size
== (size_t)num_watchers
&&
2850 watchers_set
.size() < orig_size
) { /* actually removed */
2851 ldout(cct
, 2) << "removed watcher, disabling cache" << dendl
;
2852 set_cache_enabled(false);
2856 class RGWWatcher
: public librados::WatchCtx2
{
2860 uint64_t watch_handle
;
2862 class C_ReinitWatch
: public Context
{
2863 RGWWatcher
*watcher
;
2865 explicit C_ReinitWatch(RGWWatcher
*_watcher
) : watcher(_watcher
) {}
2866 void finish(int r
) override
{
2871 RGWWatcher(RGWRados
*r
, int i
, const string
& o
) : rados(r
), index(i
), oid(o
), watch_handle(0) {}
2872 void handle_notify(uint64_t notify_id
,
2874 uint64_t notifier_id
,
2875 bufferlist
& bl
) override
{
2876 ldout(rados
->ctx(), 10) << "RGWWatcher::handle_notify() "
2877 << " notify_id " << notify_id
2878 << " cookie " << cookie
2879 << " notifier " << notifier_id
2880 << " bl.length()=" << bl
.length() << dendl
;
2881 rados
->watch_cb(notify_id
, cookie
, notifier_id
, bl
);
2883 bufferlist reply_bl
; // empty reply payload
2884 rados
->control_pool_ctx
.notify_ack(oid
, notify_id
, cookie
, reply_bl
);
2886 void handle_error(uint64_t cookie
, int err
) override
{
2887 lderr(rados
->ctx()) << "RGWWatcher::handle_error cookie " << cookie
2888 << " err " << cpp_strerror(err
) << dendl
;
2889 rados
->remove_watcher(index
);
2890 rados
->schedule_context(new C_ReinitWatch(this));
2894 int ret
= unregister_watch();
2896 ldout(rados
->ctx(), 0) << "ERROR: unregister_watch() returned ret=" << ret
<< dendl
;
2899 ret
= register_watch();
2901 ldout(rados
->ctx(), 0) << "ERROR: register_watch() returned ret=" << ret
<< dendl
;
2906 int unregister_watch() {
2907 int r
= rados
->unwatch(watch_handle
);
2911 rados
->remove_watcher(index
);
2915 int register_watch() {
2916 int r
= rados
->watch(oid
, &watch_handle
, this);
2920 rados
->add_watcher(index
);
2925 class RGWMetaNotifierManager
: public RGWCoroutinesManager
{
2927 RGWHTTPManager http_manager
;
2930 RGWMetaNotifierManager(RGWRados
*_store
) : RGWCoroutinesManager(_store
->ctx(), _store
->get_cr_registry()), store(_store
),
2931 http_manager(store
->ctx(), completion_mgr
) {
2932 http_manager
.set_threaded();
2935 int notify_all(map
<string
, RGWRESTConn
*>& conn_map
, set
<int>& shards
) {
2936 rgw_http_param_pair pairs
[] = { { "type", "metadata" },
2940 list
<RGWCoroutinesStack
*> stacks
;
2941 for (map
<string
, RGWRESTConn
*>::iterator iter
= conn_map
.begin(); iter
!= conn_map
.end(); ++iter
) {
2942 RGWRESTConn
*conn
= iter
->second
;
2943 RGWCoroutinesStack
*stack
= new RGWCoroutinesStack(store
->ctx(), this);
2944 stack
->call(new RGWPostRESTResourceCR
<set
<int>, int>(store
->ctx(), conn
, &http_manager
, "/admin/log", pairs
, shards
, NULL
));
2946 stacks
.push_back(stack
);
2952 class RGWDataNotifierManager
: public RGWCoroutinesManager
{
2954 RGWHTTPManager http_manager
;
2957 RGWDataNotifierManager(RGWRados
*_store
) : RGWCoroutinesManager(_store
->ctx(), _store
->get_cr_registry()), store(_store
),
2958 http_manager(store
->ctx(), completion_mgr
) {
2959 http_manager
.set_threaded();
2962 int notify_all(map
<string
, RGWRESTConn
*>& conn_map
, map
<int, set
<string
> >& shards
) {
2963 rgw_http_param_pair pairs
[] = { { "type", "data" },
2965 { "source-zone", store
->get_zone_params().get_id().c_str() },
2968 list
<RGWCoroutinesStack
*> stacks
;
2969 for (map
<string
, RGWRESTConn
*>::iterator iter
= conn_map
.begin(); iter
!= conn_map
.end(); ++iter
) {
2970 RGWRESTConn
*conn
= iter
->second
;
2971 RGWCoroutinesStack
*stack
= new RGWCoroutinesStack(store
->ctx(), this);
2972 stack
->call(new RGWPostRESTResourceCR
<map
<int, set
<string
> >, int>(store
->ctx(), conn
, &http_manager
, "/admin/log", pairs
, shards
, NULL
));
2974 stacks
.push_back(stack
);
2980 class RGWRadosThread
{
2981 class Worker
: public Thread
{
2983 RGWRadosThread
*processor
;
2988 Mutex::Locker
l(lock
);
2992 void wait_interval(const utime_t
& wait_time
) {
2993 Mutex::Locker
l(lock
);
2994 cond
.WaitInterval(lock
, wait_time
);
2998 Worker(CephContext
*_cct
, RGWRadosThread
*_p
) : cct(_cct
), processor(_p
), lock("RGWRadosThread::Worker") {}
2999 void *entry() override
;
3001 Mutex::Locker
l(lock
);
3012 std::atomic
<bool> down_flag
= { false };
3016 virtual uint64_t interval_msec() = 0;
3017 virtual void stop_process() {}
3019 RGWRadosThread(RGWRados
*_store
, const string
& thread_name
= "radosgw")
3020 : worker(NULL
), cct(_store
->ctx()), store(_store
), thread_name(thread_name
) {}
3021 virtual ~RGWRadosThread() {
3025 virtual int init() { return 0; }
3026 virtual int process() = 0;
3028 bool going_down() { return down_flag
; }
3040 void RGWRadosThread::start()
3042 worker
= new Worker(cct
, this);
3043 worker
->create(thread_name
.c_str());
3046 void RGWRadosThread::stop()
3058 void *RGWRadosThread::Worker::entry() {
3059 uint64_t msec
= processor
->interval_msec();
3060 utime_t interval
= utime_t(msec
/ 1000, (msec
% 1000) * 1000000);
3063 utime_t start
= ceph_clock_now();
3064 int r
= processor
->process();
3066 dout(0) << "ERROR: processor->process() returned error r=" << r
<< dendl
;
3069 if (processor
->going_down())
3072 utime_t end
= ceph_clock_now();
3075 uint64_t cur_msec
= processor
->interval_msec();
3076 if (cur_msec
!= msec
) { /* was it reconfigured? */
3078 interval
= utime_t(msec
/ 1000, (msec
% 1000) * 1000000);
3082 if (interval
<= end
)
3083 continue; // next round
3085 utime_t wait_time
= interval
;
3088 wait_interval(wait_time
);
3092 } while (!processor
->going_down());
3097 class RGWMetaNotifier
: public RGWRadosThread
{
3098 RGWMetaNotifierManager notify_mgr
;
3099 RGWMetadataLog
*const log
;
3101 uint64_t interval_msec() override
{
3102 return cct
->_conf
->rgw_md_notify_interval_msec
;
3105 RGWMetaNotifier(RGWRados
*_store
, RGWMetadataLog
* log
)
3106 : RGWRadosThread(_store
, "meta-notifier"), notify_mgr(_store
), log(log
) {}
3108 int process() override
;
3111 int RGWMetaNotifier::process()
3115 log
->read_clear_modified(shards
);
3117 if (shards
.empty()) {
3121 for (set
<int>::iterator iter
= shards
.begin(); iter
!= shards
.end(); ++iter
) {
3122 ldout(cct
, 20) << __func__
<< "(): notifying mdlog change, shard_id=" << *iter
<< dendl
;
3125 notify_mgr
.notify_all(store
->zone_conn_map
, shards
);
3130 class RGWDataNotifier
: public RGWRadosThread
{
3131 RGWDataNotifierManager notify_mgr
;
3133 uint64_t interval_msec() override
{
3134 return cct
->_conf
->get_val
<int64_t>("rgw_data_notify_interval_msec");
3137 RGWDataNotifier(RGWRados
*_store
) : RGWRadosThread(_store
, "data-notifier"), notify_mgr(_store
) {}
3139 int process() override
;
3142 int RGWDataNotifier::process()
3144 if (!store
->data_log
) {
3148 map
<int, set
<string
> > shards
;
3150 store
->data_log
->read_clear_modified(shards
);
3152 if (shards
.empty()) {
3156 for (map
<int, set
<string
> >::iterator iter
= shards
.begin(); iter
!= shards
.end(); ++iter
) {
3157 ldout(cct
, 20) << __func__
<< "(): notifying datalog change, shard_id=" << iter
->first
<< ": " << iter
->second
<< dendl
;
3160 notify_mgr
.notify_all(store
->zone_data_notify_to_map
, shards
);
3165 class RGWSyncProcessorThread
: public RGWRadosThread
{
3167 RGWSyncProcessorThread(RGWRados
*_store
, const string
& thread_name
= "radosgw") : RGWRadosThread(_store
, thread_name
) {}
3168 RGWSyncProcessorThread(RGWRados
*_store
) : RGWRadosThread(_store
) {}
3169 ~RGWSyncProcessorThread() override
{}
3170 int init() override
= 0 ;
3171 int process() override
= 0;
3174 class RGWMetaSyncProcessorThread
: public RGWSyncProcessorThread
3176 RGWMetaSyncStatusManager sync
;
3178 uint64_t interval_msec() override
{
3179 return 0; /* no interval associated, it'll run once until stopped */
3181 void stop_process() override
{
3185 RGWMetaSyncProcessorThread(RGWRados
*_store
, RGWAsyncRadosProcessor
*async_rados
)
3186 : RGWSyncProcessorThread(_store
, "meta-sync"), sync(_store
, async_rados
) {}
3188 void wakeup_sync_shards(set
<int>& shard_ids
) {
3189 for (set
<int>::iterator iter
= shard_ids
.begin(); iter
!= shard_ids
.end(); ++iter
) {
3193 RGWMetaSyncStatusManager
* get_manager() { return &sync
; }
3195 int init() override
{
3196 int ret
= sync
.init();
3198 ldout(store
->ctx(), 0) << "ERROR: sync.init() returned " << ret
<< dendl
;
3204 int process() override
{
3210 class RGWDataSyncProcessorThread
: public RGWSyncProcessorThread
3212 RGWDataSyncStatusManager sync
;
3215 uint64_t interval_msec() override
{
3217 return 0; /* no interval associated, it'll run once until stopped */
3219 #define DATA_SYNC_INIT_WAIT_SEC 20
3220 return DATA_SYNC_INIT_WAIT_SEC
* 1000;
3223 void stop_process() override
{
3227 RGWDataSyncProcessorThread(RGWRados
*_store
, RGWAsyncRadosProcessor
*async_rados
,
3228 const string
& _source_zone
,
3229 rgw::BucketChangeObserver
*observer
)
3230 : RGWSyncProcessorThread(_store
, "data-sync"),
3231 sync(_store
, async_rados
, _source_zone
, observer
),
3232 initialized(false) {}
3234 void wakeup_sync_shards(map
<int, set
<string
> >& shard_ids
) {
3235 for (map
<int, set
<string
> >::iterator iter
= shard_ids
.begin(); iter
!= shard_ids
.end(); ++iter
) {
3236 sync
.wakeup(iter
->first
, iter
->second
);
3239 RGWDataSyncStatusManager
* get_manager() { return &sync
; }
3241 int init() override
{
3245 int process() override
{
3246 while (!initialized
) {
3250 int ret
= sync
.init();
3255 /* we'll be back! */
3263 class RGWSyncLogTrimThread
: public RGWSyncProcessorThread
3265 RGWCoroutinesManager crs
;
3267 rgw::BucketTrimManager
*bucket_trim
;
3268 RGWHTTPManager http
;
3269 const utime_t trim_interval
;
3271 uint64_t interval_msec() override
{ return 0; }
3272 void stop_process() override
{ crs
.stop(); }
3274 RGWSyncLogTrimThread(RGWRados
*store
, rgw::BucketTrimManager
*bucket_trim
,
3276 : RGWSyncProcessorThread(store
, "sync-log-trim"),
3277 crs(store
->ctx(), store
->get_cr_registry()), store(store
),
3278 bucket_trim(bucket_trim
),
3279 http(store
->ctx(), crs
.get_completion_mgr()),
3280 trim_interval(interval
, 0)
3283 int init() override
{
3284 return http
.set_threaded();
3286 int process() override
{
3287 list
<RGWCoroutinesStack
*> stacks
;
3288 auto meta
= new RGWCoroutinesStack(store
->ctx(), &crs
);
3289 meta
->call(create_meta_log_trim_cr(store
, &http
,
3290 cct
->_conf
->rgw_md_log_max_shards
,
3292 stacks
.push_back(meta
);
3294 auto data
= new RGWCoroutinesStack(store
->ctx(), &crs
);
3295 data
->call(create_data_log_trim_cr(store
, &http
,
3296 cct
->_conf
->rgw_data_log_num_shards
,
3298 stacks
.push_back(data
);
3300 auto bucket
= new RGWCoroutinesStack(store
->ctx(), &crs
);
3301 bucket
->call(bucket_trim
->create_bucket_trim_cr(&http
));
3302 stacks
.push_back(bucket
);
3309 void RGWRados::wakeup_meta_sync_shards(set
<int>& shard_ids
)
3311 Mutex::Locker
l(meta_sync_thread_lock
);
3312 if (meta_sync_processor_thread
) {
3313 meta_sync_processor_thread
->wakeup_sync_shards(shard_ids
);
3317 void RGWRados::wakeup_data_sync_shards(const string
& source_zone
, map
<int, set
<string
> >& shard_ids
)
3319 ldout(ctx(), 20) << __func__
<< ": source_zone=" << source_zone
<< ", shard_ids=" << shard_ids
<< dendl
;
3320 Mutex::Locker
l(data_sync_thread_lock
);
3321 map
<string
, RGWDataSyncProcessorThread
*>::iterator iter
= data_sync_processor_threads
.find(source_zone
);
3322 if (iter
== data_sync_processor_threads
.end()) {
3323 ldout(ctx(), 10) << __func__
<< ": couldn't find sync thread for zone " << source_zone
<< ", skipping async data sync processing" << dendl
;
3327 RGWDataSyncProcessorThread
*thread
= iter
->second
;
3329 thread
->wakeup_sync_shards(shard_ids
);
3332 RGWMetaSyncStatusManager
* RGWRados::get_meta_sync_manager()
3334 Mutex::Locker
l(meta_sync_thread_lock
);
3335 if (meta_sync_processor_thread
) {
3336 return meta_sync_processor_thread
->get_manager();
3341 RGWDataSyncStatusManager
* RGWRados::get_data_sync_manager(const std::string
& source_zone
)
3343 Mutex::Locker
l(data_sync_thread_lock
);
3344 auto thread
= data_sync_processor_threads
.find(source_zone
);
3345 if (thread
== data_sync_processor_threads
.end()) {
3348 return thread
->second
->get_manager();
3351 int RGWRados::get_required_alignment(const rgw_pool
& pool
, uint64_t *alignment
)
3354 int r
= open_pool_ctx(pool
, ioctx
);
3356 ldout(cct
, 0) << "ERROR: open_pool_ctx() returned " << r
<< dendl
;
3361 r
= ioctx
.pool_requires_alignment2(&requires
);
3363 ldout(cct
, 0) << "ERROR: ioctx.pool_requires_alignment2() returned "
3374 r
= ioctx
.pool_required_alignment2(&align
);
3376 ldout(cct
, 0) << "ERROR: ioctx.pool_required_alignment2() returned "
3381 ldout(cct
, 20) << "required alignment=" << align
<< dendl
;
3387 int RGWRados::get_max_chunk_size(const rgw_pool
& pool
, uint64_t *max_chunk_size
)
3389 uint64_t alignment
= 0;
3390 int r
= get_required_alignment(pool
, &alignment
);
3395 uint64_t config_chunk_size
= cct
->_conf
->rgw_max_chunk_size
;
3397 if (alignment
== 0) {
3398 *max_chunk_size
= config_chunk_size
;
3402 if (config_chunk_size
<= alignment
) {
3403 *max_chunk_size
= alignment
;
3407 *max_chunk_size
= config_chunk_size
- (config_chunk_size
% alignment
);
3409 ldout(cct
, 20) << "max_chunk_size=" << *max_chunk_size
<< dendl
;
3414 int RGWRados::get_max_chunk_size(const string
& placement_rule
, const rgw_obj
& obj
, uint64_t *max_chunk_size
)
3417 if (!get_obj_data_pool(placement_rule
, obj
, &pool
)) {
3418 ldout(cct
, 0) << "ERROR: failed to get data pool for object " << obj
<< dendl
;
3421 return get_max_chunk_size(pool
, max_chunk_size
);
3424 class RGWIndexCompletionManager
;
3426 struct complete_op_data
{
3427 Mutex lock
{"complete_op_data"};
3428 AioCompletion
*rados_completion
{nullptr};
3429 int manager_shard_id
{-1};
3430 RGWIndexCompletionManager
*manager
{nullptr};
3434 rgw_bucket_entry_ver ver
;
3435 cls_rgw_obj_key key
;
3436 rgw_bucket_dir_entry_meta dir_meta
;
3437 list
<cls_rgw_obj_key
> remove_objs
;
3440 rgw_zone_set zones_trace
;
3442 bool stopped
{false};
3445 Mutex::Locker
l(lock
);
3450 class RGWIndexCompletionThread
: public RGWRadosThread
{
3453 uint64_t interval_msec() override
{
3457 list
<complete_op_data
*> completions
;
3459 Mutex completions_lock
;
3461 RGWIndexCompletionThread(RGWRados
*_store
)
3462 : RGWRadosThread(_store
, "index-complete"), store(_store
), completions_lock("RGWIndexCompletionThread::completions_lock") {}
3464 int process() override
;
3466 void add_completion(complete_op_data
*completion
) {
3468 Mutex::Locker
l(completions_lock
);
3469 completions
.push_back(completion
);
3476 int RGWIndexCompletionThread::process()
3478 list
<complete_op_data
*> comps
;
3481 Mutex::Locker
l(completions_lock
);
3482 completions
.swap(comps
);
3485 for (auto c
: comps
) {
3486 std::unique_ptr
<complete_op_data
> up
{c
};
3491 ldout(store
->ctx(), 20) << __func__
<< "(): handling completion for key=" << c
->key
<< dendl
;
3493 RGWRados::BucketShard
bs(store
);
3495 int r
= bs
.init(c
->obj
.bucket
, c
->obj
);
3497 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): failed to initialize BucketShard, obj=" << c
->obj
<< " r=" << r
<< dendl
;
3498 /* not much to do */
3502 r
= store
->guard_reshard(&bs
, c
->obj
, [&](RGWRados::BucketShard
*bs
) -> int {
3503 librados::ObjectWriteOperation o
;
3504 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
3505 cls_rgw_bucket_complete_op(o
, c
->op
, c
->tag
, c
->ver
, c
->key
, c
->dir_meta
, &c
->remove_objs
,
3506 c
->log_op
, c
->bilog_op
, &c
->zones_trace
);
3508 return bs
->index_ctx
.operate(bs
->bucket_obj
, &o
);
3511 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): bucket index completion failed, obj=" << c
->obj
<< " r=" << r
<< dendl
;
3512 /* ignoring error, can't do anything about it */
3515 r
= store
->data_log
->add_entry(bs
.bucket
, bs
.shard_id
);
3517 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
3524 class RGWIndexCompletionManager
{
3525 RGWRados
*store
{nullptr};
3526 vector
<Mutex
*> locks
;
3527 vector
<set
<complete_op_data
*> > completions
;
3529 RGWIndexCompletionThread
*completion_thread
{nullptr};
3533 std::atomic
<int> cur_shard
{0};
3537 RGWIndexCompletionManager(RGWRados
*_store
) : store(_store
) {
3538 num_shards
= store
->ctx()->_conf
->rgw_thread_pool_size
;
3540 for (int i
= 0; i
< num_shards
; i
++) {
3542 snprintf(buf
, sizeof(buf
), "RGWIndexCompletionManager::lock::%d", i
);
3543 locks
.push_back(new Mutex(buf
));
3546 completions
.resize(num_shards
);
3548 ~RGWIndexCompletionManager() {
3551 for (auto l
: locks
) {
3557 int result
= cur_shard
% num_shards
;
3562 void create_completion(const rgw_obj
& obj
,
3563 RGWModifyOp op
, string
& tag
,
3564 rgw_bucket_entry_ver
& ver
,
3565 const cls_rgw_obj_key
& key
,
3566 rgw_bucket_dir_entry_meta
& dir_meta
,
3567 list
<cls_rgw_obj_key
> *remove_objs
, bool log_op
,
3569 rgw_zone_set
*zones_trace
,
3570 complete_op_data
**result
);
3571 bool handle_completion(completion_t cb
, complete_op_data
*arg
);
3574 completion_thread
= new RGWIndexCompletionThread(store
);
3575 int ret
= completion_thread
->init();
3579 completion_thread
->start();
3583 if (completion_thread
) {
3584 completion_thread
->stop();
3585 delete completion_thread
;
3588 for (int i
= 0; i
< num_shards
; ++i
) {
3589 Mutex::Locker
l(*locks
[i
]);
3590 for (auto c
: completions
[i
]) {
3591 Mutex::Locker
cl(c
->lock
);
3595 completions
.clear();
3599 static void obj_complete_cb(completion_t cb
, void *arg
)
3601 complete_op_data
*completion
= (complete_op_data
*)arg
;
3602 completion
->lock
.Lock();
3603 if (completion
->stopped
) {
3604 completion
->lock
.Unlock(); /* can drop lock, no one else is referencing us */
3608 bool need_delete
= completion
->manager
->handle_completion(cb
, completion
);
3609 completion
->lock
.Unlock();
3616 void RGWIndexCompletionManager::create_completion(const rgw_obj
& obj
,
3617 RGWModifyOp op
, string
& tag
,
3618 rgw_bucket_entry_ver
& ver
,
3619 const cls_rgw_obj_key
& key
,
3620 rgw_bucket_dir_entry_meta
& dir_meta
,
3621 list
<cls_rgw_obj_key
> *remove_objs
, bool log_op
,
3623 rgw_zone_set
*zones_trace
,
3624 complete_op_data
**result
)
3626 complete_op_data
*entry
= new complete_op_data
;
3628 int shard_id
= next_shard();
3630 entry
->manager_shard_id
= shard_id
;
3631 entry
->manager
= this;
3637 entry
->dir_meta
= dir_meta
;
3638 entry
->log_op
= log_op
;
3639 entry
->bilog_op
= bilog_op
;
3642 for (auto iter
= remove_objs
->begin(); iter
!= remove_objs
->end(); ++iter
) {
3643 entry
->remove_objs
.push_back(*iter
);
3648 entry
->zones_trace
= *zones_trace
;
3650 entry
->zones_trace
.insert(store
->get_zone().id
);
3655 entry
->rados_completion
= librados::Rados::aio_create_completion(entry
, NULL
, obj_complete_cb
);
3657 Mutex::Locker
l(*locks
[shard_id
]);
3658 completions
[shard_id
].insert(entry
);
3661 bool RGWIndexCompletionManager::handle_completion(completion_t cb
, complete_op_data
*arg
)
3663 int shard_id
= arg
->manager_shard_id
;
3665 Mutex::Locker
l(*locks
[shard_id
]);
3667 auto& comps
= completions
[shard_id
];
3669 auto iter
= comps
.find(arg
);
3670 if (iter
== comps
.end()) {
3677 int r
= rados_aio_get_return_value(cb
);
3678 if (r
!= -ERR_BUSY_RESHARDING
) {
3681 completion_thread
->add_completion(arg
);
3685 void RGWRados::finalize()
3687 auto admin_socket
= cct
->get_admin_socket();
3688 for (auto cmd
: admin_commands
) {
3689 int r
= admin_socket
->unregister_command(cmd
[0]);
3691 lderr(cct
) << "ERROR: fail to unregister admin socket command (r=" << r
3696 if (run_sync_thread
) {
3697 Mutex::Locker
l(meta_sync_thread_lock
);
3698 meta_sync_processor_thread
->stop();
3700 Mutex::Locker
dl(data_sync_thread_lock
);
3701 for (auto iter
: data_sync_processor_threads
) {
3702 RGWDataSyncProcessorThread
*thread
= iter
.second
;
3705 if (sync_log_trimmer
) {
3706 sync_log_trimmer
->stop();
3710 async_rados
->stop();
3712 if (run_sync_thread
) {
3713 delete meta_sync_processor_thread
;
3714 meta_sync_processor_thread
= NULL
;
3715 Mutex::Locker
dl(data_sync_thread_lock
);
3716 for (auto iter
: data_sync_processor_threads
) {
3717 RGWDataSyncProcessorThread
*thread
= iter
.second
;
3720 data_sync_processor_threads
.clear();
3721 delete sync_log_trimmer
;
3722 sync_log_trimmer
= nullptr;
3723 bucket_trim
= boost::none
;
3728 if (need_watch_notify()) {
3732 /* delete finisher only after cleaning up watches, as watch error path might call
3733 * into finisher. We stop finisher before finalizing watch to make sure we don't
3734 * actually handle any racing work
3738 if (meta_notifier
) {
3739 meta_notifier
->stop();
3740 delete meta_notifier
;
3742 if (data_notifier
) {
3743 data_notifier
->stop();
3744 delete data_notifier
;
3760 delete rest_master_conn
;
3762 map
<string
, RGWRESTConn
*>::iterator iter
;
3763 for (iter
= zone_conn_map
.begin(); iter
!= zone_conn_map
.end(); ++iter
) {
3764 RGWRESTConn
*conn
= iter
->second
;
3768 for (iter
= zonegroup_conn_map
.begin(); iter
!= zonegroup_conn_map
.end(); ++iter
) {
3769 RGWRESTConn
*conn
= iter
->second
;
3772 RGWQuotaHandler::free_handler(quota_handler
);
3778 delete obj_tombstone_cache
;
3779 delete sync_modules_manager
;
3781 if (reshard_wait
.get()) {
3782 reshard_wait
->stop();
3783 reshard_wait
.reset();
3786 if (run_reshard_thread
) {
3787 reshard
->stop_processor();
3790 delete index_completion_manager
;
3794 * Initialize the RADOS instance and prepare to do other ops
3795 * Returns 0 on success, -ERR# on failure.
3797 int RGWRados::init_rados()
3800 auto admin_socket
= cct
->get_admin_socket();
3801 for (auto cmd
: admin_commands
) {
3802 int r
= admin_socket
->register_command(cmd
[0], cmd
[1], this,
3805 lderr(cct
) << "ERROR: fail to register admin socket command (r=" << r
3811 auto handles
= std::vector
<librados::Rados
>{cct
->_conf
->rgw_num_rados_handles
};
3813 for (auto& r
: handles
) {
3814 ret
= r
.init_with_context(cct
);
3824 sync_modules_manager
= new RGWSyncModulesManager();
3826 rgw_register_sync_modules(sync_modules_manager
);
3828 auto crs
= std::unique_ptr
<RGWCoroutinesManagerRegistry
>{
3829 new RGWCoroutinesManagerRegistry(cct
)};
3830 ret
= crs
->hook_to_admin_command("cr dump");
3835 meta_mgr
= new RGWMetadataManager(cct
, this);
3836 data_log
= new RGWDataChangesLog(cct
, this);
3837 cr_registry
= crs
.release();
3839 std::swap(handles
, rados
);
3844 int RGWRados::register_to_service_map(const string
& daemon_type
, const map
<string
, string
>& meta
)
3846 map
<string
,string
> metadata
= meta
;
3847 metadata
["num_handles"] = stringify(rados
.size());
3848 metadata
["zonegroup_id"] = zonegroup
.get_id();
3849 metadata
["zonegroup_name"] = zonegroup
.get_name();
3850 metadata
["zone_name"] = zone_name();
3851 metadata
["zone_id"] = zone_id();;
3852 string name
= cct
->_conf
->name
.get_id();
3853 if (name
.find("rgw.") == 0) {
3854 name
= name
.substr(4);
3856 int ret
= rados
[0].service_daemon_register(daemon_type
, name
, metadata
);
3858 ldout(cct
, 0) << "ERROR: service_daemon_register() returned ret=" << ret
<< ": " << cpp_strerror(-ret
) << dendl
;
3866 * Add new connection to connections map
3867 * @param zonegroup_conn_map map which new connection will be added to
3868 * @param zonegroup zonegroup which new connection will connect to
3869 * @param new_connection pointer to new connection instance
3871 static void add_new_connection_to_map(map
<string
, RGWRESTConn
*> &zonegroup_conn_map
,
3872 const RGWZoneGroup
&zonegroup
, RGWRESTConn
*new_connection
)
3874 // Delete if connection is already exists
3875 map
<string
, RGWRESTConn
*>::iterator iterZoneGroup
= zonegroup_conn_map
.find(zonegroup
.get_id());
3876 if (iterZoneGroup
!= zonegroup_conn_map
.end()) {
3877 delete iterZoneGroup
->second
;
3880 // Add new connection to connections map
3881 zonegroup_conn_map
[zonegroup
.get_id()] = new_connection
;
3884 int RGWRados::convert_regionmap()
3886 RGWZoneGroupMap zonegroupmap
;
3888 string pool_name
= cct
->_conf
->rgw_zone_root_pool
;
3889 if (pool_name
.empty()) {
3890 pool_name
= RGW_DEFAULT_ZONE_ROOT_POOL
;
3892 string oid
= region_map_oid
;
3894 rgw_pool
pool(pool_name
);
3896 RGWObjectCtx
obj_ctx(this);
3897 int ret
= rgw_get_system_obj(this, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
3898 if (ret
< 0 && ret
!= -ENOENT
) {
3900 } else if (ret
== -ENOENT
) {
3905 bufferlist::iterator iter
= bl
.begin();
3906 ::decode(zonegroupmap
, iter
);
3907 } catch (buffer::error
& err
) {
3908 ldout(cct
, 0) << "error decoding regionmap from " << pool
<< ":" << oid
<< dendl
;
3912 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroupmap
.zonegroups
.begin();
3913 iter
!= zonegroupmap
.zonegroups
.end(); ++iter
) {
3914 RGWZoneGroup
& zonegroup
= iter
->second
;
3915 ret
= zonegroup
.init(cct
, this, false);
3916 ret
= zonegroup
.update();
3917 if (ret
< 0 && ret
!= -ENOENT
) {
3918 ldout(cct
, 0) << "Error could not update zonegroup " << zonegroup
.get_name() << ": " <<
3919 cpp_strerror(-ret
) << dendl
;
3921 } else if (ret
== -ENOENT
) {
3922 ret
= zonegroup
.create();
3924 ldout(cct
, 0) << "Error could not create " << zonegroup
.get_name() << ": " <<
3925 cpp_strerror(-ret
) << dendl
;
3931 current_period
.set_user_quota(zonegroupmap
.user_quota
);
3932 current_period
.set_bucket_quota(zonegroupmap
.bucket_quota
);
3934 // remove the region_map so we don't try to convert again
3935 rgw_raw_obj
obj(pool
, oid
);
3936 ret
= delete_system_obj(obj
);
3938 ldout(cct
, 0) << "Error could not remove " << obj
3939 << " after upgrading to zonegroup map: " << cpp_strerror(ret
) << dendl
;
3947 * Replace all region configuration with zonegroup for
3948 * backward compatability
3949 * Returns 0 on success, -ERR# on failure.
3951 int RGWRados::replace_region_with_zonegroup()
3953 /* copy default region */
3954 /* convert default region to default zonegroup */
3955 string default_oid
= cct
->_conf
->rgw_default_region_info_oid
;
3956 if (default_oid
.empty()) {
3957 default_oid
= default_region_info_oid
;
3961 RGWZoneGroup default_zonegroup
;
3962 rgw_pool pool
{default_zonegroup
.get_pool(cct
)};
3963 string oid
= "converted";
3965 RGWObjectCtx
obj_ctx(this);
3967 int ret
= rgw_get_system_obj(this, obj_ctx
, pool
,oid
, bl
, NULL
, NULL
);
3968 if (ret
< 0 && ret
!= -ENOENT
) {
3969 ldout(cct
, 0) << __func__
<< " failed to read converted: ret "<< ret
<< " " << cpp_strerror(-ret
)
3972 } else if (ret
!= -ENOENT
) {
3973 ldout(cct
, 20) << "System already converted " << dendl
;
3977 string default_region
;
3978 ret
= default_zonegroup
.init(cct
, this, false, true);
3980 ldout(cct
, 0) << __func__
<< " failed init default region: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3983 ret
= default_zonegroup
.read_default_id(default_region
, true);
3984 if (ret
< 0 && ret
!= -ENOENT
) {
3985 ldout(cct
, 0) << __func__
<< " failed reading old default region: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3989 /* convert regions to zonegroups */
3990 list
<string
> regions
;
3991 ret
= list_regions(regions
);
3992 if (ret
< 0 && ret
!= -ENOENT
) {
3993 ldout(cct
, 0) << __func__
<< " failed to list regions: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3995 } else if (ret
== -ENOENT
|| regions
.empty()) {
3996 RGWZoneParams
zoneparams(default_zone_name
);
3997 int ret
= zoneparams
.init(cct
, this);
3998 if (ret
< 0 && ret
!= -ENOENT
) {
3999 ldout(cct
, 0) << __func__
<< ": error initializing default zone params: " << cpp_strerror(-ret
) << dendl
;
4002 /* update master zone */
4003 RGWZoneGroup
default_zg(default_zonegroup_name
);
4004 ret
= default_zg
.init(cct
, this);
4005 if (ret
< 0 && ret
!= -ENOENT
) {
4006 ldout(cct
, 0) << __func__
<< ": error in initializing default zonegroup: " << cpp_strerror(-ret
) << dendl
;
4009 if (ret
!= -ENOENT
&& default_zg
.master_zone
.empty()) {
4010 default_zg
.master_zone
= zoneparams
.get_id();
4011 return default_zg
.update();
4016 string master_region
, master_zone
;
4017 for (list
<string
>::iterator iter
= regions
.begin(); iter
!= regions
.end(); ++iter
) {
4018 if (*iter
!= default_zonegroup_name
){
4019 RGWZoneGroup
region(*iter
);
4020 int ret
= region
.init(cct
, this, true, true);
4022 ldout(cct
, 0) << __func__
<< " failed init region "<< *iter
<< ": " << cpp_strerror(-ret
) << dendl
;
4025 if (region
.is_master_zonegroup()) {
4026 master_region
= region
.get_id();
4027 master_zone
= region
.master_zone
;
4032 /* create realm if there is none.
4033 The realm name will be the region and zone concatenated
4034 realm id will be mds of its name */
4035 if (realm
.get_id().empty() && !master_region
.empty() && !master_zone
.empty()) {
4036 string new_realm_name
= master_region
+ "." + master_zone
;
4037 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
4038 char md5_str
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
4040 hash
.Update((const byte
*)new_realm_name
.c_str(), new_realm_name
.length());
4042 buf_to_hex(md5
, CEPH_CRYPTO_MD5_DIGESTSIZE
, md5_str
);
4043 string
new_realm_id(md5_str
);
4044 RGWRealm
new_realm(new_realm_id
,new_realm_name
);
4045 ret
= new_realm
.init(cct
, this, false);
4047 ldout(cct
, 0) << __func__
<< " Error initing new realm: " << cpp_strerror(-ret
) << dendl
;
4050 ret
= new_realm
.create();
4051 if (ret
< 0 && ret
!= -EEXIST
) {
4052 ldout(cct
, 0) << __func__
<< " Error creating new realm: " << cpp_strerror(-ret
) << dendl
;
4055 ret
= new_realm
.set_as_default();
4057 ldout(cct
, 0) << __func__
<< " Error setting realm as default: " << cpp_strerror(-ret
) << dendl
;
4060 ret
= realm
.init(cct
, this);
4062 ldout(cct
, 0) << __func__
<< " Error initing realm: " << cpp_strerror(-ret
) << dendl
;
4065 ret
= current_period
.init(cct
, this, realm
.get_id(), realm
.get_name());
4067 ldout(cct
, 0) << __func__
<< " Error initing current period: " << cpp_strerror(-ret
) << dendl
;
4072 list
<string
>::iterator iter
;
4073 /* create zonegroups */
4074 for (iter
= regions
.begin(); iter
!= regions
.end(); ++iter
)
4076 ldout(cct
, 0) << __func__
<< "Converting " << *iter
<< dendl
;
4077 /* check to see if we don't have already a zonegroup with this name */
4078 RGWZoneGroup
new_zonegroup(*iter
);
4079 ret
= new_zonegroup
.init(cct
, this);
4080 if (ret
== 0 && new_zonegroup
.get_id() != *iter
) {
4081 ldout(cct
, 0) << __func__
<< " zonegroup "<< *iter
<< " already exists id " << new_zonegroup
.get_id () <<
4082 " skipping conversion " << dendl
;
4085 RGWZoneGroup
zonegroup(*iter
);
4086 zonegroup
.set_id(*iter
);
4087 int ret
= zonegroup
.init(cct
, this, true, true);
4089 ldout(cct
, 0) << __func__
<< " failed init zonegroup: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4092 zonegroup
.realm_id
= realm
.get_id();
4093 /* fix default region master zone */
4094 if (*iter
== default_zonegroup_name
&& zonegroup
.master_zone
.empty()) {
4095 ldout(cct
, 0) << __func__
<< " Setting default zone as master for default region" << dendl
;
4096 zonegroup
.master_zone
= default_zone_name
;
4098 ret
= zonegroup
.update();
4099 if (ret
< 0 && ret
!= -EEXIST
) {
4100 ldout(cct
, 0) << __func__
<< " failed to update zonegroup " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4104 ret
= zonegroup
.update_name();
4105 if (ret
< 0 && ret
!= -EEXIST
) {
4106 ldout(cct
, 0) << __func__
<< " failed to update_name for zonegroup " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4110 if (zonegroup
.get_name() == default_region
) {
4111 ret
= zonegroup
.set_as_default();
4113 ldout(cct
, 0) << __func__
<< " failed to set_as_default " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4118 for (map
<string
, RGWZone
>::const_iterator iter
= zonegroup
.zones
.begin(); iter
!= zonegroup
.zones
.end();
4120 ldout(cct
, 0) << __func__
<< " Converting zone" << iter
->first
<< dendl
;
4121 RGWZoneParams
zoneparams(iter
->first
, iter
->first
);
4122 zoneparams
.set_id(iter
->first
);
4123 zoneparams
.realm_id
= realm
.get_id();
4124 ret
= zoneparams
.init(cct
, this);
4125 if (ret
< 0 && ret
!= -ENOENT
) {
4126 ldout(cct
, 0) << __func__
<< " failed to init zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4128 } else if (ret
== -ENOENT
) {
4129 ldout(cct
, 0) << __func__
<< " zone is part of another cluster " << iter
->first
<< " skipping " << dendl
;
4132 zonegroup
.realm_id
= realm
.get_id();
4133 ret
= zoneparams
.update();
4134 if (ret
< 0 && ret
!= -EEXIST
) {
4135 ldout(cct
, 0) << __func__
<< " failed to update zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4138 ret
= zoneparams
.update_name();
4139 if (ret
< 0 && ret
!= -EEXIST
) {
4140 ldout(cct
, 0) << __func__
<< " failed to init zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4145 if (!current_period
.get_id().empty()) {
4146 ret
= current_period
.add_zonegroup(zonegroup
);
4148 ldout(cct
, 0) << __func__
<< " failed to add zonegroup to current_period: " << cpp_strerror(-ret
) << dendl
;
4154 if (!current_period
.get_id().empty()) {
4155 ret
= current_period
.update();
4157 ldout(cct
, 0) << __func__
<< " failed to update new period: " << cpp_strerror(-ret
) << dendl
;
4160 ret
= current_period
.store_info(false);
4162 ldout(cct
, 0) << __func__
<< " failed to store new period: " << cpp_strerror(-ret
) << dendl
;
4165 ret
= current_period
.reflect();
4167 ldout(cct
, 0) << __func__
<< " failed to update local objects: " << cpp_strerror(-ret
) << dendl
;
4172 for (auto const& iter
: regions
) {
4173 RGWZoneGroup
zonegroup(iter
);
4174 int ret
= zonegroup
.init(cct
, this, true, true);
4176 ldout(cct
, 0) << __func__
<< " failed init zonegroup" << iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4179 ret
= zonegroup
.delete_obj(true);
4180 if (ret
< 0 && ret
!= -ENOENT
) {
4181 ldout(cct
, 0) << __func__
<< " failed to delete region " << iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4187 /* mark as converted */
4188 ret
= rgw_put_system_obj(this, pool
, oid
, bl
.c_str(), bl
.length(),
4189 true, NULL
, real_time(), NULL
);
4191 ldout(cct
, 0) << __func__
<< " failed to mark cluster as converted: ret "<< ret
<< " " << cpp_strerror(-ret
)
4199 int RGWRados::init_zg_from_period(bool *initialized
)
4201 *initialized
= false;
4203 if (current_period
.get_id().empty()) {
4207 int ret
= zonegroup
.init(cct
, this);
4208 ldout(cct
, 20) << "period zonegroup init ret " << ret
<< dendl
;
4209 if (ret
== -ENOENT
) {
4213 ldout(cct
, 0) << "failed reading zonegroup info: " << cpp_strerror(-ret
) << dendl
;
4216 ldout(cct
, 20) << "period zonegroup name " << zonegroup
.get_name() << dendl
;
4218 map
<string
, RGWZoneGroup
>::const_iterator iter
=
4219 current_period
.get_map().zonegroups
.find(zonegroup
.get_id());
4221 if (iter
!= current_period
.get_map().zonegroups
.end()) {
4222 ldout(cct
, 20) << "using current period zonegroup " << zonegroup
.get_name() << dendl
;
4223 zonegroup
= iter
->second
;
4224 ret
= zonegroup
.init(cct
, this, false);
4226 ldout(cct
, 0) << "failed init zonegroup: " << " " << cpp_strerror(-ret
) << dendl
;
4229 ret
= zone_params
.init(cct
, this);
4230 if (ret
< 0 && ret
!= -ENOENT
) {
4231 ldout(cct
, 0) << "failed reading zone params info: " << " " << cpp_strerror(-ret
) << dendl
;
4233 } if (ret
==-ENOENT
&& zonegroup
.get_name() == default_zonegroup_name
) {
4234 ldout(cct
, 10) << " Using default name "<< default_zone_name
<< dendl
;
4235 zone_params
.set_name(default_zone_name
);
4236 ret
= zone_params
.init(cct
, this);
4237 if (ret
< 0 && ret
!= -ENOENT
) {
4238 ldout(cct
, 0) << "failed reading zone params info: " << " " << cpp_strerror(-ret
) << dendl
;
4243 for (iter
= current_period
.get_map().zonegroups
.begin();
4244 iter
!= current_period
.get_map().zonegroups
.end(); ++iter
){
4245 const RGWZoneGroup
& zg
= iter
->second
;
4246 // use endpoints from the zonegroup's master zone
4247 auto master
= zg
.zones
.find(zg
.master_zone
);
4248 if (master
== zg
.zones
.end()) {
4249 // fix missing master zone for a single zone zonegroup
4250 if (zg
.master_zone
.empty() && zg
.zones
.size() == 1) {
4251 master
= zg
.zones
.begin();
4252 ldout(cct
, 0) << "zonegroup " << zg
.get_name() << " missing master_zone, setting zone " <<
4253 master
->second
.name
<< " id:" << master
->second
.id
<< " as master" << dendl
;
4254 if (zonegroup
.get_id() == zg
.get_id()) {
4255 zonegroup
.master_zone
= master
->second
.id
;
4256 ret
= zonegroup
.update();
4258 ldout(cct
, 0) << "error updating zonegroup : " << cpp_strerror(-ret
) << dendl
;
4262 RGWZoneGroup
fixed_zg(zg
.get_id(),zg
.get_name());
4263 ret
= fixed_zg
.init(cct
, this);
4265 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4268 fixed_zg
.master_zone
= master
->second
.id
;
4269 ret
= fixed_zg
.update();
4271 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4276 ldout(cct
, 0) << "zonegroup " << zg
.get_name() << " missing zone for master_zone=" <<
4277 zg
.master_zone
<< dendl
;
4281 const auto& endpoints
= master
->second
.endpoints
;
4282 add_new_connection_to_map(zonegroup_conn_map
, zg
, new RGWRESTConn(cct
, this, zg
.get_id(), endpoints
));
4283 if (!current_period
.get_master_zonegroup().empty() &&
4284 zg
.get_id() == current_period
.get_master_zonegroup()) {
4285 rest_master_conn
= new RGWRESTConn(cct
, this, zg
.get_id(), endpoints
);
4289 *initialized
= true;
4294 int RGWRados::init_zg_from_local(bool *creating_defaults
)
4296 int ret
= zonegroup
.init(cct
, this);
4297 if ( (ret
< 0 && ret
!= -ENOENT
) || (ret
== -ENOENT
&& !cct
->_conf
->rgw_zonegroup
.empty())) {
4298 ldout(cct
, 0) << "failed reading zonegroup info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4300 } else if (ret
== -ENOENT
) {
4301 *creating_defaults
= true;
4302 ldout(cct
, 10) << "Creating default zonegroup " << dendl
;
4303 ret
= zonegroup
.create_default();
4305 ldout(cct
, 0) << "failure in zonegroup create_default: ret "<< ret
<< " " << cpp_strerror(-ret
)
4309 ret
= zonegroup
.init(cct
, this);
4311 ldout(cct
, 0) << "failure in zonegroup create_default: ret "<< ret
<< " " << cpp_strerror(-ret
)
4316 ldout(cct
, 20) << "zonegroup " << zonegroup
.get_name() << dendl
;
4317 if (zonegroup
.is_master_zonegroup()) {
4318 // use endpoints from the zonegroup's master zone
4319 auto master
= zonegroup
.zones
.find(zonegroup
.master_zone
);
4320 if (master
== zonegroup
.zones
.end()) {
4321 // fix missing master zone for a single zone zonegroup
4322 if (zonegroup
.master_zone
.empty() && zonegroup
.zones
.size() == 1) {
4323 master
= zonegroup
.zones
.begin();
4324 ldout(cct
, 0) << "zonegroup " << zonegroup
.get_name() << " missing master_zone, setting zone " <<
4325 master
->second
.name
<< " id:" << master
->second
.id
<< " as master" << dendl
;
4326 zonegroup
.master_zone
= master
->second
.id
;
4327 ret
= zonegroup
.update();
4329 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4333 ldout(cct
, 0) << "zonegroup " << zonegroup
.get_name() << " missing zone for "
4334 "master_zone=" << zonegroup
.master_zone
<< dendl
;
4338 const auto& endpoints
= master
->second
.endpoints
;
4339 rest_master_conn
= new RGWRESTConn(cct
, this, zonegroup
.get_id(), endpoints
);
4346 bool RGWRados::zone_syncs_from(RGWZone
& target_zone
, RGWZone
& source_zone
)
4348 return target_zone
.syncs_from(source_zone
.name
) &&
4349 sync_modules_manager
->supports_data_export(source_zone
.tier_type
);
4353 * Initialize the RADOS instance and prepare to do other ops
4354 * Returns 0 on success, -ERR# on failure.
4356 int RGWRados::init_complete()
4358 int ret
= realm
.init(cct
, this);
4359 if (ret
< 0 && ret
!= -ENOENT
) {
4360 ldout(cct
, 0) << "failed reading realm info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4362 } else if (ret
!= -ENOENT
) {
4363 ldout(cct
, 20) << "realm " << realm
.get_name() << " " << realm
.get_id() << dendl
;
4364 ret
= current_period
.init(cct
, this, realm
.get_id(), realm
.get_name());
4365 if (ret
< 0 && ret
!= -ENOENT
) {
4366 ldout(cct
, 0) << "failed reading current period info: " << " " << cpp_strerror(-ret
) << dendl
;
4369 ldout(cct
, 20) << "current period " << current_period
.get_id() << dendl
;
4372 ret
= replace_region_with_zonegroup();
4374 lderr(cct
) << "failed converting region to zonegroup : ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4378 ret
= convert_regionmap();
4380 lderr(cct
) << "failed converting regionmap: " << cpp_strerror(-ret
) << dendl
;
4384 bool zg_initialized
= false;
4386 if (!current_period
.get_id().empty()) {
4387 ret
= init_zg_from_period(&zg_initialized
);
4393 bool creating_defaults
= false;
4394 bool using_local
= (!zg_initialized
);
4396 ldout(cct
, 10) << " cannot find current period zonegroup using local zonegroup" << dendl
;
4397 ret
= init_zg_from_local(&creating_defaults
);
4401 // read period_config into current_period
4402 auto& period_config
= current_period
.get_config();
4403 ret
= period_config
.read(this, zonegroup
.realm_id
);
4404 if (ret
< 0 && ret
!= -ENOENT
) {
4405 ldout(cct
, 0) << "ERROR: failed to read period config: "
4406 << cpp_strerror(ret
) << dendl
;
4411 ldout(cct
, 10) << "Cannot find current period zone using local zone" << dendl
;
4412 if (creating_defaults
&& cct
->_conf
->rgw_zone
.empty()) {
4413 ldout(cct
, 10) << " Using default name "<< default_zone_name
<< dendl
;
4414 zone_params
.set_name(default_zone_name
);
4417 ret
= zone_params
.init(cct
, this);
4418 if (ret
< 0 && ret
!= -ENOENT
) {
4419 lderr(cct
) << "failed reading zone info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4422 map
<string
, RGWZone
>::iterator zone_iter
= get_zonegroup().zones
.find(zone_params
.get_id());
4423 if (zone_iter
== get_zonegroup().zones
.end()) {
4425 lderr(cct
) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << ")" << dendl
;
4428 ldout(cct
, 1) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << "), switching to local zonegroup configuration" << dendl
;
4429 ret
= init_zg_from_local(&creating_defaults
);
4433 zone_iter
= get_zonegroup().zones
.find(zone_params
.get_id());
4435 if (zone_iter
!= get_zonegroup().zones
.end()) {
4436 zone_public_config
= zone_iter
->second
;
4437 ldout(cct
, 20) << "zone " << zone_params
.get_name() << dendl
;
4439 lderr(cct
) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << ")" << dendl
;
4443 zone_short_id
= current_period
.get_map().get_zone_short_id(zone_params
.get_id());
4445 if (run_sync_thread
) {
4446 ret
= sync_modules_manager
->create_instance(cct
, zone_public_config
.tier_type
, zone_params
.tier_config
, &sync_module
);
4448 lderr(cct
) << "ERROR: failed to init sync module instance, ret=" << ret
<< dendl
;
4453 writeable_zone
= (zone_public_config
.tier_type
.empty() || zone_public_config
.tier_type
== "rgw");
4455 init_unique_trans_id_deps();
4457 finisher
= new Finisher(cct
);
4460 period_puller
.reset(new RGWPeriodPuller(this));
4461 period_history
.reset(new RGWPeriodHistory(cct
, period_puller
.get(),
4464 if (need_watch_notify()) {
4467 lderr(cct
) << "ERROR: failed to initialize watch: " << cpp_strerror(-ret
) << dendl
;
4472 /* first build all zones index */
4473 for (auto ziter
: get_zonegroup().zones
) {
4474 const string
& id
= ziter
.first
;
4475 RGWZone
& z
= ziter
.second
;
4476 zone_id_by_name
[z
.name
] = id
;
4480 if (zone_by_id
.find(zone_id()) == zone_by_id
.end()) {
4481 ldout(cct
, 0) << "WARNING: could not find zone config in zonegroup for local zone (" << zone_id() << "), will use defaults" << dendl
;
4483 zone_public_config
= zone_by_id
[zone_id()];
4484 for (auto ziter
: get_zonegroup().zones
) {
4485 const string
& id
= ziter
.first
;
4486 RGWZone
& z
= ziter
.second
;
4487 if (id
== zone_id()) {
4490 if (z
.endpoints
.empty()) {
4491 ldout(cct
, 0) << "WARNING: can't generate connection for zone " << z
.id
<< " id " << z
.name
<< ": no endpoints defined" << dendl
;
4494 ldout(cct
, 20) << "generating connection object for zone " << z
.name
<< " id " << z
.id
<< dendl
;
4495 RGWRESTConn
*conn
= new RGWRESTConn(cct
, this, z
.id
, z
.endpoints
);
4496 zone_conn_map
[id
] = conn
;
4497 if (zone_syncs_from(zone_public_config
, z
) ||
4498 zone_syncs_from(z
, zone_public_config
)) {
4499 if (zone_syncs_from(zone_public_config
, z
)) {
4500 zone_data_sync_from_map
[id
] = conn
;
4502 if (zone_syncs_from(z
, zone_public_config
)) {
4503 zone_data_notify_to_map
[id
] = conn
;
4506 ldout(cct
, 20) << "NOTICE: not syncing to/from zone " << z
.name
<< " id " << z
.id
<< dendl
;
4510 ret
= open_root_pool_ctx();
4514 ret
= open_gc_pool_ctx();
4518 ret
= open_lc_pool_ctx();
4522 ret
= open_objexp_pool_ctx();
4526 ret
= open_reshard_pool_ctx();
4530 pools_initialized
= true;
4533 gc
->initialize(cct
, this);
4535 obj_expirer
= new RGWObjectExpirer(this);
4537 if (use_gc_thread
) {
4538 gc
->start_processor();
4539 obj_expirer
->start_processor();
4542 /* no point of running sync thread if we don't have a master zone configured
4543 or there is no rest_master_conn */
4544 if (get_zonegroup().master_zone
.empty() || !rest_master_conn
4545 || current_period
.get_id().empty()) {
4546 run_sync_thread
= false;
4549 if (run_sync_thread
) {
4550 // initialize the log period history
4551 meta_mgr
->init_oldest_log_period();
4554 async_rados
= new RGWAsyncRadosProcessor(this, cct
->_conf
->rgw_num_async_rados_threads
);
4555 async_rados
->start();
4557 ret
= meta_mgr
->init(current_period
.get_id());
4559 lderr(cct
) << "ERROR: failed to initialize metadata log: "
4560 << cpp_strerror(-ret
) << dendl
;
4564 if (is_meta_master()) {
4565 auto md_log
= meta_mgr
->get_log(current_period
.get_id());
4566 meta_notifier
= new RGWMetaNotifier(this, md_log
);
4567 meta_notifier
->start();
4570 if (run_sync_thread
) {
4571 Mutex::Locker
l(meta_sync_thread_lock
);
4572 meta_sync_processor_thread
= new RGWMetaSyncProcessorThread(this, async_rados
);
4573 ret
= meta_sync_processor_thread
->init();
4575 ldout(cct
, 0) << "ERROR: failed to initialize meta sync thread" << dendl
;
4578 meta_sync_processor_thread
->start();
4580 // configure the bucket trim manager
4581 rgw::BucketTrimConfig config
;
4582 rgw::configure_bucket_trim(cct
, config
);
4584 bucket_trim
.emplace(this, config
);
4585 ret
= bucket_trim
->init();
4587 ldout(cct
, 0) << "ERROR: failed to start bucket trim manager" << dendl
;
4591 Mutex::Locker
dl(data_sync_thread_lock
);
4592 for (auto iter
: zone_data_sync_from_map
) {
4593 ldout(cct
, 5) << "starting data sync thread for zone " << iter
.first
<< dendl
;
4594 auto *thread
= new RGWDataSyncProcessorThread(this, async_rados
, iter
.first
,
4596 ret
= thread
->init();
4598 ldout(cct
, 0) << "ERROR: failed to initialize data sync thread" << dendl
;
4602 data_sync_processor_threads
[iter
.first
] = thread
;
4604 auto interval
= cct
->_conf
->rgw_sync_log_trim_interval
;
4606 sync_log_trimmer
= new RGWSyncLogTrimThread(this, &*bucket_trim
, interval
);
4607 ret
= sync_log_trimmer
->init();
4609 ldout(cct
, 0) << "ERROR: failed to initialize sync log trim thread" << dendl
;
4612 sync_log_trimmer
->start();
4615 data_notifier
= new RGWDataNotifier(this);
4616 data_notifier
->start();
4619 lc
->initialize(cct
, this);
4622 lc
->start_processor();
4624 quota_handler
= RGWQuotaHandler::generate_handler(this, quota_threads
);
4626 bucket_index_max_shards
= (cct
->_conf
->rgw_override_bucket_index_max_shards
? cct
->_conf
->rgw_override_bucket_index_max_shards
:
4627 get_zone().bucket_index_max_shards
);
4628 if (bucket_index_max_shards
> get_max_bucket_shards()) {
4629 bucket_index_max_shards
= get_max_bucket_shards();
4630 ldout(cct
, 1) << __func__
<< " bucket index max shards is too large, reset to value: "
4631 << get_max_bucket_shards() << dendl
;
4633 ldout(cct
, 20) << __func__
<< " bucket index max shards: " << bucket_index_max_shards
<< dendl
;
4635 binfo_cache
= new RGWChainedCacheImpl
<bucket_info_entry
>;
4636 binfo_cache
->init(this);
4638 bool need_tombstone_cache
= !zone_data_notify_to_map
.empty(); /* have zones syncing from us */
4640 if (need_tombstone_cache
) {
4641 obj_tombstone_cache
= new tombstone_cache_t(cct
->_conf
->rgw_obj_tombstone_cache_size
);
4644 reshard_wait
= std::make_shared
<RGWReshardWait
>(this);
4646 reshard
= new RGWReshard(this);
4648 /* only the master zone in the zonegroup reshards buckets */
4649 run_reshard_thread
= run_reshard_thread
&& (get_zonegroup().master_zone
== zone_public_config
.id
);
4650 if (run_reshard_thread
) {
4651 reshard
->start_processor();
4654 index_completion_manager
= new RGWIndexCompletionManager(this);
4655 ret
= index_completion_manager
->start();
4661 * Initialize the RADOS instance and prepare to do other ops
4662 * Returns 0 on success, -ERR# on failure.
4664 int RGWRados::initialize()
4672 return init_complete();
4675 void RGWRados::finalize_watch()
4677 for (int i
= 0; i
< num_watchers
; i
++) {
4678 RGWWatcher
*watcher
= watchers
[i
];
4679 watcher
->unregister_watch();
4683 delete[] notify_oids
;
4687 void RGWRados::schedule_context(Context
*c
) {
4691 int RGWRados::list_raw_prefixed_objs(const rgw_pool
& pool
, const string
& prefix
, list
<string
>& result
)
4694 RGWListRawObjsCtx ctx
;
4697 int r
= list_raw_objects(pool
, prefix
, 1000,
4698 ctx
, oids
, &is_truncated
);
4702 list
<string
>::iterator iter
;
4703 for (iter
= oids
.begin(); iter
!= oids
.end(); ++iter
) {
4704 string
& val
= *iter
;
4705 if (val
.size() > prefix
.size())
4706 result
.push_back(val
.substr(prefix
.size()));
4708 } while (is_truncated
);
4713 int RGWRados::list_regions(list
<string
>& regions
)
4715 RGWZoneGroup zonegroup
;
4717 return list_raw_prefixed_objs(zonegroup
.get_pool(cct
), region_info_oid_prefix
, regions
);
4720 int RGWRados::list_zonegroups(list
<string
>& zonegroups
)
4722 RGWZoneGroup zonegroup
;
4724 return list_raw_prefixed_objs(zonegroup
.get_pool(cct
), zonegroup_names_oid_prefix
, zonegroups
);
4727 int RGWRados::list_zones(list
<string
>& zones
)
4729 RGWZoneParams zoneparams
;
4731 return list_raw_prefixed_objs(zoneparams
.get_pool(cct
), zone_names_oid_prefix
, zones
);
4734 int RGWRados::list_realms(list
<string
>& realms
)
4736 RGWRealm
realm(cct
, this);
4737 return list_raw_prefixed_objs(realm
.get_pool(cct
), realm_names_oid_prefix
, realms
);
4740 int RGWRados::list_periods(list
<string
>& periods
)
4743 list
<string
> raw_periods
;
4744 int ret
= list_raw_prefixed_objs(period
.get_pool(cct
), period
.get_info_oid_prefix(), raw_periods
);
4748 for (const auto& oid
: raw_periods
) {
4749 size_t pos
= oid
.find(".");
4750 if (pos
!= std::string::npos
) {
4751 periods
.push_back(oid
.substr(0, pos
));
4753 periods
.push_back(oid
);
4756 periods
.sort(); // unique() only detects duplicates if they're adjacent
4762 int RGWRados::list_periods(const string
& current_period
, list
<string
>& periods
)
4765 string period_id
= current_period
;
4766 while(!period_id
.empty()) {
4767 RGWPeriod
period(period_id
);
4768 ret
= period
.init(cct
, this);
4772 periods
.push_back(period
.get_id());
4773 period_id
= period
.get_predecessor();
4780 * Open the pool used as root for this gateway
4781 * Returns: 0 on success, -ERR# otherwise.
4783 int RGWRados::open_root_pool_ctx()
4785 return rgw_init_ioctx(get_rados_handle(), get_zone_params().domain_root
, root_pool_ctx
, true);
4788 int RGWRados::open_gc_pool_ctx()
4790 return rgw_init_ioctx(get_rados_handle(), get_zone_params().gc_pool
, gc_pool_ctx
, true);
4793 int RGWRados::open_lc_pool_ctx()
4795 return rgw_init_ioctx(get_rados_handle(), get_zone_params().lc_pool
, lc_pool_ctx
, true);
4798 int RGWRados::open_objexp_pool_ctx()
4800 return rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, objexp_pool_ctx
, true);
4803 int RGWRados::open_reshard_pool_ctx()
4805 return rgw_init_ioctx(get_rados_handle(), get_zone_params().reshard_pool
, reshard_pool_ctx
, true);
4808 int RGWRados::init_watch()
4810 int r
= rgw_init_ioctx(&rados
[0], get_zone_params().control_pool
, control_pool_ctx
, true);
4815 num_watchers
= cct
->_conf
->rgw_num_control_oids
;
4817 bool compat_oid
= (num_watchers
== 0);
4819 if (num_watchers
<= 0)
4822 notify_oids
= new string
[num_watchers
];
4823 watchers
= new RGWWatcher
*[num_watchers
];
4825 for (int i
=0; i
< num_watchers
; i
++) {
4826 string
& notify_oid
= notify_oids
[i
];
4827 notify_oid
= notify_oid_prefix
;
4830 snprintf(buf
, sizeof(buf
), ".%d", i
);
4831 notify_oid
.append(buf
);
4833 r
= control_pool_ctx
.create(notify_oid
, false);
4834 if (r
< 0 && r
!= -EEXIST
)
4837 RGWWatcher
*watcher
= new RGWWatcher(this, i
, notify_oid
);
4838 watchers
[i
] = watcher
;
4840 r
= watcher
->register_watch();
4845 watch_initialized
= true;
4847 set_cache_enabled(true);
4852 void RGWRados::pick_control_oid(const string
& key
, string
& notify_oid
)
4854 uint32_t r
= ceph_str_hash_linux(key
.c_str(), key
.size());
4856 int i
= r
% num_watchers
;
4858 snprintf(buf
, sizeof(buf
), ".%d", i
);
4860 notify_oid
= notify_oid_prefix
;
4861 notify_oid
.append(buf
);
4864 int RGWRados::open_pool_ctx(const rgw_pool
& pool
, librados::IoCtx
& io_ctx
)
4866 librados::Rados
*rad
= get_rados_handle();
4867 int r
= rgw_init_ioctx(rad
, pool
, io_ctx
);
4871 if (!pools_initialized
)
4874 r
= rad
->pool_create(pool
.name
.c_str());
4875 if (r
< 0 && r
!= -EEXIST
)
4878 r
= rgw_init_ioctx(rad
, pool
, io_ctx
);
4882 r
= io_ctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
4883 if (r
< 0 && r
!= -EOPNOTSUPP
)
4888 void RGWRados::build_bucket_index_marker(const string
& shard_id_str
, const string
& shard_marker
,
4891 *marker
= shard_id_str
;
4892 marker
->append(BucketIndexShardsManager::KEY_VALUE_SEPARATOR
);
4893 marker
->append(shard_marker
);
4897 int RGWRados::open_bucket_index_ctx(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
)
4899 const rgw_pool
& explicit_pool
= bucket_info
.bucket
.explicit_placement
.index_pool
;
4901 if (!explicit_pool
.empty()) {
4902 return open_pool_ctx(explicit_pool
, index_ctx
);
4905 const string
*rule
= &bucket_info
.placement_rule
;
4906 if (rule
->empty()) {
4907 rule
= &zonegroup
.default_placement
;
4909 auto iter
= zone_params
.placement_pools
.find(*rule
);
4910 if (iter
== zone_params
.placement_pools
.end()) {
4911 ldout(cct
, 0) << "could not find placement rule " << *rule
<< " within zonegroup " << dendl
;
4915 int r
= open_pool_ctx(iter
->second
.index_pool
, index_ctx
);
4923 * set up a bucket listing.
4924 * handle is filled in.
4925 * Returns 0 on success, -ERR# otherwise.
4927 int RGWRados::list_buckets_init(RGWAccessHandle
*handle
)
4929 librados::NObjectIterator
*state
= new librados::NObjectIterator(root_pool_ctx
.nobjects_begin());
4930 *handle
= (RGWAccessHandle
)state
;
4935 * get the next bucket in the listing.
4937 * handle is updated.
4938 * returns 0 on success, -ERR# otherwise.
4940 int RGWRados::list_buckets_next(rgw_bucket_dir_entry
& obj
, RGWAccessHandle
*handle
)
4942 librados::NObjectIterator
*state
= (librados::NObjectIterator
*)*handle
;
4945 if (*state
== root_pool_ctx
.nobjects_end()) {
4950 obj
.key
.name
= (*state
)->get_oid();
4951 if (obj
.key
.name
[0] == '_') {
4952 obj
.key
.name
= obj
.key
.name
.substr(1);
4956 } while (obj
.key
.name
[0] == '.'); /* skip all entries starting with '.' */
4964 struct log_list_state
{
4966 librados::IoCtx io_ctx
;
4967 librados::NObjectIterator obit
;
4970 int RGWRados::log_list_init(const string
& prefix
, RGWAccessHandle
*handle
)
4972 log_list_state
*state
= new log_list_state
;
4973 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, state
->io_ctx
);
4978 state
->prefix
= prefix
;
4979 state
->obit
= state
->io_ctx
.nobjects_begin();
4980 *handle
= (RGWAccessHandle
)state
;
4984 int RGWRados::log_list_next(RGWAccessHandle handle
, string
*name
)
4986 log_list_state
*state
= static_cast<log_list_state
*>(handle
);
4988 if (state
->obit
== state
->io_ctx
.nobjects_end()) {
4992 if (state
->prefix
.length() &&
4993 state
->obit
->get_oid().find(state
->prefix
) != 0) {
4997 *name
= state
->obit
->get_oid();
5004 int RGWRados::log_remove(const string
& name
)
5006 librados::IoCtx io_ctx
;
5007 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5010 return io_ctx
.remove(name
);
5013 struct log_show_state
{
5014 librados::IoCtx io_ctx
;
5016 bufferlist::iterator p
;
5020 log_show_state() : pos(0), eof(false) {}
5023 int RGWRados::log_show_init(const string
& name
, RGWAccessHandle
*handle
)
5025 log_show_state
*state
= new log_show_state
;
5026 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, state
->io_ctx
);
5032 *handle
= (RGWAccessHandle
)state
;
5036 int RGWRados::log_show_next(RGWAccessHandle handle
, rgw_log_entry
*entry
)
5038 log_show_state
*state
= static_cast<log_show_state
*>(handle
);
5039 off_t off
= state
->p
.get_off();
5041 ldout(cct
, 10) << "log_show_next pos " << state
->pos
<< " bl " << state
->bl
.length()
5043 << " eof " << (int)state
->eof
5046 unsigned chunk
= 1024*1024;
5047 if ((state
->bl
.length() - off
) < chunk
/2 && !state
->eof
) {
5049 int r
= state
->io_ctx
.read(state
->name
, more
, chunk
, state
->pos
);
5055 old
.substr_of(state
->bl
, off
, state
->bl
.length() - off
);
5056 } catch (buffer::error
& err
) {
5060 state
->bl
.claim(old
);
5061 state
->bl
.claim_append(more
);
5062 state
->p
= state
->bl
.begin();
5063 if ((unsigned)r
< chunk
)
5065 ldout(cct
, 10) << " read " << r
<< dendl
;
5069 return 0; // end of file
5071 ::decode(*entry
, state
->p
);
5073 catch (const buffer::error
&e
) {
5080 * usage_log_hash: get usage log key hash, based on name and index
5082 * Get the usage object name. Since a user may have more than 1
5083 * object holding that info (multiple shards), we use index to
5084 * specify that shard number. Once index exceeds max shards it
5086 * If name is not being set, results for all users will be returned
5087 * and index will wrap only after total shards number.
5089 * @param cct [in] ceph context
5090 * @param name [in] user name
5091 * @param hash [out] hash value
5092 * @param index [in] shard index number
5094 static void usage_log_hash(CephContext
*cct
, const string
& name
, string
& hash
, uint32_t index
)
5096 uint32_t val
= index
;
5098 if (!name
.empty()) {
5099 int max_user_shards
= cct
->_conf
->rgw_usage_max_user_shards
;
5100 val
%= max_user_shards
;
5101 val
+= ceph_str_hash_linux(name
.c_str(), name
.size());
5104 int max_shards
= cct
->_conf
->rgw_usage_max_shards
;
5105 snprintf(buf
, sizeof(buf
), RGW_USAGE_OBJ_PREFIX
"%u", (unsigned)(val
% max_shards
));
5109 int RGWRados::log_usage(map
<rgw_user_bucket
, RGWUsageBatch
>& usage_info
)
5113 map
<string
, rgw_usage_log_info
> log_objs
;
5118 /* restructure usage map, zone by object hash */
5119 map
<rgw_user_bucket
, RGWUsageBatch
>::iterator iter
;
5120 for (iter
= usage_info
.begin(); iter
!= usage_info
.end(); ++iter
) {
5121 const rgw_user_bucket
& ub
= iter
->first
;
5122 RGWUsageBatch
& info
= iter
->second
;
5124 if (ub
.user
.empty()) {
5125 ldout(cct
, 0) << "WARNING: RGWRados::log_usage(): user name empty (bucket=" << ub
.bucket
<< "), skipping" << dendl
;
5129 if (ub
.user
!= last_user
) {
5130 /* index *should* be random, but why waste extra cycles
5131 in most cases max user shards is not going to exceed 1,
5132 so just incrementing it */
5133 usage_log_hash(cct
, ub
.user
, hash
, index
++);
5135 last_user
= ub
.user
;
5136 vector
<rgw_usage_log_entry
>& v
= log_objs
[hash
].entries
;
5138 for (auto miter
= info
.m
.begin(); miter
!= info
.m
.end(); ++miter
) {
5139 v
.push_back(miter
->second
);
5143 map
<string
, rgw_usage_log_info
>::iterator liter
;
5145 for (liter
= log_objs
.begin(); liter
!= log_objs
.end(); ++liter
) {
5146 int r
= cls_obj_usage_log_add(liter
->first
, liter
->second
);
5153 int RGWRados::read_usage(const rgw_user
& user
, uint64_t start_epoch
, uint64_t end_epoch
, uint32_t max_entries
,
5154 bool *is_truncated
, RGWUsageIter
& usage_iter
, map
<rgw_user_bucket
, rgw_usage_log_entry
>& usage
)
5156 uint32_t num
= max_entries
;
5157 string hash
, first_hash
;
5158 string user_str
= user
.to_str();
5159 usage_log_hash(cct
, user_str
, first_hash
, 0);
5161 if (usage_iter
.index
) {
5162 usage_log_hash(cct
, user_str
, hash
, usage_iter
.index
);
5170 map
<rgw_user_bucket
, rgw_usage_log_entry
> ret_usage
;
5171 map
<rgw_user_bucket
, rgw_usage_log_entry
>::iterator iter
;
5173 int ret
= cls_obj_usage_log_read(hash
, user_str
, start_epoch
, end_epoch
, num
,
5174 usage_iter
.read_iter
, ret_usage
, is_truncated
);
5181 num
-= ret_usage
.size();
5183 for (iter
= ret_usage
.begin(); iter
!= ret_usage
.end(); ++iter
) {
5184 usage
[iter
->first
].aggregate(iter
->second
);
5188 if (!*is_truncated
) {
5189 usage_iter
.read_iter
.clear();
5190 usage_log_hash(cct
, user_str
, hash
, ++usage_iter
.index
);
5192 } while (num
&& !*is_truncated
&& hash
!= first_hash
);
5196 int RGWRados::trim_usage(rgw_user
& user
, uint64_t start_epoch
, uint64_t end_epoch
)
5199 string hash
, first_hash
;
5200 string user_str
= user
.to_str();
5201 usage_log_hash(cct
, user_str
, first_hash
, index
);
5205 int ret
= cls_obj_usage_log_trim(hash
, user_str
, start_epoch
, end_epoch
);
5207 if (ret
< 0 && ret
!= -ENOENT
)
5210 usage_log_hash(cct
, user_str
, hash
, ++index
);
5211 } while (hash
!= first_hash
);
5216 int RGWRados::key_to_shard_id(const string
& key
, int max_shards
)
5218 return rgw_shards_hash(key
, max_shards
);
5221 void RGWRados::shard_name(const string
& prefix
, unsigned max_shards
, const string
& key
, string
& name
, int *shard_id
)
5223 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
5226 *shard_id
= val
% max_shards
;
5228 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
5229 name
= prefix
+ buf
;
5232 void RGWRados::shard_name(const string
& prefix
, unsigned max_shards
, const string
& section
, const string
& key
, string
& name
)
5234 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
5235 val
^= ceph_str_hash_linux(section
.c_str(), section
.size());
5237 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
5238 name
= prefix
+ buf
;
5241 void RGWRados::shard_name(const string
& prefix
, unsigned shard_id
, string
& name
)
5244 snprintf(buf
, sizeof(buf
), "%u", shard_id
);
5245 name
= prefix
+ buf
;
5249 void RGWRados::time_log_prepare_entry(cls_log_entry
& entry
, const real_time
& ut
, const string
& section
, const string
& key
, bufferlist
& bl
)
5251 cls_log_add_prepare_entry(entry
, utime_t(ut
), section
, key
, bl
);
5254 int RGWRados::time_log_add_init(librados::IoCtx
& io_ctx
)
5256 return rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
, true);
5260 int RGWRados::time_log_add(const string
& oid
, const real_time
& ut
, const string
& section
, const string
& key
, bufferlist
& bl
)
5262 librados::IoCtx io_ctx
;
5264 int r
= time_log_add_init(io_ctx
);
5269 ObjectWriteOperation op
;
5271 cls_log_add(op
, t
, section
, key
, bl
);
5273 return io_ctx
.operate(oid
, &op
);
5276 int RGWRados::time_log_add(const string
& oid
, list
<cls_log_entry
>& entries
,
5277 librados::AioCompletion
*completion
, bool monotonic_inc
)
5279 librados::IoCtx io_ctx
;
5281 int r
= time_log_add_init(io_ctx
);
5286 ObjectWriteOperation op
;
5287 cls_log_add(op
, entries
, monotonic_inc
);
5290 r
= io_ctx
.operate(oid
, &op
);
5292 r
= io_ctx
.aio_operate(oid
, completion
, &op
);
5297 int RGWRados::time_log_list(const string
& oid
, const real_time
& start_time
, const real_time
& end_time
,
5298 int max_entries
, list
<cls_log_entry
>& entries
,
5299 const string
& marker
,
5303 librados::IoCtx io_ctx
;
5305 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5308 librados::ObjectReadOperation op
;
5310 utime_t
st(start_time
);
5311 utime_t
et(end_time
);
5313 cls_log_list(op
, st
, et
, marker
, max_entries
, entries
,
5314 out_marker
, truncated
);
5318 int ret
= io_ctx
.operate(oid
, &op
, &obl
);
5325 int RGWRados::time_log_info(const string
& oid
, cls_log_header
*header
)
5327 librados::IoCtx io_ctx
;
5329 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5332 librados::ObjectReadOperation op
;
5334 cls_log_info(op
, header
);
5338 int ret
= io_ctx
.operate(oid
, &op
, &obl
);
5345 int RGWRados::time_log_info_async(librados::IoCtx
& io_ctx
, const string
& oid
, cls_log_header
*header
, librados::AioCompletion
*completion
)
5347 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5351 librados::ObjectReadOperation op
;
5353 cls_log_info(op
, header
);
5355 int ret
= io_ctx
.aio_operate(oid
, completion
, &op
, NULL
);
5362 int RGWRados::time_log_trim(const string
& oid
, const real_time
& start_time
, const real_time
& end_time
,
5363 const string
& from_marker
, const string
& to_marker
,
5364 librados::AioCompletion
*completion
)
5366 librados::IoCtx io_ctx
;
5368 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5372 utime_t
st(start_time
);
5373 utime_t
et(end_time
);
5375 ObjectWriteOperation op
;
5376 cls_log_trim(op
, st
, et
, from_marker
, to_marker
);
5379 r
= io_ctx
.operate(oid
, &op
);
5381 r
= io_ctx
.aio_operate(oid
, completion
, &op
);
5386 string
RGWRados::objexp_hint_get_shardname(int shard_num
)
5389 snprintf(buf
, sizeof(buf
), "%010u", (unsigned)shard_num
);
5391 string
objname("obj_delete_at_hint.");
5392 return objname
+ buf
;
5395 int RGWRados::objexp_key_shard(const rgw_obj_index_key
& key
)
5397 string obj_key
= key
.name
+ key
.instance
;
5398 int num_shards
= cct
->_conf
->rgw_objexp_hints_num_shards
;
5399 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
5400 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
5401 sid
= rgw_shards_mod(sid2
, num_shards
);
5405 static string
objexp_hint_get_keyext(const string
& tenant_name
,
5406 const string
& bucket_name
,
5407 const string
& bucket_id
,
5408 const rgw_obj_key
& obj_key
)
5410 return tenant_name
+ (tenant_name
.empty() ? "" : ":") + bucket_name
+ ":" + bucket_id
+
5411 ":" + obj_key
.name
+ ":" + obj_key
.instance
;
5414 int RGWRados::objexp_hint_add(const ceph::real_time
& delete_at
,
5415 const string
& tenant_name
,
5416 const string
& bucket_name
,
5417 const string
& bucket_id
,
5418 const rgw_obj_index_key
& obj_key
)
5420 const string keyext
= objexp_hint_get_keyext(tenant_name
, bucket_name
,
5421 bucket_id
, obj_key
);
5422 objexp_hint_entry he
= {
5423 .tenant
= tenant_name
,
5424 .bucket_name
= bucket_name
,
5425 .bucket_id
= bucket_id
,
5427 .exp_time
= delete_at
};
5430 ObjectWriteOperation op
;
5431 cls_timeindex_add(op
, utime_t(delete_at
), keyext
, hebl
);
5433 string shard_name
= objexp_hint_get_shardname(objexp_key_shard(obj_key
));
5434 return objexp_pool_ctx
.operate(shard_name
, &op
);
5437 void RGWRados::objexp_get_shard(int shard_num
,
5438 string
& shard
) /* out */
5440 shard
= objexp_hint_get_shardname(shard_num
);
5443 int RGWRados::objexp_hint_list(const string
& oid
,
5444 const ceph::real_time
& start_time
,
5445 const ceph::real_time
& end_time
,
5446 const int max_entries
,
5447 const string
& marker
,
5448 list
<cls_timeindex_entry
>& entries
, /* out */
5449 string
*out_marker
, /* out */
5450 bool *truncated
) /* out */
5452 librados::ObjectReadOperation op
;
5453 cls_timeindex_list(op
, utime_t(start_time
), utime_t(end_time
), marker
, max_entries
, entries
,
5454 out_marker
, truncated
);
5457 int ret
= objexp_pool_ctx
.operate(oid
, &op
, &obl
);
5459 if ((ret
< 0 ) && (ret
!= -ENOENT
)) {
5463 if ((ret
== -ENOENT
) && truncated
) {
5470 int RGWRados::objexp_hint_parse(cls_timeindex_entry
&ti_entry
, /* in */
5471 objexp_hint_entry
& hint_entry
) /* out */
5474 bufferlist::iterator iter
= ti_entry
.value
.begin();
5475 ::decode(hint_entry
, iter
);
5476 } catch (buffer::error
& err
) {
5477 ldout(cct
, 0) << "ERROR: couldn't decode avail_pools" << dendl
;
5483 int RGWRados::objexp_hint_trim(const string
& oid
,
5484 const ceph::real_time
& start_time
,
5485 const ceph::real_time
& end_time
,
5486 const string
& from_marker
,
5487 const string
& to_marker
)
5489 int ret
= cls_timeindex_trim(objexp_pool_ctx
, oid
, utime_t(start_time
), utime_t(end_time
),
5490 from_marker
, to_marker
);
5491 if ((ret
< 0 ) && (ret
!= -ENOENT
)) {
5498 int RGWRados::lock_exclusive(rgw_pool
& pool
, const string
& oid
, timespan
& duration
,
5499 string
& zone_id
, string
& owner_id
) {
5500 librados::IoCtx io_ctx
;
5502 int r
= rgw_init_ioctx(get_rados_handle(), pool
, io_ctx
);
5506 uint64_t msec
= std::chrono::duration_cast
<std::chrono::milliseconds
>(duration
).count();
5507 utime_t
ut(msec
/ 1000, msec
% 1000);
5509 rados::cls::lock::Lock
l(log_lock_name
);
5511 l
.set_cookie(owner_id
);
5515 return l
.lock_exclusive(&io_ctx
, oid
);
5518 int RGWRados::unlock(rgw_pool
& pool
, const string
& oid
, string
& zone_id
, string
& owner_id
) {
5519 librados::IoCtx io_ctx
;
5521 int r
= rgw_init_ioctx(get_rados_handle(), pool
, io_ctx
);
5526 rados::cls::lock::Lock
l(log_lock_name
);
5528 l
.set_cookie(owner_id
);
5530 return l
.unlock(&io_ctx
, oid
);
5533 int RGWRados::decode_policy(bufferlist
& bl
, ACLOwner
*owner
)
5535 bufferlist::iterator i
= bl
.begin();
5536 RGWAccessControlPolicy
policy(cct
);
5538 policy
.decode_owner(i
);
5539 } catch (buffer::error
& err
) {
5540 ldout(cct
, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl
;
5543 *owner
= policy
.get_owner();
5547 int rgw_policy_from_attrset(CephContext
*cct
, map
<string
, bufferlist
>& attrset
, RGWAccessControlPolicy
*policy
)
5549 map
<string
, bufferlist
>::iterator aiter
= attrset
.find(RGW_ATTR_ACL
);
5550 if (aiter
== attrset
.end())
5553 bufferlist
& bl
= aiter
->second
;
5554 bufferlist::iterator iter
= bl
.begin();
5556 policy
->decode(iter
);
5557 } catch (buffer::error
& err
) {
5558 ldout(cct
, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl
;
5561 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 15)) {
5562 RGWAccessControlPolicy_S3
*s3policy
= static_cast<RGWAccessControlPolicy_S3
*>(policy
);
5563 ldout(cct
, 15) << __func__
<< " Read AccessControlPolicy";
5564 s3policy
->to_xml(*_dout
);
5571 int RGWRados::Bucket::update_bucket_id(const string
& new_bucket_id
)
5573 rgw_bucket bucket
= bucket_info
.bucket
;
5574 bucket
.update_bucket_id(new_bucket_id
);
5576 RGWObjectCtx
obj_ctx(store
);
5578 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, nullptr, nullptr);
5587 * get listing of the objects in a bucket.
5589 * max: maximum number of results to return
5590 * bucket: bucket to list contents of
5591 * prefix: only return results that match this prefix
5592 * delim: do not include results that match this string.
5593 * Any skipped results will have the matching portion of their name
5594 * inserted in common_prefixes with a "true" mark.
5595 * marker: if filled in, begin the listing with this object.
5596 * end_marker: if filled in, end the listing with this object.
5597 * result: the objects are put in here.
5598 * common_prefixes: if delim is filled in, any matching prefixes are placed here.
5599 * is_truncated: if number of objects in the bucket is bigger than max, then truncated.
5601 int RGWRados::Bucket::List::list_objects(int64_t max
,
5602 vector
<rgw_bucket_dir_entry
> *result
,
5603 map
<string
, bool> *common_prefixes
,
5606 RGWRados
*store
= target
->get_store();
5607 CephContext
*cct
= store
->ctx();
5608 int shard_id
= target
->get_shard_id();
5611 bool truncated
= true;
5612 int read_ahead
= std::max(cct
->_conf
->rgw_list_bucket_min_readahead
,max
);
5616 rgw_obj_key
marker_obj(params
.marker
.name
, params
.marker
.instance
, params
.ns
);
5617 rgw_obj_index_key cur_marker
;
5618 marker_obj
.get_index_key(&cur_marker
);
5620 rgw_obj_key
end_marker_obj(params
.end_marker
.name
, params
.end_marker
.instance
,
5622 rgw_obj_index_key cur_end_marker
;
5623 end_marker_obj
.get_index_key(&cur_end_marker
);
5624 const bool cur_end_marker_valid
= !params
.end_marker
.empty();
5626 rgw_obj_key
prefix_obj(params
.prefix
);
5627 prefix_obj
.ns
= params
.ns
;
5628 string cur_prefix
= prefix_obj
.get_index_key_name();
5630 string bigger_than_delim
;
5632 if (!params
.delim
.empty()) {
5633 unsigned long val
= decode_utf8((unsigned char *)params
.delim
.c_str(), params
.delim
.size());
5634 char buf
[params
.delim
.size() + 16];
5635 int r
= encode_utf8(val
+ 1, (unsigned char *)buf
);
5637 ldout(cct
,0) << "ERROR: encode_utf8() failed" << dendl
;
5642 bigger_than_delim
= buf
;
5644 /* if marker points at a common prefix, fast forward it into its upperbound string */
5645 int delim_pos
= cur_marker
.name
.find(params
.delim
, cur_prefix
.size());
5646 if (delim_pos
>= 0) {
5647 string s
= cur_marker
.name
.substr(0, delim_pos
);
5648 s
.append(bigger_than_delim
);
5653 string skip_after_delim
;
5654 while (truncated
&& count
<= max
) {
5655 if (skip_after_delim
> cur_marker
.name
) {
5656 cur_marker
= skip_after_delim
;
5657 ldout(cct
, 20) << "setting cur_marker=" << cur_marker
.name
<< "[" << cur_marker
.instance
<< "]" << dendl
;
5659 std::map
<string
, rgw_bucket_dir_entry
> ent_map
;
5660 int r
= store
->cls_bucket_list(target
->get_bucket_info(), shard_id
, cur_marker
, cur_prefix
,
5661 read_ahead
+ 1 - count
, params
.list_versions
, ent_map
,
5662 &truncated
, &cur_marker
);
5666 std::map
<string
, rgw_bucket_dir_entry
>::iterator eiter
;
5667 for (eiter
= ent_map
.begin(); eiter
!= ent_map
.end(); ++eiter
) {
5668 rgw_bucket_dir_entry
& entry
= eiter
->second
;
5669 rgw_obj_index_key index_key
= entry
.key
;
5671 rgw_obj_key
obj(index_key
);
5673 /* note that parse_raw_oid() here will not set the correct object's instance, as
5674 * rgw_obj_index_key encodes that separately. We don't need to set the instance because it's
5675 * not needed for the checks here and we end up using the raw entry for the return vector
5677 bool valid
= rgw_obj_key::parse_raw_oid(index_key
.name
, &obj
);
5679 ldout(cct
, 0) << "ERROR: could not parse object name: " << obj
.name
<< dendl
;
5682 bool check_ns
= (obj
.ns
== params
.ns
);
5683 if (!params
.list_versions
&& !entry
.is_visible()) {
5687 if (params
.enforce_ns
&& !check_ns
) {
5688 if (!params
.ns
.empty()) {
5689 /* we've iterated past the namespace we're searching -- done now */
5694 /* we're not looking at the namespace this object is in, next! */
5698 if (cur_end_marker_valid
&& cur_end_marker
<= index_key
) {
5704 params
.marker
= index_key
;
5705 next_marker
= index_key
;
5708 if (params
.filter
&& !params
.filter
->filter(obj
.name
, index_key
.name
))
5711 if (params
.prefix
.size() && (obj
.name
.compare(0, params
.prefix
.size(), params
.prefix
) != 0))
5714 if (!params
.delim
.empty()) {
5715 int delim_pos
= obj
.name
.find(params
.delim
, params
.prefix
.size());
5717 if (delim_pos
>= 0) {
5718 string prefix_key
= obj
.name
.substr(0, delim_pos
+ 1);
5720 if (common_prefixes
&&
5721 common_prefixes
->find(prefix_key
) == common_prefixes
->end()) {
5726 next_marker
= prefix_key
;
5727 (*common_prefixes
)[prefix_key
] = true;
5729 int marker_delim_pos
= cur_marker
.name
.find(params
.delim
, cur_prefix
.size());
5731 skip_after_delim
= cur_marker
.name
.substr(0, marker_delim_pos
);
5732 skip_after_delim
.append(bigger_than_delim
);
5734 ldout(cct
, 20) << "skip_after_delim=" << skip_after_delim
<< dendl
;
5748 result
->emplace_back(std::move(entry
));
5752 // Either the back-end telling us truncated, or we don't consume all
5753 // items returned per the amount caller request
5754 truncated
= (truncated
|| eiter
!= ent_map
.end());
5759 *is_truncated
= truncated
;
5765 * create a rados pool, associated meta info
5766 * returns 0 on success, -ERR# otherwise.
5768 int RGWRados::create_pool(const rgw_pool
& pool
)
5772 librados::Rados
*rad
= get_rados_handle();
5773 ret
= rad
->pool_create(pool
.name
.c_str(), 0);
5776 else if (ret
== -ERANGE
) {
5779 << " ERROR: librados::Rados::pool_create returned " << cpp_strerror(-ret
)
5780 << " (this can be due to a pool or placement group misconfiguration, e.g., pg_num < pgp_num)"
5786 librados::IoCtx io_ctx
;
5787 ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctx
);
5791 ret
= io_ctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
5792 if (ret
< 0 && ret
!= -EOPNOTSUPP
)
5797 int RGWRados::init_bucket_index(RGWBucketInfo
& bucket_info
, int num_shards
)
5799 librados::IoCtx index_ctx
; // context for new bucket
5801 string dir_oid
= dir_oid_prefix
;
5802 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
5807 dir_oid
.append(bucket_info
.bucket
.bucket_id
);
5809 map
<int, string
> bucket_objs
;
5810 get_bucket_index_objects(dir_oid
, num_shards
, bucket_objs
);
5812 return CLSRGWIssueBucketIndexInit(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
5815 void RGWRados::create_bucket_id(string
*bucket_id
)
5817 uint64_t iid
= instance_id();
5818 uint64_t bid
= next_bucket_id();
5819 char buf
[get_zone_params().get_id().size() + 48];
5820 snprintf(buf
, sizeof(buf
), "%s.%llu.%llu", get_zone_params().get_id().c_str(), (long long)iid
, (long long)bid
);
5824 int RGWRados::create_bucket(RGWUserInfo
& owner
, rgw_bucket
& bucket
,
5825 const string
& zonegroup_id
,
5826 const string
& placement_rule
,
5827 const string
& swift_ver_location
,
5828 const RGWQuotaInfo
* pquota_info
,
5829 map
<std::string
, bufferlist
>& attrs
,
5830 RGWBucketInfo
& info
,
5832 obj_version
*pep_objv
,
5833 real_time creation_time
,
5834 rgw_bucket
*pmaster_bucket
,
5835 uint32_t *pmaster_num_shards
,
5838 #define MAX_CREATE_RETRIES 20 /* need to bound retries */
5839 string selected_placement_rule_name
;
5840 RGWZonePlacementInfo rule_info
;
5842 for (int i
= 0; i
< MAX_CREATE_RETRIES
; i
++) {
5844 ret
= select_bucket_placement(owner
, zonegroup_id
, placement_rule
,
5845 &selected_placement_rule_name
, &rule_info
);
5849 if (!pmaster_bucket
) {
5850 create_bucket_id(&bucket
.marker
);
5851 bucket
.bucket_id
= bucket
.marker
;
5853 bucket
.marker
= pmaster_bucket
->marker
;
5854 bucket
.bucket_id
= pmaster_bucket
->bucket_id
;
5857 RGWObjVersionTracker
& objv_tracker
= info
.objv_tracker
;
5860 objv_tracker
.write_version
= *pobjv
;
5862 objv_tracker
.generate_new_write_ver(cct
);
5865 info
.bucket
= bucket
;
5866 info
.owner
= owner
.user_id
;
5867 info
.zonegroup
= zonegroup_id
;
5868 info
.placement_rule
= selected_placement_rule_name
;
5869 info
.index_type
= rule_info
.index_type
;
5870 info
.swift_ver_location
= swift_ver_location
;
5871 info
.swift_versioning
= (!swift_ver_location
.empty());
5872 if (pmaster_num_shards
) {
5873 info
.num_shards
= *pmaster_num_shards
;
5875 info
.num_shards
= bucket_index_max_shards
;
5877 info
.bucket_index_shard_hash_type
= RGWBucketInfo::MOD
;
5878 info
.requester_pays
= false;
5879 if (real_clock::is_zero(creation_time
)) {
5880 info
.creation_time
= ceph::real_clock::now();
5882 info
.creation_time
= creation_time
;
5885 info
.quota
= *pquota_info
;
5888 int r
= init_bucket_index(info
, info
.num_shards
);
5893 ret
= put_linked_bucket_info(info
, exclusive
, ceph::real_time(), pep_objv
, &attrs
, true);
5894 if (ret
== -EEXIST
) {
5895 librados::IoCtx index_ctx
;
5896 map
<int, string
> bucket_objs
;
5897 int r
= open_bucket_index(info
, index_ctx
, bucket_objs
);
5901 /* we need to reread the info and return it, caller will have a use for it */
5902 RGWObjVersionTracker instance_ver
= info
.objv_tracker
;
5903 info
.objv_tracker
.clear();
5904 RGWObjectCtx
obj_ctx(this);
5905 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, NULL
);
5910 ldout(cct
, 0) << "get_bucket_info returned " << r
<< dendl
;
5914 /* only remove it if it's a different bucket instance */
5915 if (info
.bucket
.bucket_id
!= bucket
.bucket_id
) {
5916 /* remove bucket meta instance */
5917 string entry
= bucket
.get_key();
5918 r
= rgw_bucket_instance_remove_entry(this, entry
, &instance_ver
);
5922 map
<int, string
>::const_iterator biter
;
5923 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
5924 // Do best effort removal
5925 index_ctx
.remove(biter
->second
);
5928 /* ret == -ENOENT here */
5933 /* this is highly unlikely */
5934 ldout(cct
, 0) << "ERROR: could not create bucket, continuously raced with bucket creation and removal" << dendl
;
5938 int RGWRados::select_new_bucket_location(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& request_rule
,
5939 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
5942 /* first check that zonegroup exists within current period. */
5943 RGWZoneGroup zonegroup
;
5944 int ret
= get_zonegroup(zonegroup_id
, zonegroup
);
5946 ldout(cct
, 0) << "could not find zonegroup " << zonegroup_id
<< " in current period" << dendl
;
5950 /* find placement rule. Hierarchy: request rule > user default rule > zonegroup default rule */
5951 std::map
<std::string
, RGWZoneGroupPlacementTarget
>::const_iterator titer
;
5953 if (!request_rule
.empty()) {
5954 titer
= zonegroup
.placement_targets
.find(request_rule
);
5955 if (titer
== zonegroup
.placement_targets
.end()) {
5956 ldout(cct
, 0) << "could not find requested placement id " << request_rule
5957 << " within zonegroup " << dendl
;
5958 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5960 } else if (!user_info
.default_placement
.empty()) {
5961 titer
= zonegroup
.placement_targets
.find(user_info
.default_placement
);
5962 if (titer
== zonegroup
.placement_targets
.end()) {
5963 ldout(cct
, 0) << "could not find user default placement id " << user_info
.default_placement
5964 << " within zonegroup " << dendl
;
5965 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5968 if (zonegroup
.default_placement
.empty()) { // zonegroup default rule as fallback, it should not be empty.
5969 ldout(cct
, 0) << "misconfiguration, zonegroup default placement id should not be empty." << dendl
;
5970 return -ERR_ZONEGROUP_DEFAULT_PLACEMENT_MISCONFIGURATION
;
5972 titer
= zonegroup
.placement_targets
.find(zonegroup
.default_placement
);
5973 if (titer
== zonegroup
.placement_targets
.end()) {
5974 ldout(cct
, 0) << "could not find zonegroup default placement id " << zonegroup
.default_placement
5975 << " within zonegroup " << dendl
;
5976 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5981 /* now check tag for the rule, whether user is permitted to use rule */
5982 const auto& target_rule
= titer
->second
;
5983 if (!target_rule
.user_permitted(user_info
.placement_tags
)) {
5984 ldout(cct
, 0) << "user not permitted to use placement rule " << titer
->first
<< dendl
;
5988 if (pselected_rule_name
)
5989 *pselected_rule_name
= titer
->first
;
5991 return select_bucket_location_by_rule(titer
->first
, rule_info
);
5994 int RGWRados::select_bucket_location_by_rule(const string
& location_rule
, RGWZonePlacementInfo
*rule_info
)
5996 if (location_rule
.empty()) {
5997 /* we can only reach here if we're trying to set a bucket location from a bucket
5998 * created on a different zone, using a legacy / default pool configuration
6000 return select_legacy_bucket_placement(rule_info
);
6004 * make sure that zone has this rule configured. We're
6005 * checking it for the local zone, because that's where this bucket object is going to
6008 map
<string
, RGWZonePlacementInfo
>::iterator piter
= get_zone_params().placement_pools
.find(location_rule
);
6009 if (piter
== get_zone_params().placement_pools
.end()) {
6010 /* couldn't find, means we cannot really place data for this bucket in this zone */
6011 if (get_zonegroup().equals(zonegroup
.get_id())) {
6012 /* that's a configuration error, zone should have that rule, as we're within the requested
6016 /* oh, well, data is not going to be placed here, bucket object is just a placeholder */
6021 RGWZonePlacementInfo
& placement_info
= piter
->second
;
6024 *rule_info
= placement_info
;
6030 int RGWRados::select_bucket_placement(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& placement_rule
,
6031 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
6033 if (!get_zone_params().placement_pools
.empty()) {
6034 return select_new_bucket_location(user_info
, zonegroup_id
, placement_rule
,
6035 pselected_rule_name
, rule_info
);
6038 if (pselected_rule_name
) {
6039 pselected_rule_name
->clear();
6042 return select_legacy_bucket_placement(rule_info
);
6045 int RGWRados::select_legacy_bucket_placement(RGWZonePlacementInfo
*rule_info
)
6048 map
<string
, bufferlist
> m
;
6050 bool write_map
= false;
6052 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6054 RGWObjectCtx
obj_ctx(this);
6055 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
, avail_pools
, map_bl
, NULL
, NULL
);
6061 bufferlist::iterator iter
= map_bl
.begin();
6063 } catch (buffer::error
& err
) {
6064 ldout(cct
, 0) << "ERROR: couldn't decode avail_pools" << dendl
;
6070 ret
= omap_get_all(obj
, header
, m
);
6075 if (ret
< 0 || m
.empty()) {
6076 vector
<rgw_pool
> pools
;
6077 string s
= string("default.") + default_storage_pool_suffix
;
6078 pools
.push_back(rgw_pool(s
));
6079 vector
<int> retcodes
;
6081 ret
= create_pools(pools
, retcodes
);
6084 ret
= omap_set(obj
, s
, bl
);
6092 ::encode(m
, new_bl
);
6093 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6095 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6099 map
<string
, bufferlist
>::iterator miter
;
6102 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6103 v
.push_back(miter
->first
);
6107 ret
= get_random_bytes((char *)&r
, sizeof(r
));
6111 int i
= r
% v
.size();
6115 pool_name
= miter
->first
;
6118 rule_info
->data_pool
= pool_name
;
6119 rule_info
->data_extra_pool
= pool_name
;
6120 rule_info
->index_pool
= pool_name
;
6121 rule_info
->index_type
= RGWBIType_Normal
;
6126 bool RGWRados::get_obj_data_pool(const string
& placement_rule
, const rgw_obj
& obj
, rgw_pool
*pool
)
6128 return rgw_get_obj_data_pool(zonegroup
, zone_params
, placement_rule
, obj
, pool
);
6131 bool RGWRados::obj_to_raw(const string
& placement_rule
, const rgw_obj
& obj
, rgw_raw_obj
*raw_obj
)
6133 get_obj_bucket_and_oid_loc(obj
, raw_obj
->oid
, raw_obj
->loc
);
6135 return get_obj_data_pool(placement_rule
, obj
, &raw_obj
->pool
);
6138 int RGWRados::update_placement_map()
6141 map
<string
, bufferlist
> m
;
6142 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6143 int ret
= omap_get_all(obj
, header
, m
);
6148 ::encode(m
, new_bl
);
6149 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6151 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6157 int RGWRados::add_bucket_placement(const rgw_pool
& new_pool
)
6159 librados::Rados
*rad
= get_rados_handle();
6160 int ret
= rad
->pool_lookup(new_pool
.name
.c_str());
6161 if (ret
< 0) // DNE, or something
6164 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6165 bufferlist empty_bl
;
6166 ret
= omap_set(obj
, new_pool
.to_str(), empty_bl
);
6168 // don't care about return value
6169 update_placement_map();
6174 int RGWRados::remove_bucket_placement(const rgw_pool
& old_pool
)
6176 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6177 int ret
= omap_del(obj
, old_pool
.to_str());
6179 // don't care about return value
6180 update_placement_map();
6185 int RGWRados::list_placement_set(set
<rgw_pool
>& names
)
6188 map
<string
, bufferlist
> m
;
6190 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6191 int ret
= omap_get_all(obj
, header
, m
);
6196 map
<string
, bufferlist
>::iterator miter
;
6197 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6198 names
.insert(rgw_pool(miter
->first
));
6201 return names
.size();
6204 int RGWRados::create_pools(vector
<rgw_pool
>& pools
, vector
<int>& retcodes
)
6206 vector
<librados::PoolAsyncCompletion
*> completions
;
6209 librados::Rados
*rad
= get_rados_handle();
6210 for (auto iter
= pools
.begin(); iter
!= pools
.end(); ++iter
) {
6211 librados::PoolAsyncCompletion
*c
= librados::Rados::pool_async_create_completion();
6212 completions
.push_back(c
);
6213 rgw_pool
& pool
= *iter
;
6214 int ret
= rad
->pool_create_async(pool
.name
.c_str(), c
);
6215 rets
.push_back(ret
);
6218 vector
<int>::iterator riter
;
6219 vector
<librados::PoolAsyncCompletion
*>::iterator citer
;
6222 assert(rets
.size() == completions
.size());
6223 for (riter
= rets
.begin(), citer
= completions
.begin(); riter
!= rets
.end(); ++riter
, ++citer
) {
6225 PoolAsyncCompletion
*c
= *citer
;
6228 r
= c
->get_return_value();
6230 ldout(cct
, 0) << "WARNING: async pool_create returned " << r
<< dendl
;
6235 retcodes
.push_back(r
);
6241 std::vector
<librados::IoCtx
> io_ctxs
;
6243 for (auto pool
: pools
) {
6244 io_ctxs
.emplace_back();
6245 int ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctxs
.back());
6247 ldout(cct
, 0) << "WARNING: ioctx_create returned " << ret
<< dendl
;
6250 retcodes
.push_back(ret
);
6256 completions
.clear();
6257 for (auto &io_ctx
: io_ctxs
) {
6258 librados::PoolAsyncCompletion
*c
=
6259 librados::Rados::pool_async_create_completion();
6260 completions
.push_back(c
);
6261 int ret
= io_ctx
.application_enable_async(pg_pool_t::APPLICATION_NAME_RGW
,
6267 for (auto c
: completions
) {
6269 int ret
= c
->get_return_value();
6270 if (ret
== -EOPNOTSUPP
) {
6272 } else if (ret
< 0) {
6273 ldout(cct
, 0) << "WARNING: async application_enable returned " << ret
6278 retcodes
.push_back(ret
);
6283 int RGWRados::get_obj_head_ioctx(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, librados::IoCtx
*ioctx
)
6286 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
6289 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6290 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6294 int r
= open_pool_ctx(pool
, *ioctx
);
6299 ioctx
->locator_set_key(key
);
6304 int RGWRados::get_obj_head_ref(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_rados_ref
*ref
)
6306 get_obj_bucket_and_oid_loc(obj
, ref
->oid
, ref
->key
);
6309 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6310 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6314 int r
= open_pool_ctx(pool
, ref
->ioctx
);
6319 ref
->ioctx
.locator_set_key(ref
->key
);
6324 int RGWRados::get_raw_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6331 if (ref
->oid
.empty()) {
6332 ref
->oid
= obj
.pool
.to_str();
6333 ref
->pool
= get_zone_params().domain_root
;
6335 ref
->pool
= obj
.pool
;
6337 r
= open_pool_ctx(ref
->pool
, ref
->ioctx
);
6341 ref
->ioctx
.locator_set_key(ref
->key
);
6346 int RGWRados::get_system_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6348 return get_raw_obj_ref(obj
, ref
);
6352 * fixes an issue where head objects were supposed to have a locator created, but ended
6355 int RGWRados::fix_head_obj_locator(const RGWBucketInfo
& bucket_info
, bool copy_obj
, bool remove_bad
, rgw_obj_key
& key
)
6357 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6361 rgw_obj
obj(bucket
, key
);
6363 get_obj_bucket_and_oid_loc(obj
, oid
, locator
);
6365 if (locator
.empty()) {
6366 ldout(cct
, 20) << "object does not have a locator, nothing to fix" << dendl
;
6370 librados::IoCtx ioctx
;
6372 int ret
= get_obj_head_ioctx(bucket_info
, obj
, &ioctx
);
6374 cerr
<< "ERROR: get_obj_head_ioctx() returned ret=" << ret
<< std::endl
;
6377 ioctx
.locator_set_key(string()); /* override locator for this object, use empty locator */
6382 struct timespec mtime_ts
;
6383 map
<string
, bufferlist
> attrs
;
6384 librados::ObjectReadOperation op
;
6385 op
.getxattrs(&attrs
, NULL
);
6386 op
.stat2(&size
, &mtime_ts
, NULL
);
6387 #define HEAD_SIZE 512 * 1024
6388 op
.read(0, HEAD_SIZE
, &data
, NULL
);
6390 ret
= ioctx
.operate(oid
, &op
, NULL
);
6392 lderr(cct
) << "ERROR: ioctx.operate(oid=" << oid
<< ") returned ret=" << ret
<< dendl
;
6396 if (size
> HEAD_SIZE
) {
6397 lderr(cct
) << "ERROR: returned object size (" << size
<< ") > HEAD_SIZE (" << HEAD_SIZE
<< ")" << dendl
;
6401 if (size
!= data
.length()) {
6402 lderr(cct
) << "ERROR: returned object size (" << size
<< ") != data.length() (" << data
.length() << ")" << dendl
;
6407 librados::ObjectWriteOperation wop
;
6409 wop
.mtime2(&mtime_ts
);
6411 map
<string
, bufferlist
>::iterator iter
;
6412 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6413 wop
.setxattr(iter
->first
.c_str(), iter
->second
);
6418 ioctx
.locator_set_key(locator
);
6419 ioctx
.operate(oid
, &wop
);
6423 ioctx
.locator_set_key(string());
6425 ret
= ioctx
.remove(oid
);
6427 lderr(cct
) << "ERROR: failed to remove original bad object" << dendl
;
6435 int RGWRados::move_rados_obj(librados::IoCtx
& src_ioctx
,
6436 const string
& src_oid
, const string
& src_locator
,
6437 librados::IoCtx
& dst_ioctx
,
6438 const string
& dst_oid
, const string
& dst_locator
)
6441 #define COPY_BUF_SIZE (4 * 1024 * 1024)
6443 uint64_t chunk_size
= COPY_BUF_SIZE
;
6447 struct timespec mtime_ts
;
6450 if (src_oid
== dst_oid
&& src_locator
== dst_locator
) {
6454 src_ioctx
.locator_set_key(src_locator
);
6455 dst_ioctx
.locator_set_key(dst_locator
);
6459 ObjectReadOperation rop
;
6460 ObjectWriteOperation wop
;
6463 rop
.stat2(&size
, &mtime_ts
, NULL
);
6464 mtime
= real_clock::from_timespec(mtime_ts
);
6466 rop
.read(ofs
, chunk_size
, &data
, NULL
);
6467 ret
= src_ioctx
.operate(src_oid
, &rop
, NULL
);
6472 if (data
.length() == 0) {
6477 wop
.create(true); /* make it exclusive */
6478 wop
.mtime2(&mtime_ts
);
6479 mtime
= real_clock::from_timespec(mtime_ts
);
6481 wop
.write(ofs
, data
);
6482 ret
= dst_ioctx
.operate(dst_oid
, &wop
);
6483 ofs
+= data
.length();
6484 done
= data
.length() != chunk_size
;
6488 lderr(cct
) << "ERROR: " << __func__
<< ": copying " << src_oid
<< " -> " << dst_oid
6489 << ": expected " << size
<< " bytes to copy, ended up with " << ofs
<< dendl
;
6494 src_ioctx
.remove(src_oid
);
6499 lderr(cct
) << "ERROR: failed to copy " << src_oid
<< " -> " << dst_oid
<< dendl
;
6504 * fixes an issue where head objects were supposed to have a locator created, but ended
6507 int RGWRados::fix_tail_obj_locator(const RGWBucketInfo
& bucket_info
, rgw_obj_key
& key
, bool fix
, bool *need_fix
)
6509 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6510 rgw_obj
obj(bucket
, key
);
6517 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
6522 RGWObjState
*astate
= NULL
;
6523 RGWObjectCtx
rctx(this);
6524 r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
6528 if (astate
->has_manifest
) {
6529 RGWObjManifest::obj_iterator miter
;
6530 RGWObjManifest
& manifest
= astate
->manifest
;
6531 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
6532 rgw_raw_obj raw_loc
= miter
.get_location().get_raw_obj(this);
6537 rgw_raw_obj_to_obj(manifest
.get_tail_placement().bucket
, raw_loc
, &loc
);
6539 if (loc
.key
.ns
.empty()) {
6540 /* continue, we're only interested in tail objects */
6544 get_obj_bucket_and_oid_loc(loc
, oid
, locator
);
6545 ref
.ioctx
.locator_set_key(locator
);
6547 ldout(cct
, 20) << __func__
<< ": key=" << key
<< " oid=" << oid
<< " locator=" << locator
<< dendl
;
6549 r
= ref
.ioctx
.stat(oid
, NULL
, NULL
);
6555 prepend_bucket_marker(bucket
, loc
.key
.name
, bad_loc
);
6557 /* create a new ioctx with the bad locator */
6558 librados::IoCtx src_ioctx
;
6559 src_ioctx
.dup(ref
.ioctx
);
6560 src_ioctx
.locator_set_key(bad_loc
);
6562 r
= src_ioctx
.stat(oid
, NULL
, NULL
);
6564 /* cannot find a broken part */
6567 ldout(cct
, 20) << __func__
<< ": found bad object part: " << loc
<< dendl
;
6572 r
= move_rados_obj(src_ioctx
, oid
, bad_loc
, ref
.ioctx
, oid
, locator
);
6574 lderr(cct
) << "ERROR: copy_rados_obj() on oid=" << oid
<< " returned r=" << r
<< dendl
;
6583 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, const rgw_obj
& obj
)
6587 RGWObjectCtx
obj_ctx(store
);
6589 RGWBucketInfo bucket_info
;
6590 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6595 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, obj
.get_hash_object(), &bucket_obj
, &shard_id
);
6597 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6600 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6605 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, int sid
)
6610 RGWObjectCtx
obj_ctx(store
);
6612 RGWBucketInfo bucket_info
;
6613 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6618 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, shard_id
, &bucket_obj
);
6620 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6623 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6628 int RGWRados::BucketShard::init(const RGWBucketInfo
& bucket_info
, int sid
)
6630 bucket
= bucket_info
.bucket
;
6633 int ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, shard_id
, &bucket_obj
);
6635 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6638 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6644 /* Execute @handler on last item in bucket listing for bucket specified
6645 * in @bucket_info. @obj_prefix and @obj_delim narrow down the listing
6646 * to objects matching these criterias. */
6647 int RGWRados::on_last_entry_in_listing(RGWBucketInfo
& bucket_info
,
6648 const std::string
& obj_prefix
,
6649 const std::string
& obj_delim
,
6650 std::function
<int(const rgw_bucket_dir_entry
&)> handler
)
6652 RGWRados::Bucket
target(this, bucket_info
);
6653 RGWRados::Bucket::List
list_op(&target
);
6655 list_op
.params
.prefix
= obj_prefix
;
6656 list_op
.params
.delim
= obj_delim
;
6658 ldout(cct
, 20) << "iterating listing for bucket=" << bucket_info
.bucket
.name
6659 << ", obj_prefix=" << obj_prefix
6660 << ", obj_delim=" << obj_delim
6663 bool is_truncated
= false;
6665 boost::optional
<rgw_bucket_dir_entry
> last_entry
;
6666 /* We need to rewind to the last object in a listing. */
6668 /* List bucket entries in chunks. */
6669 static constexpr int MAX_LIST_OBJS
= 100;
6670 std::vector
<rgw_bucket_dir_entry
> entries(MAX_LIST_OBJS
);
6672 int ret
= list_op
.list_objects(MAX_LIST_OBJS
, &entries
, nullptr,
6676 } else if (!entries
.empty()) {
6677 last_entry
= entries
.back();
6679 } while (is_truncated
);
6682 return handler(*last_entry
);
6685 /* Empty listing - no items we can run handler on. */
6690 int RGWRados::swift_versioning_copy(RGWObjectCtx
& obj_ctx
,
6691 const rgw_user
& user
,
6692 RGWBucketInfo
& bucket_info
,
6695 if (! swift_versioning_enabled(bucket_info
)) {
6699 obj_ctx
.obj
.set_atomic(obj
);
6701 RGWObjState
* state
= nullptr;
6702 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &state
, false);
6707 if (!state
->exists
) {
6714 const string
& src_name
= obj
.get_oid();
6715 char buf
[src_name
.size() + 32];
6716 struct timespec ts
= ceph::real_clock::to_timespec(state
->mtime
);
6717 snprintf(buf
, sizeof(buf
), "%03x%s/%lld.%06ld", (int)src_name
.size(),
6718 src_name
.c_str(), (long long)ts
.tv_sec
, ts
.tv_nsec
/ 1000);
6720 RGWBucketInfo dest_bucket_info
;
6722 r
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
, bucket_info
.swift_ver_location
, dest_bucket_info
, NULL
, NULL
);
6724 ldout(cct
, 10) << "failed to read dest bucket info: r=" << r
<< dendl
;
6726 return -ERR_PRECONDITION_FAILED
;
6731 if (dest_bucket_info
.owner
!= bucket_info
.owner
) {
6732 return -ERR_PRECONDITION_FAILED
;
6735 rgw_obj
dest_obj(dest_bucket_info
.bucket
, buf
);
6736 obj_ctx
.obj
.set_atomic(dest_obj
);
6740 r
= copy_obj(obj_ctx
,
6744 NULL
, /* req_info *info */
6750 NULL
, /* time_t *src_mtime */
6751 NULL
, /* time_t *mtime */
6752 NULL
, /* const time_t *mod_ptr */
6753 NULL
, /* const time_t *unmod_ptr */
6754 false, /* bool high_precision_time */
6755 NULL
, /* const char *if_match */
6756 NULL
, /* const char *if_nomatch */
6757 RGWRados::ATTRSMOD_NONE
,
6758 true, /* bool copy_if_newer */
6760 RGW_OBJ_CATEGORY_MAIN
,
6761 0, /* uint64_t olh_epoch */
6762 real_time(), /* time_t delete_at */
6763 NULL
, /* string *version_id */
6764 NULL
, /* string *ptag */
6765 NULL
, /* string *petag */
6766 NULL
, /* void (*progress_cb)(off_t, void *) */
6767 NULL
); /* void *progress_data */
6768 if (r
== -ECANCELED
|| r
== -ENOENT
) {
6769 /* Has already been overwritten, meaning another rgw process already
6777 int RGWRados::swift_versioning_restore(RGWObjectCtx
& obj_ctx
,
6778 const rgw_user
& user
,
6779 RGWBucketInfo
& bucket_info
,
6781 bool& restored
) /* out */
6783 if (! swift_versioning_enabled(bucket_info
)) {
6787 /* Bucket info of the bucket that stores previous versions of our object. */
6788 RGWBucketInfo archive_binfo
;
6790 int ret
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
,
6791 bucket_info
.swift_ver_location
, archive_binfo
,
6797 /* Abort the operation if the bucket storing our archive belongs to someone
6798 * else. This is a limitation in comparison to Swift as we aren't taking ACLs
6799 * into consideration. For we can live with that.
6801 * TODO: delegate this check to un upper layer and compare with ACLs. */
6802 if (bucket_info
.owner
!= archive_binfo
.owner
) {
6806 /* This code will be executed on latest version of the object. */
6807 const auto handler
= [&](const rgw_bucket_dir_entry
& entry
) -> int {
6808 std::string no_client_id
;
6809 std::string no_op_id
;
6810 std::string no_zone
;
6812 /* We don't support object versioning of Swift API on those buckets that
6813 * are already versioned using the S3 mechanism. This affects also bucket
6814 * storing archived objects. Otherwise the delete operation would create
6815 * a deletion marker. */
6816 if (archive_binfo
.versioned()) {
6818 return -ERR_PRECONDITION_FAILED
;
6821 /* We are requesting ATTRSMOD_NONE so the attr attribute is perfectly
6822 * irrelevant and may be safely skipped. */
6823 std::map
<std::string
, ceph::bufferlist
> no_attrs
;
6825 rgw_obj
archive_obj(archive_binfo
.bucket
, entry
.key
);
6826 obj_ctx
.obj
.set_atomic(archive_obj
);
6827 obj_ctx
.obj
.set_atomic(obj
);
6829 int ret
= copy_obj(obj_ctx
,
6833 nullptr, /* req_info *info */
6836 archive_obj
, /* src obj */
6837 bucket_info
, /* dest bucket info */
6838 archive_binfo
, /* src bucket info */
6839 nullptr, /* time_t *src_mtime */
6840 nullptr, /* time_t *mtime */
6841 nullptr, /* const time_t *mod_ptr */
6842 nullptr, /* const time_t *unmod_ptr */
6843 false, /* bool high_precision_time */
6844 nullptr, /* const char *if_match */
6845 nullptr, /* const char *if_nomatch */
6846 RGWRados::ATTRSMOD_NONE
,
6847 true, /* bool copy_if_newer */
6849 RGW_OBJ_CATEGORY_MAIN
,
6850 0, /* uint64_t olh_epoch */
6851 real_time(), /* time_t delete_at */
6852 nullptr, /* string *version_id */
6853 nullptr, /* string *ptag */
6854 nullptr, /* string *petag */
6855 nullptr, /* void (*progress_cb)(off_t, void *) */
6856 nullptr); /* void *progress_data */
6857 if (ret
== -ECANCELED
|| ret
== -ENOENT
) {
6858 /* Has already been overwritten, meaning another rgw process already
6861 } else if (ret
< 0) {
6867 /* Need to remove the archived copy. */
6868 ret
= delete_obj(obj_ctx
, archive_binfo
, archive_obj
,
6869 archive_binfo
.versioning_status());
6874 const std::string
& obj_name
= obj
.get_oid();
6875 const auto prefix
= boost::str(boost::format("%03x%s") % obj_name
.size()
6878 return on_last_entry_in_listing(archive_binfo
, prefix
, std::string(),
6883 * Write/overwrite an object to the bucket storage.
6884 * bucket: the bucket to store the object in
6885 * obj: the object name/key
6886 * data: the object contents/value
6887 * size: the amount of data to write (data must be this long)
6888 * accounted_size: original size of data before compression, encryption
6889 * mtime: if non-NULL, writes the given mtime to the bucket storage
6890 * attrs: all the given attrs are written to bucket storage for the given object
6891 * exclusive: create object exclusively
6892 * Returns: 0 on success, -ERR# otherwise.
6894 int RGWRados::Object::Write::_do_write_meta(uint64_t size
, uint64_t accounted_size
,
6895 map
<string
, bufferlist
>& attrs
,
6896 bool assume_noent
, bool modify_tail
,
6899 RGWRados::Bucket::UpdateIndex
*index_op
= static_cast<RGWRados::Bucket::UpdateIndex
*>(_index_op
);
6900 RGWRados
*store
= target
->get_store();
6902 ObjectWriteOperation op
;
6905 int r
= target
->get_state(&state
, false, assume_noent
);
6909 rgw_obj
& obj
= target
->get_obj();
6911 if (obj
.get_oid().empty()) {
6912 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< "(): cannot write object with empty name" << dendl
;
6917 r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
6921 bool is_olh
= state
->is_olh
;
6923 bool reset_obj
= (meta
.flags
& PUT_OBJ_CREATE
) != 0;
6925 const string
*ptag
= meta
.ptag
;
6926 if (!ptag
&& !index_op
->get_optag()->empty()) {
6927 ptag
= index_op
->get_optag();
6929 r
= target
->prepare_atomic_modification(op
, reset_obj
, ptag
, meta
.if_match
, meta
.if_nomatch
, false, modify_tail
);
6933 if (real_clock::is_zero(meta
.set_mtime
)) {
6934 meta
.set_mtime
= real_clock::now();
6937 if (state
->is_olh
) {
6938 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, state
->olh_tag
);
6941 struct timespec mtime_ts
= real_clock::to_timespec(meta
.set_mtime
);
6942 op
.mtime2(&mtime_ts
);
6945 /* if we want to overwrite the data, we also want to overwrite the
6946 xattrs, so just remove the object */
6947 op
.write_full(*meta
.data
);
6951 string content_type
;
6954 map
<string
, bufferlist
>::iterator iter
;
6956 for (iter
= meta
.rmattrs
->begin(); iter
!= meta
.rmattrs
->end(); ++iter
) {
6957 const string
& name
= iter
->first
;
6958 op
.rmxattr(name
.c_str());
6962 if (meta
.manifest
) {
6963 /* remove existing manifest attr */
6964 iter
= attrs
.find(RGW_ATTR_MANIFEST
);
6965 if (iter
!= attrs
.end())
6969 ::encode(*meta
.manifest
, bl
);
6970 op
.setxattr(RGW_ATTR_MANIFEST
, bl
);
6973 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6974 const string
& name
= iter
->first
;
6975 bufferlist
& bl
= iter
->second
;
6980 op
.setxattr(name
.c_str(), bl
);
6982 if (name
.compare(RGW_ATTR_ETAG
) == 0) {
6984 } else if (name
.compare(RGW_ATTR_CONTENT_TYPE
) == 0) {
6985 content_type
= bl
.c_str();
6986 } else if (name
.compare(RGW_ATTR_ACL
) == 0) {
6990 if (attrs
.find(RGW_ATTR_PG_VER
) == attrs
.end()) {
6991 cls_rgw_obj_store_pg_ver(op
, RGW_ATTR_PG_VER
);
6994 if (attrs
.find(RGW_ATTR_SOURCE_ZONE
) == attrs
.end()) {
6996 ::encode(store
->get_zone_short_id(), bl
);
6997 op
.setxattr(RGW_ATTR_SOURCE_ZONE
, bl
);
7008 if (!reset_obj
) { //Multipart upload, it has immutable head.
7009 orig_exists
= false;
7012 orig_exists
= state
->exists
;
7013 orig_size
= state
->accounted_size
;
7016 bool versioned_target
= (meta
.olh_epoch
> 0 || !obj
.key
.instance
.empty());
7018 bool versioned_op
= (target
->versioning_enabled() || is_olh
|| versioned_target
);
7021 index_op
->set_bilog_flags(RGW_BILOG_FLAG_VERSIONED_OP
);
7024 if (!index_op
->is_prepared()) {
7025 r
= index_op
->prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
7030 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7031 if (r
< 0) { /* we can expect to get -ECANCELED if object was replaced under,
7032 or -ENOENT if was removed, or -EEXIST if it did not exist
7033 before and now it does */
7034 if (r
== -EEXIST
&& assume_noent
) {
7035 target
->invalidate_state();
7041 epoch
= ref
.ioctx
.get_last_version();
7042 poolid
= ref
.ioctx
.get_id();
7044 r
= target
->complete_atomic_modification();
7046 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned r=" << r
<< dendl
;
7049 r
= index_op
->complete(poolid
, epoch
, size
, accounted_size
,
7050 meta
.set_mtime
, etag
, content_type
, &acl_bl
,
7051 meta
.category
, meta
.remove_objs
, meta
.user_data
);
7056 *meta
.mtime
= meta
.set_mtime
;
7059 /* note that index_op was using state so we couldn't invalidate it earlier */
7060 target
->invalidate_state();
7064 r
= store
->set_olh(target
->get_ctx(), target
->get_bucket_info(), obj
, false, NULL
, meta
.olh_epoch
, real_time(), false, meta
.zones_trace
);
7070 if (!real_clock::is_zero(meta
.delete_at
)) {
7071 rgw_obj_index_key obj_key
;
7072 obj
.key
.get_index_key(&obj_key
);
7074 r
= store
->objexp_hint_add(meta
.delete_at
,
7075 obj
.bucket
.tenant
, obj
.bucket
.name
, obj
.bucket
.bucket_id
, obj_key
);
7077 ldout(store
->ctx(), 0) << "ERROR: objexp_hint_add() returned r=" << r
<< ", object will not get removed" << dendl
;
7078 /* ignoring error, nothing we can do at this point */
7081 meta
.canceled
= false;
7083 /* update quota cache */
7084 if (meta
.completeMultipart
){
7085 store
->quota_handler
->update_stats(meta
.owner
, obj
.bucket
, (orig_exists
? 0 : 1),
7089 store
->quota_handler
->update_stats(meta
.owner
, obj
.bucket
, (orig_exists
? 0 : 1),
7090 accounted_size
, orig_size
);
7095 int ret
= index_op
->cancel();
7097 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel()() returned ret=" << ret
<< dendl
;
7100 meta
.canceled
= true;
7102 /* we lost in a race. There are a few options:
7103 * - existing object was rewritten (ECANCELED)
7104 * - non existing object was created (EEXIST)
7105 * - object was removed (ENOENT)
7106 * should treat it as a success
7108 if (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
) {
7109 if (r
== -ECANCELED
|| r
== -ENOENT
|| r
== -EEXIST
) {
7113 if (meta
.if_match
!= NULL
) {
7114 // only overwrite existing object
7115 if (strcmp(meta
.if_match
, "*") == 0) {
7117 r
= -ERR_PRECONDITION_FAILED
;
7118 } else if (r
== -ECANCELED
) {
7124 if (meta
.if_nomatch
!= NULL
) {
7125 // only create a new object
7126 if (strcmp(meta
.if_nomatch
, "*") == 0) {
7128 r
= -ERR_PRECONDITION_FAILED
;
7129 } else if (r
== -ENOENT
) {
7139 int RGWRados::Object::Write::write_meta(uint64_t size
, uint64_t accounted_size
,
7140 map
<string
, bufferlist
>& attrs
)
7142 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
7144 RGWRados::Bucket
bop(target
->get_store(), bucket_info
);
7145 RGWRados::Bucket::UpdateIndex
index_op(&bop
, target
->get_obj());
7146 index_op
.set_zones_trace(meta
.zones_trace
);
7148 bool assume_noent
= (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
);
7151 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7153 assume_noent
= false;
7156 if (!assume_noent
) {
7157 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7162 /** Write/overwrite a system object. */
7163 int RGWRados::put_system_obj_impl(rgw_raw_obj
& obj
, uint64_t size
, real_time
*mtime
,
7164 map
<std::string
, bufferlist
>& attrs
, int flags
,
7166 RGWObjVersionTracker
*objv_tracker
,
7167 real_time set_mtime
/* 0 for don't set */)
7170 int r
= get_system_obj_ref(obj
, &ref
);
7174 ObjectWriteOperation op
;
7176 if (flags
& PUT_OBJ_EXCL
) {
7177 if (!(flags
& PUT_OBJ_CREATE
))
7179 op
.create(true); // exclusive create
7182 op
.set_op_flags2(LIBRADOS_OP_FLAG_FAILOK
);
7187 objv_tracker
->prepare_op_for_write(&op
);
7190 if (real_clock::is_zero(set_mtime
)) {
7191 set_mtime
= real_clock::now();
7194 struct timespec mtime_ts
= real_clock::to_timespec(set_mtime
);
7195 op
.mtime2(&mtime_ts
);
7196 op
.write_full(data
);
7200 for (map
<string
, bufferlist
>::iterator iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
7201 const string
& name
= iter
->first
;
7202 bufferlist
& bl
= iter
->second
;
7207 op
.setxattr(name
.c_str(), bl
);
7210 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7216 objv_tracker
->apply_write();
7226 int RGWRados::put_system_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7227 off_t ofs
, bool exclusive
,
7228 RGWObjVersionTracker
*objv_tracker
)
7231 int r
= get_system_obj_ref(obj
, &ref
);
7236 ObjectWriteOperation op
;
7242 objv_tracker
->prepare_op_for_write(&op
);
7249 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7254 objv_tracker
->apply_write();
7260 * Write/overwrite an object to the bucket storage.
7261 * bucket: the bucket to store the object in
7262 * obj: the object name/key
7263 * data: the object contents/value
7264 * offset: the offet to write to in the object
7265 * If this is -1, we will overwrite the whole object.
7266 * size: the amount of data to write (data must be this long)
7267 * attrs: all the given attrs are written to bucket storage for the given object
7268 * Returns: 0 on success, -ERR# otherwise.
7271 int RGWRados::aio_put_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7272 off_t ofs
, bool exclusive
,
7276 int r
= get_raw_obj_ref(obj
, &ref
);
7281 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
7284 ObjectWriteOperation op
;
7294 r
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
7301 int RGWRados::aio_wait(void *handle
)
7303 AioCompletion
*c
= (AioCompletion
*)handle
;
7305 int ret
= c
->get_return_value();
7310 bool RGWRados::aio_completed(void *handle
)
7312 AioCompletion
*c
= (AioCompletion
*)handle
;
7313 return c
->is_safe();
7316 class RGWRadosPutObj
: public RGWGetDataCB
7320 RGWPutObjDataProcessor
*filter
;
7321 boost::optional
<RGWPutObj_Compress
>& compressor
;
7322 CompressorRef
& plugin
;
7323 RGWPutObjProcessor_Atomic
*processor
;
7324 RGWOpStateSingleOp
*opstate
;
7325 void (*progress_cb
)(off_t
, void *);
7326 void *progress_data
;
7327 bufferlist extra_data_bl
;
7328 uint64_t extra_data_left
;
7330 map
<string
, bufferlist
> src_attrs
;
7332 RGWRadosPutObj(CephContext
* cct
,
7333 CompressorRef
& plugin
,
7334 boost::optional
<RGWPutObj_Compress
>& compressor
,
7335 RGWPutObjProcessor_Atomic
*p
,
7336 RGWOpStateSingleOp
*_ops
,
7337 void (*_progress_cb
)(off_t
, void *),
7338 void *_progress_data
) :
7341 compressor(compressor
),
7345 progress_cb(_progress_cb
),
7346 progress_data(_progress_data
),
7350 int process_attrs(void) {
7351 if (extra_data_bl
.length()) {
7353 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7354 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7358 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7360 src_attrs
.erase(RGW_ATTR_COMPRESSION
);
7361 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7364 if (plugin
&& src_attrs
.find(RGW_ATTR_CRYPT_MODE
) == src_attrs
.end()) {
7365 //do not compress if object is encrypted
7366 compressor
= boost::in_place(cct
, plugin
, filter
);
7367 filter
= &*compressor
;
7372 int handle_data(bufferlist
& bl
, off_t ofs
, off_t len
) override
{
7374 progress_cb(ofs
, progress_data
);
7376 if (extra_data_left
) {
7377 size_t extra_len
= bl
.length();
7378 if (extra_len
> extra_data_left
)
7379 extra_len
= extra_data_left
;
7382 bl
.splice(0, extra_len
, &extra
);
7383 extra_data_bl
.append(extra
);
7385 extra_data_left
-= extra_len
;
7386 if (extra_data_left
== 0) {
7387 int res
= process_attrs();
7391 if (bl
.length() == 0) {
7396 // adjust ofs based on extra_data_len, so the result is a logical offset
7397 // into the object data
7398 assert(uint64_t(ofs
) >= extra_data_len
);
7399 ofs
-= extra_data_len
;
7401 data_len
+= bl
.length();
7404 bool need_opstate
= true;
7407 void *handle
= NULL
;
7409 uint64_t size
= bl
.length();
7410 int ret
= filter
->handle_data(bl
, ofs
, &handle
, &obj
, &again
);
7414 if (need_opstate
&& opstate
) {
7415 /* need to update opstate repository with new state. This is ratelimited, so we're not
7416 * really doing it every time
7418 ret
= opstate
->renew_state();
7420 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): failed to renew op state ret=" << ret
<< dendl
;
7421 int r
= filter
->throttle_data(handle
, obj
, size
, false);
7423 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): processor->throttle_data() returned " << r
<< dendl
;
7425 /* could not renew state! might have been marked as cancelled */
7428 need_opstate
= false;
7431 ret
= filter
->throttle_data(handle
, obj
, size
, false);
7439 bufferlist
& get_extra_data() { return extra_data_bl
; }
7441 map
<string
, bufferlist
>& get_attrs() { return src_attrs
; }
7443 void set_extra_data_len(uint64_t len
) override
{
7444 extra_data_left
= len
;
7445 RGWGetDataCB::set_extra_data_len(len
);
7448 uint64_t get_data_len() {
7452 int complete(const string
& etag
, real_time
*mtime
, real_time set_mtime
,
7453 map
<string
, bufferlist
>& attrs
, real_time delete_at
, rgw_zone_set
*zones_trace
) {
7454 return processor
->complete(data_len
, etag
, mtime
, set_mtime
, attrs
, delete_at
, NULL
, NULL
, NULL
, zones_trace
);
7457 bool is_canceled() {
7458 return processor
->is_canceled();
7463 * prepare attrset depending on attrs_mod.
7465 static void set_copy_attrs(map
<string
, bufferlist
>& src_attrs
,
7466 map
<string
, bufferlist
>& attrs
,
7467 RGWRados::AttrsMod attrs_mod
)
7469 switch (attrs_mod
) {
7470 case RGWRados::ATTRSMOD_NONE
:
7473 case RGWRados::ATTRSMOD_REPLACE
:
7474 if (!attrs
[RGW_ATTR_ETAG
].length()) {
7475 attrs
[RGW_ATTR_ETAG
] = src_attrs
[RGW_ATTR_ETAG
];
7477 if (!attrs
[RGW_ATTR_TAIL_TAG
].length()) {
7478 auto ttiter
= src_attrs
.find(RGW_ATTR_TAIL_TAG
);
7479 if (ttiter
!= src_attrs
.end()) {
7480 attrs
[RGW_ATTR_TAIL_TAG
] = src_attrs
[RGW_ATTR_TAIL_TAG
];
7484 case RGWRados::ATTRSMOD_MERGE
:
7485 for (map
<string
, bufferlist
>::iterator it
= src_attrs
.begin(); it
!= src_attrs
.end(); ++it
) {
7486 if (attrs
.find(it
->first
) == attrs
.end()) {
7487 attrs
[it
->first
] = it
->second
;
7494 int RGWRados::rewrite_obj(RGWBucketInfo
& dest_bucket_info
, rgw_obj
& obj
)
7496 map
<string
, bufferlist
> attrset
;
7500 RGWObjectCtx
rctx(this);
7502 RGWRados::Object
op_target(this, dest_bucket_info
, rctx
, obj
);
7503 RGWRados::Object::Read
read_op(&op_target
);
7505 read_op
.params
.attrs
= &attrset
;
7506 read_op
.params
.lastmod
= &mtime
;
7507 read_op
.params
.obj_size
= &obj_size
;
7509 int ret
= read_op
.prepare();
7513 attrset
.erase(RGW_ATTR_ID_TAG
);
7514 attrset
.erase(RGW_ATTR_TAIL_TAG
);
7516 uint64_t max_chunk_size
;
7518 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, obj
, &max_chunk_size
);
7520 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << obj
.bucket
<< dendl
;
7524 return copy_obj_data(rctx
, dest_bucket_info
, read_op
, obj_size
- 1, obj
, obj
,
7525 max_chunk_size
, NULL
, mtime
, attrset
,
7526 RGW_OBJ_CATEGORY_MAIN
, 0, real_time(),
7527 (obj
.key
.instance
.empty() ? NULL
: &(obj
.key
.instance
)),
7531 struct obj_time_weight
{
7533 uint32_t zone_short_id
;
7535 bool high_precision
;
7537 obj_time_weight() : zone_short_id(0), pg_ver(0), high_precision(false) {}
7539 bool compare_low_precision(const obj_time_weight
& rhs
) {
7540 struct timespec l
= ceph::real_clock::to_timespec(mtime
);
7541 struct timespec r
= ceph::real_clock::to_timespec(rhs
.mtime
);
7550 if (zone_short_id
!= rhs
.zone_short_id
) {
7551 return (zone_short_id
< rhs
.zone_short_id
);
7553 return (pg_ver
< rhs
.pg_ver
);
7557 bool operator<(const obj_time_weight
& rhs
) {
7558 if (!high_precision
|| !rhs
.high_precision
) {
7559 return compare_low_precision(rhs
);
7561 if (mtime
> rhs
.mtime
) {
7564 if (mtime
< rhs
.mtime
) {
7567 if (zone_short_id
!= rhs
.zone_short_id
) {
7568 return (zone_short_id
< rhs
.zone_short_id
);
7570 return (pg_ver
< rhs
.pg_ver
);
7573 void init(const real_time
& _mtime
, uint32_t _short_id
, uint64_t _pg_ver
) {
7575 zone_short_id
= _short_id
;
7579 void init(RGWObjState
*state
) {
7580 mtime
= state
->mtime
;
7581 zone_short_id
= state
->zone_short_id
;
7582 pg_ver
= state
->pg_ver
;
7586 inline ostream
& operator<<(ostream
& out
, const obj_time_weight
&o
) {
7589 if (o
.zone_short_id
!= 0 || o
.pg_ver
!= 0) {
7590 out
<< "[zid=" << o
.zone_short_id
<< ", pgv=" << o
.pg_ver
<< "]";
7596 class RGWGetExtraDataCB
: public RGWGetDataCB
{
7597 bufferlist extra_data
;
7599 RGWGetExtraDataCB() {}
7600 int handle_data(bufferlist
& bl
, off_t bl_ofs
, off_t bl_len
) override
{
7601 if (extra_data
.length() < extra_data_len
) {
7602 off_t max
= extra_data_len
- extra_data
.length();
7606 bl
.splice(0, max
, &extra_data
);
7611 bufferlist
& get_extra_data() {
7616 int RGWRados::stat_remote_obj(RGWObjectCtx
& obj_ctx
,
7617 const rgw_user
& user_id
,
7618 const string
& client_id
,
7620 const string
& source_zone
,
7622 RGWBucketInfo
& src_bucket_info
,
7623 real_time
*src_mtime
,
7625 const real_time
*mod_ptr
,
7626 const real_time
*unmod_ptr
,
7627 bool high_precision_time
,
7628 const char *if_match
,
7629 const char *if_nomatch
,
7630 map
<string
, bufferlist
> *pattrs
,
7635 /* source is in a different zonegroup, copy from there */
7637 RGWRESTStreamRWRequest
*in_stream_req
;
7639 map
<string
, bufferlist
> src_attrs
;
7640 append_rand_alpha(cct
, tag
, tag
, 32);
7641 obj_time_weight set_mtime_weight
;
7642 set_mtime_weight
.high_precision
= high_precision_time
;
7645 if (source_zone
.empty()) {
7646 if (src_bucket_info
.zonegroup
.empty()) {
7647 /* source is in the master zonegroup */
7648 conn
= rest_master_conn
;
7650 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7651 if (iter
== zonegroup_conn_map
.end()) {
7652 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7655 conn
= iter
->second
;
7658 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7659 if (iter
== zone_conn_map
.end()) {
7660 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7663 conn
= iter
->second
;
7666 RGWGetExtraDataCB cb
;
7668 map
<string
, string
> req_headers
;
7669 real_time set_mtime
;
7671 const real_time
*pmod
= mod_ptr
;
7673 obj_time_weight dest_mtime_weight
;
7675 constexpr bool prepend_meta
= true;
7676 constexpr bool get_op
= true;
7677 constexpr bool rgwx_stat
= true;
7678 constexpr bool sync_manifest
= true;
7679 constexpr bool skip_decrypt
= true;
7680 int ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7681 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7682 prepend_meta
, get_op
, rgwx_stat
,
7683 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7688 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, psize
, req_headers
);
7693 bufferlist
& extra_data_bl
= cb
.get_extra_data();
7694 if (extra_data_bl
.length()) {
7696 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7697 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7701 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7703 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7707 *src_mtime
= set_mtime
;
7711 map
<string
, bufferlist
>::iterator iter
= src_attrs
.find(RGW_ATTR_ETAG
);
7712 if (iter
!= src_attrs
.end()) {
7713 bufferlist
& etagbl
= iter
->second
;
7714 *petag
= etagbl
.to_str();
7719 *pattrs
= src_attrs
;
7725 int RGWRados::fetch_remote_obj(RGWObjectCtx
& obj_ctx
,
7726 const rgw_user
& user_id
,
7727 const string
& client_id
,
7728 const string
& op_id
,
7729 bool record_op_state
,
7731 const string
& source_zone
,
7734 RGWBucketInfo
& dest_bucket_info
,
7735 RGWBucketInfo
& src_bucket_info
,
7736 real_time
*src_mtime
,
7738 const real_time
*mod_ptr
,
7739 const real_time
*unmod_ptr
,
7740 bool high_precision_time
,
7741 const char *if_match
,
7742 const char *if_nomatch
,
7745 map
<string
, bufferlist
>& attrs
,
7746 RGWObjCategory category
,
7748 real_time delete_at
,
7751 ceph::buffer::list
*petag
,
7752 void (*progress_cb
)(off_t
, void *),
7753 void *progress_data
,
7754 rgw_zone_set
*zones_trace
)
7756 /* source is in a different zonegroup, copy from there */
7758 RGWRESTStreamRWRequest
*in_stream_req
;
7761 append_rand_alpha(cct
, tag
, tag
, 32);
7762 obj_time_weight set_mtime_weight
;
7763 set_mtime_weight
.high_precision
= high_precision_time
;
7765 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
7766 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.key
.name
,
7767 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
7768 if (version_id
&& *version_id
!= "null") {
7769 processor
.set_version_id(*version_id
);
7771 processor
.set_olh_epoch(olh_epoch
);
7772 int ret
= processor
.prepare(this, NULL
);
7778 if (source_zone
.empty()) {
7779 if (dest_bucket_info
.zonegroup
.empty()) {
7780 /* source is in the master zonegroup */
7781 conn
= rest_master_conn
;
7783 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7784 if (iter
== zonegroup_conn_map
.end()) {
7785 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7788 conn
= iter
->second
;
7791 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7792 if (iter
== zone_conn_map
.end()) {
7793 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7796 conn
= iter
->second
;
7799 string obj_name
= dest_obj
.bucket
.name
+ "/" + dest_obj
.get_oid();
7801 RGWOpStateSingleOp
*opstate
= NULL
;
7803 if (record_op_state
) {
7804 opstate
= new RGWOpStateSingleOp(this, client_id
, op_id
, obj_name
);
7806 ret
= opstate
->set_state(RGWOpState::OPSTATE_IN_PROGRESS
);
7808 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7814 boost::optional
<RGWPutObj_Compress
> compressor
;
7815 CompressorRef plugin
;
7817 const auto& compression_type
= zone_params
.get_compression_type(
7818 dest_bucket_info
.placement_rule
);
7819 if (compression_type
!= "none") {
7820 plugin
= Compressor::create(cct
, compression_type
);
7822 ldout(cct
, 1) << "Cannot load plugin for compression type "
7823 << compression_type
<< dendl
;
7827 RGWRadosPutObj
cb(cct
, plugin
, compressor
, &processor
, opstate
, progress_cb
, progress_data
);
7830 map
<string
, string
> req_headers
;
7831 real_time set_mtime
;
7833 RGWObjState
*dest_state
= NULL
;
7835 const real_time
*pmod
= mod_ptr
;
7837 obj_time_weight dest_mtime_weight
;
7839 if (copy_if_newer
) {
7840 /* need to get mtime for destination */
7841 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7845 if (!real_clock::is_zero(dest_state
->mtime
)) {
7846 dest_mtime_weight
.init(dest_state
);
7847 pmod
= &dest_mtime_weight
.mtime
;
7851 static constexpr bool prepend_meta
= true;
7852 static constexpr bool get_op
= true;
7853 static constexpr bool rgwx_stat
= false;
7854 static constexpr bool sync_manifest
= true;
7855 static constexpr bool skip_decrypt
= true;
7856 ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7857 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7858 prepend_meta
, get_op
, rgwx_stat
,
7859 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7864 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, nullptr, req_headers
);
7868 if (compressor
&& compressor
->is_compressed()) {
7870 RGWCompressionInfo cs_info
;
7871 cs_info
.compression_type
= plugin
->get_type_name();
7872 cs_info
.orig_size
= cb
.get_data_len();
7873 cs_info
.blocks
= move(compressor
->get_compression_blocks());
7874 ::encode(cs_info
, tmp
);
7875 cb
.get_attrs()[RGW_ATTR_COMPRESSION
] = tmp
;
7878 if (source_zone
.empty()) { /* need to preserve expiration if copy in the same zonegroup */
7879 cb
.get_attrs().erase(RGW_ATTR_DELETE_AT
);
7881 map
<string
, bufferlist
>::iterator iter
= cb
.get_attrs().find(RGW_ATTR_DELETE_AT
);
7882 if (iter
!= cb
.get_attrs().end()) {
7884 ::decode(delete_at
, iter
->second
);
7885 } catch (buffer::error
& err
) {
7886 ldout(cct
, 0) << "ERROR: failed to decode delete_at field in intra zone copy" << dendl
;
7892 *src_mtime
= set_mtime
;
7896 const auto iter
= cb
.get_attrs().find(RGW_ATTR_ETAG
);
7897 if (iter
!= cb
.get_attrs().end()) {
7898 *petag
= iter
->second
;
7902 if (source_zone
.empty()) {
7903 set_copy_attrs(cb
.get_attrs(), attrs
, attrs_mod
);
7905 attrs
= cb
.get_attrs();
7908 if (copy_if_newer
) {
7909 uint64_t pg_ver
= 0;
7910 auto i
= attrs
.find(RGW_ATTR_PG_VER
);
7911 if (i
!= attrs
.end() && i
->second
.length() > 0) {
7912 bufferlist::iterator iter
= i
->second
.begin();
7914 ::decode(pg_ver
, iter
);
7915 } catch (buffer::error
& err
) {
7916 ldout(ctx(), 0) << "ERROR: failed to decode pg ver attribute, ignoring" << dendl
;
7917 /* non critical error */
7920 set_mtime_weight
.init(set_mtime
, get_zone_short_id(), pg_ver
);
7923 #define MAX_COMPLETE_RETRY 100
7924 for (i
= 0; i
< MAX_COMPLETE_RETRY
; i
++) {
7925 ret
= cb
.complete(etag
, mtime
, set_mtime
, attrs
, delete_at
, zones_trace
);
7929 if (copy_if_newer
&& cb
.is_canceled()) {
7930 ldout(cct
, 20) << "raced with another write of obj: " << dest_obj
<< dendl
;
7931 obj_ctx
.obj
.invalidate(dest_obj
); /* object was overwritten */
7932 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7934 ldout(cct
, 0) << "ERROR: " << __func__
<< ": get_err_state() returned ret=" << ret
<< dendl
;
7937 dest_mtime_weight
.init(dest_state
);
7938 dest_mtime_weight
.high_precision
= high_precision_time
;
7939 if (!dest_state
->exists
||
7940 dest_mtime_weight
< set_mtime_weight
) {
7941 ldout(cct
, 20) << "retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7944 ldout(cct
, 20) << "not retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7950 if (i
== MAX_COMPLETE_RETRY
) {
7951 ldout(cct
, 0) << "ERROR: retried object completion too many times, something is wrong!" << dendl
;
7957 ret
= opstate
->set_state(RGWOpState::OPSTATE_COMPLETE
);
7959 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7966 if (copy_if_newer
&& ret
== -ERR_NOT_MODIFIED
) {
7970 RGWOpState::OpState state
;
7972 state
= RGWOpState::OPSTATE_ERROR
;
7974 state
= RGWOpState::OPSTATE_COMPLETE
;
7976 int r
= opstate
->set_state(state
);
7978 ldout(cct
, 0) << "ERROR: failed to set opstate r=" << ret
<< dendl
;
7986 int RGWRados::copy_obj_to_remote_dest(RGWObjState
*astate
,
7987 map
<string
, bufferlist
>& src_attrs
,
7988 RGWRados::Object::Read
& read_op
,
7989 const rgw_user
& user_id
,
7995 RGWRESTStreamWriteRequest
*out_stream_req
;
7997 int ret
= rest_master_conn
->put_obj_init(user_id
, dest_obj
, astate
->size
, src_attrs
, &out_stream_req
);
8002 ret
= read_op
.iterate(0, astate
->size
- 1, out_stream_req
->get_out_cb());
8004 delete out_stream_req
;
8008 ret
= rest_master_conn
->complete_request(out_stream_req
, etag
, mtime
);
8017 * dest_obj: the object to copy into
8018 * src_obj: the object to copy from
8019 * attrs: usage depends on attrs_mod parameter
8020 * attrs_mod: the modification mode of the attrs, may have the following values:
8021 * ATTRSMOD_NONE - the attributes of the source object will be
8022 * copied without modifications, attrs parameter is ignored;
8023 * ATTRSMOD_REPLACE - new object will have the attributes provided by attrs
8024 * parameter, source object attributes are not copied;
8025 * ATTRSMOD_MERGE - any conflicting meta keys on the source object's attributes
8026 * are overwritten by values contained in attrs parameter.
8027 * err: stores any errors resulting from the get of the original object
8028 * Returns: 0 on success, -ERR# otherwise.
8030 int RGWRados::copy_obj(RGWObjectCtx
& obj_ctx
,
8031 const rgw_user
& user_id
,
8032 const string
& client_id
,
8033 const string
& op_id
,
8035 const string
& source_zone
,
8038 RGWBucketInfo
& dest_bucket_info
,
8039 RGWBucketInfo
& src_bucket_info
,
8040 real_time
*src_mtime
,
8042 const real_time
*mod_ptr
,
8043 const real_time
*unmod_ptr
,
8044 bool high_precision_time
,
8045 const char *if_match
,
8046 const char *if_nomatch
,
8049 map
<string
, bufferlist
>& attrs
,
8050 RGWObjCategory category
,
8052 real_time delete_at
,
8055 ceph::buffer::list
*petag
,
8056 void (*progress_cb
)(off_t
, void *),
8057 void *progress_data
)
8061 rgw_obj shadow_obj
= dest_obj
;
8067 append_rand_alpha(cct
, dest_obj
.get_oid(), shadow_oid
, 32);
8068 shadow_obj
.init_ns(dest_obj
.bucket
, shadow_oid
, shadow_ns
);
8070 remote_dest
= !get_zonegroup().equals(dest_bucket_info
.zonegroup
);
8071 remote_src
= !get_zonegroup().equals(src_bucket_info
.zonegroup
);
8073 if (remote_src
&& remote_dest
) {
8074 ldout(cct
, 0) << "ERROR: can't copy object when both src and dest buckets are remote" << dendl
;
8078 ldout(cct
, 5) << "Copy object " << src_obj
.bucket
<< ":" << src_obj
.get_oid() << " => " << dest_obj
.bucket
<< ":" << dest_obj
.get_oid() << dendl
;
8080 if (remote_src
|| !source_zone
.empty()) {
8081 return fetch_remote_obj(obj_ctx
, user_id
, client_id
, op_id
, true, info
, source_zone
,
8082 dest_obj
, src_obj
, dest_bucket_info
, src_bucket_info
, src_mtime
, mtime
, mod_ptr
,
8083 unmod_ptr
, high_precision_time
,
8084 if_match
, if_nomatch
, attrs_mod
, copy_if_newer
, attrs
, category
,
8085 olh_epoch
, delete_at
, version_id
, ptag
, petag
, progress_cb
, progress_data
);
8088 map
<string
, bufferlist
> src_attrs
;
8089 RGWRados::Object
src_op_target(this, src_bucket_info
, obj_ctx
, src_obj
);
8090 RGWRados::Object::Read
read_op(&src_op_target
);
8092 read_op
.conds
.mod_ptr
= mod_ptr
;
8093 read_op
.conds
.unmod_ptr
= unmod_ptr
;
8094 read_op
.conds
.high_precision_time
= high_precision_time
;
8095 read_op
.conds
.if_match
= if_match
;
8096 read_op
.conds
.if_nomatch
= if_nomatch
;
8097 read_op
.params
.attrs
= &src_attrs
;
8098 read_op
.params
.lastmod
= src_mtime
;
8099 read_op
.params
.obj_size
= &obj_size
;
8101 ret
= read_op
.prepare();
8106 src_attrs
[RGW_ATTR_ACL
] = attrs
[RGW_ATTR_ACL
];
8107 src_attrs
.erase(RGW_ATTR_DELETE_AT
);
8109 set_copy_attrs(src_attrs
, attrs
, attrs_mod
);
8110 attrs
.erase(RGW_ATTR_ID_TAG
);
8111 attrs
.erase(RGW_ATTR_PG_VER
);
8112 attrs
.erase(RGW_ATTR_SOURCE_ZONE
);
8113 map
<string
, bufferlist
>::iterator cmp
= src_attrs
.find(RGW_ATTR_COMPRESSION
);
8114 if (cmp
!= src_attrs
.end())
8115 attrs
[RGW_ATTR_COMPRESSION
] = cmp
->second
;
8117 RGWObjManifest manifest
;
8118 RGWObjState
*astate
= NULL
;
8120 ret
= get_obj_state(&obj_ctx
, src_bucket_info
, src_obj
, &astate
);
8125 vector
<rgw_raw_obj
> ref_objs
;
8128 /* dest is in a different zonegroup, copy it there */
8129 return copy_obj_to_remote_dest(astate
, attrs
, read_op
, user_id
, dest_obj
, mtime
);
8131 uint64_t max_chunk_size
;
8133 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, dest_obj
, &max_chunk_size
);
8135 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << dest_obj
.bucket
<< dendl
;
8141 if (!get_obj_data_pool(src_bucket_info
.placement_rule
, src_obj
, &src_pool
)) {
8142 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << src_obj
<< dendl
;
8145 if (!get_obj_data_pool(dest_bucket_info
.placement_rule
, dest_obj
, &dest_pool
)) {
8146 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << dest_obj
<< dendl
;
8151 bool copy_data
= !astate
->has_manifest
|| (src_pool
!= dest_pool
);
8152 bool copy_first
= false;
8153 if (astate
->has_manifest
) {
8154 if (!astate
->manifest
.has_tail()) {
8157 uint64_t head_size
= astate
->manifest
.get_head_size();
8159 if (head_size
> 0) {
8160 if (head_size
> max_chunk_size
) {
8170 const auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8171 if (iter
!= attrs
.end()) {
8172 *petag
= iter
->second
;
8176 if (copy_data
) { /* refcounting tail wouldn't work here, just copy the data */
8177 return copy_obj_data(obj_ctx
, dest_bucket_info
, read_op
, obj_size
- 1, dest_obj
, src_obj
,
8178 max_chunk_size
, mtime
, real_time(), attrs
, category
, olh_epoch
, delete_at
,
8179 version_id
, ptag
, petag
);
8182 RGWObjManifest::obj_iterator miter
= astate
->manifest
.obj_begin();
8184 if (copy_first
) { // we need to copy first chunk, not increase refcount
8189 ret
= get_raw_obj_ref(miter
.get_location().get_raw_obj(this), &ref
);
8194 bool versioned_dest
= dest_bucket_info
.versioning_enabled();
8196 if (version_id
&& !version_id
->empty()) {
8197 versioned_dest
= true;
8198 dest_obj
.key
.set_instance(*version_id
);
8199 } else if (versioned_dest
) {
8200 gen_rand_obj_instance_name(&dest_obj
);
8203 bufferlist first_chunk
;
8205 bool copy_itself
= (dest_obj
== src_obj
);
8206 RGWObjManifest
*pmanifest
;
8207 ldout(cct
, 20) << "dest_obj=" << dest_obj
<< " src_obj=" << src_obj
<< " copy_itself=" << (int)copy_itself
<< dendl
;
8209 RGWRados::Object
dest_op_target(this, dest_bucket_info
, obj_ctx
, dest_obj
);
8210 RGWRados::Object::Write
write_op(&dest_op_target
);
8219 append_rand_alpha(cct
, tag
, tag
, 32);
8223 attrs
.erase(RGW_ATTR_TAIL_TAG
);
8224 manifest
= astate
->manifest
;
8225 const rgw_bucket_placement
& tail_placement
= manifest
.get_tail_placement();
8226 if (tail_placement
.bucket
.name
.empty()) {
8227 manifest
.set_tail_placement(tail_placement
.placement_rule
, src_obj
.bucket
);
8230 for (; miter
!= astate
->manifest
.obj_end(); ++miter
) {
8231 ObjectWriteOperation op
;
8232 ref_tag
= tag
+ '\0';
8233 cls_refcount_get(op
, ref_tag
, true);
8234 const rgw_raw_obj
& loc
= miter
.get_location().get_raw_obj(this);
8235 ref
.ioctx
.locator_set_key(loc
.loc
);
8237 ret
= ref
.ioctx
.operate(loc
.oid
, &op
);
8242 ref_objs
.push_back(loc
);
8245 pmanifest
= &manifest
;
8247 pmanifest
= &astate
->manifest
;
8248 /* don't send the object's tail for garbage collection */
8249 astate
->keep_tail
= true;
8253 ret
= read_op
.read(0, max_chunk_size
, first_chunk
);
8258 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, first_chunk
.length());
8260 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, 0);
8263 write_op
.meta
.data
= &first_chunk
;
8264 write_op
.meta
.manifest
= pmanifest
;
8265 write_op
.meta
.ptag
= &tag
;
8266 write_op
.meta
.owner
= dest_bucket_info
.owner
;
8267 write_op
.meta
.mtime
= mtime
;
8268 write_op
.meta
.flags
= PUT_OBJ_CREATE
;
8269 write_op
.meta
.category
= category
;
8270 write_op
.meta
.olh_epoch
= olh_epoch
;
8271 write_op
.meta
.delete_at
= delete_at
;
8272 write_op
.meta
.modify_tail
= !copy_itself
;
8274 ret
= write_op
.write_meta(obj_size
, astate
->accounted_size
, attrs
);
8283 vector
<rgw_raw_obj
>::iterator riter
;
8285 /* rollback reference */
8286 for (riter
= ref_objs
.begin(); riter
!= ref_objs
.end(); ++riter
) {
8287 ObjectWriteOperation op
;
8288 cls_refcount_put(op
, tag
, true);
8290 ref
.ioctx
.locator_set_key(riter
->loc
);
8292 int r
= ref
.ioctx
.operate(riter
->oid
, &op
);
8294 ldout(cct
, 0) << "ERROR: cleanup after error failed to drop reference on obj=" << *riter
<< dendl
;
8302 int RGWRados::copy_obj_data(RGWObjectCtx
& obj_ctx
,
8303 RGWBucketInfo
& dest_bucket_info
,
8304 RGWRados::Object::Read
& read_op
, off_t end
,
8307 uint64_t max_chunk_size
,
8309 real_time set_mtime
,
8310 map
<string
, bufferlist
>& attrs
,
8311 RGWObjCategory category
,
8313 real_time delete_at
,
8316 ceph::buffer::list
*petag
)
8318 bufferlist first_chunk
;
8319 RGWObjManifest manifest
;
8322 append_rand_alpha(cct
, tag
, tag
, 32);
8324 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
8325 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.key
.name
,
8326 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
8328 processor
.set_version_id(*version_id
);
8330 processor
.set_olh_epoch(olh_epoch
);
8331 int ret
= processor
.prepare(this, NULL
);
8339 ret
= read_op
.read(ofs
, end
, bl
);
8341 uint64_t read_len
= ret
;
8348 ret
= processor
.handle_data(bl
, ofs
, &handle
, &obj
, &again
);
8352 ret
= processor
.throttle_data(handle
, obj
, read_len
, false);
8358 } while (ofs
<= end
);
8361 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8362 if (iter
!= attrs
.end()) {
8363 bufferlist
& bl
= iter
->second
;
8364 etag
= string(bl
.c_str(), bl
.length());
8370 uint64_t accounted_size
;
8372 bool compressed
{false};
8373 RGWCompressionInfo cs_info
;
8374 ret
= rgw_compression_info_from_attrset(attrs
, compressed
, cs_info
);
8376 ldout(cct
, 0) << "ERROR: failed to read compression info" << dendl
;
8379 // pass original size if compressed
8380 accounted_size
= compressed
? cs_info
.orig_size
: ofs
;
8383 return processor
.complete(accounted_size
, etag
, mtime
, set_mtime
, attrs
, delete_at
);
8386 bool RGWRados::is_meta_master()
8388 if (!get_zonegroup().is_master_zonegroup()) {
8392 return (get_zonegroup().master_zone
== zone_public_config
.id
);
8396 * Check to see if the bucket metadata could be synced
8397 * bucket: the bucket to check
8398 * Returns false is the bucket is not synced
8400 bool RGWRados::is_syncing_bucket_meta(const rgw_bucket
& bucket
)
8403 /* no current period */
8404 if (current_period
.get_id().empty()) {
8408 /* zonegroup is not master zonegroup */
8409 if (!get_zonegroup().is_master_zonegroup()) {
8413 /* single zonegroup and a single zone */
8414 if (current_period
.is_single_zonegroup() && get_zonegroup().zones
.size() == 1) {
8418 /* zone is not master */
8419 if (get_zonegroup().master_zone
.compare(zone_public_config
.id
) != 0) {
8426 int RGWRados::check_bucket_empty(RGWBucketInfo
& bucket_info
)
8428 std::map
<string
, rgw_bucket_dir_entry
> ent_map
;
8429 rgw_obj_index_key marker
;
8434 #define NUM_ENTRIES 1000
8435 int r
= cls_bucket_list(bucket_info
, RGW_NO_SHARD
, marker
, prefix
, NUM_ENTRIES
, true, ent_map
,
8436 &is_truncated
, &marker
);
8441 std::map
<string
, rgw_bucket_dir_entry
>::iterator eiter
;
8442 for (eiter
= ent_map
.begin(); eiter
!= ent_map
.end(); ++eiter
) {
8445 if (rgw_obj_key::oid_to_key_in_ns(eiter
->second
.key
.name
, &obj
, ns
))
8448 } while (is_truncated
);
8454 * bucket: the name of the bucket to delete
8455 * Returns 0 on success, -ERR# otherwise.
8457 int RGWRados::delete_bucket(RGWBucketInfo
& bucket_info
, RGWObjVersionTracker
& objv_tracker
, bool check_empty
)
8459 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8460 librados::IoCtx index_ctx
;
8461 map
<int, string
> bucket_objs
;
8462 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8467 r
= check_bucket_empty(bucket_info
);
8473 r
= rgw_bucket_delete_bucket_obj(this, bucket
.tenant
, bucket
.name
, objv_tracker
);
8477 /* if the bucket is not synced we can remove the meta file */
8478 if (!is_syncing_bucket_meta(bucket
)) {
8479 RGWObjVersionTracker objv_tracker
;
8480 string entry
= bucket
.get_key();
8481 r
= rgw_bucket_instance_remove_entry(this, entry
, &objv_tracker
);
8485 /* remove bucket index objects*/
8486 map
<int, string
>::const_iterator biter
;
8487 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
8488 index_ctx
.remove(biter
->second
);
8494 int RGWRados::set_bucket_owner(rgw_bucket
& bucket
, ACLOwner
& owner
)
8497 map
<string
, bufferlist
> attrs
;
8498 RGWObjectCtx
obj_ctx(this);
8500 if (bucket
.bucket_id
.empty()) {
8501 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8503 r
= get_bucket_instance_info(obj_ctx
, bucket
, info
, nullptr, &attrs
);
8506 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8510 info
.owner
= owner
.get_id();
8512 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8514 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8522 int RGWRados::set_buckets_enabled(vector
<rgw_bucket
>& buckets
, bool enabled
)
8526 vector
<rgw_bucket
>::iterator iter
;
8528 for (iter
= buckets
.begin(); iter
!= buckets
.end(); ++iter
) {
8529 rgw_bucket
& bucket
= *iter
;
8531 ldout(cct
, 20) << "enabling bucket name=" << bucket
.name
<< dendl
;
8533 ldout(cct
, 20) << "disabling bucket name=" << bucket
.name
<< dendl
;
8536 map
<string
, bufferlist
> attrs
;
8537 RGWObjectCtx
obj_ctx(this);
8538 int r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8540 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8545 info
.flags
&= ~BUCKET_SUSPENDED
;
8547 info
.flags
|= BUCKET_SUSPENDED
;
8550 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8552 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8560 int RGWRados::bucket_suspended(rgw_bucket
& bucket
, bool *suspended
)
8562 RGWBucketInfo bucket_info
;
8563 RGWObjectCtx
obj_ctx(this);
8564 int ret
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, bucket_info
, NULL
);
8569 *suspended
= ((bucket_info
.flags
& BUCKET_SUSPENDED
) != 0);
8573 int RGWRados::Object::complete_atomic_modification()
8575 if (!state
->has_manifest
|| state
->keep_tail
)
8578 cls_rgw_obj_chain chain
;
8579 store
->update_gc_chain(obj
, state
->manifest
, &chain
);
8581 if (chain
.empty()) {
8585 string tag
= (state
->tail_tag
.length() > 0 ? state
->tail_tag
.to_str() : state
->obj_tag
.to_str());
8586 return store
->gc
->send_chain(chain
, tag
, false); // do it async
8589 void RGWRados::update_gc_chain(rgw_obj
& head_obj
, RGWObjManifest
& manifest
, cls_rgw_obj_chain
*chain
)
8591 RGWObjManifest::obj_iterator iter
;
8592 rgw_raw_obj raw_head
;
8593 obj_to_raw(manifest
.get_head_placement_rule(), head_obj
, &raw_head
);
8594 for (iter
= manifest
.obj_begin(); iter
!= manifest
.obj_end(); ++iter
) {
8595 const rgw_raw_obj
& mobj
= iter
.get_location().get_raw_obj(this);
8596 if (mobj
== raw_head
)
8598 cls_rgw_obj_key
key(mobj
.oid
);
8599 chain
->push_obj(mobj
.pool
.to_str(), key
, mobj
.loc
);
8603 int RGWRados::send_chain_to_gc(cls_rgw_obj_chain
& chain
, const string
& tag
, bool sync
)
8605 return gc
->send_chain(chain
, tag
, sync
);
8608 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
, string
& bucket_oid
)
8610 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8611 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8615 if (bucket
.bucket_id
.empty()) {
8616 ldout(cct
, 0) << "ERROR: empty bucket id for bucket operation" << dendl
;
8620 bucket_oid
= dir_oid_prefix
;
8621 bucket_oid
.append(bucket
.bucket_id
);
8626 int RGWRados::open_bucket_index_base(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8627 string
& bucket_oid_base
) {
8628 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8629 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8633 if (bucket
.bucket_id
.empty()) {
8634 ldout(cct
, 0) << "ERROR: empty bucket_id for bucket operation" << dendl
;
8638 bucket_oid_base
= dir_oid_prefix
;
8639 bucket_oid_base
.append(bucket
.bucket_id
);
8645 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8646 map
<int, string
>& bucket_objs
, int shard_id
, map
<int, string
> *bucket_instance_ids
) {
8647 string bucket_oid_base
;
8648 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8653 get_bucket_index_objects(bucket_oid_base
, bucket_info
.num_shards
, bucket_objs
, shard_id
);
8654 if (bucket_instance_ids
) {
8655 get_bucket_instance_ids(bucket_info
, shard_id
, bucket_instance_ids
);
8660 template<typename T
>
8661 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8662 map
<int, string
>& oids
, map
<int, T
>& bucket_objs
,
8663 int shard_id
, map
<int, string
> *bucket_instance_ids
)
8665 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, bucket_instance_ids
);
8669 map
<int, string
>::const_iterator iter
= oids
.begin();
8670 for (; iter
!= oids
.end(); ++iter
) {
8671 bucket_objs
[iter
->first
] = T();
8676 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8677 const string
& obj_key
, string
*bucket_obj
, int *shard_id
)
8679 string bucket_oid_base
;
8680 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8684 RGWObjectCtx
obj_ctx(this);
8686 ret
= get_bucket_index_object(bucket_oid_base
, obj_key
, bucket_info
.num_shards
,
8687 (RGWBucketInfo::BIShardsHashType
)bucket_info
.bucket_index_shard_hash_type
, bucket_obj
, shard_id
);
8689 ldout(cct
, 10) << "get_bucket_index_object() returned ret=" << ret
<< dendl
;
8695 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8696 int shard_id
, string
*bucket_obj
)
8698 string bucket_oid_base
;
8699 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8703 RGWObjectCtx
obj_ctx(this);
8705 get_bucket_index_object(bucket_oid_base
, bucket_info
.num_shards
,
8706 shard_id
, bucket_obj
);
8710 static void accumulate_raw_stats(const rgw_bucket_dir_header
& header
,
8711 map
<RGWObjCategory
, RGWStorageStats
>& stats
)
8713 for (const auto& pair
: header
.stats
) {
8714 const RGWObjCategory category
= static_cast<RGWObjCategory
>(pair
.first
);
8715 const rgw_bucket_category_stats
& header_stats
= pair
.second
;
8717 RGWStorageStats
& s
= stats
[category
];
8719 s
.category
= category
;
8720 s
.size
+= header_stats
.total_size
;
8721 s
.size_rounded
+= header_stats
.total_size_rounded
;
8722 s
.size_utilized
+= header_stats
.actual_size
;
8723 s
.num_objects
+= header_stats
.num_entries
;
8727 int RGWRados::bucket_check_index(RGWBucketInfo
& bucket_info
,
8728 map
<RGWObjCategory
, RGWStorageStats
> *existing_stats
,
8729 map
<RGWObjCategory
, RGWStorageStats
> *calculated_stats
)
8731 librados::IoCtx index_ctx
;
8732 // key - bucket index object id
8733 // value - bucket index check OP returned result with the given bucket index object (shard)
8734 map
<int, string
> oids
;
8735 map
<int, struct rgw_cls_check_index_ret
> bucket_objs_ret
;
8737 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, bucket_objs_ret
);
8742 ret
= CLSRGWIssueBucketCheck(index_ctx
, oids
, bucket_objs_ret
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8747 // Aggregate results (from different shards if there is any)
8748 map
<int, struct rgw_cls_check_index_ret
>::iterator iter
;
8749 for (iter
= bucket_objs_ret
.begin(); iter
!= bucket_objs_ret
.end(); ++iter
) {
8750 accumulate_raw_stats(iter
->second
.existing_header
, *existing_stats
);
8751 accumulate_raw_stats(iter
->second
.calculated_header
, *calculated_stats
);
8757 int RGWRados::bucket_rebuild_index(RGWBucketInfo
& bucket_info
)
8759 librados::IoCtx index_ctx
;
8760 map
<int, string
> bucket_objs
;
8762 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8767 return CLSRGWIssueBucketRebuild(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8770 int RGWRados::bucket_set_reshard(RGWBucketInfo
& bucket_info
, const cls_rgw_bucket_instance_entry
& entry
)
8772 librados::IoCtx index_ctx
;
8773 map
<int, string
> bucket_objs
;
8775 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8780 return CLSRGWIssueSetBucketResharding(index_ctx
, bucket_objs
, entry
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8783 int RGWRados::defer_gc(void *ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
)
8785 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
8786 std::string oid
, key
;
8787 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
8791 RGWObjState
*state
= NULL
;
8793 int r
= get_obj_state(rctx
, bucket_info
, obj
, &state
, false);
8797 if (!state
->is_atomic
) {
8798 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not deferring gc operation" << dendl
;
8804 if (state
->tail_tag
.length() > 0) {
8805 tag
= state
->tail_tag
.c_str();
8806 } else if (state
->obj_tag
.length() > 0) {
8807 tag
= state
->obj_tag
.c_str();
8809 ldout(cct
, 20) << "state->obj_tag is empty, not deferring gc operation" << dendl
;
8813 ldout(cct
, 0) << "defer chain tag=" << tag
<< dendl
;
8815 return gc
->defer_chain(tag
, false);
8818 void RGWRados::remove_rgw_head_obj(ObjectWriteOperation
& op
)
8820 list
<string
> prefixes
;
8821 prefixes
.push_back(RGW_ATTR_OLH_PREFIX
);
8822 cls_rgw_remove_obj(op
, prefixes
);
8825 void RGWRados::cls_obj_check_prefix_exist(ObjectOperation
& op
, const string
& prefix
, bool fail_if_exist
)
8827 cls_rgw_obj_check_attrs_prefix(op
, prefix
, fail_if_exist
);
8830 void RGWRados::cls_obj_check_mtime(ObjectOperation
& op
, const real_time
& mtime
, bool high_precision_time
, RGWCheckMTimeType type
)
8832 cls_rgw_obj_check_mtime(op
, mtime
, high_precision_time
, type
);
8838 * bucket: name of the bucket storing the object
8839 * obj: name of the object to delete
8840 * Returns: 0 on success, -ERR# otherwise.
8842 int RGWRados::Object::Delete::delete_obj()
8844 RGWRados
*store
= target
->get_store();
8845 rgw_obj
& src_obj
= target
->get_obj();
8846 const string
& instance
= src_obj
.key
.instance
;
8847 rgw_obj obj
= src_obj
;
8849 if (instance
== "null") {
8850 obj
.key
.instance
.clear();
8853 bool explicit_marker_version
= (!params
.marker_version_id
.empty());
8855 if (params
.versioning_status
& BUCKET_VERSIONED
|| explicit_marker_version
) {
8856 if (instance
.empty() || explicit_marker_version
) {
8857 rgw_obj marker
= obj
;
8859 if (!params
.marker_version_id
.empty()) {
8860 if (params
.marker_version_id
!= "null") {
8861 marker
.key
.set_instance(params
.marker_version_id
);
8863 } else if ((params
.versioning_status
& BUCKET_VERSIONS_SUSPENDED
) == 0) {
8864 store
->gen_rand_obj_instance_name(&marker
);
8867 result
.version_id
= marker
.key
.instance
;
8868 result
.delete_marker
= true;
8870 struct rgw_bucket_dir_entry_meta meta
;
8872 meta
.owner
= params
.obj_owner
.get_id().to_str();
8873 meta
.owner_display_name
= params
.obj_owner
.get_display_name();
8875 if (real_clock::is_zero(params
.mtime
)) {
8876 meta
.mtime
= real_clock::now();
8878 meta
.mtime
= params
.mtime
;
8881 int r
= store
->set_olh(target
->get_ctx(), target
->get_bucket_info(), marker
, true, &meta
, params
.olh_epoch
, params
.unmod_since
, params
.high_precision_time
, params
.zones_trace
);
8886 rgw_bucket_dir_entry dirent
;
8888 int r
= store
->bi_get_instance(target
->get_bucket_info(), obj
, &dirent
);
8892 result
.delete_marker
= dirent
.is_delete_marker();
8893 r
= store
->unlink_obj_instance(target
->get_ctx(), target
->get_bucket_info(), obj
, params
.olh_epoch
, params
.zones_trace
);
8897 result
.version_id
= instance
;
8901 int r
= target
->get_bucket_shard(&bs
);
8903 ldout(store
->ctx(), 5) << "failed to get BucketShard object: r=" << r
<< dendl
;
8907 if (target
->bucket_info
.datasync_flag_enabled()) {
8908 r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
8910 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
8919 int r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
8925 r
= target
->get_state(&state
, false);
8929 ObjectWriteOperation op
;
8931 if (!real_clock::is_zero(params
.unmod_since
)) {
8932 struct timespec ctime
= ceph::real_clock::to_timespec(state
->mtime
);
8933 struct timespec unmod
= ceph::real_clock::to_timespec(params
.unmod_since
);
8934 if (!params
.high_precision_time
) {
8939 ldout(store
->ctx(), 10) << "If-UnModified-Since: " << params
.unmod_since
<< " Last-Modified: " << ctime
<< dendl
;
8940 if (ctime
> unmod
) {
8941 return -ERR_PRECONDITION_FAILED
;
8944 /* only delete object if mtime is less than or equal to params.unmod_since */
8945 store
->cls_obj_check_mtime(op
, params
.unmod_since
, params
.high_precision_time
, CLS_RGW_CHECK_TIME_MTIME_LE
);
8947 uint64_t obj_size
= state
->size
;
8949 if (!real_clock::is_zero(params
.expiration_time
)) {
8951 real_time delete_at
;
8953 if (state
->get_attr(RGW_ATTR_DELETE_AT
, bl
)) {
8955 bufferlist::iterator iter
= bl
.begin();
8956 ::decode(delete_at
, iter
);
8957 } catch (buffer::error
& err
) {
8958 ldout(store
->ctx(), 0) << "ERROR: couldn't decode RGW_ATTR_DELETE_AT" << dendl
;
8962 if (params
.expiration_time
!= delete_at
) {
8963 return -ERR_PRECONDITION_FAILED
;
8966 return -ERR_PRECONDITION_FAILED
;
8970 if (!state
->exists
) {
8971 target
->invalidate_state();
8975 r
= target
->prepare_atomic_modification(op
, false, NULL
, NULL
, NULL
, true, false);
8979 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
8981 RGWRados::Bucket
bop(store
, bucket_info
);
8982 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
8984 index_op
.set_zones_trace(params
.zones_trace
);
8985 index_op
.set_bilog_flags(params
.bilog_flags
);
8987 r
= index_op
.prepare(CLS_RGW_OP_DEL
, &state
->write_tag
);
8991 store
->remove_rgw_head_obj(op
);
8992 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
8993 bool need_invalidate
= false;
8994 if (r
== -ECANCELED
) {
8995 /* raced with another operation, we can regard it as removed */
8996 need_invalidate
= true;
9000 int64_t poolid
= ref
.ioctx
.get_id();
9002 tombstone_cache_t
*obj_tombstone_cache
= store
->get_tombstone_cache();
9003 if (obj_tombstone_cache
) {
9004 tombstone_entry entry
{*state
};
9005 obj_tombstone_cache
->add(obj
, entry
);
9007 r
= index_op
.complete_del(poolid
, ref
.ioctx
.get_last_version(), state
->mtime
, params
.remove_objs
);
9009 int ret
= target
->complete_atomic_modification();
9011 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned ret=" << ret
<< dendl
;
9013 /* other than that, no need to propagate error */
9015 int ret
= index_op
.cancel();
9017 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel() returned ret=" << ret
<< dendl
;
9021 if (need_invalidate
) {
9022 target
->invalidate_state();
9028 /* update quota cache */
9029 store
->quota_handler
->update_stats(params
.bucket_owner
, obj
.bucket
, -1, 0, obj_size
);
9034 int RGWRados::delete_obj(RGWObjectCtx
& obj_ctx
,
9035 const RGWBucketInfo
& bucket_info
,
9037 int versioning_status
,
9038 uint16_t bilog_flags
,
9039 const real_time
& expiration_time
,
9040 rgw_zone_set
*zones_trace
)
9042 RGWRados::Object
del_target(this, bucket_info
, obj_ctx
, obj
);
9043 RGWRados::Object::Delete
del_op(&del_target
);
9045 del_op
.params
.bucket_owner
= bucket_info
.owner
;
9046 del_op
.params
.versioning_status
= versioning_status
;
9047 del_op
.params
.bilog_flags
= bilog_flags
;
9048 del_op
.params
.expiration_time
= expiration_time
;
9049 del_op
.params
.zones_trace
= zones_trace
;
9051 return del_op
.delete_obj();
9054 int RGWRados::delete_raw_obj(const rgw_raw_obj
& obj
)
9057 int r
= get_raw_obj_ref(obj
, &ref
);
9062 ObjectWriteOperation op
;
9065 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9072 int RGWRados::delete_system_obj(rgw_raw_obj
& obj
, RGWObjVersionTracker
*objv_tracker
)
9075 ldout(cct
, 1) << "delete_system_obj got empty object name "
9076 << obj
<< ", returning EINVAL" << dendl
;
9080 int r
= get_raw_obj_ref(obj
, &ref
);
9085 ObjectWriteOperation op
;
9088 objv_tracker
->prepare_op_for_write(&op
);
9092 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9099 int RGWRados::delete_obj_index(const rgw_obj
& obj
)
9101 std::string oid
, key
;
9102 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
9104 RGWObjectCtx
obj_ctx(this);
9106 RGWBucketInfo bucket_info
;
9107 int ret
= get_bucket_instance_info(obj_ctx
, obj
.bucket
, bucket_info
, NULL
, NULL
);
9109 ldout(cct
, 0) << "ERROR: " << __func__
<< "() get_bucket_instance_info(bucket=" << obj
.bucket
<< ") returned ret=" << ret
<< dendl
;
9113 RGWRados::Bucket
bop(this, bucket_info
);
9114 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9116 real_time removed_mtime
;
9117 int r
= index_op
.complete_del(-1 /* pool */, 0, removed_mtime
, NULL
);
9122 static void generate_fake_tag(RGWRados
*store
, map
<string
, bufferlist
>& attrset
, RGWObjManifest
& manifest
, bufferlist
& manifest_bl
, bufferlist
& tag_bl
)
9126 RGWObjManifest::obj_iterator mi
= manifest
.obj_begin();
9127 if (mi
!= manifest
.obj_end()) {
9128 if (manifest
.has_tail()) // first object usually points at the head, let's skip to a more unique part
9130 tag
= mi
.get_location().get_raw_obj(store
).oid
;
9134 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
9135 char md5_str
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
9137 hash
.Update((const byte
*)manifest_bl
.c_str(), manifest_bl
.length());
9139 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_ETAG
);
9140 if (iter
!= attrset
.end()) {
9141 bufferlist
& bl
= iter
->second
;
9142 hash
.Update((const byte
*)bl
.c_str(), bl
.length());
9146 buf_to_hex(md5
, CEPH_CRYPTO_MD5_DIGESTSIZE
, md5_str
);
9147 tag
.append(md5_str
);
9149 ldout(store
->ctx(), 10) << "generate_fake_tag new tag=" << tag
<< dendl
;
9151 tag_bl
.append(tag
.c_str(), tag
.size() + 1);
9154 static bool is_olh(map
<string
, bufferlist
>& attrs
)
9156 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_INFO
);
9157 return (iter
!= attrs
.end());
9160 static bool has_olh_tag(map
<string
, bufferlist
>& attrs
)
9162 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_ID_TAG
);
9163 return (iter
!= attrs
.end());
9166 int RGWRados::get_olh_target_state(RGWObjectCtx
& obj_ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9167 RGWObjState
*olh_state
, RGWObjState
**target_state
)
9169 assert(olh_state
->is_olh
);
9172 int r
= RGWRados::follow_olh(bucket_info
, obj_ctx
, olh_state
, obj
, &target
); /* might return -EAGAIN */
9176 r
= get_obj_state(&obj_ctx
, bucket_info
, target
, target_state
, false);
9184 int RGWRados::get_system_obj_state_impl(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9190 RGWRawObjState
*s
= rctx
->raw
.get_state(obj
);
9191 ldout(cct
, 20) << "get_system_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9199 int r
= raw_obj_stat(obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), objv_tracker
);
9202 s
->has_attrs
= true;
9203 s
->mtime
= real_time();
9210 s
->has_attrs
= true;
9211 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9213 if (s
->obj_tag
.length())
9214 ldout(cct
, 20) << "get_system_obj_state: setting s->obj_tag to "
9215 << s
->obj_tag
.c_str() << dendl
;
9217 ldout(cct
, 20) << "get_system_obj_state: s->obj_tag was set empty" << dendl
;
9222 int RGWRados::get_system_obj_state(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9227 ret
= get_system_obj_state_impl(rctx
, obj
, state
, objv_tracker
);
9228 } while (ret
== -EAGAIN
);
9233 int RGWRados::get_obj_state_impl(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9234 RGWObjState
**state
, bool follow_olh
, bool assume_noent
)
9240 bool need_follow_olh
= follow_olh
&& obj
.key
.instance
.empty();
9242 RGWObjState
*s
= rctx
->obj
.get_state(obj
);
9243 ldout(cct
, 20) << "get_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9246 if (s
->is_olh
&& need_follow_olh
) {
9247 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9254 rgw_raw_obj raw_obj
;
9255 obj_to_raw(bucket_info
.placement_rule
, obj
, &raw_obj
);
9259 if (!assume_noent
) {
9260 r
= RGWRados::raw_obj_stat(raw_obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), NULL
);
9265 s
->has_attrs
= true;
9266 tombstone_entry entry
;
9267 if (obj_tombstone_cache
&& obj_tombstone_cache
->find(obj
, entry
)) {
9268 s
->mtime
= entry
.mtime
;
9269 s
->zone_short_id
= entry
.zone_short_id
;
9270 s
->pg_ver
= entry
.pg_ver
;
9271 ldout(cct
, 20) << __func__
<< "(): found obj in tombstone cache: obj=" << obj
9272 << " mtime=" << s
->mtime
<< " pgv=" << s
->pg_ver
<< dendl
;
9274 s
->mtime
= real_time();
9282 s
->has_attrs
= true;
9283 s
->accounted_size
= s
->size
;
9285 auto iter
= s
->attrset
.find(RGW_ATTR_COMPRESSION
);
9286 const bool compressed
= (iter
!= s
->attrset
.end());
9288 // use uncompressed size for accounted_size
9290 RGWCompressionInfo info
;
9291 auto p
= iter
->second
.begin();
9293 s
->accounted_size
= info
.orig_size
;
9294 } catch (buffer::error
&) {
9295 dout(0) << "ERROR: could not decode compression info for object: " << obj
<< dendl
;
9300 iter
= s
->attrset
.find(RGW_ATTR_SHADOW_OBJ
);
9301 if (iter
!= s
->attrset
.end()) {
9302 bufferlist bl
= iter
->second
;
9303 bufferlist::iterator it
= bl
.begin();
9304 it
.copy(bl
.length(), s
->shadow_obj
);
9305 s
->shadow_obj
[bl
.length()] = '\0';
9307 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9308 auto ttiter
= s
->attrset
.find(RGW_ATTR_TAIL_TAG
);
9309 if (ttiter
!= s
->attrset
.end()) {
9310 s
->tail_tag
= s
->attrset
[RGW_ATTR_TAIL_TAG
];
9313 bufferlist manifest_bl
= s
->attrset
[RGW_ATTR_MANIFEST
];
9314 if (manifest_bl
.length()) {
9315 bufferlist::iterator miter
= manifest_bl
.begin();
9317 ::decode(s
->manifest
, miter
);
9318 s
->has_manifest
= true;
9319 s
->manifest
.set_head(bucket_info
.placement_rule
, obj
, s
->size
); /* patch manifest to reflect the head we just read, some manifests might be
9320 broken due to old bugs */
9321 s
->size
= s
->manifest
.get_obj_size();
9323 s
->accounted_size
= s
->size
;
9324 } catch (buffer::error
& err
) {
9325 ldout(cct
, 0) << "ERROR: couldn't decode manifest" << dendl
;
9328 ldout(cct
, 10) << "manifest: total_size = " << s
->manifest
.get_obj_size() << dendl
;
9329 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20) && s
->manifest
.has_explicit_objs()) {
9330 RGWObjManifest::obj_iterator mi
;
9331 for (mi
= s
->manifest
.obj_begin(); mi
!= s
->manifest
.obj_end(); ++mi
) {
9332 ldout(cct
, 20) << "manifest: ofs=" << mi
.get_ofs() << " loc=" << mi
.get_location().get_raw_obj(this) << dendl
;
9336 if (!s
->obj_tag
.length()) {
9338 * Uh oh, something's wrong, object with manifest should have tag. Let's
9339 * create one out of the manifest, would be unique
9341 generate_fake_tag(this, s
->attrset
, s
->manifest
, manifest_bl
, s
->obj_tag
);
9345 map
<string
, bufferlist
>::iterator aiter
= s
->attrset
.find(RGW_ATTR_PG_VER
);
9346 if (aiter
!= s
->attrset
.end()) {
9347 bufferlist
& pg_ver_bl
= aiter
->second
;
9348 if (pg_ver_bl
.length()) {
9349 bufferlist::iterator pgbl
= pg_ver_bl
.begin();
9351 ::decode(s
->pg_ver
, pgbl
);
9352 } catch (buffer::error
& err
) {
9353 ldout(cct
, 0) << "ERROR: couldn't decode pg ver attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9357 aiter
= s
->attrset
.find(RGW_ATTR_SOURCE_ZONE
);
9358 if (aiter
!= s
->attrset
.end()) {
9359 bufferlist
& zone_short_id_bl
= aiter
->second
;
9360 if (zone_short_id_bl
.length()) {
9361 bufferlist::iterator zbl
= zone_short_id_bl
.begin();
9363 ::decode(s
->zone_short_id
, zbl
);
9364 } catch (buffer::error
& err
) {
9365 ldout(cct
, 0) << "ERROR: couldn't decode zone short id attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9369 if (s
->obj_tag
.length())
9370 ldout(cct
, 20) << "get_obj_state: setting s->obj_tag to " << s
->obj_tag
.c_str() << dendl
;
9372 ldout(cct
, 20) << "get_obj_state: s->obj_tag was set empty" << dendl
;
9374 /* an object might not be olh yet, but could have olh id tag, so we should set it anyway if
9375 * it exist, and not only if is_olh() returns true
9377 iter
= s
->attrset
.find(RGW_ATTR_OLH_ID_TAG
);
9378 if (iter
!= s
->attrset
.end()) {
9379 s
->olh_tag
= iter
->second
;
9382 if (is_olh(s
->attrset
)) {
9385 ldout(cct
, 20) << __func__
<< ": setting s->olh_tag to " << string(s
->olh_tag
.c_str(), s
->olh_tag
.length()) << dendl
;
9387 if (need_follow_olh
) {
9388 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9395 int RGWRados::get_obj_state(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWObjState
**state
,
9396 bool follow_olh
, bool assume_noent
)
9401 ret
= get_obj_state_impl(rctx
, bucket_info
, obj
, state
, follow_olh
, assume_noent
);
9402 } while (ret
== -EAGAIN
);
9407 int RGWRados::Object::get_manifest(RGWObjManifest
**pmanifest
)
9409 RGWObjState
*astate
;
9410 int r
= get_state(&astate
, true);
9415 *pmanifest
= &astate
->manifest
;
9420 int RGWRados::Object::Read::get_attr(const char *name
, bufferlist
& dest
)
9423 int r
= source
->get_state(&state
, true);
9428 if (!state
->get_attr(name
, dest
))
9435 int RGWRados::Object::Stat::stat_async()
9437 RGWObjectCtx
& ctx
= source
->get_ctx();
9438 rgw_obj
& obj
= source
->get_obj();
9439 RGWRados
*store
= source
->get_store();
9441 RGWObjState
*s
= ctx
.obj
.get_state(obj
); /* calling this one directly because otherwise a sync request will be sent */
9445 result
.size
= s
->size
;
9446 result
.mtime
= ceph::real_clock::to_timespec(s
->mtime
);
9447 result
.attrs
= s
->attrset
;
9448 result
.has_manifest
= s
->has_manifest
;
9449 result
.manifest
= s
->manifest
;
9455 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
9457 int r
= store
->get_obj_head_ioctx(source
->get_bucket_info(), obj
, &state
.io_ctx
);
9462 librados::ObjectReadOperation op
;
9463 op
.stat2(&result
.size
, &result
.mtime
, NULL
);
9464 op
.getxattrs(&result
.attrs
, NULL
);
9465 state
.completion
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
9466 state
.io_ctx
.locator_set_key(loc
);
9467 r
= state
.io_ctx
.aio_operate(oid
, state
.completion
, &op
, NULL
);
9469 ldout(store
->ctx(), 5) << __func__
9470 << ": ERROR: aio_operate() returned ret=" << r
9479 int RGWRados::Object::Stat::wait()
9481 if (!state
.completion
) {
9485 state
.completion
->wait_for_safe();
9486 state
.ret
= state
.completion
->get_return_value();
9487 state
.completion
->release();
9489 if (state
.ret
!= 0) {
9496 int RGWRados::Object::Stat::finish()
9498 map
<string
, bufferlist
>::iterator iter
= result
.attrs
.find(RGW_ATTR_MANIFEST
);
9499 if (iter
!= result
.attrs
.end()) {
9500 bufferlist
& bl
= iter
->second
;
9501 bufferlist::iterator biter
= bl
.begin();
9503 ::decode(result
.manifest
, biter
);
9504 } catch (buffer::error
& err
) {
9505 RGWRados
*store
= source
->get_store();
9506 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< ": failed to decode manifest" << dendl
;
9509 result
.has_manifest
= true;
9516 * Get an attribute for a system object.
9517 * obj: the object to get attr
9518 * name: name of the attr to retrieve
9519 * dest: bufferlist to store the result in
9520 * Returns: 0 on success, -ERR# otherwise.
9522 int RGWRados::system_obj_get_attr(rgw_raw_obj
& obj
, const char *name
, bufferlist
& dest
)
9525 int r
= get_system_obj_ref(obj
, &ref
);
9530 ObjectReadOperation op
;
9533 op
.getxattr(name
, &dest
, &rval
);
9535 r
= ref
.ioctx
.operate(ref
.oid
, &op
, NULL
);
9542 int RGWRados::append_atomic_test(RGWObjectCtx
*rctx
,
9543 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9544 ObjectOperation
& op
, RGWObjState
**pstate
)
9549 int r
= get_obj_state(rctx
, bucket_info
, obj
, pstate
, false);
9553 RGWObjState
*state
= *pstate
;
9555 if (!state
->is_atomic
) {
9556 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not appending atomic test" << dendl
;
9560 if (state
->obj_tag
.length() > 0 && !state
->fake_tag
) {// check for backward compatibility
9561 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9563 ldout(cct
, 20) << "state->obj_tag is empty, not appending atomic test" << dendl
;
9568 int RGWRados::Object::get_state(RGWObjState
**pstate
, bool follow_olh
, bool assume_noent
)
9570 return store
->get_obj_state(&ctx
, bucket_info
, obj
, pstate
, follow_olh
, assume_noent
);
9573 void RGWRados::Object::invalidate_state()
9575 ctx
.obj
.invalidate(obj
);
9578 void RGWRados::SystemObject::invalidate_state()
9580 ctx
.raw
.invalidate(obj
);
9583 int RGWRados::Object::prepare_atomic_modification(ObjectWriteOperation
& op
, bool reset_obj
, const string
*ptag
,
9584 const char *if_match
, const char *if_nomatch
, bool removal_op
,
9587 int r
= get_state(&state
, false);
9591 bool need_guard
= (state
->has_manifest
|| (state
->obj_tag
.length() != 0) ||
9592 if_match
!= NULL
|| if_nomatch
!= NULL
) &&
9595 if (!state
->is_atomic
) {
9596 ldout(store
->ctx(), 20) << "prepare_atomic_modification: state is not atomic. state=" << (void *)state
<< dendl
;
9600 store
->remove_rgw_head_obj(op
); // we're not dropping reference here, actually removing object
9607 /* first verify that the object wasn't replaced under */
9608 if (if_nomatch
== NULL
|| strcmp(if_nomatch
, "*") != 0) {
9609 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9610 // FIXME: need to add FAIL_NOTEXIST_OK for racing deletion
9614 if (strcmp(if_match
, "*") == 0) {
9615 // test the object is existing
9616 if (!state
->exists
) {
9617 return -ERR_PRECONDITION_FAILED
;
9621 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9622 strncmp(if_match
, bl
.c_str(), bl
.length()) != 0) {
9623 return -ERR_PRECONDITION_FAILED
;
9629 if (strcmp(if_nomatch
, "*") == 0) {
9630 // test the object is NOT existing
9631 if (state
->exists
) {
9632 return -ERR_PRECONDITION_FAILED
;
9636 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9637 strncmp(if_nomatch
, bl
.c_str(), bl
.length()) == 0) {
9638 return -ERR_PRECONDITION_FAILED
;
9645 if (state
->exists
) {
9647 store
->remove_rgw_head_obj(op
);
9654 /* the object is being removed, no need to update its tag */
9659 state
->write_tag
= *ptag
;
9661 append_rand_alpha(store
->ctx(), state
->write_tag
, state
->write_tag
, 32);
9664 bl
.append(state
->write_tag
.c_str(), state
->write_tag
.size() + 1);
9666 ldout(store
->ctx(), 10) << "setting object write_tag=" << state
->write_tag
<< dendl
;
9668 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9670 op
.setxattr(RGW_ATTR_TAIL_TAG
, bl
);
9676 int RGWRados::system_obj_set_attr(void *ctx
, rgw_raw_obj
& obj
, const char *name
, bufferlist
& bl
,
9677 RGWObjVersionTracker
*objv_tracker
)
9679 map
<string
, bufferlist
> attrs
;
9681 return system_obj_set_attrs(ctx
, obj
, attrs
, NULL
, objv_tracker
);
9684 int RGWRados::system_obj_set_attrs(void *ctx
, rgw_raw_obj
& obj
,
9685 map
<string
, bufferlist
>& attrs
,
9686 map
<string
, bufferlist
>* rmattrs
,
9687 RGWObjVersionTracker
*objv_tracker
)
9690 int r
= get_system_obj_ref(obj
, &ref
);
9694 ObjectWriteOperation op
;
9697 objv_tracker
->prepare_op_for_write(&op
);
9700 map
<string
, bufferlist
>::iterator iter
;
9702 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9703 const string
& name
= iter
->first
;
9704 op
.rmxattr(name
.c_str());
9708 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9709 const string
& name
= iter
->first
;
9710 bufferlist
& bl
= iter
->second
;
9715 op
.setxattr(name
.c_str(), bl
);
9723 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9731 * Set an attr on an object.
9732 * bucket: name of the bucket holding the object
9733 * obj: name of the object to set the attr on
9734 * name: the attr to set
9735 * bl: the contents of the attr
9736 * Returns: 0 on success, -ERR# otherwise.
9738 int RGWRados::set_attr(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, const char *name
, bufferlist
& bl
)
9740 map
<string
, bufferlist
> attrs
;
9742 return set_attrs(ctx
, bucket_info
, obj
, attrs
, NULL
);
9745 int RGWRados::set_attrs(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
,
9746 map
<string
, bufferlist
>& attrs
,
9747 map
<string
, bufferlist
>* rmattrs
)
9750 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
9754 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
9756 ObjectWriteOperation op
;
9757 RGWObjState
*state
= NULL
;
9759 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &state
);
9763 map
<string
, bufferlist
>::iterator iter
;
9765 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9766 const string
& name
= iter
->first
;
9767 op
.rmxattr(name
.c_str());
9771 const rgw_bucket
& bucket
= obj
.bucket
;
9773 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9774 const string
& name
= iter
->first
;
9775 bufferlist
& bl
= iter
->second
;
9780 op
.setxattr(name
.c_str(), bl
);
9782 if (name
.compare(RGW_ATTR_DELETE_AT
) == 0) {
9787 rgw_obj_index_key obj_key
;
9788 obj
.key
.get_index_key(&obj_key
);
9790 objexp_hint_add(ts
, bucket
.tenant
, bucket
.name
, bucket
.bucket_id
, obj_key
);
9791 } catch (buffer::error
& err
) {
9792 ldout(cct
, 0) << "ERROR: failed to decode " RGW_ATTR_DELETE_AT
<< " attr" << dendl
;
9800 RGWObjectCtx
obj_ctx(this);
9803 RGWRados::Bucket
bop(this, bucket_info
);
9804 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9808 append_rand_alpha(cct
, tag
, tag
, 32);
9809 state
->write_tag
= tag
;
9810 r
= index_op
.prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
9815 bl
.append(tag
.c_str(), tag
.size() + 1);
9816 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9820 real_time mtime
= real_clock::now();
9821 struct timespec mtime_ts
= real_clock::to_timespec(mtime
);
9822 op
.mtime2(&mtime_ts
);
9823 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9826 bufferlist acl_bl
= attrs
[RGW_ATTR_ACL
];
9827 bufferlist etag_bl
= attrs
[RGW_ATTR_ETAG
];
9828 bufferlist content_type_bl
= attrs
[RGW_ATTR_CONTENT_TYPE
];
9829 string
etag(etag_bl
.c_str(), etag_bl
.length());
9830 string
content_type(content_type_bl
.c_str(), content_type_bl
.length());
9831 uint64_t epoch
= ref
.ioctx
.get_last_version();
9832 int64_t poolid
= ref
.ioctx
.get_id();
9833 r
= index_op
.complete(poolid
, epoch
, state
->size
, state
->accounted_size
,
9834 mtime
, etag
, content_type
, &acl_bl
,
9835 RGW_OBJ_CATEGORY_MAIN
, NULL
);
9837 int ret
= index_op
.cancel();
9839 ldout(cct
, 0) << "ERROR: complete_update_index_cancel() returned ret=" << ret
<< dendl
;
9847 state
->obj_tag
.swap(bl
);
9849 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9850 state
->attrset
.erase(iter
->first
);
9853 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9854 state
->attrset
[iter
->first
] = iter
->second
;
9861 int RGWRados::Object::Read::prepare()
9863 RGWRados
*store
= source
->get_store();
9864 CephContext
*cct
= store
->ctx();
9868 map
<string
, bufferlist
>::iterator iter
;
9870 RGWObjState
*astate
;
9871 int r
= source
->get_state(&astate
, true);
9875 if (!astate
->exists
) {
9879 const RGWBucketInfo
& bucket_info
= source
->get_bucket_info();
9881 state
.obj
= astate
->obj
;
9882 store
->obj_to_raw(bucket_info
.placement_rule
, state
.obj
, &state
.head_obj
);
9884 r
= store
->get_obj_head_ioctx(bucket_info
, state
.obj
, &state
.io_ctx
);
9889 *params
.attrs
= astate
->attrset
;
9890 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
9891 for (iter
= params
.attrs
->begin(); iter
!= params
.attrs
->end(); ++iter
) {
9892 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
9897 /* Convert all times go GMT to make them compatible */
9898 if (conds
.mod_ptr
|| conds
.unmod_ptr
) {
9899 obj_time_weight src_weight
;
9900 src_weight
.init(astate
);
9901 src_weight
.high_precision
= conds
.high_precision_time
;
9903 obj_time_weight dest_weight
;
9904 dest_weight
.high_precision
= conds
.high_precision_time
;
9906 if (conds
.mod_ptr
) {
9907 dest_weight
.init(*conds
.mod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9908 ldout(cct
, 10) << "If-Modified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9909 if (!(dest_weight
< src_weight
)) {
9910 return -ERR_NOT_MODIFIED
;
9914 if (conds
.unmod_ptr
) {
9915 dest_weight
.init(*conds
.unmod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9916 ldout(cct
, 10) << "If-UnModified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9917 if (dest_weight
< src_weight
) {
9918 return -ERR_PRECONDITION_FAILED
;
9922 if (conds
.if_match
|| conds
.if_nomatch
) {
9923 r
= get_attr(RGW_ATTR_ETAG
, etag
);
9927 if (conds
.if_match
) {
9928 string if_match_str
= rgw_string_unquote(conds
.if_match
);
9929 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-Match: " << if_match_str
<< dendl
;
9930 if (if_match_str
.compare(etag
.c_str()) != 0) {
9931 return -ERR_PRECONDITION_FAILED
;
9935 if (conds
.if_nomatch
) {
9936 string if_nomatch_str
= rgw_string_unquote(conds
.if_nomatch
);
9937 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-NoMatch: " << if_nomatch_str
<< dendl
;
9938 if (if_nomatch_str
.compare(etag
.c_str()) == 0) {
9939 return -ERR_NOT_MODIFIED
;
9944 if (params
.obj_size
)
9945 *params
.obj_size
= astate
->size
;
9947 *params
.lastmod
= astate
->mtime
;
9952 int RGWRados::Object::Read::range_to_ofs(uint64_t obj_size
, int64_t &ofs
, int64_t &end
)
9959 } else if (end
< 0) {
9964 if (ofs
>= (off_t
)obj_size
) {
9967 if (end
>= (off_t
)obj_size
) {
9974 int RGWRados::SystemObject::get_state(RGWRawObjState
**pstate
, RGWObjVersionTracker
*objv_tracker
)
9976 return store
->get_system_obj_state(&ctx
, obj
, pstate
, objv_tracker
);
9979 int RGWRados::stat_system_obj(RGWObjectCtx
& obj_ctx
,
9980 RGWRados::SystemObject::Read::GetObjState
& state
,
9982 map
<string
, bufferlist
> *attrs
,
9985 RGWObjVersionTracker
*objv_tracker
)
9987 RGWRawObjState
*astate
= NULL
;
9989 int r
= get_system_obj_state(&obj_ctx
, obj
, &astate
, objv_tracker
);
9993 if (!astate
->exists
) {
9998 *attrs
= astate
->attrset
;
9999 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
10000 map
<string
, bufferlist
>::iterator iter
;
10001 for (iter
= attrs
->begin(); iter
!= attrs
->end(); ++iter
) {
10002 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
10008 *obj_size
= astate
->size
;
10010 *lastmod
= astate
->mtime
;
10016 int RGWRados::Bucket::UpdateIndex::guard_reshard(BucketShard
**pbs
, std::function
<int(BucketShard
*)> call
)
10018 RGWRados
*store
= target
->get_store();
10022 #define NUM_RESHARD_RETRIES 10
10023 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
10024 int ret
= get_bucket_shard(&bs
);
10026 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10030 if (r
!= -ERR_BUSY_RESHARDING
) {
10033 ldout(store
->ctx(), 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
10034 string new_bucket_id
;
10035 r
= store
->block_while_resharding(bs
, &new_bucket_id
);
10036 if (r
== -ERR_BUSY_RESHARDING
) {
10042 ldout(store
->ctx(), 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
10043 i
= 0; /* resharding is finished, make sure we can retry */
10044 r
= target
->update_bucket_id(new_bucket_id
);
10046 ldout(store
->ctx(), 0) << "ERROR: update_bucket_id() new_bucket_id=" << new_bucket_id
<< " returned r=" << r
<< dendl
;
10063 int RGWRados::SystemObject::Read::stat(RGWObjVersionTracker
*objv_tracker
)
10065 RGWRados
*store
= source
->get_store();
10066 rgw_raw_obj
& obj
= source
->get_obj();
10068 return store
->stat_system_obj(source
->get_ctx(), state
, obj
, stat_params
.attrs
,
10069 stat_params
.lastmod
, stat_params
.obj_size
, objv_tracker
);
10072 int RGWRados::Bucket::UpdateIndex::prepare(RGWModifyOp op
, const string
*write_tag
)
10077 RGWRados
*store
= target
->get_store();
10079 if (write_tag
&& write_tag
->length()) {
10080 optag
= string(write_tag
->c_str(), write_tag
->length());
10082 if (optag
.empty()) {
10083 append_rand_alpha(store
->ctx(), optag
, optag
, 32);
10087 int r
= guard_reshard(nullptr, [&](BucketShard
*bs
) -> int {
10088 return store
->cls_obj_prepare_op(*bs
, op
, optag
, obj
, bilog_flags
, zones_trace
);
10099 int RGWRados::Bucket::UpdateIndex::complete(int64_t poolid
, uint64_t epoch
,
10100 uint64_t size
, uint64_t accounted_size
,
10101 ceph::real_time
& ut
, const string
& etag
,
10102 const string
& content_type
,
10103 bufferlist
*acl_bl
,
10104 RGWObjCategory category
,
10105 list
<rgw_obj_index_key
> *remove_objs
, const string
*user_data
)
10110 RGWRados
*store
= target
->get_store();
10113 int ret
= get_bucket_shard(&bs
);
10115 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10119 rgw_bucket_dir_entry ent
;
10120 obj
.key
.get_index_key(&ent
.key
);
10121 ent
.meta
.size
= size
;
10122 ent
.meta
.accounted_size
= accounted_size
;
10123 ent
.meta
.mtime
= ut
;
10124 ent
.meta
.etag
= etag
;
10126 ent
.meta
.user_data
= *user_data
;
10129 if (acl_bl
&& acl_bl
->length()) {
10130 int ret
= store
->decode_policy(*acl_bl
, &owner
);
10132 ldout(store
->ctx(), 0) << "WARNING: could not decode policy ret=" << ret
<< dendl
;
10135 ent
.meta
.owner
= owner
.get_id().to_str();
10136 ent
.meta
.owner_display_name
= owner
.get_display_name();
10137 ent
.meta
.content_type
= content_type
;
10139 ret
= store
->cls_obj_complete_add(*bs
, obj
, optag
, poolid
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
10141 if (target
->bucket_info
.datasync_flag_enabled()) {
10142 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10144 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10151 int RGWRados::Bucket::UpdateIndex::complete_del(int64_t poolid
, uint64_t epoch
,
10152 real_time
& removed_mtime
,
10153 list
<rgw_obj_index_key
> *remove_objs
)
10158 RGWRados
*store
= target
->get_store();
10161 int ret
= get_bucket_shard(&bs
);
10163 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10167 ret
= store
->cls_obj_complete_del(*bs
, optag
, poolid
, epoch
, obj
, removed_mtime
, remove_objs
, bilog_flags
, zones_trace
);
10169 if (target
->bucket_info
.datasync_flag_enabled()) {
10170 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10172 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10180 int RGWRados::Bucket::UpdateIndex::cancel()
10185 RGWRados
*store
= target
->get_store();
10188 int ret
= guard_reshard(&bs
, [&](BucketShard
*bs
) -> int {
10189 return store
->cls_obj_complete_cancel(*bs
, optag
, obj
, bilog_flags
, zones_trace
);
10193 * need to update data log anyhow, so that whoever follows needs to update its internal markers
10194 * for following the specific bucket shard log. Otherwise they end up staying behind, and users
10195 * have no way to tell that they're all caught up
10197 if (target
->bucket_info
.datasync_flag_enabled()) {
10198 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10200 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10207 int RGWRados::Object::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
)
10209 RGWRados
*store
= source
->get_store();
10210 CephContext
*cct
= store
->ctx();
10212 rgw_raw_obj read_obj
;
10213 uint64_t read_ofs
= ofs
;
10214 uint64_t len
, read_len
;
10215 bool reading_from_head
= true;
10216 ObjectReadOperation op
;
10218 bool merge_bl
= false;
10219 bufferlist
*pbl
= &bl
;
10220 bufferlist read_bl
;
10221 uint64_t max_chunk_size
;
10223 RGWObjState
*astate
;
10224 int r
= source
->get_state(&astate
, true);
10231 len
= end
- ofs
+ 1;
10233 if (astate
->has_manifest
&& astate
->manifest
.has_tail()) {
10234 /* now get the relevant object part */
10235 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10237 uint64_t stripe_ofs
= iter
.get_stripe_ofs();
10238 read_obj
= iter
.get_location().get_raw_obj(store
);
10239 len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10240 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10241 reading_from_head
= (read_obj
== state
.head_obj
);
10243 read_obj
= state
.head_obj
;
10246 r
= store
->get_max_chunk_size(read_obj
.pool
, &max_chunk_size
);
10248 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for pool " << read_obj
.pool
<< dendl
;
10252 if (len
> max_chunk_size
)
10253 len
= max_chunk_size
;
10256 state
.io_ctx
.locator_set_key(read_obj
.loc
);
10260 if (reading_from_head
) {
10261 /* only when reading from the head object do we need to do the atomic test */
10262 r
= store
->append_atomic_test(&source
->get_ctx(), source
->get_bucket_info(), state
.obj
, op
, &astate
);
10266 if (astate
&& astate
->prefetch_data
) {
10267 if (!ofs
&& astate
->data
.length() >= len
) {
10269 return bl
.length();
10272 if (ofs
< astate
->data
.length()) {
10273 unsigned copy_len
= min((uint64_t)astate
->data
.length() - ofs
, len
);
10274 astate
->data
.copy(ofs
, copy_len
, bl
);
10275 read_len
-= copy_len
;
10276 read_ofs
+= copy_len
;
10278 return bl
.length();
10286 ldout(cct
, 20) << "rados->read obj-ofs=" << ofs
<< " read_ofs=" << read_ofs
<< " read_len=" << read_len
<< dendl
;
10287 op
.read(read_ofs
, read_len
, pbl
, NULL
);
10289 r
= state
.io_ctx
.operate(read_obj
.oid
, &op
, NULL
);
10290 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10297 bl
.append(read_bl
);
10300 return bl
.length();
10303 int RGWRados::SystemObject::Read::GetObjState::get_ref(RGWRados
*store
, rgw_raw_obj
& obj
, rgw_rados_ref
**pref
)
10306 int r
= store
->get_raw_obj_ref(obj
, &ref
);
10317 int RGWRados::get_system_obj(RGWObjectCtx
& obj_ctx
, RGWRados::SystemObject::Read::GetObjState
& read_state
,
10318 RGWObjVersionTracker
*objv_tracker
, rgw_raw_obj
& obj
,
10319 bufferlist
& bl
, off_t ofs
, off_t end
,
10320 map
<string
, bufferlist
> *attrs
,
10321 rgw_cache_entry_info
*cache_info
,
10322 boost::optional
<obj_version
>)
10325 ObjectReadOperation op
;
10330 len
= end
- ofs
+ 1;
10332 if (objv_tracker
) {
10333 objv_tracker
->prepare_op_for_read(&op
);
10336 ldout(cct
, 20) << "rados->read ofs=" << ofs
<< " len=" << len
<< dendl
;
10337 op
.read(ofs
, len
, &bl
, NULL
);
10340 op
.getxattrs(attrs
, NULL
);
10343 rgw_rados_ref
*ref
;
10344 int r
= read_state
.get_ref(this, obj
, &ref
);
10346 ldout(cct
, 20) << "read_state.get_ref() on obj=" << obj
<< " returned " << r
<< dendl
;
10349 r
= ref
->ioctx
.operate(ref
->oid
, &op
, NULL
);
10351 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10354 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10356 uint64_t op_ver
= ref
->ioctx
.get_last_version();
10358 if (read_state
.last_ver
> 0 &&
10359 read_state
.last_ver
!= op_ver
) {
10360 ldout(cct
, 5) << "raced with an object write, abort" << dendl
;
10364 read_state
.last_ver
= op_ver
;
10366 return bl
.length();
10369 int RGWRados::SystemObject::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
,
10370 RGWObjVersionTracker
*objv_tracker
,
10371 boost::optional
<obj_version
> refresh_version
)
10373 RGWRados
*store
= source
->get_store();
10374 rgw_raw_obj
& obj
= source
->get_obj();
10376 return store
->get_system_obj(source
->get_ctx(), state
, objv_tracker
, obj
, bl
,
10377 ofs
, end
, read_params
.attrs
,
10378 read_params
.cache_info
, refresh_version
);
10381 int RGWRados::SystemObject::Read::get_attr(const char *name
, bufferlist
& dest
)
10383 RGWRados
*store
= source
->get_store();
10384 rgw_raw_obj
& obj
= source
->get_obj();
10386 return store
->system_obj_get_attr(obj
, name
, dest
);
10389 struct get_obj_data
;
10391 struct get_obj_aio_data
{
10392 struct get_obj_data
*op_data
;
10397 struct get_obj_io
{
10402 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
);
10404 struct get_obj_data
: public RefCountedObject
{
10409 map
<off_t
, get_obj_io
> io_map
;
10410 map
<off_t
, librados::AioCompletion
*> completion_map
;
10411 uint64_t total_read
;
10414 list
<get_obj_aio_data
> aio_data
;
10415 RGWGetDataCB
*client_cb
;
10416 std::atomic
<bool> cancelled
= { false };
10417 std::atomic
<int64_t> err_code
= { 0 };
10419 list
<bufferlist
> read_list
;
10421 explicit get_obj_data(CephContext
*_cct
)
10423 rados(NULL
), ctx(NULL
),
10424 total_read(0), lock("get_obj_data"), data_lock("get_obj_data::data_lock"),
10426 throttle(cct
, "get_obj_data", cct
->_conf
->rgw_get_obj_window_size
, false) {}
10427 ~get_obj_data() override
{ }
10428 void set_cancelled(int r
) {
10433 bool is_cancelled() {
10437 int get_err_code() {
10441 int wait_next_io(bool *done
) {
10443 map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10444 if (iter
== completion_map
.end()) {
10449 off_t cur_ofs
= iter
->first
;
10450 librados::AioCompletion
*c
= iter
->second
;
10453 c
->wait_for_safe_and_cb();
10454 int r
= c
->get_return_value();
10457 completion_map
.erase(cur_ofs
);
10459 if (completion_map
.empty()) {
10469 void add_io(off_t ofs
, off_t len
, bufferlist
**pbl
, AioCompletion
**pc
) {
10470 Mutex::Locker
l(lock
);
10472 const auto& io_iter
= io_map
.insert(
10473 map
<off_t
, get_obj_io
>::value_type(ofs
, get_obj_io()));
10475 assert(io_iter
.second
); // assert new insertion
10477 get_obj_io
& io
= (io_iter
.first
)->second
;
10480 struct get_obj_aio_data aio
;
10483 aio
.op_data
= this;
10485 aio_data
.push_back(aio
);
10487 struct get_obj_aio_data
*paio_data
= &aio_data
.back(); /* last element */
10489 librados::AioCompletion
*c
= librados::Rados::aio_create_completion((void *)paio_data
, NULL
, _get_obj_aio_completion_cb
);
10490 completion_map
[ofs
] = c
;
10494 /* we have a reference per IO, plus one reference for the calling function.
10495 * reference is dropped for each callback, plus when we're done iterating
10496 * over the parts */
10500 void cancel_io(off_t ofs
) {
10501 ldout(cct
, 20) << "get_obj_data::cancel_io() ofs=" << ofs
<< dendl
;
10503 map
<off_t
, AioCompletion
*>::iterator iter
= completion_map
.find(ofs
);
10504 if (iter
!= completion_map
.end()) {
10505 AioCompletion
*c
= iter
->second
;
10507 completion_map
.erase(ofs
);
10512 /* we don't drop a reference here -- e.g., not calling d->put(), because we still
10513 * need IoCtx to live, as io callback may still be called
10517 void cancel_all_io() {
10518 ldout(cct
, 20) << "get_obj_data::cancel_all_io()" << dendl
;
10519 Mutex::Locker
l(lock
);
10520 for (map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10521 iter
!= completion_map
.end(); ++iter
) {
10522 librados::AioCompletion
*c
= iter
->second
;
10527 int get_complete_ios(off_t ofs
, list
<bufferlist
>& bl_list
) {
10528 Mutex::Locker
l(lock
);
10530 map
<off_t
, get_obj_io
>::iterator liter
= io_map
.begin();
10532 if (liter
== io_map
.end() ||
10533 liter
->first
!= ofs
) {
10537 map
<off_t
, librados::AioCompletion
*>::iterator aiter
;
10538 aiter
= completion_map
.find(ofs
);
10539 if (aiter
== completion_map
.end()) {
10540 /* completion map does not hold this io, it was cancelled */
10544 AioCompletion
*completion
= aiter
->second
;
10545 int r
= completion
->get_return_value();
10549 for (; aiter
!= completion_map
.end(); ++aiter
) {
10550 completion
= aiter
->second
;
10551 if (!completion
->is_safe()) {
10552 /* reached a request that is not yet complete, stop */
10556 r
= completion
->get_return_value();
10558 set_cancelled(r
); /* mark it as cancelled, so that we don't continue processing next operations */
10564 map
<off_t
, get_obj_io
>::iterator old_liter
= liter
++;
10565 bl_list
.push_back(old_liter
->second
.bl
);
10566 io_map
.erase(old_liter
);
10573 static int _get_obj_iterate_cb(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, const rgw_raw_obj
& read_obj
, off_t obj_ofs
, off_t read_ofs
, off_t len
, bool is_head_obj
, RGWObjState
*astate
, void *arg
)
10575 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10577 return d
->rados
->get_obj_iterate_cb(d
->ctx
, astate
, bucket_info
, obj
, read_obj
, obj_ofs
, read_ofs
, len
, is_head_obj
, arg
);
10580 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
)
10582 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10583 struct get_obj_data
*d
= aio_data
->op_data
;
10585 d
->rados
->get_obj_aio_completion_cb(cb
, arg
);
10589 void RGWRados::get_obj_aio_completion_cb(completion_t c
, void *arg
)
10591 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10592 struct get_obj_data
*d
= aio_data
->op_data
;
10593 off_t ofs
= aio_data
->ofs
;
10594 off_t len
= aio_data
->len
;
10596 list
<bufferlist
> bl_list
;
10597 list
<bufferlist
>::iterator iter
;
10600 ldout(cct
, 20) << "get_obj_aio_completion_cb: io completion ofs=" << ofs
<< " len=" << len
<< dendl
;
10601 d
->throttle
.put(len
);
10603 r
= rados_aio_get_return_value(c
);
10605 ldout(cct
, 0) << "ERROR: got unexpected error when trying to read object: " << r
<< dendl
;
10606 d
->set_cancelled(r
);
10610 if (d
->is_cancelled()) {
10614 d
->data_lock
.Lock();
10616 r
= d
->get_complete_ios(ofs
, bl_list
);
10621 d
->read_list
.splice(d
->read_list
.end(), bl_list
);
10624 d
->data_lock
.Unlock();
10630 int RGWRados::flush_read_list(struct get_obj_data
*d
)
10632 d
->data_lock
.Lock();
10633 list
<bufferlist
> l
;
10634 l
.swap(d
->read_list
);
10636 d
->read_list
.clear();
10638 d
->data_lock
.Unlock();
10642 list
<bufferlist
>::iterator iter
;
10643 for (iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
10644 bufferlist
& bl
= *iter
;
10645 r
= d
->client_cb
->handle_data(bl
, 0, bl
.length());
10647 dout(0) << "ERROR: flush_read_list(): d->client_cb->handle_data() returned " << r
<< dendl
;
10652 d
->data_lock
.Lock();
10655 d
->set_cancelled(r
);
10657 d
->data_lock
.Unlock();
10661 int RGWRados::get_obj_iterate_cb(RGWObjectCtx
*ctx
, RGWObjState
*astate
,
10662 const RGWBucketInfo
& bucket_info
,
10663 const rgw_obj
& obj
,
10664 const rgw_raw_obj
& read_obj
,
10666 off_t read_ofs
, off_t len
,
10667 bool is_head_obj
, void *arg
)
10669 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
10670 ObjectReadOperation op
;
10671 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10679 /* only when reading from the head object do we need to do the atomic test */
10680 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &astate
);
10685 obj_ofs
< astate
->data
.length()) {
10686 unsigned chunk_len
= min((uint64_t)astate
->data
.length() - obj_ofs
, (uint64_t)len
);
10688 d
->data_lock
.Lock();
10689 r
= d
->client_cb
->handle_data(astate
->data
, obj_ofs
, chunk_len
);
10690 d
->data_lock
.Unlock();
10695 d
->total_read
+= chunk_len
;
10699 read_ofs
+= chunk_len
;
10700 obj_ofs
+= chunk_len
;
10706 d
->throttle
.get(len
);
10707 if (d
->is_cancelled()) {
10708 return d
->get_err_code();
10711 /* add io after we check that we're not cancelled, otherwise we're going to have trouble
10714 d
->add_io(obj_ofs
, len
, &pbl
, &c
);
10716 ldout(cct
, 20) << "rados->get_obj_iterate_cb oid=" << read_obj
.oid
<< " obj-ofs=" << obj_ofs
<< " read_ofs=" << read_ofs
<< " len=" << len
<< dendl
;
10717 op
.read(read_ofs
, len
, pbl
, NULL
);
10719 librados::IoCtx
io_ctx(d
->io_ctx
);
10720 io_ctx
.locator_set_key(read_obj
.loc
);
10722 r
= io_ctx
.aio_operate(read_obj
.oid
, c
, &op
, NULL
);
10724 ldout(cct
, 0) << "rados->aio_operate r=" << r
<< dendl
;
10728 // Flush data to client if there is any
10729 r
= flush_read_list(d
);
10736 ldout(cct
, 20) << "cancelling io r=" << r
<< " obj_ofs=" << obj_ofs
<< dendl
;
10737 d
->set_cancelled(r
);
10738 d
->cancel_io(obj_ofs
);
10743 int RGWRados::Object::Read::iterate(int64_t ofs
, int64_t end
, RGWGetDataCB
*cb
)
10745 RGWRados
*store
= source
->get_store();
10746 CephContext
*cct
= store
->ctx();
10748 struct get_obj_data
*data
= new get_obj_data(cct
);
10751 RGWObjectCtx
& obj_ctx
= source
->get_ctx();
10753 data
->rados
= store
;
10754 data
->io_ctx
.dup(state
.io_ctx
);
10755 data
->client_cb
= cb
;
10757 int r
= store
->iterate_obj(obj_ctx
, source
->get_bucket_info(), state
.obj
, ofs
, end
, cct
->_conf
->rgw_get_obj_max_req_size
, _get_obj_iterate_cb
, (void *)data
);
10759 data
->cancel_all_io();
10764 r
= data
->wait_next_io(&done
);
10766 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10767 data
->cancel_all_io();
10770 r
= store
->flush_read_list(data
);
10772 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10773 data
->cancel_all_io();
10783 int RGWRados::iterate_obj(RGWObjectCtx
& obj_ctx
,
10784 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
10785 off_t ofs
, off_t end
,
10786 uint64_t max_chunk_size
,
10787 int (*iterate_obj_cb
)(const RGWBucketInfo
&, const rgw_obj
& obj
,
10788 const rgw_raw_obj
&, off_t
, off_t
, off_t
, bool,
10789 RGWObjState
*, void *),
10792 rgw_raw_obj head_obj
;
10793 rgw_raw_obj read_obj
;
10794 uint64_t read_ofs
= ofs
;
10796 bool reading_from_head
= true;
10797 RGWObjState
*astate
= NULL
;
10799 obj_to_raw(bucket_info
.placement_rule
, obj
, &head_obj
);
10801 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &astate
, false);
10809 len
= end
- ofs
+ 1;
10811 if (astate
->has_manifest
) {
10812 /* now get the relevant object stripe */
10813 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10815 RGWObjManifest::obj_iterator obj_end
= astate
->manifest
.obj_end();
10817 for (; iter
!= obj_end
&& ofs
<= end
; ++iter
) {
10818 off_t stripe_ofs
= iter
.get_stripe_ofs();
10819 off_t next_stripe_ofs
= stripe_ofs
+ iter
.get_stripe_size();
10821 while (ofs
< next_stripe_ofs
&& ofs
<= end
) {
10822 read_obj
= iter
.get_location().get_raw_obj(this);
10823 uint64_t read_len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10824 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10826 if (read_len
> max_chunk_size
) {
10827 read_len
= max_chunk_size
;
10830 reading_from_head
= (read_obj
== head_obj
);
10831 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, read_ofs
, read_len
, reading_from_head
, astate
, arg
);
10841 while (ofs
<= end
) {
10842 read_obj
= head_obj
;
10843 uint64_t read_len
= min(len
, max_chunk_size
);
10845 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, ofs
, read_len
, reading_from_head
, astate
, arg
);
10858 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectWriteOperation
*op
)
10861 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10866 return ref
.ioctx
.operate(ref
.oid
, op
);
10869 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectReadOperation
*op
)
10872 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10879 return ref
.ioctx
.operate(ref
.oid
, op
, &outbl
);
10882 int RGWRados::olh_init_modification_impl(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, string
*op_tag
)
10884 ObjectWriteOperation op
;
10886 assert(olh_obj
.key
.instance
.empty());
10888 bool has_tag
= (state
.exists
&& has_olh_tag(state
.attrset
));
10890 if (!state
.exists
) {
10893 op
.assert_exists();
10894 struct timespec mtime_ts
= real_clock::to_timespec(state
.mtime
);
10895 op
.mtime2(&mtime_ts
);
10899 * 3 possible cases: olh object doesn't exist, it exists as an olh, it exists as a regular object.
10900 * If it exists as a regular object we'll need to transform it into an olh. We'll do it in two
10901 * steps, first change its tag and set the olh pending attrs. Once write is done we'll need to
10902 * truncate it, remove extra attrs, and send it to the garbage collection. The bucket index olh
10903 * log will reflect that.
10905 * Need to generate separate olh and obj tags, as olh can be colocated with object data. obj_tag
10906 * is used for object data instance, olh_tag for olh instance.
10909 /* guard against racing writes */
10910 bucket_index_guard_olh_op(state
, op
);
10916 int ret
= gen_rand_alphanumeric_lower(cct
, &obj_tag
, 32);
10918 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10922 bl
.append(obj_tag
.c_str(), obj_tag
.size());
10923 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
10925 state
.attrset
[RGW_ATTR_ID_TAG
] = bl
;
10926 state
.obj_tag
= bl
;
10930 ret
= gen_rand_alphanumeric_lower(cct
, &olh_tag
, 32);
10932 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10936 olh_bl
.append(olh_tag
.c_str(), olh_tag
.size());
10937 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, olh_bl
);
10939 state
.attrset
[RGW_ATTR_OLH_ID_TAG
] = olh_bl
;
10940 state
.olh_tag
= olh_bl
;
10941 state
.is_olh
= true;
10944 op
.setxattr(RGW_ATTR_OLH_VER
, verbl
);
10948 RGWOLHPendingInfo pending_info
;
10949 pending_info
.time
= real_clock::now();
10950 ::encode(pending_info
, bl
);
10952 #define OLH_PENDING_TAG_LEN 32
10953 /* tag will start with current time epoch, this so that entries are sorted by time */
10955 utime_t
ut(pending_info
.time
);
10956 snprintf(buf
, sizeof(buf
), "%016llx", (unsigned long long)ut
.sec());
10960 int ret
= gen_rand_alphanumeric_lower(cct
, &s
, OLH_PENDING_TAG_LEN
- op_tag
->size());
10962 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10967 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
10968 attr_name
.append(*op_tag
);
10970 op
.setxattr(attr_name
.c_str(), bl
);
10972 ret
= obj_operate(bucket_info
, olh_obj
, &op
);
10977 state
.exists
= true;
10978 state
.attrset
[attr_name
] = bl
;
10983 int RGWRados::olh_init_modification(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj
, string
*op_tag
)
10987 ret
= olh_init_modification_impl(bucket_info
, state
, obj
, op_tag
);
10988 if (ret
== -EEXIST
) {
10995 int RGWRados::guard_reshard(BucketShard
*bs
, const rgw_obj
& obj_instance
, std::function
<int(BucketShard
*)> call
)
10998 const rgw_obj
*pobj
= &obj_instance
;
11001 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
11002 r
= bs
->init(pobj
->bucket
, *pobj
);
11004 ldout(cct
, 5) << "bs.init() returned ret=" << r
<< dendl
;
11008 if (r
!= -ERR_BUSY_RESHARDING
) {
11011 ldout(cct
, 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
11012 string new_bucket_id
;
11013 r
= block_while_resharding(bs
, &new_bucket_id
);
11014 if (r
== -ERR_BUSY_RESHARDING
) {
11020 ldout(cct
, 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
11021 i
= 0; /* resharding is finished, make sure we can retry */
11024 obj
.bucket
.update_bucket_id(new_bucket_id
);
11035 int RGWRados::block_while_resharding(RGWRados::BucketShard
*bs
, string
*new_bucket_id
)
11037 std::shared_ptr
<RGWReshardWait
> waiter
= reshard_wait
;
11039 return waiter
->block_while_resharding(bs
, new_bucket_id
);
11042 int RGWRados::bucket_index_link_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& olh_state
, const rgw_obj
& obj_instance
,
11043 bool delete_marker
,
11044 const string
& op_tag
,
11045 struct rgw_bucket_dir_entry_meta
*meta
,
11046 uint64_t olh_epoch
,
11047 real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*_zones_trace
)
11050 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11055 rgw_zone_set zones_trace
;
11056 if (_zones_trace
) {
11057 zones_trace
= *_zones_trace
;
11059 zones_trace
.insert(get_zone().id
);
11062 BucketShard
bs(this);
11064 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
11065 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11066 librados::ObjectWriteOperation op
;
11067 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11068 return cls_rgw_bucket_link_olh(bs
->index_ctx
, op
,
11069 bs
->bucket_obj
, key
, olh_state
.olh_tag
, delete_marker
, op_tag
, meta
, olh_epoch
,
11070 unmod_since
, high_precision_time
,
11071 get_zone().log_data
, zones_trace
);
11074 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
11081 void RGWRados::bucket_index_guard_olh_op(RGWObjState
& olh_state
, ObjectOperation
& op
)
11083 ldout(cct
, 20) << __func__
<< "(): olh_state.olh_tag=" << string(olh_state
.olh_tag
.c_str(), olh_state
.olh_tag
.length()) << dendl
;
11084 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_state
.olh_tag
);
11087 int RGWRados::bucket_index_unlink_instance(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj_instance
,
11088 const string
& op_tag
, const string
& olh_tag
, uint64_t olh_epoch
, rgw_zone_set
*_zones_trace
)
11091 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11096 rgw_zone_set zones_trace
;
11097 if (_zones_trace
) {
11098 zones_trace
= *_zones_trace
;
11100 zones_trace
.insert(get_zone().id
);
11102 BucketShard
bs(this);
11104 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
11105 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11106 librados::ObjectWriteOperation op
;
11107 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11108 return cls_rgw_bucket_unlink_instance(bs
->index_ctx
, op
, bs
->bucket_obj
, key
, op_tag
,
11109 olh_tag
, olh_epoch
, get_zone().log_data
, zones_trace
);
11112 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
11119 int RGWRados::bucket_index_read_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
,
11120 const rgw_obj
& obj_instance
, uint64_t ver_marker
,
11121 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > *log
,
11122 bool *is_truncated
)
11125 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11130 BucketShard
bs(this);
11131 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11133 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11137 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11139 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11141 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11142 ObjectReadOperation op
;
11143 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11144 return cls_rgw_get_olh_log(bs
->index_ctx
, bs
->bucket_obj
, op
,
11145 key
, ver_marker
, olh_tag
, log
, is_truncated
);
11148 ldout(cct
, 20) << "cls_rgw_get_olh_log() returned r=" << r
<< dendl
;
11155 int RGWRados::bucket_index_trim_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
, uint64_t ver
)
11158 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11163 BucketShard
bs(this);
11164 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11166 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11170 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11172 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11174 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11175 ObjectWriteOperation op
;
11176 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11177 cls_rgw_trim_olh_log(op
, key
, ver
, olh_tag
);
11178 return pbs
->index_ctx
.operate(pbs
->bucket_obj
, &op
);
11181 ldout(cct
, 20) << "cls_rgw_trim_olh_log() returned r=" << ret
<< dendl
;
11188 int RGWRados::bucket_index_clear_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
)
11191 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11196 BucketShard
bs(this);
11198 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11200 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11202 int ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11203 ObjectWriteOperation op
;
11204 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11205 return cls_rgw_clear_olh(pbs
->index_ctx
, op
, pbs
->bucket_obj
, key
, olh_tag
);
11208 ldout(cct
, 5) << "cls_rgw_clear_olh() returned ret=" << ret
<< dendl
;
11215 int RGWRados::apply_olh_log(RGWObjectCtx
& obj_ctx
, RGWObjState
& state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
11216 bufferlist
& olh_tag
, map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >& log
,
11217 uint64_t *plast_ver
, rgw_zone_set
* zones_trace
)
11223 librados::ObjectWriteOperation op
;
11225 uint64_t last_ver
= log
.rbegin()->first
;
11226 *plast_ver
= last_ver
;
11228 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >::iterator iter
= log
.begin();
11230 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11231 op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11233 struct timespec mtime_ts
= real_clock::to_timespec(state
.mtime
);
11234 op
.mtime2(&mtime_ts
);
11236 bool need_to_link
= false;
11237 cls_rgw_obj_key key
;
11238 bool delete_marker
= false;
11239 list
<cls_rgw_obj_key
> remove_instances
;
11240 bool need_to_remove
= false;
11242 for (iter
= log
.begin(); iter
!= log
.end(); ++iter
) {
11243 vector
<rgw_bucket_olh_log_entry
>::iterator viter
= iter
->second
.begin();
11244 for (; viter
!= iter
->second
.end(); ++viter
) {
11245 rgw_bucket_olh_log_entry
& entry
= *viter
;
11247 ldout(cct
, 20) << "olh_log_entry: op=" << (int)entry
.op
11248 << " key=" << entry
.key
.name
<< "[" << entry
.key
.instance
<< "] "
11249 << (entry
.delete_marker
? "(delete)" : "") << dendl
;
11250 switch (entry
.op
) {
11251 case CLS_RGW_OLH_OP_REMOVE_INSTANCE
:
11252 remove_instances
.push_back(entry
.key
);
11254 case CLS_RGW_OLH_OP_LINK_OLH
:
11255 need_to_link
= true;
11256 need_to_remove
= false;
11258 delete_marker
= entry
.delete_marker
;
11260 case CLS_RGW_OLH_OP_UNLINK_OLH
:
11261 need_to_remove
= true;
11262 need_to_link
= false;
11265 ldout(cct
, 0) << "ERROR: apply_olh_log: invalid op: " << (int)entry
.op
<< dendl
;
11268 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
11269 attr_name
.append(entry
.op_tag
);
11270 op
.rmxattr(attr_name
.c_str());
11275 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
11280 const rgw_bucket
& bucket
= obj
.bucket
;
11282 if (need_to_link
) {
11283 rgw_obj
target(bucket
, key
);
11285 info
.target
= target
;
11286 info
.removed
= delete_marker
;
11288 ::encode(info
, bl
);
11289 op
.setxattr(RGW_ATTR_OLH_INFO
, bl
);
11292 /* first remove object instances */
11293 for (list
<cls_rgw_obj_key
>::iterator liter
= remove_instances
.begin();
11294 liter
!= remove_instances
.end(); ++liter
) {
11295 cls_rgw_obj_key
& key
= *liter
;
11296 rgw_obj
obj_instance(bucket
, key
);
11297 int ret
= delete_obj(obj_ctx
, bucket_info
, obj_instance
, 0, RGW_BILOG_FLAG_VERSIONED_OP
, ceph::real_time(), zones_trace
);
11298 if (ret
< 0 && ret
!= -ENOENT
) {
11299 ldout(cct
, 0) << "ERROR: delete_obj() returned " << ret
<< " obj_instance=" << obj_instance
<< dendl
;
11304 /* update olh object */
11305 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11306 if (r
== -ECANCELED
) {
11310 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11314 r
= bucket_index_trim_olh_log(bucket_info
, state
, obj
, last_ver
);
11316 ldout(cct
, 0) << "ERROR: could not trim olh log, r=" << r
<< dendl
;
11320 if (need_to_remove
) {
11321 ObjectWriteOperation rm_op
;
11323 rm_op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11324 rm_op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11325 cls_obj_check_prefix_exist(rm_op
, RGW_ATTR_OLH_PENDING_PREFIX
, true); /* fail if found one of these, pending modification */
11328 r
= ref
.ioctx
.operate(ref
.oid
, &rm_op
);
11329 if (r
== -ECANCELED
) {
11330 return 0; /* someone else won this race */
11333 * only clear if was successful, otherwise we might clobber pending operations on this object
11335 r
= bucket_index_clear_olh(bucket_info
, state
, obj
);
11337 ldout(cct
, 0) << "ERROR: could not clear bucket index olh entries r=" << r
<< dendl
;
11347 * read olh log and apply it
11349 int RGWRados::update_olh(RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_zone_set
*zones_trace
)
11351 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > log
;
11353 uint64_t ver_marker
= 0;
11356 int ret
= bucket_index_read_olh_log(bucket_info
, *state
, obj
, ver_marker
, &log
, &is_truncated
);
11360 ret
= apply_olh_log(obj_ctx
, *state
, bucket_info
, obj
, state
->olh_tag
, log
, &ver_marker
, zones_trace
);
11364 } while (is_truncated
);
11369 int RGWRados::set_olh(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
, bool delete_marker
, rgw_bucket_dir_entry_meta
*meta
,
11370 uint64_t olh_epoch
, real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*zones_trace
)
11374 rgw_obj olh_obj
= target_obj
;
11375 olh_obj
.key
.instance
.clear();
11377 RGWObjState
*state
= NULL
;
11382 #define MAX_ECANCELED_RETRY 100
11383 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11384 if (ret
== -ECANCELED
) {
11385 obj_ctx
.obj
.invalidate(olh_obj
);
11388 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11393 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11395 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11396 if (ret
== -ECANCELED
) {
11401 ret
= bucket_index_link_olh(bucket_info
, *state
, target_obj
, delete_marker
, op_tag
, meta
, olh_epoch
, unmod_since
, high_precision_time
, zones_trace
);
11403 ldout(cct
, 20) << "bucket_index_link_olh() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11404 if (ret
== -ECANCELED
) {
11412 if (i
== MAX_ECANCELED_RETRY
) {
11413 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11417 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11418 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11422 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11429 int RGWRados::unlink_obj_instance(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
,
11430 uint64_t olh_epoch
, rgw_zone_set
*zones_trace
)
11434 rgw_obj olh_obj
= target_obj
;
11435 olh_obj
.key
.instance
.clear();
11437 RGWObjState
*state
= NULL
;
11442 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11443 if (ret
== -ECANCELED
) {
11444 obj_ctx
.obj
.invalidate(olh_obj
);
11447 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11451 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11453 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11454 if (ret
== -ECANCELED
) {
11460 string
olh_tag(state
->olh_tag
.c_str(), state
->olh_tag
.length());
11462 ret
= bucket_index_unlink_instance(bucket_info
, target_obj
, op_tag
, olh_tag
, olh_epoch
, zones_trace
);
11464 ldout(cct
, 20) << "bucket_index_unlink_instance() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11465 if (ret
== -ECANCELED
) {
11473 if (i
== MAX_ECANCELED_RETRY
) {
11474 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11478 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
, zones_trace
);
11479 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11483 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11490 void RGWRados::gen_rand_obj_instance_name(rgw_obj
*target_obj
)
11492 #define OBJ_INSTANCE_LEN 32
11493 char buf
[OBJ_INSTANCE_LEN
+ 1];
11495 gen_rand_alphanumeric_no_underscore(cct
, buf
, OBJ_INSTANCE_LEN
); /* don't want it to get url escaped,
11496 no underscore for instance name due to the way we encode the raw keys */
11498 target_obj
->key
.set_instance(buf
);
11501 static void filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
11502 map
<string
, bufferlist
> *attrset
)
11505 map
<string
, bufferlist
>::iterator iter
;
11506 for (iter
= unfiltered_attrset
.lower_bound(check_prefix
);
11507 iter
!= unfiltered_attrset
.end(); ++iter
) {
11508 if (!boost::algorithm::starts_with(iter
->first
, check_prefix
))
11510 (*attrset
)[iter
->first
] = iter
->second
;
11514 int RGWRados::get_olh(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWOLHInfo
*olh
)
11516 map
<string
, bufferlist
> unfiltered_attrset
;
11518 ObjectReadOperation op
;
11519 op
.getxattrs(&unfiltered_attrset
, NULL
);
11522 int r
= obj_operate(bucket_info
, obj
, &op
);
11527 map
<string
, bufferlist
> attrset
;
11529 filter_attrset(unfiltered_attrset
, RGW_ATTR_OLH_PREFIX
, &attrset
);
11531 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_OLH_INFO
);
11532 if (iter
== attrset
.end()) { /* not an olh */
11537 bufferlist::iterator biter
= iter
->second
.begin();
11538 ::decode(*olh
, biter
);
11539 } catch (buffer::error
& err
) {
11540 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11547 void RGWRados::check_pending_olh_entries(map
<string
, bufferlist
>& pending_entries
,
11548 map
<string
, bufferlist
> *rm_pending_entries
)
11550 map
<string
, bufferlist
>::iterator iter
= pending_entries
.begin();
11552 real_time now
= real_clock::now();
11554 while (iter
!= pending_entries
.end()) {
11555 bufferlist::iterator biter
= iter
->second
.begin();
11556 RGWOLHPendingInfo pending_info
;
11558 ::decode(pending_info
, biter
);
11559 } catch (buffer::error
& err
) {
11560 /* skipping bad entry, we could remove it but it might hide a bug */
11561 ldout(cct
, 0) << "ERROR: failed to decode pending entry " << iter
->first
<< dendl
;
11566 map
<string
, bufferlist
>::iterator cur_iter
= iter
;
11568 if (now
- pending_info
.time
>= make_timespan(cct
->_conf
->rgw_olh_pending_timeout_sec
)) {
11569 (*rm_pending_entries
)[cur_iter
->first
] = cur_iter
->second
;
11570 pending_entries
.erase(cur_iter
);
11572 /* entries names are sorted by time (rounded to a second) */
11578 int RGWRados::remove_olh_pending_entries(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, map
<string
, bufferlist
>& pending_attrs
)
11580 ObjectWriteOperation op
;
11582 bucket_index_guard_olh_op(state
, op
);
11584 for (map
<string
, bufferlist
>::iterator iter
= pending_attrs
.begin(); iter
!= pending_attrs
.end(); ++iter
) {
11585 op
.rmxattr(iter
->first
.c_str());
11589 int r
= get_obj_head_ref(bucket_info
, olh_obj
, &ref
);
11594 /* update olh object */
11595 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11596 if (r
== -ENOENT
|| r
== -ECANCELED
) {
11597 /* raced with some other change, shouldn't sweat about it */
11601 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11608 int RGWRados::follow_olh(const RGWBucketInfo
& bucket_info
, RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const rgw_obj
& olh_obj
, rgw_obj
*target
)
11610 map
<string
, bufferlist
> pending_entries
;
11611 filter_attrset(state
->attrset
, RGW_ATTR_OLH_PENDING_PREFIX
, &pending_entries
);
11613 map
<string
, bufferlist
> rm_pending_entries
;
11614 check_pending_olh_entries(pending_entries
, &rm_pending_entries
);
11616 if (!rm_pending_entries
.empty()) {
11617 int ret
= remove_olh_pending_entries(bucket_info
, *state
, olh_obj
, rm_pending_entries
);
11619 ldout(cct
, 20) << "ERROR: rm_pending_entries returned ret=" << ret
<< dendl
;
11623 if (!pending_entries
.empty()) {
11624 ldout(cct
, 20) << __func__
<< "(): found pending entries, need to update_olh() on bucket=" << olh_obj
.bucket
<< dendl
;
11626 int ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11632 map
<string
, bufferlist
>::iterator iter
= state
->attrset
.find(RGW_ATTR_OLH_INFO
);
11633 assert(iter
!= state
->attrset
.end());
11636 bufferlist::iterator biter
= iter
->second
.begin();
11637 ::decode(olh
, biter
);
11638 } catch (buffer::error
& err
) {
11639 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11647 *target
= olh
.target
;
11652 int RGWRados::raw_obj_stat(rgw_raw_obj
& obj
, uint64_t *psize
, real_time
*pmtime
, uint64_t *epoch
,
11653 map
<string
, bufferlist
> *attrs
, bufferlist
*first_chunk
,
11654 RGWObjVersionTracker
*objv_tracker
)
11657 int r
= get_raw_obj_ref(obj
, &ref
);
11662 map
<string
, bufferlist
> unfiltered_attrset
;
11664 struct timespec mtime_ts
;
11666 ObjectReadOperation op
;
11667 if (objv_tracker
) {
11668 objv_tracker
->prepare_op_for_read(&op
);
11671 op
.getxattrs(&unfiltered_attrset
, NULL
);
11673 if (psize
|| pmtime
) {
11674 op
.stat2(&size
, &mtime_ts
, NULL
);
11677 op
.read(0, cct
->_conf
->rgw_max_chunk_size
, first_chunk
, NULL
);
11680 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &outbl
);
11683 *epoch
= ref
.ioctx
.get_last_version();
11692 *pmtime
= ceph::real_clock::from_timespec(mtime_ts
);
11694 filter_attrset(unfiltered_attrset
, RGW_ATTR_PREFIX
, attrs
);
11700 int RGWRados::get_bucket_stats(RGWBucketInfo
& bucket_info
, int shard_id
, string
*bucket_ver
, string
*master_ver
,
11701 map
<RGWObjCategory
, RGWStorageStats
>& stats
, string
*max_marker
, bool *syncstopped
)
11703 map
<string
, rgw_bucket_dir_header
> headers
;
11704 map
<int, string
> bucket_instance_ids
;
11705 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11710 assert(headers
.size() == bucket_instance_ids
.size());
11712 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11713 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11714 BucketIndexShardsManager ver_mgr
;
11715 BucketIndexShardsManager master_ver_mgr
;
11716 BucketIndexShardsManager marker_mgr
;
11718 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11719 accumulate_raw_stats(iter
->second
, stats
);
11720 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.ver
);
11721 ver_mgr
.add(viter
->first
, string(buf
));
11722 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.master_ver
);
11723 master_ver_mgr
.add(viter
->first
, string(buf
));
11724 if (shard_id
>= 0) {
11725 *max_marker
= iter
->second
.max_marker
;
11727 marker_mgr
.add(viter
->first
, iter
->second
.max_marker
);
11729 if (syncstopped
!= NULL
)
11730 *syncstopped
= iter
->second
.syncstopped
;
11732 ver_mgr
.to_string(bucket_ver
);
11733 master_ver_mgr
.to_string(master_ver
);
11734 if (shard_id
< 0) {
11735 marker_mgr
.to_string(max_marker
);
11740 int RGWRados::get_bi_log_status(RGWBucketInfo
& bucket_info
, int shard_id
,
11741 map
<int, string
>& markers
)
11743 map
<string
, rgw_bucket_dir_header
> headers
;
11744 map
<int, string
> bucket_instance_ids
;
11745 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11749 assert(headers
.size() == bucket_instance_ids
.size());
11751 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11752 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11754 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11755 if (shard_id
>= 0) {
11756 markers
[shard_id
] = iter
->second
.max_marker
;
11758 markers
[viter
->first
] = iter
->second
.max_marker
;
11764 class RGWGetBucketStatsContext
: public RGWGetDirHeader_CB
{
11765 RGWGetBucketStats_CB
*cb
;
11767 map
<RGWObjCategory
, RGWStorageStats
> stats
;
11773 RGWGetBucketStatsContext(RGWGetBucketStats_CB
*_cb
, uint32_t _pendings
)
11774 : cb(_cb
), pendings(_pendings
), stats(), ret_code(0), should_cb(true),
11775 lock("RGWGetBucketStatsContext") {}
11777 void handle_response(int r
, rgw_bucket_dir_header
& header
) override
{
11778 Mutex::Locker
l(lock
);
11781 accumulate_raw_stats(header
, stats
);
11786 // Are we all done?
11787 if (--pendings
== 0) {
11789 cb
->set_response(&stats
);
11791 cb
->handle_response(ret_code
);
11798 Mutex::Locker
l(lock
);
11803 int RGWRados::get_bucket_stats_async(RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetBucketStats_CB
*ctx
)
11806 RGWGetBucketStatsContext
*get_ctx
= new RGWGetBucketStatsContext(ctx
, bucket_info
.num_shards
? : 1);
11808 int r
= cls_bucket_head_async(bucket_info
, shard_id
, get_ctx
, &num_aio
);
11812 get_ctx
->unset_cb();
11819 class RGWGetUserStatsContext
: public RGWGetUserHeader_CB
{
11820 RGWGetUserStats_CB
*cb
;
11823 explicit RGWGetUserStatsContext(RGWGetUserStats_CB
* const cb
)
11826 void handle_response(int r
, cls_user_header
& header
) override
{
11827 const cls_user_stats
& hs
= header
.stats
;
11829 RGWStorageStats stats
;
11831 stats
.size
= hs
.total_bytes
;
11832 stats
.size_rounded
= hs
.total_bytes_rounded
;
11833 stats
.num_objects
= hs
.total_entries
;
11835 cb
->set_response(stats
);
11838 cb
->handle_response(r
);
11844 int RGWRados::get_user_stats(const rgw_user
& user
, RGWStorageStats
& stats
)
11846 string user_str
= user
.to_str();
11848 cls_user_header header
;
11849 int r
= cls_user_get_header(user_str
, &header
);
11853 const cls_user_stats
& hs
= header
.stats
;
11855 stats
.size
= hs
.total_bytes
;
11856 stats
.size_rounded
= hs
.total_bytes_rounded
;
11857 stats
.num_objects
= hs
.total_entries
;
11862 int RGWRados::get_user_stats_async(const rgw_user
& user
, RGWGetUserStats_CB
*ctx
)
11864 string user_str
= user
.to_str();
11866 RGWGetUserStatsContext
*get_ctx
= new RGWGetUserStatsContext(ctx
);
11867 int r
= cls_user_get_header_async(user_str
, get_ctx
);
11877 void RGWRados::get_bucket_meta_oid(const rgw_bucket
& bucket
, string
& oid
)
11879 oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ bucket
.get_key(':');
11882 void RGWRados::get_bucket_instance_obj(const rgw_bucket
& bucket
, rgw_raw_obj
& obj
)
11884 if (!bucket
.oid
.empty()) {
11885 obj
.init(get_zone_params().domain_root
, bucket
.oid
);
11888 get_bucket_meta_oid(bucket
, oid
);
11889 obj
.init(get_zone_params().domain_root
, oid
);
11893 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const string
& meta_key
, RGWBucketInfo
& info
,
11894 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11896 size_t pos
= meta_key
.find(':');
11897 if (pos
== string::npos
) {
11900 string oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ meta_key
;
11901 rgw_bucket_instance_key_to_oid(oid
);
11903 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11906 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const rgw_bucket
& bucket
, RGWBucketInfo
& info
,
11907 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11910 if (bucket
.oid
.empty()) {
11911 get_bucket_meta_oid(bucket
, oid
);
11916 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11919 int RGWRados::get_bucket_instance_from_oid(RGWObjectCtx
& obj_ctx
, const string
& oid
, RGWBucketInfo
& info
,
11920 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
,
11921 rgw_cache_entry_info
*cache_info
,
11922 boost::optional
<obj_version
> refresh_version
)
11924 ldout(cct
, 20) << "reading from " << get_zone_params().domain_root
<< ":" << oid
<< dendl
;
11928 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
,
11929 oid
, epbl
, &info
.objv_tracker
, pmtime
, pattrs
,
11930 cache_info
, refresh_version
);
11935 bufferlist::iterator iter
= epbl
.begin();
11937 ::decode(info
, iter
);
11938 } catch (buffer::error
& err
) {
11939 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11942 info
.bucket
.oid
= oid
;
11946 int RGWRados::get_bucket_entrypoint_info(RGWObjectCtx
& obj_ctx
,
11947 const string
& tenant_name
,
11948 const string
& bucket_name
,
11949 RGWBucketEntryPoint
& entry_point
,
11950 RGWObjVersionTracker
*objv_tracker
,
11952 map
<string
, bufferlist
> *pattrs
,
11953 rgw_cache_entry_info
*cache_info
,
11954 boost::optional
<obj_version
> refresh_version
)
11957 string bucket_entry
;
11959 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
11960 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
,
11961 bucket_entry
, bl
, objv_tracker
, pmtime
, pattrs
,
11962 cache_info
, refresh_version
);
11967 bufferlist::iterator iter
= bl
.begin();
11969 ::decode(entry_point
, iter
);
11970 } catch (buffer::error
& err
) {
11971 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11977 int RGWRados::convert_old_bucket_info(RGWObjectCtx
& obj_ctx
,
11978 const string
& tenant_name
,
11979 const string
& bucket_name
)
11981 RGWBucketEntryPoint entry_point
;
11982 real_time ep_mtime
;
11983 RGWObjVersionTracker ot
;
11984 map
<string
, bufferlist
> attrs
;
11985 RGWBucketInfo info
;
11987 ldout(cct
, 10) << "RGWRados::convert_old_bucket_info(): bucket=" << bucket_name
<< dendl
;
11989 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, entry_point
, &ot
, &ep_mtime
, &attrs
);
11991 ldout(cct
, 0) << "ERROR: get_bucket_entrypoint_info() returned " << ret
<< " bucket=" << bucket_name
<< dendl
;
11995 if (!entry_point
.has_bucket_info
) {
11996 /* already converted! */
12000 info
= entry_point
.old_bucket_info
;
12001 info
.bucket
.oid
= bucket_name
;
12002 info
.ep_objv
= ot
.read_version
;
12004 ot
.generate_new_write_ver(cct
);
12006 ret
= put_linked_bucket_info(info
, false, ep_mtime
, &ot
.write_version
, &attrs
, true);
12008 ldout(cct
, 0) << "ERROR: failed to put_linked_bucket_info(): " << ret
<< dendl
;
12015 int RGWRados::_get_bucket_info(RGWObjectCtx
& obj_ctx
,
12016 const string
& tenant
,
12017 const string
& bucket_name
,
12018 RGWBucketInfo
& info
,
12020 map
<string
, bufferlist
> *pattrs
,
12021 boost::optional
<obj_version
> refresh_version
)
12023 bucket_info_entry e
;
12024 string bucket_entry
;
12025 rgw_make_bucket_entry_name(tenant
, bucket_name
, bucket_entry
);
12028 if (binfo_cache
->find(bucket_entry
, &e
)) {
12029 if (refresh_version
&&
12030 e
.info
.objv_tracker
.read_version
.compare(&(*refresh_version
))) {
12031 lderr(cct
) << "WARNING: The bucket info cache is inconsistent. This is "
12032 << "a failure that should be debugged. I am a nice machine, "
12033 << "so I will try to recover." << dendl
;
12034 binfo_cache
->invalidate(bucket_entry
);
12044 RGWBucketEntryPoint entry_point
;
12045 real_time ep_mtime
;
12046 RGWObjVersionTracker ot
;
12047 rgw_cache_entry_info entry_cache_info
;
12048 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant
, bucket_name
,
12049 entry_point
, &ot
, &ep_mtime
, pattrs
,
12050 &entry_cache_info
, refresh_version
);
12052 /* only init these fields */
12053 info
.bucket
.tenant
= tenant
;
12054 info
.bucket
.name
= bucket_name
;
12058 if (entry_point
.has_bucket_info
) {
12059 info
= entry_point
.old_bucket_info
;
12060 info
.bucket
.oid
= bucket_name
;
12061 info
.bucket
.tenant
= tenant
;
12062 info
.ep_objv
= ot
.read_version
;
12063 ldout(cct
, 20) << "rgw_get_bucket_info: old bucket info, bucket=" << info
.bucket
<< " owner " << info
.owner
<< dendl
;
12067 /* data is in the bucket instance object, we need to get attributes from there, clear everything
12074 ldout(cct
, 20) << "rgw_get_bucket_info: bucket instance: " << entry_point
.bucket
<< dendl
;
12077 /* read bucket instance info */
12080 get_bucket_meta_oid(entry_point
.bucket
, oid
);
12082 rgw_cache_entry_info cache_info
;
12084 ret
= get_bucket_instance_from_oid(obj_ctx
, oid
, e
.info
, &e
.mtime
, &e
.attrs
,
12085 &cache_info
, refresh_version
);
12086 e
.info
.ep_objv
= ot
.read_version
;
12089 lderr(cct
) << "ERROR: get_bucket_instance_from_oid failed: " << ret
<< dendl
;
12090 info
.bucket
.tenant
= tenant
;
12091 info
.bucket
.name
= bucket_name
;
12092 // XXX and why return anything in case of an error anyway?
12101 list
<rgw_cache_entry_info
*> cache_info_entries
;
12102 cache_info_entries
.push_back(&entry_cache_info
);
12103 cache_info_entries
.push_back(&cache_info
);
12106 /* chain to both bucket entry point and bucket instance */
12107 if (!binfo_cache
->put(this, bucket_entry
, &e
, cache_info_entries
)) {
12108 ldout(cct
, 20) << "couldn't put binfo cache entry, might have raced with data changes" << dendl
;
12111 if (refresh_version
&&
12112 refresh_version
->compare(&info
.objv_tracker
.read_version
)) {
12113 lderr(cct
) << "WARNING: The OSD has the same version I have. Something may "
12114 << "have gone squirrelly. An administrator may have forced a "
12115 << "change; otherwise there is a problem somewhere." << dendl
;
12121 int RGWRados::get_bucket_info(RGWObjectCtx
& obj_ctx
,
12122 const string
& tenant
, const string
& bucket_name
,
12123 RGWBucketInfo
& info
,
12124 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
12126 return _get_bucket_info(obj_ctx
, tenant
, bucket_name
, info
, pmtime
,
12127 pattrs
, boost::none
);
12130 int RGWRados::try_refresh_bucket_info(RGWBucketInfo
& info
,
12131 ceph::real_time
*pmtime
,
12132 map
<string
, bufferlist
> *pattrs
)
12134 RGWObjectCtx
obj_ctx(this);
12136 return _get_bucket_info(obj_ctx
, info
.bucket
.tenant
, info
.bucket
.name
,
12137 info
, pmtime
, pattrs
, info
.objv_tracker
.read_version
);
12140 int RGWRados::put_bucket_entrypoint_info(const string
& tenant_name
, const string
& bucket_name
, RGWBucketEntryPoint
& entry_point
,
12141 bool exclusive
, RGWObjVersionTracker
& objv_tracker
, real_time mtime
,
12142 map
<string
, bufferlist
> *pattrs
)
12145 ::encode(entry_point
, epbl
);
12146 string bucket_entry
;
12147 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
12148 return rgw_bucket_store_info(this, bucket_entry
, epbl
, exclusive
, pattrs
, &objv_tracker
, mtime
);
12151 int RGWRados::put_bucket_instance_info(RGWBucketInfo
& info
, bool exclusive
,
12152 real_time mtime
, map
<string
, bufferlist
> *pattrs
)
12154 info
.has_instance_obj
= true;
12157 ::encode(info
, bl
);
12159 string key
= info
.bucket
.get_key(); /* when we go through meta api, we don't use oid directly */
12160 int ret
= rgw_bucket_instance_store_info(this, key
, bl
, exclusive
, pattrs
, &info
.objv_tracker
, mtime
);
12161 if (ret
== -EEXIST
) {
12162 /* well, if it's exclusive we shouldn't overwrite it, because we might race with another
12163 * bucket operation on this specific bucket (e.g., being synced from the master), but
12164 * since bucket instace meta object is unique for this specific bucket instace, we don't
12165 * need to return an error.
12166 * A scenario where we'd get -EEXIST here, is in a multi-zone config, we're not on the
12167 * master, creating a bucket, sending bucket creation to the master, we create the bucket
12168 * locally, while in the sync thread we sync the new bucket.
12175 int RGWRados::put_linked_bucket_info(RGWBucketInfo
& info
, bool exclusive
, real_time mtime
, obj_version
*pep_objv
,
12176 map
<string
, bufferlist
> *pattrs
, bool create_entry_point
)
12178 bool create_head
= !info
.has_instance_obj
|| create_entry_point
;
12180 int ret
= put_bucket_instance_info(info
, exclusive
, mtime
, pattrs
);
12186 return 0; /* done! */
12188 RGWBucketEntryPoint entry_point
;
12189 entry_point
.bucket
= info
.bucket
;
12190 entry_point
.owner
= info
.owner
;
12191 entry_point
.creation_time
= info
.creation_time
;
12192 entry_point
.linked
= true;
12193 RGWObjVersionTracker ot
;
12194 if (pep_objv
&& !pep_objv
->tag
.empty()) {
12195 ot
.write_version
= *pep_objv
;
12197 ot
.generate_new_write_ver(cct
);
12199 *pep_objv
= ot
.write_version
;
12202 ret
= put_bucket_entrypoint_info(info
.bucket
.tenant
, info
.bucket
.name
, entry_point
, exclusive
, ot
, mtime
, NULL
);
12209 int RGWRados::omap_get_vals(rgw_raw_obj
& obj
, bufferlist
& header
, const string
& marker
, uint64_t count
, std::map
<string
, bufferlist
>& m
)
12212 int r
= get_raw_obj_ref(obj
, &ref
);
12217 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, marker
, count
, &m
);
12225 int RGWRados::omap_get_all(rgw_raw_obj
& obj
, bufferlist
& header
,
12226 std::map
<string
, bufferlist
>& m
)
12229 int r
= get_raw_obj_ref(obj
, &ref
);
12234 #define MAX_OMAP_GET_ENTRIES 1024
12235 const int count
= MAX_OMAP_GET_ENTRIES
;
12236 string start_after
;
12239 std::map
<string
, bufferlist
> t
;
12240 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, start_after
, count
, &t
);
12247 start_after
= t
.rbegin()->first
;
12248 m
.insert(t
.begin(), t
.end());
12253 int RGWRados::omap_set(rgw_raw_obj
& obj
, const std::string
& key
, bufferlist
& bl
)
12256 int r
= get_raw_obj_ref(obj
, &ref
);
12260 ldout(cct
, 15) << "omap_set obj=" << obj
<< " key=" << key
<< dendl
;
12262 map
<string
, bufferlist
> m
;
12265 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12270 int RGWRados::omap_set(rgw_raw_obj
& obj
, std::map
<std::string
, bufferlist
>& m
)
12273 int r
= get_raw_obj_ref(obj
, &ref
);
12278 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12283 int RGWRados::omap_del(rgw_raw_obj
& obj
, const std::string
& key
)
12286 int r
= get_raw_obj_ref(obj
, &ref
);
12294 r
= ref
.ioctx
.omap_rm_keys(ref
.oid
, k
);
12298 int RGWRados::update_containers_stats(map
<string
, RGWBucketEnt
>& m
)
12300 RGWObjectCtx
obj_ctx(this);
12302 map
<string
, RGWBucketEnt
>::iterator iter
;
12303 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
12304 RGWBucketEnt
& ent
= iter
->second
;
12305 rgw_bucket
& bucket
= ent
.bucket
;
12308 ent
.size_rounded
= 0;
12310 map
<string
, rgw_bucket_dir_header
> headers
;
12312 RGWBucketInfo bucket_info
;
12313 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
12318 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
12322 map
<string
, rgw_bucket_dir_header
>::iterator hiter
= headers
.begin();
12323 for (; hiter
!= headers
.end(); ++hiter
) {
12324 RGWObjCategory category
= main_category
;
12325 map
<uint8_t, struct rgw_bucket_category_stats
>::iterator iter
= (hiter
->second
.stats
).find((uint8_t)category
);
12326 if (iter
!= hiter
->second
.stats
.end()) {
12327 struct rgw_bucket_category_stats
& stats
= iter
->second
;
12328 ent
.count
+= stats
.num_entries
;
12329 ent
.size
+= stats
.total_size
;
12330 ent
.size_rounded
+= stats
.total_size_rounded
;
12334 // fill in placement_rule from the bucket instance for use in swift's
12335 // per-storage policy statistics
12336 ent
.placement_rule
= std::move(bucket_info
.placement_rule
);
12342 int RGWRados::append_async(rgw_raw_obj
& obj
, size_t size
, bufferlist
& bl
)
12345 int r
= get_raw_obj_ref(obj
, &ref
);
12349 librados::Rados
*rad
= get_rados_handle();
12350 librados::AioCompletion
*completion
= rad
->aio_create_completion(NULL
, NULL
, NULL
);
12352 r
= ref
.ioctx
.aio_append(ref
.oid
, completion
, bl
, size
);
12353 completion
->release();
12357 int RGWRados::distribute(const string
& key
, bufferlist
& bl
)
12360 * we were called before watch was initialized. This can only happen if we're updating some system
12361 * config object (e.g., zone info) during init. Don't try to distribute the cache info for these
12362 * objects, they're currently only read on startup anyway.
12364 if (!watch_initialized
)
12368 pick_control_oid(key
, notify_oid
);
12370 ldout(cct
, 10) << "distributing notification oid=" << notify_oid
<< " bl.length()=" << bl
.length() << dendl
;
12371 return control_pool_ctx
.notify2(notify_oid
, bl
, 0, NULL
);
12374 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, RGWPoolIterCtx
& ctx
)
12376 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12377 librados::NObjectIterator
& iter
= ctx
.iter
;
12379 int r
= open_pool_ctx(pool
, io_ctx
);
12383 iter
= io_ctx
.nobjects_begin();
12388 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, const string
& cursor
, RGWPoolIterCtx
& ctx
)
12390 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12391 librados::NObjectIterator
& iter
= ctx
.iter
;
12393 int r
= open_pool_ctx(pool
, io_ctx
);
12397 librados::ObjectCursor oc
;
12398 if (!oc
.from_str(cursor
)) {
12399 ldout(cct
, 10) << "failed to parse cursor: " << cursor
<< dendl
;
12403 iter
= io_ctx
.nobjects_begin(oc
);
12408 string
RGWRados::pool_iterate_get_cursor(RGWPoolIterCtx
& ctx
)
12410 return ctx
.iter
.get_cursor().to_str();
12413 int RGWRados::pool_iterate(RGWPoolIterCtx
& ctx
, uint32_t num
, vector
<rgw_bucket_dir_entry
>& objs
,
12414 bool *is_truncated
, RGWAccessListFilter
*filter
)
12416 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12417 librados::NObjectIterator
& iter
= ctx
.iter
;
12419 if (iter
== io_ctx
.nobjects_end())
12424 for (i
= 0; i
< num
&& iter
!= io_ctx
.nobjects_end(); ++i
, ++iter
) {
12425 rgw_bucket_dir_entry e
;
12427 string oid
= iter
->get_oid();
12428 ldout(cct
, 20) << "RGWRados::pool_iterate: got " << oid
<< dendl
;
12430 // fill it in with initial values; we may correct later
12431 if (filter
&& !filter
->filter(oid
, oid
))
12439 *is_truncated
= (iter
!= io_ctx
.nobjects_end());
12441 return objs
.size();
12443 struct RGWAccessListFilterPrefix
: public RGWAccessListFilter
{
12446 explicit RGWAccessListFilterPrefix(const string
& _prefix
) : prefix(_prefix
) {}
12447 bool filter(string
& name
, string
& key
) override
{
12448 return (prefix
.compare(key
.substr(0, prefix
.size())) == 0);
12452 int RGWRados::list_raw_objects_init(const rgw_pool
& pool
, const string
& marker
, RGWListRawObjsCtx
*ctx
)
12454 if (!ctx
->initialized
) {
12455 int r
= pool_iterate_begin(pool
, marker
, ctx
->iter_ctx
);
12457 ldout(cct
, 10) << "failed to list objects pool_iterate_begin() returned r=" << r
<< dendl
;
12460 ctx
->initialized
= true;
12465 int RGWRados::list_raw_objects_next(const string
& prefix_filter
, int max
,
12466 RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12467 bool *is_truncated
)
12469 if (!ctx
.initialized
) {
12472 RGWAccessListFilterPrefix
filter(prefix_filter
);
12473 vector
<rgw_bucket_dir_entry
> objs
;
12474 int r
= pool_iterate(ctx
.iter_ctx
, max
, objs
, is_truncated
, &filter
);
12477 ldout(cct
, 10) << "failed to list objects pool_iterate returned r=" << r
<< dendl
;
12481 vector
<rgw_bucket_dir_entry
>::iterator iter
;
12482 for (iter
= objs
.begin(); iter
!= objs
.end(); ++iter
) {
12483 oids
.push_back(iter
->key
.name
);
12486 return oids
.size();
12489 int RGWRados::list_raw_objects(const rgw_pool
& pool
, const string
& prefix_filter
,
12490 int max
, RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12491 bool *is_truncated
)
12493 if (!ctx
.initialized
) {
12494 int r
= list_raw_objects_init(pool
, string(), &ctx
);
12500 return list_raw_objects_next(prefix_filter
, max
, ctx
, oids
, is_truncated
);
12503 string
RGWRados::list_raw_objs_get_cursor(RGWListRawObjsCtx
& ctx
)
12505 return pool_iterate_get_cursor(ctx
.iter_ctx
);
12508 int RGWRados::list_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& marker
, uint32_t max
,
12509 std::list
<rgw_bi_log_entry
>& result
, bool *truncated
)
12511 ldout(cct
, 20) << __func__
<< ": " << bucket_info
.bucket
<< " marker " << marker
<< " shard_id=" << shard_id
<< " max " << max
<< dendl
;
12514 librados::IoCtx index_ctx
;
12515 map
<int, string
> oids
;
12516 map
<int, cls_rgw_bi_log_list_ret
> bi_log_lists
;
12517 map
<int, string
> bucket_instance_ids
;
12518 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, &bucket_instance_ids
);
12522 BucketIndexShardsManager marker_mgr
;
12523 bool has_shards
= (oids
.size() > 1 || shard_id
>= 0);
12524 // If there are multiple shards for the bucket index object, the marker
12525 // should have the pattern '{shard_id_1}#{shard_marker_1},{shard_id_2}#
12526 // {shard_marker_2}...', if there is no sharding, the bi_log_list should
12527 // only contain one record, and the key is the bucket instance id.
12528 r
= marker_mgr
.from_string(marker
, shard_id
);
12532 r
= CLSRGWIssueBILogList(index_ctx
, marker_mgr
, max
, oids
, bi_log_lists
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12536 map
<int, list
<rgw_bi_log_entry
>::iterator
> vcurrents
;
12537 map
<int, list
<rgw_bi_log_entry
>::iterator
> vends
;
12539 *truncated
= false;
12541 map
<int, cls_rgw_bi_log_list_ret
>::iterator miter
= bi_log_lists
.begin();
12542 for (; miter
!= bi_log_lists
.end(); ++miter
) {
12543 int shard_id
= miter
->first
;
12544 vcurrents
[shard_id
] = miter
->second
.entries
.begin();
12545 vends
[shard_id
] = miter
->second
.entries
.end();
12547 *truncated
= (*truncated
|| miter
->second
.truncated
);
12552 bool has_more
= true;
12553 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator viter
;
12554 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator eiter
;
12555 while (total
< max
&& has_more
) {
12558 viter
= vcurrents
.begin();
12559 eiter
= vends
.begin();
12561 for (; total
< max
&& viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12562 assert (eiter
!= vends
.end());
12564 int shard_id
= viter
->first
;
12565 list
<rgw_bi_log_entry
>::iterator
& liter
= viter
->second
;
12567 if (liter
== eiter
->second
){
12570 rgw_bi_log_entry
& entry
= *(liter
);
12573 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
12575 build_bucket_index_marker(buf
, entry
.id
, &tmp_id
);
12576 entry
.id
.swap(tmp_id
);
12578 marker_mgr
.add(shard_id
, entry
.id
);
12579 result
.push_back(entry
);
12587 for (viter
= vcurrents
.begin(), eiter
= vends
.begin(); viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12588 assert (eiter
!= vends
.end());
12589 *truncated
= (*truncated
|| (viter
->second
!= eiter
->second
));
12593 // Refresh marker, if there are multiple shards, the output will look like
12594 // '{shard_oid_1}#{shard_marker_1},{shard_oid_2}#{shard_marker_2}...',
12595 // if there is no sharding, the simply marker (without oid) is returned
12597 marker_mgr
.to_string(&marker
);
12599 if (!result
.empty()) {
12600 marker
= result
.rbegin()->id
;
12607 int RGWRados::trim_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& start_marker
, string
& end_marker
)
12609 librados::IoCtx index_ctx
;
12610 map
<int, string
> bucket_objs
;
12612 BucketIndexShardsManager start_marker_mgr
;
12613 BucketIndexShardsManager end_marker_mgr
;
12615 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12620 r
= start_marker_mgr
.from_string(start_marker
, shard_id
);
12625 r
= end_marker_mgr
.from_string(end_marker
, shard_id
);
12630 return CLSRGWIssueBILogTrim(index_ctx
, start_marker_mgr
, end_marker_mgr
, bucket_objs
,
12631 cct
->_conf
->rgw_bucket_index_max_aio
)();
12636 int RGWRados::resync_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12638 librados::IoCtx index_ctx
;
12639 map
<int, string
> bucket_objs
;
12640 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12644 return CLSRGWIssueResyncBucketBILog(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12647 int RGWRados::stop_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12649 librados::IoCtx index_ctx
;
12650 map
<int, string
> bucket_objs
;
12651 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12655 return CLSRGWIssueBucketBILogStop(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12658 int RGWRados::bi_get_instance(const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, rgw_bucket_dir_entry
*dirent
)
12661 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
12666 rgw_cls_bi_entry bi_entry
;
12667 r
= bi_get(obj
.bucket
, obj
, InstanceIdx
, &bi_entry
);
12668 if (r
< 0 && r
!= -ENOENT
) {
12669 ldout(cct
, 0) << "ERROR: bi_get() returned r=" << r
<< dendl
;
12674 bufferlist::iterator iter
= bi_entry
.data
.begin();
12676 ::decode(*dirent
, iter
);
12677 } catch (buffer::error
& err
) {
12678 ldout(cct
, 0) << "ERROR: failed to decode bi_entry()" << dendl
;
12685 int RGWRados::bi_get(rgw_bucket
& bucket
, rgw_obj
& obj
, BIIndexType index_type
, rgw_cls_bi_entry
*entry
)
12687 BucketShard
bs(this);
12688 int ret
= bs
.init(bucket
, obj
);
12690 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12694 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12696 ret
= cls_rgw_bi_get(bs
.index_ctx
, bs
.bucket_obj
, index_type
, key
, entry
);
12703 void RGWRados::bi_put(ObjectWriteOperation
& op
, BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12705 cls_rgw_bi_put(op
, bs
.bucket_obj
, entry
);
12708 int RGWRados::bi_put(BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12710 int ret
= cls_rgw_bi_put(bs
.index_ctx
, bs
.bucket_obj
, entry
);
12717 int RGWRados::bi_put(rgw_bucket
& bucket
, rgw_obj
& obj
, rgw_cls_bi_entry
& entry
)
12719 BucketShard
bs(this);
12720 int ret
= bs
.init(bucket
, obj
);
12722 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12726 return bi_put(bs
, entry
);
12729 int RGWRados::bi_list(rgw_bucket
& bucket
, const string
& obj_name
, const string
& marker
, uint32_t max
, list
<rgw_cls_bi_entry
> *entries
, bool *is_truncated
)
12731 rgw_obj
obj(bucket
, obj_name
);
12732 BucketShard
bs(this);
12733 int ret
= bs
.init(bucket
, obj
);
12735 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12739 ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, obj_name
, marker
, max
, entries
, is_truncated
);
12740 if (ret
== -ENOENT
) {
12741 *is_truncated
= false;
12749 int RGWRados::bi_list(BucketShard
& bs
, const string
& filter_obj
, const string
& marker
, uint32_t max
, list
<rgw_cls_bi_entry
> *entries
, bool *is_truncated
)
12751 int ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, filter_obj
, marker
, max
, entries
, is_truncated
);
12758 int RGWRados::bi_remove(BucketShard
& bs
)
12760 int ret
= bs
.index_ctx
.remove(bs
.bucket_obj
);
12761 if (ret
== -ENOENT
) {
12765 ldout(cct
, 5) << "bs.index_ctx.remove(" << bs
.bucket_obj
<< ") returned ret=" << ret
<< dendl
;
12772 int RGWRados::bi_list(rgw_bucket
& bucket
, int shard_id
, const string
& filter_obj
, const string
& marker
, uint32_t max
, list
<rgw_cls_bi_entry
> *entries
, bool *is_truncated
)
12774 BucketShard
bs(this);
12775 int ret
= bs
.init(bucket
, shard_id
);
12777 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12781 return bi_list(bs
, filter_obj
, marker
, max
, entries
, is_truncated
);
12784 int RGWRados::gc_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12786 return gc_pool_ctx
.operate(oid
, op
);
12789 int RGWRados::gc_aio_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12791 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
12792 int r
= gc_pool_ctx
.aio_operate(oid
, c
, op
);
12797 int RGWRados::gc_operate(string
& oid
, librados::ObjectReadOperation
*op
, bufferlist
*pbl
)
12799 return gc_pool_ctx
.operate(oid
, op
, pbl
);
12802 int RGWRados::list_gc_objs(int *index
, string
& marker
, uint32_t max
, bool expired_only
, std::list
<cls_rgw_gc_obj_info
>& result
, bool *truncated
)
12804 return gc
->list(index
, marker
, max
, expired_only
, result
, truncated
);
12807 int RGWRados::process_gc()
12809 return gc
->process();
12812 int RGWRados::list_lc_progress(const string
& marker
, uint32_t max_entries
, map
<string
, int> *progress_map
)
12814 return lc
->list_lc_progress(marker
, max_entries
, progress_map
);
12817 int RGWRados::process_lc()
12819 return lc
->process();
12822 int RGWRados::process_expire_objects()
12824 obj_expirer
->inspect_all_shards(utime_t(), ceph_clock_now());
12828 int RGWRados::cls_rgw_init_index(librados::IoCtx
& index_ctx
, librados::ObjectWriteOperation
& op
, string
& oid
)
12831 cls_rgw_bucket_init(op
);
12832 return index_ctx
.operate(oid
, &op
);
12835 int RGWRados::cls_obj_prepare_op(BucketShard
& bs
, RGWModifyOp op
, string
& tag
,
12836 rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12838 rgw_zone_set zones_trace
;
12839 if (_zones_trace
) {
12840 zones_trace
= *_zones_trace
;
12843 zones_trace
.insert(get_zone().id
);
12846 ObjectWriteOperation o
;
12847 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12848 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12849 cls_rgw_bucket_prepare_op(o
, op
, tag
, key
, obj
.key
.get_loc(), get_zone().log_data
, bilog_flags
, zones_trace
);
12850 return bs
.index_ctx
.operate(bs
.bucket_obj
, &o
);
12853 int RGWRados::cls_obj_complete_op(BucketShard
& bs
, const rgw_obj
& obj
, RGWModifyOp op
, string
& tag
,
12854 int64_t pool
, uint64_t epoch
,
12855 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12856 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12858 ObjectWriteOperation o
;
12859 rgw_bucket_dir_entry_meta dir_meta
;
12860 dir_meta
= ent
.meta
;
12861 dir_meta
.category
= category
;
12863 rgw_bucket_entry_ver ver
;
12866 cls_rgw_obj_key
key(ent
.key
.name
, ent
.key
.instance
);
12867 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12868 cls_rgw_bucket_complete_op(o
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12869 get_zone().log_data
, bilog_flags
, _zones_trace
);
12870 complete_op_data
*arg
;
12871 index_completion_manager
->create_completion(obj
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12872 get_zone().log_data
, bilog_flags
, _zones_trace
, &arg
);
12873 librados::AioCompletion
*completion
= arg
->rados_completion
;
12874 int ret
= bs
.index_ctx
.aio_operate(bs
.bucket_obj
, arg
->rados_completion
, &o
);
12875 completion
->release(); /* can't reference arg here, as it might have already been released */
12879 int RGWRados::cls_obj_complete_add(BucketShard
& bs
, const rgw_obj
& obj
, string
& tag
,
12880 int64_t pool
, uint64_t epoch
,
12881 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12882 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12884 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_ADD
, tag
, pool
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
12887 int RGWRados::cls_obj_complete_del(BucketShard
& bs
, string
& tag
,
12888 int64_t pool
, uint64_t epoch
,
12890 real_time
& removed_mtime
,
12891 list
<rgw_obj_index_key
> *remove_objs
,
12892 uint16_t bilog_flags
,
12893 rgw_zone_set
*zones_trace
)
12895 rgw_bucket_dir_entry ent
;
12896 ent
.meta
.mtime
= removed_mtime
;
12897 obj
.key
.get_index_key(&ent
.key
);
12898 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_DEL
, tag
, pool
, epoch
, ent
, RGW_OBJ_CATEGORY_NONE
, remove_objs
, bilog_flags
, zones_trace
);
12901 int RGWRados::cls_obj_complete_cancel(BucketShard
& bs
, string
& tag
, rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12903 rgw_bucket_dir_entry ent
;
12904 obj
.key
.get_index_key(&ent
.key
);
12905 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_CANCEL
, tag
, -1 /* pool id */, 0, ent
, RGW_OBJ_CATEGORY_NONE
, NULL
, bilog_flags
, zones_trace
);
12908 int RGWRados::cls_obj_set_bucket_tag_timeout(RGWBucketInfo
& bucket_info
, uint64_t timeout
)
12910 librados::IoCtx index_ctx
;
12911 map
<int, string
> bucket_objs
;
12912 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
12916 return CLSRGWIssueSetTagTimeout(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
, timeout
)();
12919 int RGWRados::cls_bucket_list(RGWBucketInfo
& bucket_info
, int shard_id
, rgw_obj_index_key
& start
, const string
& prefix
,
12920 uint32_t num_entries
, bool list_versions
, map
<string
, rgw_bucket_dir_entry
>& m
,
12921 bool *is_truncated
, rgw_obj_index_key
*last_entry
,
12922 bool (*force_check_filter
)(const string
& name
))
12924 ldout(cct
, 10) << "cls_bucket_list " << bucket_info
.bucket
<< " start " << start
.name
<< "[" << start
.instance
<< "] num_entries " << num_entries
<< dendl
;
12926 librados::IoCtx index_ctx
;
12927 // key - oid (for different shards if there is any)
12928 // value - list result for the corresponding oid (shard), it is filled by the AIO callback
12929 map
<int, string
> oids
;
12930 map
<int, struct rgw_cls_list_ret
> list_results
;
12931 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
);
12935 cls_rgw_obj_key
start_key(start
.name
, start
.instance
);
12936 r
= CLSRGWIssueBucketList(index_ctx
, start_key
, prefix
, num_entries
, list_versions
,
12937 oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12941 // Create a list of iterators that are used to iterate each shard
12942 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vcurrents(list_results
.size());
12943 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vends(list_results
.size());
12944 vector
<string
> vnames(list_results
.size());
12945 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
12946 *is_truncated
= false;
12947 for (; iter
!= list_results
.end(); ++iter
) {
12948 vcurrents
.push_back(iter
->second
.dir
.m
.begin());
12949 vends
.push_back(iter
->second
.dir
.m
.end());
12950 vnames
.push_back(oids
[iter
->first
]);
12951 *is_truncated
= (*is_truncated
|| iter
->second
.is_truncated
);
12954 // Create a map to track the next candidate entry from each shard, if the entry
12955 // from a specified shard is selected/erased, the next entry from that shard will
12956 // be inserted for next round selection
12957 map
<string
, size_t> candidates
;
12958 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
12959 if (vcurrents
[i
] != vends
[i
]) {
12960 candidates
[vcurrents
[i
]->first
] = i
;
12964 map
<string
, bufferlist
> updates
;
12965 uint32_t count
= 0;
12966 while (count
< num_entries
&& !candidates
.empty()) {
12968 // Select the next one
12969 int pos
= candidates
.begin()->second
;
12970 const string
& name
= vcurrents
[pos
]->first
;
12971 struct rgw_bucket_dir_entry
& dirent
= vcurrents
[pos
]->second
;
12973 bool force_check
= force_check_filter
&&
12974 force_check_filter(dirent
.key
.name
);
12975 if ((!dirent
.exists
&& !dirent
.is_delete_marker()) ||
12976 !dirent
.pending_map
.empty() ||
12978 /* there are uncommitted ops. We need to check the current state,
12979 * and if the tags are old we need to do cleanup as well. */
12980 librados::IoCtx sub_ctx
;
12981 sub_ctx
.dup(index_ctx
);
12982 r
= check_disk_state(sub_ctx
, bucket_info
, dirent
, dirent
, updates
[vnames
[pos
]]);
12983 if (r
< 0 && r
!= -ENOENT
) {
12988 ldout(cct
, 10) << "RGWRados::cls_bucket_list: got " << dirent
.key
.name
<< "[" << dirent
.key
.instance
<< "]" << dendl
;
12989 m
[name
] = std::move(dirent
);
12993 // Refresh the candidates map
12994 candidates
.erase(candidates
.begin());
12996 if (vcurrents
[pos
] != vends
[pos
]) {
12997 candidates
[vcurrents
[pos
]->first
] = pos
;
13001 // Suggest updates if there is any
13002 map
<string
, bufferlist
>::iterator miter
= updates
.begin();
13003 for (; miter
!= updates
.end(); ++miter
) {
13004 if (miter
->second
.length()) {
13005 ObjectWriteOperation o
;
13006 cls_rgw_suggest_changes(o
, miter
->second
);
13007 // we don't care if we lose suggested updates, send them off blindly
13008 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
13009 index_ctx
.aio_operate(miter
->first
, c
, &o
);
13014 // Check if all the returned entries are consumed or not
13015 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
13016 if (vcurrents
[i
] != vends
[i
])
13017 *is_truncated
= true;
13020 *last_entry
= m
.rbegin()->first
;
13025 int RGWRados::cls_obj_usage_log_add(const string
& oid
, rgw_usage_log_info
& info
)
13027 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13030 int r
= get_raw_obj_ref(obj
, &ref
);
13035 ObjectWriteOperation op
;
13036 cls_rgw_usage_log_add(op
, info
);
13038 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13042 int RGWRados::cls_obj_usage_log_read(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
, uint32_t max_entries
,
13043 string
& read_iter
, map
<rgw_user_bucket
, rgw_usage_log_entry
>& usage
, bool *is_truncated
)
13045 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13048 int r
= get_raw_obj_ref(obj
, &ref
);
13053 *is_truncated
= false;
13055 r
= cls_rgw_usage_log_read(ref
.ioctx
, ref
.oid
, user
, start_epoch
, end_epoch
,
13056 max_entries
, read_iter
, usage
, is_truncated
);
13061 int RGWRados::cls_obj_usage_log_trim(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
)
13063 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13066 int r
= get_raw_obj_ref(obj
, &ref
);
13071 r
= cls_rgw_usage_log_trim(ref
.ioctx
, ref
.oid
, user
, start_epoch
, end_epoch
);
13075 int RGWRados::remove_objs_from_index(RGWBucketInfo
& bucket_info
, list
<rgw_obj_index_key
>& oid_list
)
13077 librados::IoCtx index_ctx
;
13080 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
13082 int r
= open_bucket_index(bucket_info
, index_ctx
, dir_oid
);
13086 bufferlist updates
;
13088 for (auto iter
= oid_list
.begin(); iter
!= oid_list
.end(); ++iter
) {
13089 rgw_bucket_dir_entry entry
;
13091 dout(2) << "RGWRados::remove_objs_from_index bucket=" << bucket_info
.bucket
<< " obj=" << entry
.key
.name
<< ":" << entry
.key
.instance
<< dendl
;
13092 entry
.ver
.epoch
= (uint64_t)-1; // ULLONG_MAX, needed to that objclass doesn't skip out request
13093 updates
.append(CEPH_RGW_REMOVE
| suggest_flag
);
13094 ::encode(entry
, updates
);
13099 r
= index_ctx
.exec(dir_oid
, RGW_CLASS
, RGW_DIR_SUGGEST_CHANGES
, updates
, out
);
13104 int RGWRados::check_disk_state(librados::IoCtx io_ctx
,
13105 const RGWBucketInfo
& bucket_info
,
13106 rgw_bucket_dir_entry
& list_state
,
13107 rgw_bucket_dir_entry
& object
,
13108 bufferlist
& suggested_updates
)
13110 const rgw_bucket
& bucket
= bucket_info
.bucket
;
13111 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
13115 rgw_obj
obj(bucket
, list_state
.key
);
13118 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
13120 if (loc
!= list_state
.locator
) {
13121 ldout(cct
, 0) << "WARNING: generated locator (" << loc
<< ") is different from listed locator (" << list_state
.locator
<< ")" << dendl
;
13124 io_ctx
.locator_set_key(list_state
.locator
);
13126 RGWObjState
*astate
= NULL
;
13127 RGWObjectCtx
rctx(this);
13128 int r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
13132 list_state
.pending_map
.clear(); // we don't need this and it inflates size
13133 if (!astate
->exists
) {
13134 /* object doesn't exist right now -- hopefully because it's
13135 * marked as !exists and got deleted */
13136 if (list_state
.exists
) {
13137 /* FIXME: what should happen now? Work out if there are any
13138 * non-bad ways this could happen (there probably are, but annoying
13141 // encode a suggested removal of that key
13142 list_state
.ver
.epoch
= io_ctx
.get_last_version();
13143 list_state
.ver
.pool
= io_ctx
.get_id();
13144 cls_rgw_encode_suggestion(CEPH_RGW_REMOVE
, list_state
, suggested_updates
);
13149 string content_type
;
13152 object
.meta
.size
= astate
->size
;
13153 object
.meta
.accounted_size
= astate
->accounted_size
;
13154 object
.meta
.mtime
= astate
->mtime
;
13156 map
<string
, bufferlist
>::iterator iter
= astate
->attrset
.find(RGW_ATTR_ETAG
);
13157 if (iter
!= astate
->attrset
.end()) {
13158 etag
= iter
->second
.c_str();
13160 iter
= astate
->attrset
.find(RGW_ATTR_CONTENT_TYPE
);
13161 if (iter
!= astate
->attrset
.end()) {
13162 content_type
= iter
->second
.c_str();
13164 iter
= astate
->attrset
.find(RGW_ATTR_ACL
);
13165 if (iter
!= astate
->attrset
.end()) {
13166 r
= decode_policy(iter
->second
, &owner
);
13168 dout(0) << "WARNING: could not decode policy for object: " << obj
<< dendl
;
13172 if (astate
->has_manifest
) {
13173 RGWObjManifest::obj_iterator miter
;
13174 RGWObjManifest
& manifest
= astate
->manifest
;
13175 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
13176 const rgw_raw_obj
& raw_loc
= miter
.get_location().get_raw_obj(this);
13178 rgw_raw_obj_to_obj(manifest
.get_obj().bucket
, raw_loc
, &loc
);
13180 if (loc
.key
.ns
== RGW_OBJ_NS_MULTIPART
) {
13181 dout(10) << "check_disk_state(): removing manifest part from index: " << loc
<< dendl
;
13182 r
= delete_obj_index(loc
);
13184 dout(0) << "WARNING: delete_obj_index() returned r=" << r
<< dendl
;
13190 object
.meta
.etag
= etag
;
13191 object
.meta
.content_type
= content_type
;
13192 object
.meta
.owner
= owner
.get_id().to_str();
13193 object
.meta
.owner_display_name
= owner
.get_display_name();
13195 // encode suggested updates
13196 list_state
.ver
.pool
= io_ctx
.get_id();
13197 list_state
.ver
.epoch
= astate
->epoch
;
13198 list_state
.meta
.size
= object
.meta
.size
;
13199 list_state
.meta
.accounted_size
= object
.meta
.accounted_size
;
13200 list_state
.meta
.mtime
= object
.meta
.mtime
;
13201 list_state
.meta
.category
= main_category
;
13202 list_state
.meta
.etag
= etag
;
13203 list_state
.meta
.content_type
= content_type
;
13204 if (astate
->obj_tag
.length() > 0)
13205 list_state
.tag
= astate
->obj_tag
.c_str();
13206 list_state
.meta
.owner
= owner
.get_id().to_str();
13207 list_state
.meta
.owner_display_name
= owner
.get_display_name();
13209 list_state
.exists
= true;
13210 cls_rgw_encode_suggestion(CEPH_RGW_UPDATE
| suggest_flag
, list_state
, suggested_updates
);
13214 int RGWRados::cls_bucket_head(const RGWBucketInfo
& bucket_info
, int shard_id
, map
<string
, struct rgw_bucket_dir_header
>& headers
, map
<int, string
> *bucket_instance_ids
)
13216 librados::IoCtx index_ctx
;
13217 map
<int, string
> oids
;
13218 map
<int, struct rgw_cls_list_ret
> list_results
;
13219 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, list_results
, shard_id
, bucket_instance_ids
);
13223 r
= CLSRGWIssueGetDirHeader(index_ctx
, oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
13227 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
13228 for(; iter
!= list_results
.end(); ++iter
) {
13229 headers
[oids
[iter
->first
]] = iter
->second
.dir
.header
;
13234 int RGWRados::cls_bucket_head_async(const RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetDirHeader_CB
*ctx
, int *num_aio
)
13236 librados::IoCtx index_ctx
;
13237 map
<int, string
> bucket_objs
;
13238 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
13242 map
<int, string
>::iterator iter
= bucket_objs
.begin();
13243 for (; iter
!= bucket_objs
.end(); ++iter
) {
13244 r
= cls_rgw_get_dir_header_async(index_ctx
, iter
->second
, static_cast<RGWGetDirHeader_CB
*>(ctx
->get()));
13255 int RGWRados::cls_user_get_header(const string
& user_id
, cls_user_header
*header
)
13257 string buckets_obj_id
;
13258 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13259 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13262 int r
= get_raw_obj_ref(obj
, &ref
);
13267 librados::ObjectReadOperation op
;
13269 ::cls_user_get_header(op
, header
, &rc
);
13271 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13280 int RGWRados::cls_user_get_header_async(const string
& user_id
, RGWGetUserHeader_CB
*ctx
)
13282 string buckets_obj_id
;
13283 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13284 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13287 int r
= get_raw_obj_ref(obj
, &ref
);
13292 r
= ::cls_user_get_header_async(ref
.ioctx
, ref
.oid
, ctx
);
13299 int RGWRados::cls_user_sync_bucket_stats(rgw_raw_obj
& user_obj
, const RGWBucketInfo
& bucket_info
)
13301 map
<string
, struct rgw_bucket_dir_header
> headers
;
13302 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13304 ldout(cct
, 20) << "cls_bucket_header() returned " << r
<< dendl
;
13308 cls_user_bucket_entry entry
;
13310 bucket_info
.bucket
.convert(&entry
.bucket
);
13312 for (const auto& hiter
: headers
) {
13313 for (const auto& iter
: hiter
.second
.stats
) {
13314 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13315 entry
.size
+= header_stats
.total_size
;
13316 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13317 entry
.count
+= header_stats
.num_entries
;
13321 list
<cls_user_bucket_entry
> entries
;
13322 entries
.push_back(entry
);
13324 r
= cls_user_update_buckets(user_obj
, entries
, false);
13326 ldout(cct
, 20) << "cls_user_update_buckets() returned " << r
<< dendl
;
13333 int RGWRados::cls_user_get_bucket_stats(const rgw_bucket
& bucket
, cls_user_bucket_entry
& entry
)
13335 map
<string
, struct rgw_bucket_dir_header
> headers
;
13336 RGWBucketInfo bucket_info
;
13337 RGWObjectCtx
obj_ctx(this);
13338 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
13343 ret
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13345 ldout(cct
, 20) << "cls_bucket_header() returned " << ret
<< dendl
;
13349 bucket
.convert(&entry
.bucket
);
13351 for (const auto& hiter
: headers
) {
13352 for (const auto& iter
: hiter
.second
.stats
) {
13353 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13354 entry
.size
+= header_stats
.total_size
;
13355 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13356 entry
.count
+= header_stats
.num_entries
;
13363 int RGWRados::cls_user_list_buckets(rgw_raw_obj
& obj
,
13364 const string
& in_marker
,
13365 const string
& end_marker
,
13366 const int max_entries
,
13367 list
<cls_user_bucket_entry
>& entries
,
13368 string
* const out_marker
,
13369 bool * const truncated
)
13372 int r
= get_raw_obj_ref(obj
, &ref
);
13377 librados::ObjectReadOperation op
;
13380 cls_user_bucket_list(op
, in_marker
, end_marker
, max_entries
, entries
, out_marker
, truncated
, &rc
);
13382 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13391 int RGWRados::cls_user_update_buckets(rgw_raw_obj
& obj
, list
<cls_user_bucket_entry
>& entries
, bool add
)
13394 int r
= get_raw_obj_ref(obj
, &ref
);
13399 librados::ObjectWriteOperation op
;
13400 cls_user_set_buckets(op
, entries
, add
);
13401 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13408 int RGWRados::complete_sync_user_stats(const rgw_user
& user_id
)
13410 string buckets_obj_id
;
13411 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13412 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13413 return cls_user_complete_stats_sync(obj
);
13416 int RGWRados::cls_user_complete_stats_sync(rgw_raw_obj
& obj
)
13419 int r
= get_raw_obj_ref(obj
, &ref
);
13424 librados::ObjectWriteOperation op
;
13425 ::cls_user_complete_stats_sync(op
);
13426 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13433 int RGWRados::cls_user_add_bucket(rgw_raw_obj
& obj
, const cls_user_bucket_entry
& entry
)
13435 list
<cls_user_bucket_entry
> l
;
13436 l
.push_back(entry
);
13438 return cls_user_update_buckets(obj
, l
, true);
13441 int RGWRados::cls_user_remove_bucket(rgw_raw_obj
& obj
, const cls_user_bucket
& bucket
)
13444 int r
= get_system_obj_ref(obj
, &ref
);
13449 librados::ObjectWriteOperation op
;
13450 ::cls_user_remove_bucket(op
, bucket
);
13451 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13458 int RGWRados::check_bucket_shards(const RGWBucketInfo
& bucket_info
, const rgw_bucket
& bucket
,
13459 RGWQuotaInfo
& bucket_quota
)
13461 if (!cct
->_conf
->rgw_dynamic_resharding
) {
13465 bool need_resharding
= false;
13466 int num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13467 uint32_t suggested_num_shards
;
13469 int ret
= quota_handler
->check_bucket_shards((uint64_t)cct
->_conf
->rgw_max_objs_per_shard
,
13470 num_source_shards
, bucket_info
.owner
, bucket
, bucket_quota
,
13471 1, need_resharding
, &suggested_num_shards
);
13476 if (need_resharding
) {
13477 ldout(cct
, 20) << __func__
<< " bucket " << bucket
.name
<< " need resharding " <<
13478 " old num shards " << bucket_info
.num_shards
<< " new num shards " << suggested_num_shards
<<
13480 return add_bucket_to_reshard(bucket_info
, suggested_num_shards
);
13486 int RGWRados::add_bucket_to_reshard(const RGWBucketInfo
& bucket_info
, uint32_t new_num_shards
)
13488 RGWReshard
reshard(this);
13490 uint32_t num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13492 new_num_shards
= min(new_num_shards
, get_max_bucket_shards());
13493 if (new_num_shards
<= num_source_shards
) {
13494 ldout(cct
, 20) << "not resharding bucket name=" << bucket_info
.bucket
.name
<< ", orig_num=" << num_source_shards
<< ", new_num_shards=" << new_num_shards
<< dendl
;
13498 cls_rgw_reshard_entry entry
;
13499 entry
.time
= real_clock::now();
13500 entry
.tenant
= bucket_info
.owner
.tenant
;
13501 entry
.bucket_name
= bucket_info
.bucket
.name
;
13502 entry
.bucket_id
= bucket_info
.bucket
.bucket_id
;
13503 entry
.old_num_shards
= num_source_shards
;
13504 entry
.new_num_shards
= new_num_shards
;
13506 return reshard
.add(entry
);
13509 int RGWRados::check_quota(const rgw_user
& bucket_owner
, rgw_bucket
& bucket
,
13510 RGWQuotaInfo
& user_quota
, RGWQuotaInfo
& bucket_quota
, uint64_t obj_size
)
13512 return quota_handler
->check_quota(bucket_owner
, bucket
, user_quota
, bucket_quota
, 1, obj_size
);
13515 void RGWRados::get_bucket_index_objects(const string
& bucket_oid_base
,
13516 uint32_t num_shards
, map
<int, string
>& bucket_objects
, int shard_id
)
13519 bucket_objects
[0] = bucket_oid_base
;
13521 char buf
[bucket_oid_base
.size() + 32];
13522 if (shard_id
< 0) {
13523 for (uint32_t i
= 0; i
< num_shards
; ++i
) {
13524 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), i
);
13525 bucket_objects
[i
] = buf
;
13528 if ((uint32_t)shard_id
> num_shards
) {
13531 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13532 bucket_objects
[shard_id
] = buf
;
13537 void RGWRados::get_bucket_instance_ids(const RGWBucketInfo
& bucket_info
, int shard_id
, map
<int, string
> *result
)
13539 const rgw_bucket
& bucket
= bucket_info
.bucket
;
13540 string plain_id
= bucket
.name
+ ":" + bucket
.bucket_id
;
13541 if (!bucket_info
.num_shards
) {
13542 (*result
)[0] = plain_id
;
13545 if (shard_id
< 0) {
13546 for (uint32_t i
= 0; i
< bucket_info
.num_shards
; ++i
) {
13547 snprintf(buf
, sizeof(buf
), ":%d", i
);
13548 (*result
)[i
] = plain_id
+ buf
;
13551 if ((uint32_t)shard_id
> bucket_info
.num_shards
) {
13554 snprintf(buf
, sizeof(buf
), ":%d", shard_id
);
13555 (*result
)[shard_id
] = plain_id
+ buf
;
13560 int RGWRados::get_target_shard_id(const RGWBucketInfo
& bucket_info
, const string
& obj_key
,
13564 switch (bucket_info
.bucket_index_shard_hash_type
) {
13565 case RGWBucketInfo::MOD
:
13566 if (!bucket_info
.num_shards
) {
13571 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13572 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13573 sid
= rgw_shards_mod(sid2
, bucket_info
.num_shards
);
13575 *shard_id
= (int)sid
;
13585 void RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, uint32_t num_shards
,
13586 int shard_id
, string
*bucket_obj
)
13589 // By default with no sharding, we use the bucket oid as itself
13590 (*bucket_obj
) = bucket_oid_base
;
13592 char buf
[bucket_oid_base
.size() + 32];
13593 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13594 (*bucket_obj
) = buf
;
13598 int RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, const string
& obj_key
,
13599 uint32_t num_shards
, RGWBucketInfo::BIShardsHashType hash_type
, string
*bucket_obj
, int *shard_id
)
13602 switch (hash_type
) {
13603 case RGWBucketInfo::MOD
:
13605 // By default with no sharding, we use the bucket oid as itself
13606 (*bucket_obj
) = bucket_oid_base
;
13611 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13612 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13613 sid
= rgw_shards_mod(sid2
, num_shards
);
13614 char buf
[bucket_oid_base
.size() + 32];
13615 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), sid
);
13616 (*bucket_obj
) = buf
;
13618 *shard_id
= (int)sid
;
13628 void RGWStateLog::oid_str(int shard
, string
& oid
) {
13629 oid
= RGW_STATELOG_OBJ_PREFIX
+ module_name
+ ".";
13631 snprintf(buf
, sizeof(buf
), "%d", shard
);
13635 int RGWStateLog::get_shard_num(const string
& object
) {
13636 uint32_t val
= ceph_str_hash_linux(object
.c_str(), object
.length());
13637 return val
% num_shards
;
13640 string
RGWStateLog::get_oid(const string
& object
) {
13641 int shard
= get_shard_num(object
);
13643 oid_str(shard
, oid
);
13647 int RGWStateLog::open_ioctx(librados::IoCtx
& ioctx
) {
13649 store
->get_log_pool(pool
);
13650 int r
= rgw_init_ioctx(store
->get_rados_handle(), pool
, ioctx
);
13652 lderr(store
->ctx()) << "ERROR: could not open rados pool" << dendl
;
13658 int RGWStateLog::store_entry(const string
& client_id
, const string
& op_id
, const string
& object
,
13659 uint32_t state
, bufferlist
*bl
, uint32_t *check_state
)
13661 if (client_id
.empty() ||
13664 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13667 librados::IoCtx ioctx
;
13668 int r
= open_ioctx(ioctx
);
13672 string oid
= get_oid(object
);
13674 librados::ObjectWriteOperation op
;
13676 cls_statelog_check_state(op
, client_id
, op_id
, object
, *check_state
);
13678 utime_t ts
= ceph_clock_now();
13680 cls_statelog_add(op
, client_id
, op_id
, object
, ts
, state
, (bl
? *bl
: nobl
));
13681 r
= ioctx
.operate(oid
, &op
);
13689 int RGWStateLog::remove_entry(const string
& client_id
, const string
& op_id
, const string
& object
)
13691 if (client_id
.empty() ||
13694 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13697 librados::IoCtx ioctx
;
13698 int r
= open_ioctx(ioctx
);
13702 string oid
= get_oid(object
);
13704 librados::ObjectWriteOperation op
;
13705 cls_statelog_remove_by_object(op
, object
, op_id
);
13706 r
= ioctx
.operate(oid
, &op
);
13714 void RGWStateLog::init_list_entries(const string
& client_id
, const string
& op_id
, const string
& object
,
13717 list_state
*state
= new list_state
;
13718 state
->client_id
= client_id
;
13719 state
->op_id
= op_id
;
13720 state
->object
= object
;
13721 if (object
.empty()) {
13722 state
->cur_shard
= 0;
13723 state
->max_shard
= num_shards
- 1;
13725 state
->cur_shard
= state
->max_shard
= get_shard_num(object
);
13727 *handle
= (void *)state
;
13730 int RGWStateLog::list_entries(void *handle
, int max_entries
,
13731 list
<cls_statelog_entry
>& entries
,
13734 list_state
*state
= static_cast<list_state
*>(handle
);
13736 librados::IoCtx ioctx
;
13737 int r
= open_ioctx(ioctx
);
13743 for (; state
->cur_shard
<= state
->max_shard
&& max_entries
> 0; ++state
->cur_shard
) {
13745 oid_str(state
->cur_shard
, oid
);
13747 librados::ObjectReadOperation op
;
13748 list
<cls_statelog_entry
> ents
;
13750 cls_statelog_list(op
, state
->client_id
, state
->op_id
, state
->object
, state
->marker
,
13751 max_entries
, ents
, &state
->marker
, &truncated
);
13753 r
= ioctx
.operate(oid
, &op
, &ibl
);
13754 if (r
== -ENOENT
) {
13759 ldout(store
->ctx(), 0) << "cls_statelog_list returned " << r
<< dendl
;
13764 state
->marker
.clear();
13767 max_entries
-= ents
.size();
13769 entries
.splice(entries
.end(), ents
);
13775 *done
= (state
->cur_shard
> state
->max_shard
);
13780 void RGWStateLog::finish_list_entries(void *handle
)
13782 list_state
*state
= static_cast<list_state
*>(handle
);
13786 void RGWStateLog::dump_entry(const cls_statelog_entry
& entry
, Formatter
*f
)
13788 f
->open_object_section("statelog_entry");
13789 f
->dump_string("client_id", entry
.client_id
);
13790 f
->dump_string("op_id", entry
.op_id
);
13791 f
->dump_string("object", entry
.object
);
13792 entry
.timestamp
.gmtime_nsec(f
->dump_stream("timestamp"));
13793 if (!dump_entry_internal(entry
, f
)) {
13794 f
->dump_int("state", entry
.state
);
13796 f
->close_section();
13799 RGWOpState::RGWOpState(RGWRados
*_store
) : RGWStateLog(_store
, _store
->ctx()->_conf
->rgw_num_zone_opstate_shards
, string("obj_opstate"))
13803 bool RGWOpState::dump_entry_internal(const cls_statelog_entry
& entry
, Formatter
*f
)
13806 switch ((OpState
)entry
.state
) {
13807 case OPSTATE_UNKNOWN
:
13810 case OPSTATE_IN_PROGRESS
:
13813 case OPSTATE_COMPLETE
:
13816 case OPSTATE_ERROR
:
13819 case OPSTATE_ABORT
:
13822 case OPSTATE_CANCELLED
:
13828 f
->dump_string("state", s
);
13832 int RGWOpState::state_from_str(const string
& s
, OpState
*state
)
13834 if (s
== "unknown") {
13835 *state
= OPSTATE_UNKNOWN
;
13836 } else if (s
== "in-progress") {
13837 *state
= OPSTATE_IN_PROGRESS
;
13838 } else if (s
== "complete") {
13839 *state
= OPSTATE_COMPLETE
;
13840 } else if (s
== "error") {
13841 *state
= OPSTATE_ERROR
;
13842 } else if (s
== "abort") {
13843 *state
= OPSTATE_ABORT
;
13844 } else if (s
== "cancelled") {
13845 *state
= OPSTATE_CANCELLED
;
13853 int RGWOpState::set_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13855 uint32_t s
= (uint32_t)state
;
13856 return store_entry(client_id
, op_id
, object
, s
, NULL
, NULL
);
13859 int RGWOpState::renew_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13861 uint32_t s
= (uint32_t)state
;
13862 return store_entry(client_id
, op_id
, object
, s
, NULL
, &s
);
13865 RGWOpStateSingleOp::RGWOpStateSingleOp(RGWRados
*store
, const string
& cid
, const string
& oid
,
13866 const string
& obj
) : os(store
), client_id(cid
), op_id(oid
), object(obj
)
13868 cct
= store
->ctx();
13869 cur_state
= RGWOpState::OPSTATE_UNKNOWN
;
13872 int RGWOpStateSingleOp::set_state(RGWOpState::OpState state
) {
13873 last_update
= real_clock::now();
13875 return os
.set_state(client_id
, op_id
, object
, state
);
13878 int RGWOpStateSingleOp::renew_state() {
13879 real_time now
= real_clock::now();
13881 int rate_limit_sec
= cct
->_conf
->rgw_opstate_ratelimit_sec
;
13883 if (rate_limit_sec
&& now
- last_update
< make_timespan(rate_limit_sec
)) {
13888 return os
.renew_state(client_id
, op_id
, object
, cur_state
);
13892 uint64_t RGWRados::instance_id()
13894 return get_rados_handle()->get_instance_id();
13897 uint64_t RGWRados::next_bucket_id()
13899 Mutex::Locker
l(bucket_id_lock
);
13900 return ++max_bucket_id
;
13903 RGWRados
*RGWStoreManager::init_storage_provider(CephContext
*cct
, bool use_gc_thread
, bool use_lc_thread
, bool quota_threads
, bool run_sync_thread
, bool run_reshard_thread
)
13905 int use_cache
= cct
->_conf
->rgw_cache_enabled
;
13906 RGWRados
*store
= NULL
;
13908 store
= new RGWRados
;
13910 store
= new RGWCache
<RGWRados
>;
13913 if (store
->initialize(cct
, use_gc_thread
, use_lc_thread
, quota_threads
, run_sync_thread
, run_reshard_thread
) < 0) {
13921 RGWRados
*RGWStoreManager::init_raw_storage_provider(CephContext
*cct
)
13923 RGWRados
*store
= NULL
;
13924 store
= new RGWRados
;
13926 store
->set_context(cct
);
13928 if (store
->init_rados() < 0) {
13936 void RGWStoreManager::close_storage(RGWRados
*store
)
13946 librados::Rados
* RGWRados::get_rados_handle()
13948 if (rados
.size() == 1) {
13951 handle_lock
.get_read();
13952 pthread_t id
= pthread_self();
13953 std::map
<pthread_t
, int>:: iterator it
= rados_map
.find(id
);
13955 if (it
!= rados_map
.end()) {
13956 handle_lock
.put_read();
13957 return &rados
[it
->second
];
13959 handle_lock
.put_read();
13960 handle_lock
.get_write();
13961 const uint32_t handle
= next_rados_handle
;
13962 rados_map
[id
] = handle
;
13963 if (++next_rados_handle
== rados
.size()) {
13964 next_rados_handle
= 0;
13966 handle_lock
.put_write();
13967 return &rados
[handle
];
13972 int RGWRados::delete_raw_obj_aio(const rgw_raw_obj
& obj
, list
<librados::AioCompletion
*>& handles
)
13975 int ret
= get_raw_obj_ref(obj
, &ref
);
13977 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
13981 ObjectWriteOperation op
;
13982 list
<string
> prefixes
;
13983 cls_rgw_remove_obj(op
, prefixes
);
13985 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
13986 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
13988 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
13993 handles
.push_back(c
);
13998 int RGWRados::delete_obj_aio(const rgw_obj
& obj
,
13999 RGWBucketInfo
& bucket_info
, RGWObjState
*astate
,
14000 list
<librados::AioCompletion
*>& handles
, bool keep_index_consistent
)
14003 int ret
= get_obj_head_ref(bucket_info
, obj
, &ref
);
14005 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
14009 if (keep_index_consistent
) {
14010 RGWRados::Bucket
bop(this, bucket_info
);
14011 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
14013 ret
= index_op
.prepare(CLS_RGW_OP_DEL
, &astate
->write_tag
);
14015 lderr(cct
) << "ERROR: failed to prepare index op with ret=" << ret
<< dendl
;
14020 ObjectWriteOperation op
;
14021 list
<string
> prefixes
;
14022 cls_rgw_remove_obj(op
, prefixes
);
14024 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
14025 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
14027 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
14032 handles
.push_back(c
);
14034 if (keep_index_consistent
) {
14035 ret
= delete_obj_index(obj
);
14037 lderr(cct
) << "ERROR: failed to delete obj index with ret=" << ret
<< dendl
;
14044 int rgw_compression_info_from_attrset(map
<string
, bufferlist
>& attrs
, bool& need_decompress
, RGWCompressionInfo
& cs_info
) {
14045 map
<string
, bufferlist
>::iterator value
= attrs
.find(RGW_ATTR_COMPRESSION
);
14046 if (value
!= attrs
.end()) {
14047 bufferlist::iterator bliter
= value
->second
.begin();
14049 ::decode(cs_info
, bliter
);
14050 } catch (buffer::error
& err
) {
14053 if (cs_info
.blocks
.size() == 0) {
14056 if (cs_info
.compression_type
!= "none")
14057 need_decompress
= true;
14059 need_decompress
= false;
14062 need_decompress
= false;
14067 bool RGWRados::call(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
14070 if (command
== "cache list") {
14071 boost::optional
<std::string
> filter
;
14072 auto i
= cmdmap
.find("filter");
14073 if (i
!= cmdmap
.cend()) {
14074 filter
= boost::get
<std::string
>(i
->second
);
14076 std::unique_ptr
<Formatter
> f(ceph::Formatter::create(format
, "table"));
14078 f
->open_array_section("cache_entries");
14079 call_list(filter
, f
.get());
14080 f
->close_section();
14084 out
.append("Unable to create Formatter.\n");
14087 } else if (command
== "cache inspect") {
14088 std::unique_ptr
<Formatter
> f(ceph::Formatter::create(format
, "json-pretty"));
14090 const auto& target
= boost::get
<std::string
>(cmdmap
["target"]);
14091 if (call_inspect(target
, f
.get())) {
14095 out
.append(string("Unable to find entry ") + target
+ string(".\n"));
14099 out
.append("Unable to create Formatter.\n");
14102 } else if (command
== "cache erase") {
14103 const auto& target
= boost::get
<std::string
>(cmdmap
["target"]);
14104 if (call_erase(target
)) {
14107 out
.append(string("Unable to find entry ") + target
+ string(".\n"));
14110 } else if (command
== "cache zap") {
14117 void RGWRados::call_list(const boost::optional
<std::string
>&,
14123 bool RGWRados::call_inspect(const std::string
&, Formatter
*)
14128 bool RGWRados::call_erase(const std::string
&) {
14132 void RGWRados::call_zap() {