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."
5781 << " pg_num < pgp_num or mon_max_pg_per_osd exceeded)"
5787 librados::IoCtx io_ctx
;
5788 ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctx
);
5792 ret
= io_ctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
5793 if (ret
< 0 && ret
!= -EOPNOTSUPP
)
5798 int RGWRados::init_bucket_index(RGWBucketInfo
& bucket_info
, int num_shards
)
5800 librados::IoCtx index_ctx
; // context for new bucket
5802 string dir_oid
= dir_oid_prefix
;
5803 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
5808 dir_oid
.append(bucket_info
.bucket
.bucket_id
);
5810 map
<int, string
> bucket_objs
;
5811 get_bucket_index_objects(dir_oid
, num_shards
, bucket_objs
);
5813 return CLSRGWIssueBucketIndexInit(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
5816 void RGWRados::create_bucket_id(string
*bucket_id
)
5818 uint64_t iid
= instance_id();
5819 uint64_t bid
= next_bucket_id();
5820 char buf
[get_zone_params().get_id().size() + 48];
5821 snprintf(buf
, sizeof(buf
), "%s.%llu.%llu", get_zone_params().get_id().c_str(), (long long)iid
, (long long)bid
);
5825 int RGWRados::create_bucket(RGWUserInfo
& owner
, rgw_bucket
& bucket
,
5826 const string
& zonegroup_id
,
5827 const string
& placement_rule
,
5828 const string
& swift_ver_location
,
5829 const RGWQuotaInfo
* pquota_info
,
5830 map
<std::string
, bufferlist
>& attrs
,
5831 RGWBucketInfo
& info
,
5833 obj_version
*pep_objv
,
5834 real_time creation_time
,
5835 rgw_bucket
*pmaster_bucket
,
5836 uint32_t *pmaster_num_shards
,
5839 #define MAX_CREATE_RETRIES 20 /* need to bound retries */
5840 string selected_placement_rule_name
;
5841 RGWZonePlacementInfo rule_info
;
5843 for (int i
= 0; i
< MAX_CREATE_RETRIES
; i
++) {
5845 ret
= select_bucket_placement(owner
, zonegroup_id
, placement_rule
,
5846 &selected_placement_rule_name
, &rule_info
);
5850 if (!pmaster_bucket
) {
5851 create_bucket_id(&bucket
.marker
);
5852 bucket
.bucket_id
= bucket
.marker
;
5854 bucket
.marker
= pmaster_bucket
->marker
;
5855 bucket
.bucket_id
= pmaster_bucket
->bucket_id
;
5858 RGWObjVersionTracker
& objv_tracker
= info
.objv_tracker
;
5861 objv_tracker
.write_version
= *pobjv
;
5863 objv_tracker
.generate_new_write_ver(cct
);
5866 info
.bucket
= bucket
;
5867 info
.owner
= owner
.user_id
;
5868 info
.zonegroup
= zonegroup_id
;
5869 info
.placement_rule
= selected_placement_rule_name
;
5870 info
.index_type
= rule_info
.index_type
;
5871 info
.swift_ver_location
= swift_ver_location
;
5872 info
.swift_versioning
= (!swift_ver_location
.empty());
5873 if (pmaster_num_shards
) {
5874 info
.num_shards
= *pmaster_num_shards
;
5876 info
.num_shards
= bucket_index_max_shards
;
5878 info
.bucket_index_shard_hash_type
= RGWBucketInfo::MOD
;
5879 info
.requester_pays
= false;
5880 if (real_clock::is_zero(creation_time
)) {
5881 info
.creation_time
= ceph::real_clock::now();
5883 info
.creation_time
= creation_time
;
5886 info
.quota
= *pquota_info
;
5889 int r
= init_bucket_index(info
, info
.num_shards
);
5894 ret
= put_linked_bucket_info(info
, exclusive
, ceph::real_time(), pep_objv
, &attrs
, true);
5895 if (ret
== -EEXIST
) {
5896 librados::IoCtx index_ctx
;
5897 map
<int, string
> bucket_objs
;
5898 int r
= open_bucket_index(info
, index_ctx
, bucket_objs
);
5902 /* we need to reread the info and return it, caller will have a use for it */
5903 RGWObjVersionTracker instance_ver
= info
.objv_tracker
;
5904 info
.objv_tracker
.clear();
5905 RGWObjectCtx
obj_ctx(this);
5906 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, NULL
);
5911 ldout(cct
, 0) << "get_bucket_info returned " << r
<< dendl
;
5915 /* only remove it if it's a different bucket instance */
5916 if (info
.bucket
.bucket_id
!= bucket
.bucket_id
) {
5917 /* remove bucket meta instance */
5918 string entry
= bucket
.get_key();
5919 r
= rgw_bucket_instance_remove_entry(this, entry
, &instance_ver
);
5923 map
<int, string
>::const_iterator biter
;
5924 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
5925 // Do best effort removal
5926 index_ctx
.remove(biter
->second
);
5929 /* ret == -ENOENT here */
5934 /* this is highly unlikely */
5935 ldout(cct
, 0) << "ERROR: could not create bucket, continuously raced with bucket creation and removal" << dendl
;
5939 int RGWRados::select_new_bucket_location(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& request_rule
,
5940 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
5943 /* first check that zonegroup exists within current period. */
5944 RGWZoneGroup zonegroup
;
5945 int ret
= get_zonegroup(zonegroup_id
, zonegroup
);
5947 ldout(cct
, 0) << "could not find zonegroup " << zonegroup_id
<< " in current period" << dendl
;
5951 /* find placement rule. Hierarchy: request rule > user default rule > zonegroup default rule */
5952 std::map
<std::string
, RGWZoneGroupPlacementTarget
>::const_iterator titer
;
5954 if (!request_rule
.empty()) {
5955 titer
= zonegroup
.placement_targets
.find(request_rule
);
5956 if (titer
== zonegroup
.placement_targets
.end()) {
5957 ldout(cct
, 0) << "could not find requested placement id " << request_rule
5958 << " within zonegroup " << dendl
;
5959 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5961 } else if (!user_info
.default_placement
.empty()) {
5962 titer
= zonegroup
.placement_targets
.find(user_info
.default_placement
);
5963 if (titer
== zonegroup
.placement_targets
.end()) {
5964 ldout(cct
, 0) << "could not find user default placement id " << user_info
.default_placement
5965 << " within zonegroup " << dendl
;
5966 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5969 if (zonegroup
.default_placement
.empty()) { // zonegroup default rule as fallback, it should not be empty.
5970 ldout(cct
, 0) << "misconfiguration, zonegroup default placement id should not be empty." << dendl
;
5971 return -ERR_ZONEGROUP_DEFAULT_PLACEMENT_MISCONFIGURATION
;
5973 titer
= zonegroup
.placement_targets
.find(zonegroup
.default_placement
);
5974 if (titer
== zonegroup
.placement_targets
.end()) {
5975 ldout(cct
, 0) << "could not find zonegroup default placement id " << zonegroup
.default_placement
5976 << " within zonegroup " << dendl
;
5977 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5982 /* now check tag for the rule, whether user is permitted to use rule */
5983 const auto& target_rule
= titer
->second
;
5984 if (!target_rule
.user_permitted(user_info
.placement_tags
)) {
5985 ldout(cct
, 0) << "user not permitted to use placement rule " << titer
->first
<< dendl
;
5989 if (pselected_rule_name
)
5990 *pselected_rule_name
= titer
->first
;
5992 return select_bucket_location_by_rule(titer
->first
, rule_info
);
5995 int RGWRados::select_bucket_location_by_rule(const string
& location_rule
, RGWZonePlacementInfo
*rule_info
)
5997 if (location_rule
.empty()) {
5998 /* we can only reach here if we're trying to set a bucket location from a bucket
5999 * created on a different zone, using a legacy / default pool configuration
6001 return select_legacy_bucket_placement(rule_info
);
6005 * make sure that zone has this rule configured. We're
6006 * checking it for the local zone, because that's where this bucket object is going to
6009 map
<string
, RGWZonePlacementInfo
>::iterator piter
= get_zone_params().placement_pools
.find(location_rule
);
6010 if (piter
== get_zone_params().placement_pools
.end()) {
6011 /* couldn't find, means we cannot really place data for this bucket in this zone */
6012 if (get_zonegroup().equals(zonegroup
.get_id())) {
6013 /* that's a configuration error, zone should have that rule, as we're within the requested
6017 /* oh, well, data is not going to be placed here, bucket object is just a placeholder */
6022 RGWZonePlacementInfo
& placement_info
= piter
->second
;
6025 *rule_info
= placement_info
;
6031 int RGWRados::select_bucket_placement(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& placement_rule
,
6032 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
6034 if (!get_zone_params().placement_pools
.empty()) {
6035 return select_new_bucket_location(user_info
, zonegroup_id
, placement_rule
,
6036 pselected_rule_name
, rule_info
);
6039 if (pselected_rule_name
) {
6040 pselected_rule_name
->clear();
6043 return select_legacy_bucket_placement(rule_info
);
6046 int RGWRados::select_legacy_bucket_placement(RGWZonePlacementInfo
*rule_info
)
6049 map
<string
, bufferlist
> m
;
6051 bool write_map
= false;
6053 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6055 RGWObjectCtx
obj_ctx(this);
6056 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
, avail_pools
, map_bl
, NULL
, NULL
);
6062 bufferlist::iterator iter
= map_bl
.begin();
6064 } catch (buffer::error
& err
) {
6065 ldout(cct
, 0) << "ERROR: couldn't decode avail_pools" << dendl
;
6071 ret
= omap_get_all(obj
, header
, m
);
6076 if (ret
< 0 || m
.empty()) {
6077 vector
<rgw_pool
> pools
;
6078 string s
= string("default.") + default_storage_pool_suffix
;
6079 pools
.push_back(rgw_pool(s
));
6080 vector
<int> retcodes
;
6082 ret
= create_pools(pools
, retcodes
);
6085 ret
= omap_set(obj
, s
, bl
);
6093 ::encode(m
, new_bl
);
6094 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6096 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6100 map
<string
, bufferlist
>::iterator miter
;
6103 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6104 v
.push_back(miter
->first
);
6108 ret
= get_random_bytes((char *)&r
, sizeof(r
));
6112 int i
= r
% v
.size();
6116 pool_name
= miter
->first
;
6119 rule_info
->data_pool
= pool_name
;
6120 rule_info
->data_extra_pool
= pool_name
;
6121 rule_info
->index_pool
= pool_name
;
6122 rule_info
->index_type
= RGWBIType_Normal
;
6127 bool RGWRados::get_obj_data_pool(const string
& placement_rule
, const rgw_obj
& obj
, rgw_pool
*pool
)
6129 return rgw_get_obj_data_pool(zonegroup
, zone_params
, placement_rule
, obj
, pool
);
6132 bool RGWRados::obj_to_raw(const string
& placement_rule
, const rgw_obj
& obj
, rgw_raw_obj
*raw_obj
)
6134 get_obj_bucket_and_oid_loc(obj
, raw_obj
->oid
, raw_obj
->loc
);
6136 return get_obj_data_pool(placement_rule
, obj
, &raw_obj
->pool
);
6139 int RGWRados::update_placement_map()
6142 map
<string
, bufferlist
> m
;
6143 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6144 int ret
= omap_get_all(obj
, header
, m
);
6149 ::encode(m
, new_bl
);
6150 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6152 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6158 int RGWRados::add_bucket_placement(const rgw_pool
& new_pool
)
6160 librados::Rados
*rad
= get_rados_handle();
6161 int ret
= rad
->pool_lookup(new_pool
.name
.c_str());
6162 if (ret
< 0) // DNE, or something
6165 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6166 bufferlist empty_bl
;
6167 ret
= omap_set(obj
, new_pool
.to_str(), empty_bl
);
6169 // don't care about return value
6170 update_placement_map();
6175 int RGWRados::remove_bucket_placement(const rgw_pool
& old_pool
)
6177 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6178 int ret
= omap_del(obj
, old_pool
.to_str());
6180 // don't care about return value
6181 update_placement_map();
6186 int RGWRados::list_placement_set(set
<rgw_pool
>& names
)
6189 map
<string
, bufferlist
> m
;
6191 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6192 int ret
= omap_get_all(obj
, header
, m
);
6197 map
<string
, bufferlist
>::iterator miter
;
6198 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6199 names
.insert(rgw_pool(miter
->first
));
6202 return names
.size();
6205 int RGWRados::create_pools(vector
<rgw_pool
>& pools
, vector
<int>& retcodes
)
6207 vector
<librados::PoolAsyncCompletion
*> completions
;
6210 librados::Rados
*rad
= get_rados_handle();
6211 for (auto iter
= pools
.begin(); iter
!= pools
.end(); ++iter
) {
6212 librados::PoolAsyncCompletion
*c
= librados::Rados::pool_async_create_completion();
6213 completions
.push_back(c
);
6214 rgw_pool
& pool
= *iter
;
6215 int ret
= rad
->pool_create_async(pool
.name
.c_str(), c
);
6216 rets
.push_back(ret
);
6219 vector
<int>::iterator riter
;
6220 vector
<librados::PoolAsyncCompletion
*>::iterator citer
;
6223 assert(rets
.size() == completions
.size());
6224 for (riter
= rets
.begin(), citer
= completions
.begin(); riter
!= rets
.end(); ++riter
, ++citer
) {
6226 PoolAsyncCompletion
*c
= *citer
;
6229 r
= c
->get_return_value();
6231 ldout(cct
, 0) << "WARNING: async pool_create returned " << r
<< dendl
;
6236 retcodes
.push_back(r
);
6242 std::vector
<librados::IoCtx
> io_ctxs
;
6244 for (auto pool
: pools
) {
6245 io_ctxs
.emplace_back();
6246 int ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctxs
.back());
6248 ldout(cct
, 0) << "WARNING: ioctx_create returned " << ret
<< dendl
;
6251 retcodes
.push_back(ret
);
6257 completions
.clear();
6258 for (auto &io_ctx
: io_ctxs
) {
6259 librados::PoolAsyncCompletion
*c
=
6260 librados::Rados::pool_async_create_completion();
6261 completions
.push_back(c
);
6262 int ret
= io_ctx
.application_enable_async(pg_pool_t::APPLICATION_NAME_RGW
,
6268 for (auto c
: completions
) {
6270 int ret
= c
->get_return_value();
6271 if (ret
== -EOPNOTSUPP
) {
6273 } else if (ret
< 0) {
6274 ldout(cct
, 0) << "WARNING: async application_enable returned " << ret
6279 retcodes
.push_back(ret
);
6284 int RGWRados::get_obj_head_ioctx(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, librados::IoCtx
*ioctx
)
6287 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
6290 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6291 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6295 int r
= open_pool_ctx(pool
, *ioctx
);
6300 ioctx
->locator_set_key(key
);
6305 int RGWRados::get_obj_head_ref(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_rados_ref
*ref
)
6307 get_obj_bucket_and_oid_loc(obj
, ref
->oid
, ref
->key
);
6310 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6311 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6315 int r
= open_pool_ctx(pool
, ref
->ioctx
);
6320 ref
->ioctx
.locator_set_key(ref
->key
);
6325 int RGWRados::get_raw_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6332 if (ref
->oid
.empty()) {
6333 ref
->oid
= obj
.pool
.to_str();
6334 ref
->pool
= get_zone_params().domain_root
;
6336 ref
->pool
= obj
.pool
;
6338 r
= open_pool_ctx(ref
->pool
, ref
->ioctx
);
6342 ref
->ioctx
.locator_set_key(ref
->key
);
6347 int RGWRados::get_system_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6349 return get_raw_obj_ref(obj
, ref
);
6353 * fixes an issue where head objects were supposed to have a locator created, but ended
6356 int RGWRados::fix_head_obj_locator(const RGWBucketInfo
& bucket_info
, bool copy_obj
, bool remove_bad
, rgw_obj_key
& key
)
6358 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6362 rgw_obj
obj(bucket
, key
);
6364 get_obj_bucket_and_oid_loc(obj
, oid
, locator
);
6366 if (locator
.empty()) {
6367 ldout(cct
, 20) << "object does not have a locator, nothing to fix" << dendl
;
6371 librados::IoCtx ioctx
;
6373 int ret
= get_obj_head_ioctx(bucket_info
, obj
, &ioctx
);
6375 cerr
<< "ERROR: get_obj_head_ioctx() returned ret=" << ret
<< std::endl
;
6378 ioctx
.locator_set_key(string()); /* override locator for this object, use empty locator */
6383 struct timespec mtime_ts
;
6384 map
<string
, bufferlist
> attrs
;
6385 librados::ObjectReadOperation op
;
6386 op
.getxattrs(&attrs
, NULL
);
6387 op
.stat2(&size
, &mtime_ts
, NULL
);
6388 #define HEAD_SIZE 512 * 1024
6389 op
.read(0, HEAD_SIZE
, &data
, NULL
);
6391 ret
= ioctx
.operate(oid
, &op
, NULL
);
6393 lderr(cct
) << "ERROR: ioctx.operate(oid=" << oid
<< ") returned ret=" << ret
<< dendl
;
6397 if (size
> HEAD_SIZE
) {
6398 lderr(cct
) << "ERROR: returned object size (" << size
<< ") > HEAD_SIZE (" << HEAD_SIZE
<< ")" << dendl
;
6402 if (size
!= data
.length()) {
6403 lderr(cct
) << "ERROR: returned object size (" << size
<< ") != data.length() (" << data
.length() << ")" << dendl
;
6408 librados::ObjectWriteOperation wop
;
6410 wop
.mtime2(&mtime_ts
);
6412 map
<string
, bufferlist
>::iterator iter
;
6413 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6414 wop
.setxattr(iter
->first
.c_str(), iter
->second
);
6419 ioctx
.locator_set_key(locator
);
6420 ioctx
.operate(oid
, &wop
);
6424 ioctx
.locator_set_key(string());
6426 ret
= ioctx
.remove(oid
);
6428 lderr(cct
) << "ERROR: failed to remove original bad object" << dendl
;
6436 int RGWRados::move_rados_obj(librados::IoCtx
& src_ioctx
,
6437 const string
& src_oid
, const string
& src_locator
,
6438 librados::IoCtx
& dst_ioctx
,
6439 const string
& dst_oid
, const string
& dst_locator
)
6442 #define COPY_BUF_SIZE (4 * 1024 * 1024)
6444 uint64_t chunk_size
= COPY_BUF_SIZE
;
6448 struct timespec mtime_ts
;
6451 if (src_oid
== dst_oid
&& src_locator
== dst_locator
) {
6455 src_ioctx
.locator_set_key(src_locator
);
6456 dst_ioctx
.locator_set_key(dst_locator
);
6460 ObjectReadOperation rop
;
6461 ObjectWriteOperation wop
;
6464 rop
.stat2(&size
, &mtime_ts
, NULL
);
6465 mtime
= real_clock::from_timespec(mtime_ts
);
6467 rop
.read(ofs
, chunk_size
, &data
, NULL
);
6468 ret
= src_ioctx
.operate(src_oid
, &rop
, NULL
);
6473 if (data
.length() == 0) {
6478 wop
.create(true); /* make it exclusive */
6479 wop
.mtime2(&mtime_ts
);
6480 mtime
= real_clock::from_timespec(mtime_ts
);
6482 wop
.write(ofs
, data
);
6483 ret
= dst_ioctx
.operate(dst_oid
, &wop
);
6484 ofs
+= data
.length();
6485 done
= data
.length() != chunk_size
;
6489 lderr(cct
) << "ERROR: " << __func__
<< ": copying " << src_oid
<< " -> " << dst_oid
6490 << ": expected " << size
<< " bytes to copy, ended up with " << ofs
<< dendl
;
6495 src_ioctx
.remove(src_oid
);
6500 lderr(cct
) << "ERROR: failed to copy " << src_oid
<< " -> " << dst_oid
<< dendl
;
6505 * fixes an issue where head objects were supposed to have a locator created, but ended
6508 int RGWRados::fix_tail_obj_locator(const RGWBucketInfo
& bucket_info
, rgw_obj_key
& key
, bool fix
, bool *need_fix
)
6510 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6511 rgw_obj
obj(bucket
, key
);
6518 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
6523 RGWObjState
*astate
= NULL
;
6524 RGWObjectCtx
rctx(this);
6525 r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
6529 if (astate
->has_manifest
) {
6530 RGWObjManifest::obj_iterator miter
;
6531 RGWObjManifest
& manifest
= astate
->manifest
;
6532 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
6533 rgw_raw_obj raw_loc
= miter
.get_location().get_raw_obj(this);
6538 rgw_raw_obj_to_obj(manifest
.get_tail_placement().bucket
, raw_loc
, &loc
);
6540 if (loc
.key
.ns
.empty()) {
6541 /* continue, we're only interested in tail objects */
6545 get_obj_bucket_and_oid_loc(loc
, oid
, locator
);
6546 ref
.ioctx
.locator_set_key(locator
);
6548 ldout(cct
, 20) << __func__
<< ": key=" << key
<< " oid=" << oid
<< " locator=" << locator
<< dendl
;
6550 r
= ref
.ioctx
.stat(oid
, NULL
, NULL
);
6556 prepend_bucket_marker(bucket
, loc
.key
.name
, bad_loc
);
6558 /* create a new ioctx with the bad locator */
6559 librados::IoCtx src_ioctx
;
6560 src_ioctx
.dup(ref
.ioctx
);
6561 src_ioctx
.locator_set_key(bad_loc
);
6563 r
= src_ioctx
.stat(oid
, NULL
, NULL
);
6565 /* cannot find a broken part */
6568 ldout(cct
, 20) << __func__
<< ": found bad object part: " << loc
<< dendl
;
6573 r
= move_rados_obj(src_ioctx
, oid
, bad_loc
, ref
.ioctx
, oid
, locator
);
6575 lderr(cct
) << "ERROR: copy_rados_obj() on oid=" << oid
<< " returned r=" << r
<< dendl
;
6584 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, const rgw_obj
& obj
)
6588 RGWObjectCtx
obj_ctx(store
);
6590 RGWBucketInfo bucket_info
;
6591 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6596 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, obj
.get_hash_object(), &bucket_obj
, &shard_id
);
6598 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6601 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6606 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, int sid
)
6611 RGWObjectCtx
obj_ctx(store
);
6613 RGWBucketInfo bucket_info
;
6614 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6619 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, shard_id
, &bucket_obj
);
6621 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6624 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6629 int RGWRados::BucketShard::init(const RGWBucketInfo
& bucket_info
, int sid
)
6631 bucket
= bucket_info
.bucket
;
6634 int ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, shard_id
, &bucket_obj
);
6636 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6639 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6645 /* Execute @handler on last item in bucket listing for bucket specified
6646 * in @bucket_info. @obj_prefix and @obj_delim narrow down the listing
6647 * to objects matching these criterias. */
6648 int RGWRados::on_last_entry_in_listing(RGWBucketInfo
& bucket_info
,
6649 const std::string
& obj_prefix
,
6650 const std::string
& obj_delim
,
6651 std::function
<int(const rgw_bucket_dir_entry
&)> handler
)
6653 RGWRados::Bucket
target(this, bucket_info
);
6654 RGWRados::Bucket::List
list_op(&target
);
6656 list_op
.params
.prefix
= obj_prefix
;
6657 list_op
.params
.delim
= obj_delim
;
6659 ldout(cct
, 20) << "iterating listing for bucket=" << bucket_info
.bucket
.name
6660 << ", obj_prefix=" << obj_prefix
6661 << ", obj_delim=" << obj_delim
6664 bool is_truncated
= false;
6666 boost::optional
<rgw_bucket_dir_entry
> last_entry
;
6667 /* We need to rewind to the last object in a listing. */
6669 /* List bucket entries in chunks. */
6670 static constexpr int MAX_LIST_OBJS
= 100;
6671 std::vector
<rgw_bucket_dir_entry
> entries(MAX_LIST_OBJS
);
6673 int ret
= list_op
.list_objects(MAX_LIST_OBJS
, &entries
, nullptr,
6677 } else if (!entries
.empty()) {
6678 last_entry
= entries
.back();
6680 } while (is_truncated
);
6683 return handler(*last_entry
);
6686 /* Empty listing - no items we can run handler on. */
6691 int RGWRados::swift_versioning_copy(RGWObjectCtx
& obj_ctx
,
6692 const rgw_user
& user
,
6693 RGWBucketInfo
& bucket_info
,
6696 if (! swift_versioning_enabled(bucket_info
)) {
6700 obj_ctx
.obj
.set_atomic(obj
);
6702 RGWObjState
* state
= nullptr;
6703 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &state
, false);
6708 if (!state
->exists
) {
6715 const string
& src_name
= obj
.get_oid();
6716 char buf
[src_name
.size() + 32];
6717 struct timespec ts
= ceph::real_clock::to_timespec(state
->mtime
);
6718 snprintf(buf
, sizeof(buf
), "%03x%s/%lld.%06ld", (int)src_name
.size(),
6719 src_name
.c_str(), (long long)ts
.tv_sec
, ts
.tv_nsec
/ 1000);
6721 RGWBucketInfo dest_bucket_info
;
6723 r
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
, bucket_info
.swift_ver_location
, dest_bucket_info
, NULL
, NULL
);
6725 ldout(cct
, 10) << "failed to read dest bucket info: r=" << r
<< dendl
;
6727 return -ERR_PRECONDITION_FAILED
;
6732 if (dest_bucket_info
.owner
!= bucket_info
.owner
) {
6733 return -ERR_PRECONDITION_FAILED
;
6736 rgw_obj
dest_obj(dest_bucket_info
.bucket
, buf
);
6737 obj_ctx
.obj
.set_atomic(dest_obj
);
6741 r
= copy_obj(obj_ctx
,
6745 NULL
, /* req_info *info */
6751 NULL
, /* time_t *src_mtime */
6752 NULL
, /* time_t *mtime */
6753 NULL
, /* const time_t *mod_ptr */
6754 NULL
, /* const time_t *unmod_ptr */
6755 false, /* bool high_precision_time */
6756 NULL
, /* const char *if_match */
6757 NULL
, /* const char *if_nomatch */
6758 RGWRados::ATTRSMOD_NONE
,
6759 true, /* bool copy_if_newer */
6761 RGW_OBJ_CATEGORY_MAIN
,
6762 0, /* uint64_t olh_epoch */
6763 real_time(), /* time_t delete_at */
6764 NULL
, /* string *version_id */
6765 NULL
, /* string *ptag */
6766 NULL
, /* string *petag */
6767 NULL
, /* void (*progress_cb)(off_t, void *) */
6768 NULL
); /* void *progress_data */
6769 if (r
== -ECANCELED
|| r
== -ENOENT
) {
6770 /* Has already been overwritten, meaning another rgw process already
6778 int RGWRados::swift_versioning_restore(RGWObjectCtx
& obj_ctx
,
6779 const rgw_user
& user
,
6780 RGWBucketInfo
& bucket_info
,
6782 bool& restored
) /* out */
6784 if (! swift_versioning_enabled(bucket_info
)) {
6788 /* Bucket info of the bucket that stores previous versions of our object. */
6789 RGWBucketInfo archive_binfo
;
6791 int ret
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
,
6792 bucket_info
.swift_ver_location
, archive_binfo
,
6798 /* Abort the operation if the bucket storing our archive belongs to someone
6799 * else. This is a limitation in comparison to Swift as we aren't taking ACLs
6800 * into consideration. For we can live with that.
6802 * TODO: delegate this check to un upper layer and compare with ACLs. */
6803 if (bucket_info
.owner
!= archive_binfo
.owner
) {
6807 /* This code will be executed on latest version of the object. */
6808 const auto handler
= [&](const rgw_bucket_dir_entry
& entry
) -> int {
6809 std::string no_client_id
;
6810 std::string no_op_id
;
6811 std::string no_zone
;
6813 /* We don't support object versioning of Swift API on those buckets that
6814 * are already versioned using the S3 mechanism. This affects also bucket
6815 * storing archived objects. Otherwise the delete operation would create
6816 * a deletion marker. */
6817 if (archive_binfo
.versioned()) {
6819 return -ERR_PRECONDITION_FAILED
;
6822 /* We are requesting ATTRSMOD_NONE so the attr attribute is perfectly
6823 * irrelevant and may be safely skipped. */
6824 std::map
<std::string
, ceph::bufferlist
> no_attrs
;
6826 rgw_obj
archive_obj(archive_binfo
.bucket
, entry
.key
);
6827 obj_ctx
.obj
.set_atomic(archive_obj
);
6828 obj_ctx
.obj
.set_atomic(obj
);
6830 int ret
= copy_obj(obj_ctx
,
6834 nullptr, /* req_info *info */
6837 archive_obj
, /* src obj */
6838 bucket_info
, /* dest bucket info */
6839 archive_binfo
, /* src bucket info */
6840 nullptr, /* time_t *src_mtime */
6841 nullptr, /* time_t *mtime */
6842 nullptr, /* const time_t *mod_ptr */
6843 nullptr, /* const time_t *unmod_ptr */
6844 false, /* bool high_precision_time */
6845 nullptr, /* const char *if_match */
6846 nullptr, /* const char *if_nomatch */
6847 RGWRados::ATTRSMOD_NONE
,
6848 true, /* bool copy_if_newer */
6850 RGW_OBJ_CATEGORY_MAIN
,
6851 0, /* uint64_t olh_epoch */
6852 real_time(), /* time_t delete_at */
6853 nullptr, /* string *version_id */
6854 nullptr, /* string *ptag */
6855 nullptr, /* string *petag */
6856 nullptr, /* void (*progress_cb)(off_t, void *) */
6857 nullptr); /* void *progress_data */
6858 if (ret
== -ECANCELED
|| ret
== -ENOENT
) {
6859 /* Has already been overwritten, meaning another rgw process already
6862 } else if (ret
< 0) {
6868 /* Need to remove the archived copy. */
6869 ret
= delete_obj(obj_ctx
, archive_binfo
, archive_obj
,
6870 archive_binfo
.versioning_status());
6875 const std::string
& obj_name
= obj
.get_oid();
6876 const auto prefix
= boost::str(boost::format("%03x%s") % obj_name
.size()
6879 return on_last_entry_in_listing(archive_binfo
, prefix
, std::string(),
6884 * Write/overwrite an object to the bucket storage.
6885 * bucket: the bucket to store the object in
6886 * obj: the object name/key
6887 * data: the object contents/value
6888 * size: the amount of data to write (data must be this long)
6889 * accounted_size: original size of data before compression, encryption
6890 * mtime: if non-NULL, writes the given mtime to the bucket storage
6891 * attrs: all the given attrs are written to bucket storage for the given object
6892 * exclusive: create object exclusively
6893 * Returns: 0 on success, -ERR# otherwise.
6895 int RGWRados::Object::Write::_do_write_meta(uint64_t size
, uint64_t accounted_size
,
6896 map
<string
, bufferlist
>& attrs
,
6897 bool assume_noent
, bool modify_tail
,
6900 RGWRados::Bucket::UpdateIndex
*index_op
= static_cast<RGWRados::Bucket::UpdateIndex
*>(_index_op
);
6901 RGWRados
*store
= target
->get_store();
6903 ObjectWriteOperation op
;
6906 int r
= target
->get_state(&state
, false, assume_noent
);
6910 rgw_obj
& obj
= target
->get_obj();
6912 if (obj
.get_oid().empty()) {
6913 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< "(): cannot write object with empty name" << dendl
;
6918 r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
6922 bool is_olh
= state
->is_olh
;
6924 bool reset_obj
= (meta
.flags
& PUT_OBJ_CREATE
) != 0;
6926 const string
*ptag
= meta
.ptag
;
6927 if (!ptag
&& !index_op
->get_optag()->empty()) {
6928 ptag
= index_op
->get_optag();
6930 r
= target
->prepare_atomic_modification(op
, reset_obj
, ptag
, meta
.if_match
, meta
.if_nomatch
, false, modify_tail
);
6934 if (real_clock::is_zero(meta
.set_mtime
)) {
6935 meta
.set_mtime
= real_clock::now();
6938 if (state
->is_olh
) {
6939 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, state
->olh_tag
);
6942 struct timespec mtime_ts
= real_clock::to_timespec(meta
.set_mtime
);
6943 op
.mtime2(&mtime_ts
);
6946 /* if we want to overwrite the data, we also want to overwrite the
6947 xattrs, so just remove the object */
6948 op
.write_full(*meta
.data
);
6952 string content_type
;
6955 map
<string
, bufferlist
>::iterator iter
;
6957 for (iter
= meta
.rmattrs
->begin(); iter
!= meta
.rmattrs
->end(); ++iter
) {
6958 const string
& name
= iter
->first
;
6959 op
.rmxattr(name
.c_str());
6963 if (meta
.manifest
) {
6964 /* remove existing manifest attr */
6965 iter
= attrs
.find(RGW_ATTR_MANIFEST
);
6966 if (iter
!= attrs
.end())
6970 ::encode(*meta
.manifest
, bl
);
6971 op
.setxattr(RGW_ATTR_MANIFEST
, bl
);
6974 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6975 const string
& name
= iter
->first
;
6976 bufferlist
& bl
= iter
->second
;
6981 op
.setxattr(name
.c_str(), bl
);
6983 if (name
.compare(RGW_ATTR_ETAG
) == 0) {
6985 } else if (name
.compare(RGW_ATTR_CONTENT_TYPE
) == 0) {
6986 content_type
= bl
.c_str();
6987 } else if (name
.compare(RGW_ATTR_ACL
) == 0) {
6991 if (attrs
.find(RGW_ATTR_PG_VER
) == attrs
.end()) {
6992 cls_rgw_obj_store_pg_ver(op
, RGW_ATTR_PG_VER
);
6995 if (attrs
.find(RGW_ATTR_SOURCE_ZONE
) == attrs
.end()) {
6997 ::encode(store
->get_zone_short_id(), bl
);
6998 op
.setxattr(RGW_ATTR_SOURCE_ZONE
, bl
);
7009 if (!reset_obj
) { //Multipart upload, it has immutable head.
7010 orig_exists
= false;
7013 orig_exists
= state
->exists
;
7014 orig_size
= state
->accounted_size
;
7017 bool versioned_target
= (meta
.olh_epoch
> 0 || !obj
.key
.instance
.empty());
7019 bool versioned_op
= (target
->versioning_enabled() || is_olh
|| versioned_target
);
7022 index_op
->set_bilog_flags(RGW_BILOG_FLAG_VERSIONED_OP
);
7025 if (!index_op
->is_prepared()) {
7026 r
= index_op
->prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
7031 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7032 if (r
< 0) { /* we can expect to get -ECANCELED if object was replaced under,
7033 or -ENOENT if was removed, or -EEXIST if it did not exist
7034 before and now it does */
7035 if (r
== -EEXIST
&& assume_noent
) {
7036 target
->invalidate_state();
7042 epoch
= ref
.ioctx
.get_last_version();
7043 poolid
= ref
.ioctx
.get_id();
7045 r
= target
->complete_atomic_modification();
7047 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned r=" << r
<< dendl
;
7050 r
= index_op
->complete(poolid
, epoch
, size
, accounted_size
,
7051 meta
.set_mtime
, etag
, content_type
, &acl_bl
,
7052 meta
.category
, meta
.remove_objs
, meta
.user_data
);
7057 *meta
.mtime
= meta
.set_mtime
;
7060 /* note that index_op was using state so we couldn't invalidate it earlier */
7061 target
->invalidate_state();
7065 r
= store
->set_olh(target
->get_ctx(), target
->get_bucket_info(), obj
, false, NULL
, meta
.olh_epoch
, real_time(), false, meta
.zones_trace
);
7071 if (!real_clock::is_zero(meta
.delete_at
)) {
7072 rgw_obj_index_key obj_key
;
7073 obj
.key
.get_index_key(&obj_key
);
7075 r
= store
->objexp_hint_add(meta
.delete_at
,
7076 obj
.bucket
.tenant
, obj
.bucket
.name
, obj
.bucket
.bucket_id
, obj_key
);
7078 ldout(store
->ctx(), 0) << "ERROR: objexp_hint_add() returned r=" << r
<< ", object will not get removed" << dendl
;
7079 /* ignoring error, nothing we can do at this point */
7082 meta
.canceled
= false;
7084 /* update quota cache */
7085 if (meta
.completeMultipart
){
7086 store
->quota_handler
->update_stats(meta
.owner
, obj
.bucket
, (orig_exists
? 0 : 1),
7090 store
->quota_handler
->update_stats(meta
.owner
, obj
.bucket
, (orig_exists
? 0 : 1),
7091 accounted_size
, orig_size
);
7096 int ret
= index_op
->cancel();
7098 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel()() returned ret=" << ret
<< dendl
;
7101 meta
.canceled
= true;
7103 /* we lost in a race. There are a few options:
7104 * - existing object was rewritten (ECANCELED)
7105 * - non existing object was created (EEXIST)
7106 * - object was removed (ENOENT)
7107 * should treat it as a success
7109 if (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
) {
7110 if (r
== -ECANCELED
|| r
== -ENOENT
|| r
== -EEXIST
) {
7114 if (meta
.if_match
!= NULL
) {
7115 // only overwrite existing object
7116 if (strcmp(meta
.if_match
, "*") == 0) {
7118 r
= -ERR_PRECONDITION_FAILED
;
7119 } else if (r
== -ECANCELED
) {
7125 if (meta
.if_nomatch
!= NULL
) {
7126 // only create a new object
7127 if (strcmp(meta
.if_nomatch
, "*") == 0) {
7129 r
= -ERR_PRECONDITION_FAILED
;
7130 } else if (r
== -ENOENT
) {
7140 int RGWRados::Object::Write::write_meta(uint64_t size
, uint64_t accounted_size
,
7141 map
<string
, bufferlist
>& attrs
)
7143 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
7145 RGWRados::Bucket
bop(target
->get_store(), bucket_info
);
7146 RGWRados::Bucket::UpdateIndex
index_op(&bop
, target
->get_obj());
7147 index_op
.set_zones_trace(meta
.zones_trace
);
7149 bool assume_noent
= (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
);
7152 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7154 assume_noent
= false;
7157 if (!assume_noent
) {
7158 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7163 /** Write/overwrite a system object. */
7164 int RGWRados::put_system_obj_impl(rgw_raw_obj
& obj
, uint64_t size
, real_time
*mtime
,
7165 map
<std::string
, bufferlist
>& attrs
, int flags
,
7167 RGWObjVersionTracker
*objv_tracker
,
7168 real_time set_mtime
/* 0 for don't set */)
7171 int r
= get_system_obj_ref(obj
, &ref
);
7175 ObjectWriteOperation op
;
7177 if (flags
& PUT_OBJ_EXCL
) {
7178 if (!(flags
& PUT_OBJ_CREATE
))
7180 op
.create(true); // exclusive create
7183 op
.set_op_flags2(LIBRADOS_OP_FLAG_FAILOK
);
7188 objv_tracker
->prepare_op_for_write(&op
);
7191 if (real_clock::is_zero(set_mtime
)) {
7192 set_mtime
= real_clock::now();
7195 struct timespec mtime_ts
= real_clock::to_timespec(set_mtime
);
7196 op
.mtime2(&mtime_ts
);
7197 op
.write_full(data
);
7201 for (map
<string
, bufferlist
>::iterator iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
7202 const string
& name
= iter
->first
;
7203 bufferlist
& bl
= iter
->second
;
7208 op
.setxattr(name
.c_str(), bl
);
7211 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7217 objv_tracker
->apply_write();
7227 int RGWRados::put_system_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7228 off_t ofs
, bool exclusive
,
7229 RGWObjVersionTracker
*objv_tracker
)
7232 int r
= get_system_obj_ref(obj
, &ref
);
7237 ObjectWriteOperation op
;
7243 objv_tracker
->prepare_op_for_write(&op
);
7250 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7255 objv_tracker
->apply_write();
7261 * Write/overwrite an object to the bucket storage.
7262 * bucket: the bucket to store the object in
7263 * obj: the object name/key
7264 * data: the object contents/value
7265 * offset: the offet to write to in the object
7266 * If this is -1, we will overwrite the whole object.
7267 * size: the amount of data to write (data must be this long)
7268 * attrs: all the given attrs are written to bucket storage for the given object
7269 * Returns: 0 on success, -ERR# otherwise.
7272 int RGWRados::aio_put_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7273 off_t ofs
, bool exclusive
,
7277 int r
= get_raw_obj_ref(obj
, &ref
);
7282 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
7285 ObjectWriteOperation op
;
7295 r
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
7302 int RGWRados::aio_wait(void *handle
)
7304 AioCompletion
*c
= (AioCompletion
*)handle
;
7306 int ret
= c
->get_return_value();
7311 bool RGWRados::aio_completed(void *handle
)
7313 AioCompletion
*c
= (AioCompletion
*)handle
;
7314 return c
->is_safe();
7317 class RGWRadosPutObj
: public RGWGetDataCB
7321 RGWPutObjDataProcessor
*filter
;
7322 boost::optional
<RGWPutObj_Compress
>& compressor
;
7323 CompressorRef
& plugin
;
7324 RGWPutObjProcessor_Atomic
*processor
;
7325 RGWOpStateSingleOp
*opstate
;
7326 void (*progress_cb
)(off_t
, void *);
7327 void *progress_data
;
7328 bufferlist extra_data_bl
;
7329 uint64_t extra_data_left
;
7331 map
<string
, bufferlist
> src_attrs
;
7333 RGWRadosPutObj(CephContext
* cct
,
7334 CompressorRef
& plugin
,
7335 boost::optional
<RGWPutObj_Compress
>& compressor
,
7336 RGWPutObjProcessor_Atomic
*p
,
7337 RGWOpStateSingleOp
*_ops
,
7338 void (*_progress_cb
)(off_t
, void *),
7339 void *_progress_data
) :
7342 compressor(compressor
),
7346 progress_cb(_progress_cb
),
7347 progress_data(_progress_data
),
7351 int process_attrs(void) {
7352 if (extra_data_bl
.length()) {
7354 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7355 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7359 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7361 src_attrs
.erase(RGW_ATTR_COMPRESSION
);
7362 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7365 if (plugin
&& src_attrs
.find(RGW_ATTR_CRYPT_MODE
) == src_attrs
.end()) {
7366 //do not compress if object is encrypted
7367 compressor
= boost::in_place(cct
, plugin
, filter
);
7368 filter
= &*compressor
;
7373 int handle_data(bufferlist
& bl
, off_t ofs
, off_t len
) override
{
7375 progress_cb(ofs
, progress_data
);
7377 if (extra_data_left
) {
7378 size_t extra_len
= bl
.length();
7379 if (extra_len
> extra_data_left
)
7380 extra_len
= extra_data_left
;
7383 bl
.splice(0, extra_len
, &extra
);
7384 extra_data_bl
.append(extra
);
7386 extra_data_left
-= extra_len
;
7387 if (extra_data_left
== 0) {
7388 int res
= process_attrs();
7392 if (bl
.length() == 0) {
7397 // adjust ofs based on extra_data_len, so the result is a logical offset
7398 // into the object data
7399 assert(uint64_t(ofs
) >= extra_data_len
);
7400 ofs
-= extra_data_len
;
7402 data_len
+= bl
.length();
7405 bool need_opstate
= true;
7408 void *handle
= NULL
;
7410 uint64_t size
= bl
.length();
7411 int ret
= filter
->handle_data(bl
, ofs
, &handle
, &obj
, &again
);
7415 if (need_opstate
&& opstate
) {
7416 /* need to update opstate repository with new state. This is ratelimited, so we're not
7417 * really doing it every time
7419 ret
= opstate
->renew_state();
7421 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): failed to renew op state ret=" << ret
<< dendl
;
7422 int r
= filter
->throttle_data(handle
, obj
, size
, false);
7424 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): processor->throttle_data() returned " << r
<< dendl
;
7426 /* could not renew state! might have been marked as cancelled */
7429 need_opstate
= false;
7432 ret
= filter
->throttle_data(handle
, obj
, size
, false);
7440 bufferlist
& get_extra_data() { return extra_data_bl
; }
7442 map
<string
, bufferlist
>& get_attrs() { return src_attrs
; }
7444 void set_extra_data_len(uint64_t len
) override
{
7445 extra_data_left
= len
;
7446 RGWGetDataCB::set_extra_data_len(len
);
7449 uint64_t get_data_len() {
7453 int complete(const string
& etag
, real_time
*mtime
, real_time set_mtime
,
7454 map
<string
, bufferlist
>& attrs
, real_time delete_at
, rgw_zone_set
*zones_trace
) {
7455 return processor
->complete(data_len
, etag
, mtime
, set_mtime
, attrs
, delete_at
, NULL
, NULL
, NULL
, zones_trace
);
7458 bool is_canceled() {
7459 return processor
->is_canceled();
7464 * prepare attrset depending on attrs_mod.
7466 static void set_copy_attrs(map
<string
, bufferlist
>& src_attrs
,
7467 map
<string
, bufferlist
>& attrs
,
7468 RGWRados::AttrsMod attrs_mod
)
7470 switch (attrs_mod
) {
7471 case RGWRados::ATTRSMOD_NONE
:
7474 case RGWRados::ATTRSMOD_REPLACE
:
7475 if (!attrs
[RGW_ATTR_ETAG
].length()) {
7476 attrs
[RGW_ATTR_ETAG
] = src_attrs
[RGW_ATTR_ETAG
];
7478 if (!attrs
[RGW_ATTR_TAIL_TAG
].length()) {
7479 auto ttiter
= src_attrs
.find(RGW_ATTR_TAIL_TAG
);
7480 if (ttiter
!= src_attrs
.end()) {
7481 attrs
[RGW_ATTR_TAIL_TAG
] = src_attrs
[RGW_ATTR_TAIL_TAG
];
7485 case RGWRados::ATTRSMOD_MERGE
:
7486 for (map
<string
, bufferlist
>::iterator it
= src_attrs
.begin(); it
!= src_attrs
.end(); ++it
) {
7487 if (attrs
.find(it
->first
) == attrs
.end()) {
7488 attrs
[it
->first
] = it
->second
;
7495 int RGWRados::rewrite_obj(RGWBucketInfo
& dest_bucket_info
, rgw_obj
& obj
)
7497 map
<string
, bufferlist
> attrset
;
7501 RGWObjectCtx
rctx(this);
7503 RGWRados::Object
op_target(this, dest_bucket_info
, rctx
, obj
);
7504 RGWRados::Object::Read
read_op(&op_target
);
7506 read_op
.params
.attrs
= &attrset
;
7507 read_op
.params
.lastmod
= &mtime
;
7508 read_op
.params
.obj_size
= &obj_size
;
7510 int ret
= read_op
.prepare();
7514 attrset
.erase(RGW_ATTR_ID_TAG
);
7515 attrset
.erase(RGW_ATTR_TAIL_TAG
);
7517 uint64_t max_chunk_size
;
7519 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, obj
, &max_chunk_size
);
7521 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << obj
.bucket
<< dendl
;
7525 return copy_obj_data(rctx
, dest_bucket_info
, read_op
, obj_size
- 1, obj
, obj
,
7526 max_chunk_size
, NULL
, mtime
, attrset
,
7527 RGW_OBJ_CATEGORY_MAIN
, 0, real_time(),
7528 (obj
.key
.instance
.empty() ? NULL
: &(obj
.key
.instance
)),
7532 struct obj_time_weight
{
7534 uint32_t zone_short_id
;
7536 bool high_precision
;
7538 obj_time_weight() : zone_short_id(0), pg_ver(0), high_precision(false) {}
7540 bool compare_low_precision(const obj_time_weight
& rhs
) {
7541 struct timespec l
= ceph::real_clock::to_timespec(mtime
);
7542 struct timespec r
= ceph::real_clock::to_timespec(rhs
.mtime
);
7551 if (zone_short_id
!= rhs
.zone_short_id
) {
7552 return (zone_short_id
< rhs
.zone_short_id
);
7554 return (pg_ver
< rhs
.pg_ver
);
7558 bool operator<(const obj_time_weight
& rhs
) {
7559 if (!high_precision
|| !rhs
.high_precision
) {
7560 return compare_low_precision(rhs
);
7562 if (mtime
> rhs
.mtime
) {
7565 if (mtime
< rhs
.mtime
) {
7568 if (zone_short_id
!= rhs
.zone_short_id
) {
7569 return (zone_short_id
< rhs
.zone_short_id
);
7571 return (pg_ver
< rhs
.pg_ver
);
7574 void init(const real_time
& _mtime
, uint32_t _short_id
, uint64_t _pg_ver
) {
7576 zone_short_id
= _short_id
;
7580 void init(RGWObjState
*state
) {
7581 mtime
= state
->mtime
;
7582 zone_short_id
= state
->zone_short_id
;
7583 pg_ver
= state
->pg_ver
;
7587 inline ostream
& operator<<(ostream
& out
, const obj_time_weight
&o
) {
7590 if (o
.zone_short_id
!= 0 || o
.pg_ver
!= 0) {
7591 out
<< "[zid=" << o
.zone_short_id
<< ", pgv=" << o
.pg_ver
<< "]";
7597 class RGWGetExtraDataCB
: public RGWGetDataCB
{
7598 bufferlist extra_data
;
7600 RGWGetExtraDataCB() {}
7601 int handle_data(bufferlist
& bl
, off_t bl_ofs
, off_t bl_len
) override
{
7602 if (extra_data
.length() < extra_data_len
) {
7603 off_t max
= extra_data_len
- extra_data
.length();
7607 bl
.splice(0, max
, &extra_data
);
7612 bufferlist
& get_extra_data() {
7617 int RGWRados::stat_remote_obj(RGWObjectCtx
& obj_ctx
,
7618 const rgw_user
& user_id
,
7619 const string
& client_id
,
7621 const string
& source_zone
,
7623 RGWBucketInfo
& src_bucket_info
,
7624 real_time
*src_mtime
,
7626 const real_time
*mod_ptr
,
7627 const real_time
*unmod_ptr
,
7628 bool high_precision_time
,
7629 const char *if_match
,
7630 const char *if_nomatch
,
7631 map
<string
, bufferlist
> *pattrs
,
7636 /* source is in a different zonegroup, copy from there */
7638 RGWRESTStreamRWRequest
*in_stream_req
;
7640 map
<string
, bufferlist
> src_attrs
;
7641 append_rand_alpha(cct
, tag
, tag
, 32);
7642 obj_time_weight set_mtime_weight
;
7643 set_mtime_weight
.high_precision
= high_precision_time
;
7646 if (source_zone
.empty()) {
7647 if (src_bucket_info
.zonegroup
.empty()) {
7648 /* source is in the master zonegroup */
7649 conn
= rest_master_conn
;
7651 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7652 if (iter
== zonegroup_conn_map
.end()) {
7653 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7656 conn
= iter
->second
;
7659 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7660 if (iter
== zone_conn_map
.end()) {
7661 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7664 conn
= iter
->second
;
7667 RGWGetExtraDataCB cb
;
7669 map
<string
, string
> req_headers
;
7670 real_time set_mtime
;
7672 const real_time
*pmod
= mod_ptr
;
7674 obj_time_weight dest_mtime_weight
;
7676 constexpr bool prepend_meta
= true;
7677 constexpr bool get_op
= true;
7678 constexpr bool rgwx_stat
= true;
7679 constexpr bool sync_manifest
= true;
7680 constexpr bool skip_decrypt
= true;
7681 int ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7682 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7683 prepend_meta
, get_op
, rgwx_stat
,
7684 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7689 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, psize
, req_headers
);
7694 bufferlist
& extra_data_bl
= cb
.get_extra_data();
7695 if (extra_data_bl
.length()) {
7697 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7698 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7702 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7704 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7708 *src_mtime
= set_mtime
;
7712 map
<string
, bufferlist
>::iterator iter
= src_attrs
.find(RGW_ATTR_ETAG
);
7713 if (iter
!= src_attrs
.end()) {
7714 bufferlist
& etagbl
= iter
->second
;
7715 *petag
= etagbl
.to_str();
7720 *pattrs
= src_attrs
;
7726 int RGWRados::fetch_remote_obj(RGWObjectCtx
& obj_ctx
,
7727 const rgw_user
& user_id
,
7728 const string
& client_id
,
7729 const string
& op_id
,
7730 bool record_op_state
,
7732 const string
& source_zone
,
7735 RGWBucketInfo
& dest_bucket_info
,
7736 RGWBucketInfo
& src_bucket_info
,
7737 real_time
*src_mtime
,
7739 const real_time
*mod_ptr
,
7740 const real_time
*unmod_ptr
,
7741 bool high_precision_time
,
7742 const char *if_match
,
7743 const char *if_nomatch
,
7746 map
<string
, bufferlist
>& attrs
,
7747 RGWObjCategory category
,
7749 real_time delete_at
,
7752 ceph::buffer::list
*petag
,
7753 void (*progress_cb
)(off_t
, void *),
7754 void *progress_data
,
7755 rgw_zone_set
*zones_trace
)
7757 /* source is in a different zonegroup, copy from there */
7759 RGWRESTStreamRWRequest
*in_stream_req
;
7762 append_rand_alpha(cct
, tag
, tag
, 32);
7763 obj_time_weight set_mtime_weight
;
7764 set_mtime_weight
.high_precision
= high_precision_time
;
7766 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
7767 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.key
.name
,
7768 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
7769 if (version_id
&& *version_id
!= "null") {
7770 processor
.set_version_id(*version_id
);
7772 processor
.set_olh_epoch(olh_epoch
);
7773 int ret
= processor
.prepare(this, NULL
);
7779 if (source_zone
.empty()) {
7780 if (dest_bucket_info
.zonegroup
.empty()) {
7781 /* source is in the master zonegroup */
7782 conn
= rest_master_conn
;
7784 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7785 if (iter
== zonegroup_conn_map
.end()) {
7786 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7789 conn
= iter
->second
;
7792 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7793 if (iter
== zone_conn_map
.end()) {
7794 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7797 conn
= iter
->second
;
7800 string obj_name
= dest_obj
.bucket
.name
+ "/" + dest_obj
.get_oid();
7802 RGWOpStateSingleOp
*opstate
= NULL
;
7804 if (record_op_state
) {
7805 opstate
= new RGWOpStateSingleOp(this, client_id
, op_id
, obj_name
);
7807 ret
= opstate
->set_state(RGWOpState::OPSTATE_IN_PROGRESS
);
7809 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7815 boost::optional
<RGWPutObj_Compress
> compressor
;
7816 CompressorRef plugin
;
7818 const auto& compression_type
= zone_params
.get_compression_type(
7819 dest_bucket_info
.placement_rule
);
7820 if (compression_type
!= "none") {
7821 plugin
= Compressor::create(cct
, compression_type
);
7823 ldout(cct
, 1) << "Cannot load plugin for compression type "
7824 << compression_type
<< dendl
;
7828 RGWRadosPutObj
cb(cct
, plugin
, compressor
, &processor
, opstate
, progress_cb
, progress_data
);
7831 map
<string
, string
> req_headers
;
7832 real_time set_mtime
;
7834 RGWObjState
*dest_state
= NULL
;
7836 const real_time
*pmod
= mod_ptr
;
7838 obj_time_weight dest_mtime_weight
;
7840 if (copy_if_newer
) {
7841 /* need to get mtime for destination */
7842 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7846 if (!real_clock::is_zero(dest_state
->mtime
)) {
7847 dest_mtime_weight
.init(dest_state
);
7848 pmod
= &dest_mtime_weight
.mtime
;
7852 static constexpr bool prepend_meta
= true;
7853 static constexpr bool get_op
= true;
7854 static constexpr bool rgwx_stat
= false;
7855 static constexpr bool sync_manifest
= true;
7856 static constexpr bool skip_decrypt
= true;
7857 ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7858 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7859 prepend_meta
, get_op
, rgwx_stat
,
7860 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7865 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, nullptr, req_headers
);
7869 if (compressor
&& compressor
->is_compressed()) {
7871 RGWCompressionInfo cs_info
;
7872 cs_info
.compression_type
= plugin
->get_type_name();
7873 cs_info
.orig_size
= cb
.get_data_len();
7874 cs_info
.blocks
= move(compressor
->get_compression_blocks());
7875 ::encode(cs_info
, tmp
);
7876 cb
.get_attrs()[RGW_ATTR_COMPRESSION
] = tmp
;
7879 if (source_zone
.empty()) { /* need to preserve expiration if copy in the same zonegroup */
7880 cb
.get_attrs().erase(RGW_ATTR_DELETE_AT
);
7882 map
<string
, bufferlist
>::iterator iter
= cb
.get_attrs().find(RGW_ATTR_DELETE_AT
);
7883 if (iter
!= cb
.get_attrs().end()) {
7885 ::decode(delete_at
, iter
->second
);
7886 } catch (buffer::error
& err
) {
7887 ldout(cct
, 0) << "ERROR: failed to decode delete_at field in intra zone copy" << dendl
;
7893 *src_mtime
= set_mtime
;
7897 const auto iter
= cb
.get_attrs().find(RGW_ATTR_ETAG
);
7898 if (iter
!= cb
.get_attrs().end()) {
7899 *petag
= iter
->second
;
7903 if (source_zone
.empty()) {
7904 set_copy_attrs(cb
.get_attrs(), attrs
, attrs_mod
);
7906 attrs
= cb
.get_attrs();
7909 if (copy_if_newer
) {
7910 uint64_t pg_ver
= 0;
7911 auto i
= attrs
.find(RGW_ATTR_PG_VER
);
7912 if (i
!= attrs
.end() && i
->second
.length() > 0) {
7913 bufferlist::iterator iter
= i
->second
.begin();
7915 ::decode(pg_ver
, iter
);
7916 } catch (buffer::error
& err
) {
7917 ldout(ctx(), 0) << "ERROR: failed to decode pg ver attribute, ignoring" << dendl
;
7918 /* non critical error */
7921 set_mtime_weight
.init(set_mtime
, get_zone_short_id(), pg_ver
);
7924 #define MAX_COMPLETE_RETRY 100
7925 for (i
= 0; i
< MAX_COMPLETE_RETRY
; i
++) {
7926 ret
= cb
.complete(etag
, mtime
, set_mtime
, attrs
, delete_at
, zones_trace
);
7930 if (copy_if_newer
&& cb
.is_canceled()) {
7931 ldout(cct
, 20) << "raced with another write of obj: " << dest_obj
<< dendl
;
7932 obj_ctx
.obj
.invalidate(dest_obj
); /* object was overwritten */
7933 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7935 ldout(cct
, 0) << "ERROR: " << __func__
<< ": get_err_state() returned ret=" << ret
<< dendl
;
7938 dest_mtime_weight
.init(dest_state
);
7939 dest_mtime_weight
.high_precision
= high_precision_time
;
7940 if (!dest_state
->exists
||
7941 dest_mtime_weight
< set_mtime_weight
) {
7942 ldout(cct
, 20) << "retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7945 ldout(cct
, 20) << "not retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7951 if (i
== MAX_COMPLETE_RETRY
) {
7952 ldout(cct
, 0) << "ERROR: retried object completion too many times, something is wrong!" << dendl
;
7958 ret
= opstate
->set_state(RGWOpState::OPSTATE_COMPLETE
);
7960 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7967 if (copy_if_newer
&& ret
== -ERR_NOT_MODIFIED
) {
7971 RGWOpState::OpState state
;
7973 state
= RGWOpState::OPSTATE_ERROR
;
7975 state
= RGWOpState::OPSTATE_COMPLETE
;
7977 int r
= opstate
->set_state(state
);
7979 ldout(cct
, 0) << "ERROR: failed to set opstate r=" << ret
<< dendl
;
7987 int RGWRados::copy_obj_to_remote_dest(RGWObjState
*astate
,
7988 map
<string
, bufferlist
>& src_attrs
,
7989 RGWRados::Object::Read
& read_op
,
7990 const rgw_user
& user_id
,
7996 RGWRESTStreamWriteRequest
*out_stream_req
;
7998 int ret
= rest_master_conn
->put_obj_init(user_id
, dest_obj
, astate
->size
, src_attrs
, &out_stream_req
);
8003 ret
= read_op
.iterate(0, astate
->size
- 1, out_stream_req
->get_out_cb());
8005 delete out_stream_req
;
8009 ret
= rest_master_conn
->complete_request(out_stream_req
, etag
, mtime
);
8018 * dest_obj: the object to copy into
8019 * src_obj: the object to copy from
8020 * attrs: usage depends on attrs_mod parameter
8021 * attrs_mod: the modification mode of the attrs, may have the following values:
8022 * ATTRSMOD_NONE - the attributes of the source object will be
8023 * copied without modifications, attrs parameter is ignored;
8024 * ATTRSMOD_REPLACE - new object will have the attributes provided by attrs
8025 * parameter, source object attributes are not copied;
8026 * ATTRSMOD_MERGE - any conflicting meta keys on the source object's attributes
8027 * are overwritten by values contained in attrs parameter.
8028 * err: stores any errors resulting from the get of the original object
8029 * Returns: 0 on success, -ERR# otherwise.
8031 int RGWRados::copy_obj(RGWObjectCtx
& obj_ctx
,
8032 const rgw_user
& user_id
,
8033 const string
& client_id
,
8034 const string
& op_id
,
8036 const string
& source_zone
,
8039 RGWBucketInfo
& dest_bucket_info
,
8040 RGWBucketInfo
& src_bucket_info
,
8041 real_time
*src_mtime
,
8043 const real_time
*mod_ptr
,
8044 const real_time
*unmod_ptr
,
8045 bool high_precision_time
,
8046 const char *if_match
,
8047 const char *if_nomatch
,
8050 map
<string
, bufferlist
>& attrs
,
8051 RGWObjCategory category
,
8053 real_time delete_at
,
8056 ceph::buffer::list
*petag
,
8057 void (*progress_cb
)(off_t
, void *),
8058 void *progress_data
)
8062 rgw_obj shadow_obj
= dest_obj
;
8068 append_rand_alpha(cct
, dest_obj
.get_oid(), shadow_oid
, 32);
8069 shadow_obj
.init_ns(dest_obj
.bucket
, shadow_oid
, shadow_ns
);
8071 remote_dest
= !get_zonegroup().equals(dest_bucket_info
.zonegroup
);
8072 remote_src
= !get_zonegroup().equals(src_bucket_info
.zonegroup
);
8074 if (remote_src
&& remote_dest
) {
8075 ldout(cct
, 0) << "ERROR: can't copy object when both src and dest buckets are remote" << dendl
;
8079 ldout(cct
, 5) << "Copy object " << src_obj
.bucket
<< ":" << src_obj
.get_oid() << " => " << dest_obj
.bucket
<< ":" << dest_obj
.get_oid() << dendl
;
8081 if (remote_src
|| !source_zone
.empty()) {
8082 return fetch_remote_obj(obj_ctx
, user_id
, client_id
, op_id
, true, info
, source_zone
,
8083 dest_obj
, src_obj
, dest_bucket_info
, src_bucket_info
, src_mtime
, mtime
, mod_ptr
,
8084 unmod_ptr
, high_precision_time
,
8085 if_match
, if_nomatch
, attrs_mod
, copy_if_newer
, attrs
, category
,
8086 olh_epoch
, delete_at
, version_id
, ptag
, petag
, progress_cb
, progress_data
);
8089 map
<string
, bufferlist
> src_attrs
;
8090 RGWRados::Object
src_op_target(this, src_bucket_info
, obj_ctx
, src_obj
);
8091 RGWRados::Object::Read
read_op(&src_op_target
);
8093 read_op
.conds
.mod_ptr
= mod_ptr
;
8094 read_op
.conds
.unmod_ptr
= unmod_ptr
;
8095 read_op
.conds
.high_precision_time
= high_precision_time
;
8096 read_op
.conds
.if_match
= if_match
;
8097 read_op
.conds
.if_nomatch
= if_nomatch
;
8098 read_op
.params
.attrs
= &src_attrs
;
8099 read_op
.params
.lastmod
= src_mtime
;
8100 read_op
.params
.obj_size
= &obj_size
;
8102 ret
= read_op
.prepare();
8106 if (src_attrs
.count(RGW_ATTR_CRYPT_MODE
)) {
8107 // Current implementation does not follow S3 spec and even
8108 // may result in data corruption silently when copying
8109 // multipart objects acorss pools. So reject COPY operations
8110 //on encrypted objects before it is fully functional.
8111 ldout(cct
, 0) << "ERROR: copy op for encrypted object " << src_obj
8112 << " has not been implemented." << dendl
;
8113 return -ERR_NOT_IMPLEMENTED
;
8116 src_attrs
[RGW_ATTR_ACL
] = attrs
[RGW_ATTR_ACL
];
8117 src_attrs
.erase(RGW_ATTR_DELETE_AT
);
8119 set_copy_attrs(src_attrs
, attrs
, attrs_mod
);
8120 attrs
.erase(RGW_ATTR_ID_TAG
);
8121 attrs
.erase(RGW_ATTR_PG_VER
);
8122 attrs
.erase(RGW_ATTR_SOURCE_ZONE
);
8123 map
<string
, bufferlist
>::iterator cmp
= src_attrs
.find(RGW_ATTR_COMPRESSION
);
8124 if (cmp
!= src_attrs
.end())
8125 attrs
[RGW_ATTR_COMPRESSION
] = cmp
->second
;
8127 RGWObjManifest manifest
;
8128 RGWObjState
*astate
= NULL
;
8130 ret
= get_obj_state(&obj_ctx
, src_bucket_info
, src_obj
, &astate
);
8135 vector
<rgw_raw_obj
> ref_objs
;
8138 /* dest is in a different zonegroup, copy it there */
8139 return copy_obj_to_remote_dest(astate
, attrs
, read_op
, user_id
, dest_obj
, mtime
);
8141 uint64_t max_chunk_size
;
8143 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, dest_obj
, &max_chunk_size
);
8145 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << dest_obj
.bucket
<< dendl
;
8151 if (!get_obj_data_pool(src_bucket_info
.placement_rule
, src_obj
, &src_pool
)) {
8152 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << src_obj
<< dendl
;
8155 if (!get_obj_data_pool(dest_bucket_info
.placement_rule
, dest_obj
, &dest_pool
)) {
8156 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << dest_obj
<< dendl
;
8161 bool copy_data
= !astate
->has_manifest
|| (src_pool
!= dest_pool
);
8162 bool copy_first
= false;
8163 if (astate
->has_manifest
) {
8164 if (!astate
->manifest
.has_tail()) {
8167 uint64_t head_size
= astate
->manifest
.get_head_size();
8169 if (head_size
> 0) {
8170 if (head_size
> max_chunk_size
) {
8180 const auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8181 if (iter
!= attrs
.end()) {
8182 *petag
= iter
->second
;
8186 if (copy_data
) { /* refcounting tail wouldn't work here, just copy the data */
8187 return copy_obj_data(obj_ctx
, dest_bucket_info
, read_op
, obj_size
- 1, dest_obj
, src_obj
,
8188 max_chunk_size
, mtime
, real_time(), attrs
, category
, olh_epoch
, delete_at
,
8189 version_id
, ptag
, petag
);
8192 RGWObjManifest::obj_iterator miter
= astate
->manifest
.obj_begin();
8194 if (copy_first
) { // we need to copy first chunk, not increase refcount
8199 ret
= get_raw_obj_ref(miter
.get_location().get_raw_obj(this), &ref
);
8204 bool versioned_dest
= dest_bucket_info
.versioning_enabled();
8206 if (version_id
&& !version_id
->empty()) {
8207 versioned_dest
= true;
8208 dest_obj
.key
.set_instance(*version_id
);
8209 } else if (versioned_dest
) {
8210 gen_rand_obj_instance_name(&dest_obj
);
8213 bufferlist first_chunk
;
8215 bool copy_itself
= (dest_obj
== src_obj
);
8216 RGWObjManifest
*pmanifest
;
8217 ldout(cct
, 20) << "dest_obj=" << dest_obj
<< " src_obj=" << src_obj
<< " copy_itself=" << (int)copy_itself
<< dendl
;
8219 RGWRados::Object
dest_op_target(this, dest_bucket_info
, obj_ctx
, dest_obj
);
8220 RGWRados::Object::Write
write_op(&dest_op_target
);
8229 append_rand_alpha(cct
, tag
, tag
, 32);
8233 attrs
.erase(RGW_ATTR_TAIL_TAG
);
8234 manifest
= astate
->manifest
;
8235 const rgw_bucket_placement
& tail_placement
= manifest
.get_tail_placement();
8236 if (tail_placement
.bucket
.name
.empty()) {
8237 manifest
.set_tail_placement(tail_placement
.placement_rule
, src_obj
.bucket
);
8240 for (; miter
!= astate
->manifest
.obj_end(); ++miter
) {
8241 ObjectWriteOperation op
;
8242 ref_tag
= tag
+ '\0';
8243 cls_refcount_get(op
, ref_tag
, true);
8244 const rgw_raw_obj
& loc
= miter
.get_location().get_raw_obj(this);
8245 ref
.ioctx
.locator_set_key(loc
.loc
);
8247 ret
= ref
.ioctx
.operate(loc
.oid
, &op
);
8252 ref_objs
.push_back(loc
);
8255 pmanifest
= &manifest
;
8257 pmanifest
= &astate
->manifest
;
8258 /* don't send the object's tail for garbage collection */
8259 astate
->keep_tail
= true;
8263 ret
= read_op
.read(0, max_chunk_size
, first_chunk
);
8268 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, first_chunk
.length());
8270 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, 0);
8273 write_op
.meta
.data
= &first_chunk
;
8274 write_op
.meta
.manifest
= pmanifest
;
8275 write_op
.meta
.ptag
= &tag
;
8276 write_op
.meta
.owner
= dest_bucket_info
.owner
;
8277 write_op
.meta
.mtime
= mtime
;
8278 write_op
.meta
.flags
= PUT_OBJ_CREATE
;
8279 write_op
.meta
.category
= category
;
8280 write_op
.meta
.olh_epoch
= olh_epoch
;
8281 write_op
.meta
.delete_at
= delete_at
;
8282 write_op
.meta
.modify_tail
= !copy_itself
;
8284 ret
= write_op
.write_meta(obj_size
, astate
->accounted_size
, attrs
);
8293 vector
<rgw_raw_obj
>::iterator riter
;
8295 /* rollback reference */
8296 for (riter
= ref_objs
.begin(); riter
!= ref_objs
.end(); ++riter
) {
8297 ObjectWriteOperation op
;
8298 cls_refcount_put(op
, tag
, true);
8300 ref
.ioctx
.locator_set_key(riter
->loc
);
8302 int r
= ref
.ioctx
.operate(riter
->oid
, &op
);
8304 ldout(cct
, 0) << "ERROR: cleanup after error failed to drop reference on obj=" << *riter
<< dendl
;
8312 int RGWRados::copy_obj_data(RGWObjectCtx
& obj_ctx
,
8313 RGWBucketInfo
& dest_bucket_info
,
8314 RGWRados::Object::Read
& read_op
, off_t end
,
8317 uint64_t max_chunk_size
,
8319 real_time set_mtime
,
8320 map
<string
, bufferlist
>& attrs
,
8321 RGWObjCategory category
,
8323 real_time delete_at
,
8326 ceph::buffer::list
*petag
)
8328 bufferlist first_chunk
;
8329 RGWObjManifest manifest
;
8332 append_rand_alpha(cct
, tag
, tag
, 32);
8334 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
8335 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.key
.name
,
8336 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
8338 processor
.set_version_id(*version_id
);
8340 processor
.set_olh_epoch(olh_epoch
);
8341 int ret
= processor
.prepare(this, NULL
);
8349 ret
= read_op
.read(ofs
, end
, bl
);
8351 uint64_t read_len
= ret
;
8358 ret
= processor
.handle_data(bl
, ofs
, &handle
, &obj
, &again
);
8362 ret
= processor
.throttle_data(handle
, obj
, read_len
, false);
8368 } while (ofs
<= end
);
8371 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8372 if (iter
!= attrs
.end()) {
8373 bufferlist
& bl
= iter
->second
;
8374 etag
= string(bl
.c_str(), bl
.length());
8380 uint64_t accounted_size
;
8382 bool compressed
{false};
8383 RGWCompressionInfo cs_info
;
8384 ret
= rgw_compression_info_from_attrset(attrs
, compressed
, cs_info
);
8386 ldout(cct
, 0) << "ERROR: failed to read compression info" << dendl
;
8389 // pass original size if compressed
8390 accounted_size
= compressed
? cs_info
.orig_size
: ofs
;
8393 return processor
.complete(accounted_size
, etag
, mtime
, set_mtime
, attrs
, delete_at
);
8396 bool RGWRados::is_meta_master()
8398 if (!get_zonegroup().is_master_zonegroup()) {
8402 return (get_zonegroup().master_zone
== zone_public_config
.id
);
8406 * Check to see if the bucket metadata could be synced
8407 * bucket: the bucket to check
8408 * Returns false is the bucket is not synced
8410 bool RGWRados::is_syncing_bucket_meta(const rgw_bucket
& bucket
)
8413 /* no current period */
8414 if (current_period
.get_id().empty()) {
8418 /* zonegroup is not master zonegroup */
8419 if (!get_zonegroup().is_master_zonegroup()) {
8423 /* single zonegroup and a single zone */
8424 if (current_period
.is_single_zonegroup() && get_zonegroup().zones
.size() == 1) {
8428 /* zone is not master */
8429 if (get_zonegroup().master_zone
.compare(zone_public_config
.id
) != 0) {
8436 int RGWRados::check_bucket_empty(RGWBucketInfo
& bucket_info
)
8438 std::map
<string
, rgw_bucket_dir_entry
> ent_map
;
8439 rgw_obj_index_key marker
;
8444 #define NUM_ENTRIES 1000
8445 int r
= cls_bucket_list(bucket_info
, RGW_NO_SHARD
, marker
, prefix
, NUM_ENTRIES
, true, ent_map
,
8446 &is_truncated
, &marker
);
8451 std::map
<string
, rgw_bucket_dir_entry
>::iterator eiter
;
8452 for (eiter
= ent_map
.begin(); eiter
!= ent_map
.end(); ++eiter
) {
8455 if (rgw_obj_key::oid_to_key_in_ns(eiter
->second
.key
.name
, &obj
, ns
))
8458 } while (is_truncated
);
8464 * bucket: the name of the bucket to delete
8465 * Returns 0 on success, -ERR# otherwise.
8467 int RGWRados::delete_bucket(RGWBucketInfo
& bucket_info
, RGWObjVersionTracker
& objv_tracker
, bool check_empty
)
8469 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8470 librados::IoCtx index_ctx
;
8471 map
<int, string
> bucket_objs
;
8472 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8477 r
= check_bucket_empty(bucket_info
);
8483 r
= rgw_bucket_delete_bucket_obj(this, bucket
.tenant
, bucket
.name
, objv_tracker
);
8487 /* if the bucket is not synced we can remove the meta file */
8488 if (!is_syncing_bucket_meta(bucket
)) {
8489 RGWObjVersionTracker objv_tracker
;
8490 string entry
= bucket
.get_key();
8491 r
= rgw_bucket_instance_remove_entry(this, entry
, &objv_tracker
);
8495 /* remove bucket index objects*/
8496 map
<int, string
>::const_iterator biter
;
8497 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
8498 index_ctx
.remove(biter
->second
);
8504 int RGWRados::set_bucket_owner(rgw_bucket
& bucket
, ACLOwner
& owner
)
8507 map
<string
, bufferlist
> attrs
;
8508 RGWObjectCtx
obj_ctx(this);
8510 if (bucket
.bucket_id
.empty()) {
8511 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8513 r
= get_bucket_instance_info(obj_ctx
, bucket
, info
, nullptr, &attrs
);
8516 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8520 info
.owner
= owner
.get_id();
8522 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8524 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8532 int RGWRados::set_buckets_enabled(vector
<rgw_bucket
>& buckets
, bool enabled
)
8536 vector
<rgw_bucket
>::iterator iter
;
8538 for (iter
= buckets
.begin(); iter
!= buckets
.end(); ++iter
) {
8539 rgw_bucket
& bucket
= *iter
;
8541 ldout(cct
, 20) << "enabling bucket name=" << bucket
.name
<< dendl
;
8543 ldout(cct
, 20) << "disabling bucket name=" << bucket
.name
<< dendl
;
8546 map
<string
, bufferlist
> attrs
;
8547 RGWObjectCtx
obj_ctx(this);
8548 int r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8550 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8555 info
.flags
&= ~BUCKET_SUSPENDED
;
8557 info
.flags
|= BUCKET_SUSPENDED
;
8560 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8562 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8570 int RGWRados::bucket_suspended(rgw_bucket
& bucket
, bool *suspended
)
8572 RGWBucketInfo bucket_info
;
8573 RGWObjectCtx
obj_ctx(this);
8574 int ret
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, bucket_info
, NULL
);
8579 *suspended
= ((bucket_info
.flags
& BUCKET_SUSPENDED
) != 0);
8583 int RGWRados::Object::complete_atomic_modification()
8585 if (!state
->has_manifest
|| state
->keep_tail
)
8588 cls_rgw_obj_chain chain
;
8589 store
->update_gc_chain(obj
, state
->manifest
, &chain
);
8591 if (chain
.empty()) {
8595 string tag
= (state
->tail_tag
.length() > 0 ? state
->tail_tag
.to_str() : state
->obj_tag
.to_str());
8596 return store
->gc
->send_chain(chain
, tag
, false); // do it async
8599 void RGWRados::update_gc_chain(rgw_obj
& head_obj
, RGWObjManifest
& manifest
, cls_rgw_obj_chain
*chain
)
8601 RGWObjManifest::obj_iterator iter
;
8602 rgw_raw_obj raw_head
;
8603 obj_to_raw(manifest
.get_head_placement_rule(), head_obj
, &raw_head
);
8604 for (iter
= manifest
.obj_begin(); iter
!= manifest
.obj_end(); ++iter
) {
8605 const rgw_raw_obj
& mobj
= iter
.get_location().get_raw_obj(this);
8606 if (mobj
== raw_head
)
8608 cls_rgw_obj_key
key(mobj
.oid
);
8609 chain
->push_obj(mobj
.pool
.to_str(), key
, mobj
.loc
);
8613 int RGWRados::send_chain_to_gc(cls_rgw_obj_chain
& chain
, const string
& tag
, bool sync
)
8615 return gc
->send_chain(chain
, tag
, sync
);
8618 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
, string
& bucket_oid
)
8620 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8621 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8625 if (bucket
.bucket_id
.empty()) {
8626 ldout(cct
, 0) << "ERROR: empty bucket id for bucket operation" << dendl
;
8630 bucket_oid
= dir_oid_prefix
;
8631 bucket_oid
.append(bucket
.bucket_id
);
8636 int RGWRados::open_bucket_index_base(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8637 string
& bucket_oid_base
) {
8638 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8639 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8643 if (bucket
.bucket_id
.empty()) {
8644 ldout(cct
, 0) << "ERROR: empty bucket_id for bucket operation" << dendl
;
8648 bucket_oid_base
= dir_oid_prefix
;
8649 bucket_oid_base
.append(bucket
.bucket_id
);
8655 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8656 map
<int, string
>& bucket_objs
, int shard_id
, map
<int, string
> *bucket_instance_ids
) {
8657 string bucket_oid_base
;
8658 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8663 get_bucket_index_objects(bucket_oid_base
, bucket_info
.num_shards
, bucket_objs
, shard_id
);
8664 if (bucket_instance_ids
) {
8665 get_bucket_instance_ids(bucket_info
, shard_id
, bucket_instance_ids
);
8670 template<typename T
>
8671 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8672 map
<int, string
>& oids
, map
<int, T
>& bucket_objs
,
8673 int shard_id
, map
<int, string
> *bucket_instance_ids
)
8675 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, bucket_instance_ids
);
8679 map
<int, string
>::const_iterator iter
= oids
.begin();
8680 for (; iter
!= oids
.end(); ++iter
) {
8681 bucket_objs
[iter
->first
] = T();
8686 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8687 const string
& obj_key
, string
*bucket_obj
, int *shard_id
)
8689 string bucket_oid_base
;
8690 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8694 RGWObjectCtx
obj_ctx(this);
8696 ret
= get_bucket_index_object(bucket_oid_base
, obj_key
, bucket_info
.num_shards
,
8697 (RGWBucketInfo::BIShardsHashType
)bucket_info
.bucket_index_shard_hash_type
, bucket_obj
, shard_id
);
8699 ldout(cct
, 10) << "get_bucket_index_object() returned ret=" << ret
<< dendl
;
8705 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8706 int shard_id
, string
*bucket_obj
)
8708 string bucket_oid_base
;
8709 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8713 RGWObjectCtx
obj_ctx(this);
8715 get_bucket_index_object(bucket_oid_base
, bucket_info
.num_shards
,
8716 shard_id
, bucket_obj
);
8720 static void accumulate_raw_stats(const rgw_bucket_dir_header
& header
,
8721 map
<RGWObjCategory
, RGWStorageStats
>& stats
)
8723 for (const auto& pair
: header
.stats
) {
8724 const RGWObjCategory category
= static_cast<RGWObjCategory
>(pair
.first
);
8725 const rgw_bucket_category_stats
& header_stats
= pair
.second
;
8727 RGWStorageStats
& s
= stats
[category
];
8729 s
.category
= category
;
8730 s
.size
+= header_stats
.total_size
;
8731 s
.size_rounded
+= header_stats
.total_size_rounded
;
8732 s
.size_utilized
+= header_stats
.actual_size
;
8733 s
.num_objects
+= header_stats
.num_entries
;
8737 int RGWRados::bucket_check_index(RGWBucketInfo
& bucket_info
,
8738 map
<RGWObjCategory
, RGWStorageStats
> *existing_stats
,
8739 map
<RGWObjCategory
, RGWStorageStats
> *calculated_stats
)
8741 librados::IoCtx index_ctx
;
8742 // key - bucket index object id
8743 // value - bucket index check OP returned result with the given bucket index object (shard)
8744 map
<int, string
> oids
;
8745 map
<int, struct rgw_cls_check_index_ret
> bucket_objs_ret
;
8747 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, bucket_objs_ret
);
8752 ret
= CLSRGWIssueBucketCheck(index_ctx
, oids
, bucket_objs_ret
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8757 // Aggregate results (from different shards if there is any)
8758 map
<int, struct rgw_cls_check_index_ret
>::iterator iter
;
8759 for (iter
= bucket_objs_ret
.begin(); iter
!= bucket_objs_ret
.end(); ++iter
) {
8760 accumulate_raw_stats(iter
->second
.existing_header
, *existing_stats
);
8761 accumulate_raw_stats(iter
->second
.calculated_header
, *calculated_stats
);
8767 int RGWRados::bucket_rebuild_index(RGWBucketInfo
& bucket_info
)
8769 librados::IoCtx index_ctx
;
8770 map
<int, string
> bucket_objs
;
8772 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8777 return CLSRGWIssueBucketRebuild(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8780 int RGWRados::bucket_set_reshard(RGWBucketInfo
& bucket_info
, const cls_rgw_bucket_instance_entry
& entry
)
8782 librados::IoCtx index_ctx
;
8783 map
<int, string
> bucket_objs
;
8785 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8790 return CLSRGWIssueSetBucketResharding(index_ctx
, bucket_objs
, entry
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8793 int RGWRados::defer_gc(void *ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
)
8795 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
8796 std::string oid
, key
;
8797 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
8801 RGWObjState
*state
= NULL
;
8803 int r
= get_obj_state(rctx
, bucket_info
, obj
, &state
, false);
8807 if (!state
->is_atomic
) {
8808 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not deferring gc operation" << dendl
;
8814 if (state
->tail_tag
.length() > 0) {
8815 tag
= state
->tail_tag
.c_str();
8816 } else if (state
->obj_tag
.length() > 0) {
8817 tag
= state
->obj_tag
.c_str();
8819 ldout(cct
, 20) << "state->obj_tag is empty, not deferring gc operation" << dendl
;
8823 ldout(cct
, 0) << "defer chain tag=" << tag
<< dendl
;
8825 return gc
->defer_chain(tag
, false);
8828 void RGWRados::remove_rgw_head_obj(ObjectWriteOperation
& op
)
8830 list
<string
> prefixes
;
8831 prefixes
.push_back(RGW_ATTR_OLH_PREFIX
);
8832 cls_rgw_remove_obj(op
, prefixes
);
8835 void RGWRados::cls_obj_check_prefix_exist(ObjectOperation
& op
, const string
& prefix
, bool fail_if_exist
)
8837 cls_rgw_obj_check_attrs_prefix(op
, prefix
, fail_if_exist
);
8840 void RGWRados::cls_obj_check_mtime(ObjectOperation
& op
, const real_time
& mtime
, bool high_precision_time
, RGWCheckMTimeType type
)
8842 cls_rgw_obj_check_mtime(op
, mtime
, high_precision_time
, type
);
8848 * bucket: name of the bucket storing the object
8849 * obj: name of the object to delete
8850 * Returns: 0 on success, -ERR# otherwise.
8852 int RGWRados::Object::Delete::delete_obj()
8854 RGWRados
*store
= target
->get_store();
8855 rgw_obj
& src_obj
= target
->get_obj();
8856 const string
& instance
= src_obj
.key
.instance
;
8857 rgw_obj obj
= src_obj
;
8859 if (instance
== "null") {
8860 obj
.key
.instance
.clear();
8863 bool explicit_marker_version
= (!params
.marker_version_id
.empty());
8865 if (params
.versioning_status
& BUCKET_VERSIONED
|| explicit_marker_version
) {
8866 if (instance
.empty() || explicit_marker_version
) {
8867 rgw_obj marker
= obj
;
8869 if (!params
.marker_version_id
.empty()) {
8870 if (params
.marker_version_id
!= "null") {
8871 marker
.key
.set_instance(params
.marker_version_id
);
8873 } else if ((params
.versioning_status
& BUCKET_VERSIONS_SUSPENDED
) == 0) {
8874 store
->gen_rand_obj_instance_name(&marker
);
8877 result
.version_id
= marker
.key
.instance
;
8878 result
.delete_marker
= true;
8880 struct rgw_bucket_dir_entry_meta meta
;
8882 meta
.owner
= params
.obj_owner
.get_id().to_str();
8883 meta
.owner_display_name
= params
.obj_owner
.get_display_name();
8885 if (real_clock::is_zero(params
.mtime
)) {
8886 meta
.mtime
= real_clock::now();
8888 meta
.mtime
= params
.mtime
;
8891 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
);
8896 rgw_bucket_dir_entry dirent
;
8898 int r
= store
->bi_get_instance(target
->get_bucket_info(), obj
, &dirent
);
8902 result
.delete_marker
= dirent
.is_delete_marker();
8903 r
= store
->unlink_obj_instance(target
->get_ctx(), target
->get_bucket_info(), obj
, params
.olh_epoch
, params
.zones_trace
);
8907 result
.version_id
= instance
;
8911 int r
= target
->get_bucket_shard(&bs
);
8913 ldout(store
->ctx(), 5) << "failed to get BucketShard object: r=" << r
<< dendl
;
8917 if (target
->bucket_info
.datasync_flag_enabled()) {
8918 r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
8920 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
8929 int r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
8935 r
= target
->get_state(&state
, false);
8939 ObjectWriteOperation op
;
8941 if (!real_clock::is_zero(params
.unmod_since
)) {
8942 struct timespec ctime
= ceph::real_clock::to_timespec(state
->mtime
);
8943 struct timespec unmod
= ceph::real_clock::to_timespec(params
.unmod_since
);
8944 if (!params
.high_precision_time
) {
8949 ldout(store
->ctx(), 10) << "If-UnModified-Since: " << params
.unmod_since
<< " Last-Modified: " << ctime
<< dendl
;
8950 if (ctime
> unmod
) {
8951 return -ERR_PRECONDITION_FAILED
;
8954 /* only delete object if mtime is less than or equal to params.unmod_since */
8955 store
->cls_obj_check_mtime(op
, params
.unmod_since
, params
.high_precision_time
, CLS_RGW_CHECK_TIME_MTIME_LE
);
8957 uint64_t obj_size
= state
->size
;
8959 if (!real_clock::is_zero(params
.expiration_time
)) {
8961 real_time delete_at
;
8963 if (state
->get_attr(RGW_ATTR_DELETE_AT
, bl
)) {
8965 bufferlist::iterator iter
= bl
.begin();
8966 ::decode(delete_at
, iter
);
8967 } catch (buffer::error
& err
) {
8968 ldout(store
->ctx(), 0) << "ERROR: couldn't decode RGW_ATTR_DELETE_AT" << dendl
;
8972 if (params
.expiration_time
!= delete_at
) {
8973 return -ERR_PRECONDITION_FAILED
;
8976 return -ERR_PRECONDITION_FAILED
;
8980 if (!state
->exists
) {
8981 target
->invalidate_state();
8985 r
= target
->prepare_atomic_modification(op
, false, NULL
, NULL
, NULL
, true, false);
8989 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
8991 RGWRados::Bucket
bop(store
, bucket_info
);
8992 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
8994 index_op
.set_zones_trace(params
.zones_trace
);
8995 index_op
.set_bilog_flags(params
.bilog_flags
);
8997 r
= index_op
.prepare(CLS_RGW_OP_DEL
, &state
->write_tag
);
9001 store
->remove_rgw_head_obj(op
);
9002 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9004 /* raced with another operation, object state is indeterminate */
9005 const bool need_invalidate
= (r
== -ECANCELED
);
9007 int64_t poolid
= ref
.ioctx
.get_id();
9009 tombstone_cache_t
*obj_tombstone_cache
= store
->get_tombstone_cache();
9010 if (obj_tombstone_cache
) {
9011 tombstone_entry entry
{*state
};
9012 obj_tombstone_cache
->add(obj
, entry
);
9014 r
= index_op
.complete_del(poolid
, ref
.ioctx
.get_last_version(), state
->mtime
, params
.remove_objs
);
9016 int ret
= target
->complete_atomic_modification();
9018 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned ret=" << ret
<< dendl
;
9020 /* other than that, no need to propagate error */
9022 int ret
= index_op
.cancel();
9024 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel() returned ret=" << ret
<< dendl
;
9028 if (need_invalidate
) {
9029 target
->invalidate_state();
9035 /* update quota cache */
9036 store
->quota_handler
->update_stats(params
.bucket_owner
, obj
.bucket
, -1, 0, obj_size
);
9041 int RGWRados::delete_obj(RGWObjectCtx
& obj_ctx
,
9042 const RGWBucketInfo
& bucket_info
,
9044 int versioning_status
,
9045 uint16_t bilog_flags
,
9046 const real_time
& expiration_time
,
9047 rgw_zone_set
*zones_trace
)
9049 RGWRados::Object
del_target(this, bucket_info
, obj_ctx
, obj
);
9050 RGWRados::Object::Delete
del_op(&del_target
);
9052 del_op
.params
.bucket_owner
= bucket_info
.owner
;
9053 del_op
.params
.versioning_status
= versioning_status
;
9054 del_op
.params
.bilog_flags
= bilog_flags
;
9055 del_op
.params
.expiration_time
= expiration_time
;
9056 del_op
.params
.zones_trace
= zones_trace
;
9058 return del_op
.delete_obj();
9061 int RGWRados::delete_raw_obj(const rgw_raw_obj
& obj
)
9064 int r
= get_raw_obj_ref(obj
, &ref
);
9069 ObjectWriteOperation op
;
9072 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9079 int RGWRados::delete_system_obj(rgw_raw_obj
& obj
, RGWObjVersionTracker
*objv_tracker
)
9082 ldout(cct
, 1) << "delete_system_obj got empty object name "
9083 << obj
<< ", returning EINVAL" << dendl
;
9087 int r
= get_raw_obj_ref(obj
, &ref
);
9092 ObjectWriteOperation op
;
9095 objv_tracker
->prepare_op_for_write(&op
);
9099 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9106 int RGWRados::delete_obj_index(const rgw_obj
& obj
)
9108 std::string oid
, key
;
9109 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
9111 RGWObjectCtx
obj_ctx(this);
9113 RGWBucketInfo bucket_info
;
9114 int ret
= get_bucket_instance_info(obj_ctx
, obj
.bucket
, bucket_info
, NULL
, NULL
);
9116 ldout(cct
, 0) << "ERROR: " << __func__
<< "() get_bucket_instance_info(bucket=" << obj
.bucket
<< ") returned ret=" << ret
<< dendl
;
9120 RGWRados::Bucket
bop(this, bucket_info
);
9121 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9123 real_time removed_mtime
;
9124 int r
= index_op
.complete_del(-1 /* pool */, 0, removed_mtime
, NULL
);
9129 static void generate_fake_tag(RGWRados
*store
, map
<string
, bufferlist
>& attrset
, RGWObjManifest
& manifest
, bufferlist
& manifest_bl
, bufferlist
& tag_bl
)
9133 RGWObjManifest::obj_iterator mi
= manifest
.obj_begin();
9134 if (mi
!= manifest
.obj_end()) {
9135 if (manifest
.has_tail()) // first object usually points at the head, let's skip to a more unique part
9137 tag
= mi
.get_location().get_raw_obj(store
).oid
;
9141 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
9142 char md5_str
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
9144 hash
.Update((const byte
*)manifest_bl
.c_str(), manifest_bl
.length());
9146 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_ETAG
);
9147 if (iter
!= attrset
.end()) {
9148 bufferlist
& bl
= iter
->second
;
9149 hash
.Update((const byte
*)bl
.c_str(), bl
.length());
9153 buf_to_hex(md5
, CEPH_CRYPTO_MD5_DIGESTSIZE
, md5_str
);
9154 tag
.append(md5_str
);
9156 ldout(store
->ctx(), 10) << "generate_fake_tag new tag=" << tag
<< dendl
;
9158 tag_bl
.append(tag
.c_str(), tag
.size() + 1);
9161 static bool is_olh(map
<string
, bufferlist
>& attrs
)
9163 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_INFO
);
9164 return (iter
!= attrs
.end());
9167 static bool has_olh_tag(map
<string
, bufferlist
>& attrs
)
9169 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_ID_TAG
);
9170 return (iter
!= attrs
.end());
9173 int RGWRados::get_olh_target_state(RGWObjectCtx
& obj_ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9174 RGWObjState
*olh_state
, RGWObjState
**target_state
)
9176 assert(olh_state
->is_olh
);
9179 int r
= RGWRados::follow_olh(bucket_info
, obj_ctx
, olh_state
, obj
, &target
); /* might return -EAGAIN */
9183 r
= get_obj_state(&obj_ctx
, bucket_info
, target
, target_state
, false);
9191 int RGWRados::get_system_obj_state_impl(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9197 RGWRawObjState
*s
= rctx
->raw
.get_state(obj
);
9198 ldout(cct
, 20) << "get_system_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9206 int r
= raw_obj_stat(obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), objv_tracker
);
9209 s
->has_attrs
= true;
9210 s
->mtime
= real_time();
9217 s
->has_attrs
= true;
9218 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9220 if (s
->obj_tag
.length())
9221 ldout(cct
, 20) << "get_system_obj_state: setting s->obj_tag to "
9222 << s
->obj_tag
.c_str() << dendl
;
9224 ldout(cct
, 20) << "get_system_obj_state: s->obj_tag was set empty" << dendl
;
9229 int RGWRados::get_system_obj_state(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9234 ret
= get_system_obj_state_impl(rctx
, obj
, state
, objv_tracker
);
9235 } while (ret
== -EAGAIN
);
9240 int RGWRados::get_obj_state_impl(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9241 RGWObjState
**state
, bool follow_olh
, bool assume_noent
)
9247 bool need_follow_olh
= follow_olh
&& obj
.key
.instance
.empty();
9249 RGWObjState
*s
= rctx
->obj
.get_state(obj
);
9250 ldout(cct
, 20) << "get_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9253 if (s
->is_olh
&& need_follow_olh
) {
9254 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9261 rgw_raw_obj raw_obj
;
9262 obj_to_raw(bucket_info
.placement_rule
, obj
, &raw_obj
);
9266 if (!assume_noent
) {
9267 r
= RGWRados::raw_obj_stat(raw_obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), NULL
);
9272 s
->has_attrs
= true;
9273 tombstone_entry entry
;
9274 if (obj_tombstone_cache
&& obj_tombstone_cache
->find(obj
, entry
)) {
9275 s
->mtime
= entry
.mtime
;
9276 s
->zone_short_id
= entry
.zone_short_id
;
9277 s
->pg_ver
= entry
.pg_ver
;
9278 ldout(cct
, 20) << __func__
<< "(): found obj in tombstone cache: obj=" << obj
9279 << " mtime=" << s
->mtime
<< " pgv=" << s
->pg_ver
<< dendl
;
9281 s
->mtime
= real_time();
9289 s
->has_attrs
= true;
9290 s
->accounted_size
= s
->size
;
9292 auto iter
= s
->attrset
.find(RGW_ATTR_COMPRESSION
);
9293 const bool compressed
= (iter
!= s
->attrset
.end());
9295 // use uncompressed size for accounted_size
9297 RGWCompressionInfo info
;
9298 auto p
= iter
->second
.begin();
9300 s
->accounted_size
= info
.orig_size
;
9301 } catch (buffer::error
&) {
9302 dout(0) << "ERROR: could not decode compression info for object: " << obj
<< dendl
;
9307 iter
= s
->attrset
.find(RGW_ATTR_SHADOW_OBJ
);
9308 if (iter
!= s
->attrset
.end()) {
9309 bufferlist bl
= iter
->second
;
9310 bufferlist::iterator it
= bl
.begin();
9311 it
.copy(bl
.length(), s
->shadow_obj
);
9312 s
->shadow_obj
[bl
.length()] = '\0';
9314 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9315 auto ttiter
= s
->attrset
.find(RGW_ATTR_TAIL_TAG
);
9316 if (ttiter
!= s
->attrset
.end()) {
9317 s
->tail_tag
= s
->attrset
[RGW_ATTR_TAIL_TAG
];
9320 bufferlist manifest_bl
= s
->attrset
[RGW_ATTR_MANIFEST
];
9321 if (manifest_bl
.length()) {
9322 bufferlist::iterator miter
= manifest_bl
.begin();
9324 ::decode(s
->manifest
, miter
);
9325 s
->has_manifest
= true;
9326 s
->manifest
.set_head(bucket_info
.placement_rule
, obj
, s
->size
); /* patch manifest to reflect the head we just read, some manifests might be
9327 broken due to old bugs */
9328 s
->size
= s
->manifest
.get_obj_size();
9330 s
->accounted_size
= s
->size
;
9331 } catch (buffer::error
& err
) {
9332 ldout(cct
, 0) << "ERROR: couldn't decode manifest" << dendl
;
9335 ldout(cct
, 10) << "manifest: total_size = " << s
->manifest
.get_obj_size() << dendl
;
9336 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20) && s
->manifest
.has_explicit_objs()) {
9337 RGWObjManifest::obj_iterator mi
;
9338 for (mi
= s
->manifest
.obj_begin(); mi
!= s
->manifest
.obj_end(); ++mi
) {
9339 ldout(cct
, 20) << "manifest: ofs=" << mi
.get_ofs() << " loc=" << mi
.get_location().get_raw_obj(this) << dendl
;
9343 if (!s
->obj_tag
.length()) {
9345 * Uh oh, something's wrong, object with manifest should have tag. Let's
9346 * create one out of the manifest, would be unique
9348 generate_fake_tag(this, s
->attrset
, s
->manifest
, manifest_bl
, s
->obj_tag
);
9352 map
<string
, bufferlist
>::iterator aiter
= s
->attrset
.find(RGW_ATTR_PG_VER
);
9353 if (aiter
!= s
->attrset
.end()) {
9354 bufferlist
& pg_ver_bl
= aiter
->second
;
9355 if (pg_ver_bl
.length()) {
9356 bufferlist::iterator pgbl
= pg_ver_bl
.begin();
9358 ::decode(s
->pg_ver
, pgbl
);
9359 } catch (buffer::error
& err
) {
9360 ldout(cct
, 0) << "ERROR: couldn't decode pg ver attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9364 aiter
= s
->attrset
.find(RGW_ATTR_SOURCE_ZONE
);
9365 if (aiter
!= s
->attrset
.end()) {
9366 bufferlist
& zone_short_id_bl
= aiter
->second
;
9367 if (zone_short_id_bl
.length()) {
9368 bufferlist::iterator zbl
= zone_short_id_bl
.begin();
9370 ::decode(s
->zone_short_id
, zbl
);
9371 } catch (buffer::error
& err
) {
9372 ldout(cct
, 0) << "ERROR: couldn't decode zone short id attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9376 if (s
->obj_tag
.length())
9377 ldout(cct
, 20) << "get_obj_state: setting s->obj_tag to " << s
->obj_tag
.c_str() << dendl
;
9379 ldout(cct
, 20) << "get_obj_state: s->obj_tag was set empty" << dendl
;
9381 /* an object might not be olh yet, but could have olh id tag, so we should set it anyway if
9382 * it exist, and not only if is_olh() returns true
9384 iter
= s
->attrset
.find(RGW_ATTR_OLH_ID_TAG
);
9385 if (iter
!= s
->attrset
.end()) {
9386 s
->olh_tag
= iter
->second
;
9389 if (is_olh(s
->attrset
)) {
9392 ldout(cct
, 20) << __func__
<< ": setting s->olh_tag to " << string(s
->olh_tag
.c_str(), s
->olh_tag
.length()) << dendl
;
9394 if (need_follow_olh
) {
9395 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9402 int RGWRados::get_obj_state(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWObjState
**state
,
9403 bool follow_olh
, bool assume_noent
)
9408 ret
= get_obj_state_impl(rctx
, bucket_info
, obj
, state
, follow_olh
, assume_noent
);
9409 } while (ret
== -EAGAIN
);
9414 int RGWRados::Object::get_manifest(RGWObjManifest
**pmanifest
)
9416 RGWObjState
*astate
;
9417 int r
= get_state(&astate
, true);
9422 *pmanifest
= &astate
->manifest
;
9427 int RGWRados::Object::Read::get_attr(const char *name
, bufferlist
& dest
)
9430 int r
= source
->get_state(&state
, true);
9435 if (!state
->get_attr(name
, dest
))
9442 int RGWRados::Object::Stat::stat_async()
9444 RGWObjectCtx
& ctx
= source
->get_ctx();
9445 rgw_obj
& obj
= source
->get_obj();
9446 RGWRados
*store
= source
->get_store();
9448 RGWObjState
*s
= ctx
.obj
.get_state(obj
); /* calling this one directly because otherwise a sync request will be sent */
9452 result
.size
= s
->size
;
9453 result
.mtime
= ceph::real_clock::to_timespec(s
->mtime
);
9454 result
.attrs
= s
->attrset
;
9455 result
.has_manifest
= s
->has_manifest
;
9456 result
.manifest
= s
->manifest
;
9462 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
9464 int r
= store
->get_obj_head_ioctx(source
->get_bucket_info(), obj
, &state
.io_ctx
);
9469 librados::ObjectReadOperation op
;
9470 op
.stat2(&result
.size
, &result
.mtime
, NULL
);
9471 op
.getxattrs(&result
.attrs
, NULL
);
9472 state
.completion
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
9473 state
.io_ctx
.locator_set_key(loc
);
9474 r
= state
.io_ctx
.aio_operate(oid
, state
.completion
, &op
, NULL
);
9476 ldout(store
->ctx(), 5) << __func__
9477 << ": ERROR: aio_operate() returned ret=" << r
9486 int RGWRados::Object::Stat::wait()
9488 if (!state
.completion
) {
9492 state
.completion
->wait_for_safe();
9493 state
.ret
= state
.completion
->get_return_value();
9494 state
.completion
->release();
9496 if (state
.ret
!= 0) {
9503 int RGWRados::Object::Stat::finish()
9505 map
<string
, bufferlist
>::iterator iter
= result
.attrs
.find(RGW_ATTR_MANIFEST
);
9506 if (iter
!= result
.attrs
.end()) {
9507 bufferlist
& bl
= iter
->second
;
9508 bufferlist::iterator biter
= bl
.begin();
9510 ::decode(result
.manifest
, biter
);
9511 } catch (buffer::error
& err
) {
9512 RGWRados
*store
= source
->get_store();
9513 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< ": failed to decode manifest" << dendl
;
9516 result
.has_manifest
= true;
9523 * Get an attribute for a system object.
9524 * obj: the object to get attr
9525 * name: name of the attr to retrieve
9526 * dest: bufferlist to store the result in
9527 * Returns: 0 on success, -ERR# otherwise.
9529 int RGWRados::system_obj_get_attr(rgw_raw_obj
& obj
, const char *name
, bufferlist
& dest
)
9532 int r
= get_system_obj_ref(obj
, &ref
);
9537 ObjectReadOperation op
;
9540 op
.getxattr(name
, &dest
, &rval
);
9542 r
= ref
.ioctx
.operate(ref
.oid
, &op
, NULL
);
9549 int RGWRados::append_atomic_test(RGWObjectCtx
*rctx
,
9550 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9551 ObjectOperation
& op
, RGWObjState
**pstate
)
9556 int r
= get_obj_state(rctx
, bucket_info
, obj
, pstate
, false);
9560 RGWObjState
*state
= *pstate
;
9562 if (!state
->is_atomic
) {
9563 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not appending atomic test" << dendl
;
9567 if (state
->obj_tag
.length() > 0 && !state
->fake_tag
) {// check for backward compatibility
9568 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9570 ldout(cct
, 20) << "state->obj_tag is empty, not appending atomic test" << dendl
;
9575 int RGWRados::Object::get_state(RGWObjState
**pstate
, bool follow_olh
, bool assume_noent
)
9577 return store
->get_obj_state(&ctx
, bucket_info
, obj
, pstate
, follow_olh
, assume_noent
);
9580 void RGWRados::Object::invalidate_state()
9582 ctx
.obj
.invalidate(obj
);
9585 void RGWRados::SystemObject::invalidate_state()
9587 ctx
.raw
.invalidate(obj
);
9590 int RGWRados::Object::prepare_atomic_modification(ObjectWriteOperation
& op
, bool reset_obj
, const string
*ptag
,
9591 const char *if_match
, const char *if_nomatch
, bool removal_op
,
9594 int r
= get_state(&state
, false);
9598 bool need_guard
= (state
->has_manifest
|| (state
->obj_tag
.length() != 0) ||
9599 if_match
!= NULL
|| if_nomatch
!= NULL
) &&
9602 if (!state
->is_atomic
) {
9603 ldout(store
->ctx(), 20) << "prepare_atomic_modification: state is not atomic. state=" << (void *)state
<< dendl
;
9607 store
->remove_rgw_head_obj(op
); // we're not dropping reference here, actually removing object
9614 /* first verify that the object wasn't replaced under */
9615 if (if_nomatch
== NULL
|| strcmp(if_nomatch
, "*") != 0) {
9616 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9617 // FIXME: need to add FAIL_NOTEXIST_OK for racing deletion
9621 if (strcmp(if_match
, "*") == 0) {
9622 // test the object is existing
9623 if (!state
->exists
) {
9624 return -ERR_PRECONDITION_FAILED
;
9628 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9629 strncmp(if_match
, bl
.c_str(), bl
.length()) != 0) {
9630 return -ERR_PRECONDITION_FAILED
;
9636 if (strcmp(if_nomatch
, "*") == 0) {
9637 // test the object is NOT existing
9638 if (state
->exists
) {
9639 return -ERR_PRECONDITION_FAILED
;
9643 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9644 strncmp(if_nomatch
, bl
.c_str(), bl
.length()) == 0) {
9645 return -ERR_PRECONDITION_FAILED
;
9652 if (state
->exists
) {
9654 store
->remove_rgw_head_obj(op
);
9661 /* the object is being removed, no need to update its tag */
9666 state
->write_tag
= *ptag
;
9668 append_rand_alpha(store
->ctx(), state
->write_tag
, state
->write_tag
, 32);
9671 bl
.append(state
->write_tag
.c_str(), state
->write_tag
.size() + 1);
9673 ldout(store
->ctx(), 10) << "setting object write_tag=" << state
->write_tag
<< dendl
;
9675 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9677 op
.setxattr(RGW_ATTR_TAIL_TAG
, bl
);
9683 int RGWRados::system_obj_set_attr(void *ctx
, rgw_raw_obj
& obj
, const char *name
, bufferlist
& bl
,
9684 RGWObjVersionTracker
*objv_tracker
)
9686 map
<string
, bufferlist
> attrs
;
9688 return system_obj_set_attrs(ctx
, obj
, attrs
, NULL
, objv_tracker
);
9691 int RGWRados::system_obj_set_attrs(void *ctx
, rgw_raw_obj
& obj
,
9692 map
<string
, bufferlist
>& attrs
,
9693 map
<string
, bufferlist
>* rmattrs
,
9694 RGWObjVersionTracker
*objv_tracker
)
9697 int r
= get_system_obj_ref(obj
, &ref
);
9701 ObjectWriteOperation op
;
9704 objv_tracker
->prepare_op_for_write(&op
);
9707 map
<string
, bufferlist
>::iterator iter
;
9709 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9710 const string
& name
= iter
->first
;
9711 op
.rmxattr(name
.c_str());
9715 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9716 const string
& name
= iter
->first
;
9717 bufferlist
& bl
= iter
->second
;
9722 op
.setxattr(name
.c_str(), bl
);
9730 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9738 * Set an attr on an object.
9739 * bucket: name of the bucket holding the object
9740 * obj: name of the object to set the attr on
9741 * name: the attr to set
9742 * bl: the contents of the attr
9743 * Returns: 0 on success, -ERR# otherwise.
9745 int RGWRados::set_attr(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, const char *name
, bufferlist
& bl
)
9747 map
<string
, bufferlist
> attrs
;
9749 return set_attrs(ctx
, bucket_info
, obj
, attrs
, NULL
);
9752 int RGWRados::set_attrs(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
,
9753 map
<string
, bufferlist
>& attrs
,
9754 map
<string
, bufferlist
>* rmattrs
)
9757 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
9761 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
9763 ObjectWriteOperation op
;
9764 RGWObjState
*state
= NULL
;
9766 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &state
);
9770 map
<string
, bufferlist
>::iterator iter
;
9772 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9773 const string
& name
= iter
->first
;
9774 op
.rmxattr(name
.c_str());
9778 const rgw_bucket
& bucket
= obj
.bucket
;
9780 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9781 const string
& name
= iter
->first
;
9782 bufferlist
& bl
= iter
->second
;
9787 op
.setxattr(name
.c_str(), bl
);
9789 if (name
.compare(RGW_ATTR_DELETE_AT
) == 0) {
9794 rgw_obj_index_key obj_key
;
9795 obj
.key
.get_index_key(&obj_key
);
9797 objexp_hint_add(ts
, bucket
.tenant
, bucket
.name
, bucket
.bucket_id
, obj_key
);
9798 } catch (buffer::error
& err
) {
9799 ldout(cct
, 0) << "ERROR: failed to decode " RGW_ATTR_DELETE_AT
<< " attr" << dendl
;
9807 RGWObjectCtx
obj_ctx(this);
9810 RGWRados::Bucket
bop(this, bucket_info
);
9811 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9815 append_rand_alpha(cct
, tag
, tag
, 32);
9816 state
->write_tag
= tag
;
9817 r
= index_op
.prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
9822 bl
.append(tag
.c_str(), tag
.size() + 1);
9823 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9827 real_time mtime
= real_clock::now();
9828 struct timespec mtime_ts
= real_clock::to_timespec(mtime
);
9829 op
.mtime2(&mtime_ts
);
9830 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9833 bufferlist acl_bl
= attrs
[RGW_ATTR_ACL
];
9834 bufferlist etag_bl
= attrs
[RGW_ATTR_ETAG
];
9835 bufferlist content_type_bl
= attrs
[RGW_ATTR_CONTENT_TYPE
];
9836 string
etag(etag_bl
.c_str(), etag_bl
.length());
9837 string
content_type(content_type_bl
.c_str(), content_type_bl
.length());
9838 uint64_t epoch
= ref
.ioctx
.get_last_version();
9839 int64_t poolid
= ref
.ioctx
.get_id();
9840 r
= index_op
.complete(poolid
, epoch
, state
->size
, state
->accounted_size
,
9841 mtime
, etag
, content_type
, &acl_bl
,
9842 RGW_OBJ_CATEGORY_MAIN
, NULL
);
9844 int ret
= index_op
.cancel();
9846 ldout(cct
, 0) << "ERROR: complete_update_index_cancel() returned ret=" << ret
<< dendl
;
9854 state
->obj_tag
.swap(bl
);
9856 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9857 state
->attrset
.erase(iter
->first
);
9860 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9861 state
->attrset
[iter
->first
] = iter
->second
;
9868 int RGWRados::Object::Read::prepare()
9870 RGWRados
*store
= source
->get_store();
9871 CephContext
*cct
= store
->ctx();
9875 map
<string
, bufferlist
>::iterator iter
;
9877 RGWObjState
*astate
;
9878 int r
= source
->get_state(&astate
, true);
9882 if (!astate
->exists
) {
9886 const RGWBucketInfo
& bucket_info
= source
->get_bucket_info();
9888 state
.obj
= astate
->obj
;
9889 store
->obj_to_raw(bucket_info
.placement_rule
, state
.obj
, &state
.head_obj
);
9891 r
= store
->get_obj_head_ioctx(bucket_info
, state
.obj
, &state
.io_ctx
);
9896 *params
.attrs
= astate
->attrset
;
9897 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
9898 for (iter
= params
.attrs
->begin(); iter
!= params
.attrs
->end(); ++iter
) {
9899 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
9904 /* Convert all times go GMT to make them compatible */
9905 if (conds
.mod_ptr
|| conds
.unmod_ptr
) {
9906 obj_time_weight src_weight
;
9907 src_weight
.init(astate
);
9908 src_weight
.high_precision
= conds
.high_precision_time
;
9910 obj_time_weight dest_weight
;
9911 dest_weight
.high_precision
= conds
.high_precision_time
;
9913 if (conds
.mod_ptr
) {
9914 dest_weight
.init(*conds
.mod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9915 ldout(cct
, 10) << "If-Modified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9916 if (!(dest_weight
< src_weight
)) {
9917 return -ERR_NOT_MODIFIED
;
9921 if (conds
.unmod_ptr
) {
9922 dest_weight
.init(*conds
.unmod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9923 ldout(cct
, 10) << "If-UnModified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9924 if (dest_weight
< src_weight
) {
9925 return -ERR_PRECONDITION_FAILED
;
9929 if (conds
.if_match
|| conds
.if_nomatch
) {
9930 r
= get_attr(RGW_ATTR_ETAG
, etag
);
9934 if (conds
.if_match
) {
9935 string if_match_str
= rgw_string_unquote(conds
.if_match
);
9936 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-Match: " << if_match_str
<< dendl
;
9937 if (if_match_str
.compare(etag
.c_str()) != 0) {
9938 return -ERR_PRECONDITION_FAILED
;
9942 if (conds
.if_nomatch
) {
9943 string if_nomatch_str
= rgw_string_unquote(conds
.if_nomatch
);
9944 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-NoMatch: " << if_nomatch_str
<< dendl
;
9945 if (if_nomatch_str
.compare(etag
.c_str()) == 0) {
9946 return -ERR_NOT_MODIFIED
;
9951 if (params
.obj_size
)
9952 *params
.obj_size
= astate
->size
;
9954 *params
.lastmod
= astate
->mtime
;
9959 int RGWRados::Object::Read::range_to_ofs(uint64_t obj_size
, int64_t &ofs
, int64_t &end
)
9966 } else if (end
< 0) {
9971 if (ofs
>= (off_t
)obj_size
) {
9974 if (end
>= (off_t
)obj_size
) {
9981 int RGWRados::SystemObject::get_state(RGWRawObjState
**pstate
, RGWObjVersionTracker
*objv_tracker
)
9983 return store
->get_system_obj_state(&ctx
, obj
, pstate
, objv_tracker
);
9986 int RGWRados::stat_system_obj(RGWObjectCtx
& obj_ctx
,
9987 RGWRados::SystemObject::Read::GetObjState
& state
,
9989 map
<string
, bufferlist
> *attrs
,
9992 RGWObjVersionTracker
*objv_tracker
)
9994 RGWRawObjState
*astate
= NULL
;
9996 int r
= get_system_obj_state(&obj_ctx
, obj
, &astate
, objv_tracker
);
10000 if (!astate
->exists
) {
10005 *attrs
= astate
->attrset
;
10006 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
10007 map
<string
, bufferlist
>::iterator iter
;
10008 for (iter
= attrs
->begin(); iter
!= attrs
->end(); ++iter
) {
10009 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
10015 *obj_size
= astate
->size
;
10017 *lastmod
= astate
->mtime
;
10023 int RGWRados::Bucket::UpdateIndex::guard_reshard(BucketShard
**pbs
, std::function
<int(BucketShard
*)> call
)
10025 RGWRados
*store
= target
->get_store();
10029 #define NUM_RESHARD_RETRIES 10
10030 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
10031 int ret
= get_bucket_shard(&bs
);
10033 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10037 if (r
!= -ERR_BUSY_RESHARDING
) {
10040 ldout(store
->ctx(), 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
10041 string new_bucket_id
;
10042 r
= store
->block_while_resharding(bs
, &new_bucket_id
);
10043 if (r
== -ERR_BUSY_RESHARDING
) {
10049 ldout(store
->ctx(), 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
10050 i
= 0; /* resharding is finished, make sure we can retry */
10051 r
= target
->update_bucket_id(new_bucket_id
);
10053 ldout(store
->ctx(), 0) << "ERROR: update_bucket_id() new_bucket_id=" << new_bucket_id
<< " returned r=" << r
<< dendl
;
10070 int RGWRados::SystemObject::Read::stat(RGWObjVersionTracker
*objv_tracker
)
10072 RGWRados
*store
= source
->get_store();
10073 rgw_raw_obj
& obj
= source
->get_obj();
10075 return store
->stat_system_obj(source
->get_ctx(), state
, obj
, stat_params
.attrs
,
10076 stat_params
.lastmod
, stat_params
.obj_size
, objv_tracker
);
10079 int RGWRados::Bucket::UpdateIndex::prepare(RGWModifyOp op
, const string
*write_tag
)
10084 RGWRados
*store
= target
->get_store();
10086 if (write_tag
&& write_tag
->length()) {
10087 optag
= string(write_tag
->c_str(), write_tag
->length());
10089 if (optag
.empty()) {
10090 append_rand_alpha(store
->ctx(), optag
, optag
, 32);
10094 int r
= guard_reshard(nullptr, [&](BucketShard
*bs
) -> int {
10095 return store
->cls_obj_prepare_op(*bs
, op
, optag
, obj
, bilog_flags
, zones_trace
);
10106 int RGWRados::Bucket::UpdateIndex::complete(int64_t poolid
, uint64_t epoch
,
10107 uint64_t size
, uint64_t accounted_size
,
10108 ceph::real_time
& ut
, const string
& etag
,
10109 const string
& content_type
,
10110 bufferlist
*acl_bl
,
10111 RGWObjCategory category
,
10112 list
<rgw_obj_index_key
> *remove_objs
, const string
*user_data
)
10117 RGWRados
*store
= target
->get_store();
10120 int ret
= get_bucket_shard(&bs
);
10122 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10126 rgw_bucket_dir_entry ent
;
10127 obj
.key
.get_index_key(&ent
.key
);
10128 ent
.meta
.size
= size
;
10129 ent
.meta
.accounted_size
= accounted_size
;
10130 ent
.meta
.mtime
= ut
;
10131 ent
.meta
.etag
= etag
;
10133 ent
.meta
.user_data
= *user_data
;
10136 if (acl_bl
&& acl_bl
->length()) {
10137 int ret
= store
->decode_policy(*acl_bl
, &owner
);
10139 ldout(store
->ctx(), 0) << "WARNING: could not decode policy ret=" << ret
<< dendl
;
10142 ent
.meta
.owner
= owner
.get_id().to_str();
10143 ent
.meta
.owner_display_name
= owner
.get_display_name();
10144 ent
.meta
.content_type
= content_type
;
10146 ret
= store
->cls_obj_complete_add(*bs
, obj
, optag
, poolid
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
10148 if (target
->bucket_info
.datasync_flag_enabled()) {
10149 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10151 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10158 int RGWRados::Bucket::UpdateIndex::complete_del(int64_t poolid
, uint64_t epoch
,
10159 real_time
& removed_mtime
,
10160 list
<rgw_obj_index_key
> *remove_objs
)
10165 RGWRados
*store
= target
->get_store();
10168 int ret
= get_bucket_shard(&bs
);
10170 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10174 ret
= store
->cls_obj_complete_del(*bs
, optag
, poolid
, epoch
, obj
, removed_mtime
, remove_objs
, bilog_flags
, zones_trace
);
10176 if (target
->bucket_info
.datasync_flag_enabled()) {
10177 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10179 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10187 int RGWRados::Bucket::UpdateIndex::cancel()
10192 RGWRados
*store
= target
->get_store();
10195 int ret
= guard_reshard(&bs
, [&](BucketShard
*bs
) -> int {
10196 return store
->cls_obj_complete_cancel(*bs
, optag
, obj
, bilog_flags
, zones_trace
);
10200 * need to update data log anyhow, so that whoever follows needs to update its internal markers
10201 * for following the specific bucket shard log. Otherwise they end up staying behind, and users
10202 * have no way to tell that they're all caught up
10204 if (target
->bucket_info
.datasync_flag_enabled()) {
10205 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10207 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10214 int RGWRados::Object::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
)
10216 RGWRados
*store
= source
->get_store();
10217 CephContext
*cct
= store
->ctx();
10219 rgw_raw_obj read_obj
;
10220 uint64_t read_ofs
= ofs
;
10221 uint64_t len
, read_len
;
10222 bool reading_from_head
= true;
10223 ObjectReadOperation op
;
10225 bool merge_bl
= false;
10226 bufferlist
*pbl
= &bl
;
10227 bufferlist read_bl
;
10228 uint64_t max_chunk_size
;
10230 RGWObjState
*astate
;
10231 int r
= source
->get_state(&astate
, true);
10238 len
= end
- ofs
+ 1;
10240 if (astate
->has_manifest
&& astate
->manifest
.has_tail()) {
10241 /* now get the relevant object part */
10242 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10244 uint64_t stripe_ofs
= iter
.get_stripe_ofs();
10245 read_obj
= iter
.get_location().get_raw_obj(store
);
10246 len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10247 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10248 reading_from_head
= (read_obj
== state
.head_obj
);
10250 read_obj
= state
.head_obj
;
10253 r
= store
->get_max_chunk_size(read_obj
.pool
, &max_chunk_size
);
10255 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for pool " << read_obj
.pool
<< dendl
;
10259 if (len
> max_chunk_size
)
10260 len
= max_chunk_size
;
10263 state
.io_ctx
.locator_set_key(read_obj
.loc
);
10267 if (reading_from_head
) {
10268 /* only when reading from the head object do we need to do the atomic test */
10269 r
= store
->append_atomic_test(&source
->get_ctx(), source
->get_bucket_info(), state
.obj
, op
, &astate
);
10273 if (astate
&& astate
->prefetch_data
) {
10274 if (!ofs
&& astate
->data
.length() >= len
) {
10276 return bl
.length();
10279 if (ofs
< astate
->data
.length()) {
10280 unsigned copy_len
= min((uint64_t)astate
->data
.length() - ofs
, len
);
10281 astate
->data
.copy(ofs
, copy_len
, bl
);
10282 read_len
-= copy_len
;
10283 read_ofs
+= copy_len
;
10285 return bl
.length();
10293 ldout(cct
, 20) << "rados->read obj-ofs=" << ofs
<< " read_ofs=" << read_ofs
<< " read_len=" << read_len
<< dendl
;
10294 op
.read(read_ofs
, read_len
, pbl
, NULL
);
10296 r
= state
.io_ctx
.operate(read_obj
.oid
, &op
, NULL
);
10297 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10304 bl
.append(read_bl
);
10307 return bl
.length();
10310 int RGWRados::SystemObject::Read::GetObjState::get_ref(RGWRados
*store
, rgw_raw_obj
& obj
, rgw_rados_ref
**pref
)
10313 int r
= store
->get_raw_obj_ref(obj
, &ref
);
10324 int RGWRados::get_system_obj(RGWObjectCtx
& obj_ctx
, RGWRados::SystemObject::Read::GetObjState
& read_state
,
10325 RGWObjVersionTracker
*objv_tracker
, rgw_raw_obj
& obj
,
10326 bufferlist
& bl
, off_t ofs
, off_t end
,
10327 map
<string
, bufferlist
> *attrs
,
10328 rgw_cache_entry_info
*cache_info
,
10329 boost::optional
<obj_version
>)
10332 ObjectReadOperation op
;
10337 len
= end
- ofs
+ 1;
10339 if (objv_tracker
) {
10340 objv_tracker
->prepare_op_for_read(&op
);
10343 ldout(cct
, 20) << "rados->read ofs=" << ofs
<< " len=" << len
<< dendl
;
10344 op
.read(ofs
, len
, &bl
, NULL
);
10347 op
.getxattrs(attrs
, NULL
);
10350 rgw_rados_ref
*ref
;
10351 int r
= read_state
.get_ref(this, obj
, &ref
);
10353 ldout(cct
, 20) << "read_state.get_ref() on obj=" << obj
<< " returned " << r
<< dendl
;
10356 r
= ref
->ioctx
.operate(ref
->oid
, &op
, NULL
);
10358 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10361 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10363 uint64_t op_ver
= ref
->ioctx
.get_last_version();
10365 if (read_state
.last_ver
> 0 &&
10366 read_state
.last_ver
!= op_ver
) {
10367 ldout(cct
, 5) << "raced with an object write, abort" << dendl
;
10371 read_state
.last_ver
= op_ver
;
10373 return bl
.length();
10376 int RGWRados::SystemObject::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
,
10377 RGWObjVersionTracker
*objv_tracker
,
10378 boost::optional
<obj_version
> refresh_version
)
10380 RGWRados
*store
= source
->get_store();
10381 rgw_raw_obj
& obj
= source
->get_obj();
10383 return store
->get_system_obj(source
->get_ctx(), state
, objv_tracker
, obj
, bl
,
10384 ofs
, end
, read_params
.attrs
,
10385 read_params
.cache_info
, refresh_version
);
10388 int RGWRados::SystemObject::Read::get_attr(const char *name
, bufferlist
& dest
)
10390 RGWRados
*store
= source
->get_store();
10391 rgw_raw_obj
& obj
= source
->get_obj();
10393 return store
->system_obj_get_attr(obj
, name
, dest
);
10396 struct get_obj_data
;
10398 struct get_obj_aio_data
{
10399 struct get_obj_data
*op_data
;
10404 struct get_obj_io
{
10409 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
);
10411 struct get_obj_data
: public RefCountedObject
{
10416 map
<off_t
, get_obj_io
> io_map
;
10417 map
<off_t
, librados::AioCompletion
*> completion_map
;
10418 uint64_t total_read
;
10421 list
<get_obj_aio_data
> aio_data
;
10422 RGWGetDataCB
*client_cb
;
10423 std::atomic
<bool> cancelled
= { false };
10424 std::atomic
<int64_t> err_code
= { 0 };
10426 list
<bufferlist
> read_list
;
10428 explicit get_obj_data(CephContext
*_cct
)
10430 rados(NULL
), ctx(NULL
),
10431 total_read(0), lock("get_obj_data"), data_lock("get_obj_data::data_lock"),
10433 throttle(cct
, "get_obj_data", cct
->_conf
->rgw_get_obj_window_size
, false) {}
10434 ~get_obj_data() override
{ }
10435 void set_cancelled(int r
) {
10440 bool is_cancelled() {
10444 int get_err_code() {
10448 int wait_next_io(bool *done
) {
10450 map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10451 if (iter
== completion_map
.end()) {
10456 off_t cur_ofs
= iter
->first
;
10457 librados::AioCompletion
*c
= iter
->second
;
10460 c
->wait_for_safe_and_cb();
10461 int r
= c
->get_return_value();
10464 completion_map
.erase(cur_ofs
);
10466 if (completion_map
.empty()) {
10476 void add_io(off_t ofs
, off_t len
, bufferlist
**pbl
, AioCompletion
**pc
) {
10477 Mutex::Locker
l(lock
);
10479 const auto& io_iter
= io_map
.insert(
10480 map
<off_t
, get_obj_io
>::value_type(ofs
, get_obj_io()));
10482 assert(io_iter
.second
); // assert new insertion
10484 get_obj_io
& io
= (io_iter
.first
)->second
;
10487 struct get_obj_aio_data aio
;
10490 aio
.op_data
= this;
10492 aio_data
.push_back(aio
);
10494 struct get_obj_aio_data
*paio_data
= &aio_data
.back(); /* last element */
10496 librados::AioCompletion
*c
= librados::Rados::aio_create_completion((void *)paio_data
, NULL
, _get_obj_aio_completion_cb
);
10497 completion_map
[ofs
] = c
;
10501 /* we have a reference per IO, plus one reference for the calling function.
10502 * reference is dropped for each callback, plus when we're done iterating
10503 * over the parts */
10507 void cancel_io(off_t ofs
) {
10508 ldout(cct
, 20) << "get_obj_data::cancel_io() ofs=" << ofs
<< dendl
;
10510 map
<off_t
, AioCompletion
*>::iterator iter
= completion_map
.find(ofs
);
10511 if (iter
!= completion_map
.end()) {
10512 AioCompletion
*c
= iter
->second
;
10514 completion_map
.erase(ofs
);
10519 /* we don't drop a reference here -- e.g., not calling d->put(), because we still
10520 * need IoCtx to live, as io callback may still be called
10524 void cancel_all_io() {
10525 ldout(cct
, 20) << "get_obj_data::cancel_all_io()" << dendl
;
10526 Mutex::Locker
l(lock
);
10527 for (map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10528 iter
!= completion_map
.end(); ++iter
) {
10529 librados::AioCompletion
*c
= iter
->second
;
10534 int get_complete_ios(off_t ofs
, list
<bufferlist
>& bl_list
) {
10535 Mutex::Locker
l(lock
);
10537 map
<off_t
, get_obj_io
>::iterator liter
= io_map
.begin();
10539 if (liter
== io_map
.end() ||
10540 liter
->first
!= ofs
) {
10544 map
<off_t
, librados::AioCompletion
*>::iterator aiter
;
10545 aiter
= completion_map
.find(ofs
);
10546 if (aiter
== completion_map
.end()) {
10547 /* completion map does not hold this io, it was cancelled */
10551 AioCompletion
*completion
= aiter
->second
;
10552 int r
= completion
->get_return_value();
10556 for (; aiter
!= completion_map
.end(); ++aiter
) {
10557 completion
= aiter
->second
;
10558 if (!completion
->is_safe()) {
10559 /* reached a request that is not yet complete, stop */
10563 r
= completion
->get_return_value();
10565 set_cancelled(r
); /* mark it as cancelled, so that we don't continue processing next operations */
10571 map
<off_t
, get_obj_io
>::iterator old_liter
= liter
++;
10572 bl_list
.push_back(old_liter
->second
.bl
);
10573 io_map
.erase(old_liter
);
10580 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
)
10582 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10584 return d
->rados
->get_obj_iterate_cb(d
->ctx
, astate
, bucket_info
, obj
, read_obj
, obj_ofs
, read_ofs
, len
, is_head_obj
, arg
);
10587 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
)
10589 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10590 struct get_obj_data
*d
= aio_data
->op_data
;
10592 d
->rados
->get_obj_aio_completion_cb(cb
, arg
);
10596 void RGWRados::get_obj_aio_completion_cb(completion_t c
, void *arg
)
10598 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10599 struct get_obj_data
*d
= aio_data
->op_data
;
10600 off_t ofs
= aio_data
->ofs
;
10601 off_t len
= aio_data
->len
;
10603 list
<bufferlist
> bl_list
;
10604 list
<bufferlist
>::iterator iter
;
10607 ldout(cct
, 20) << "get_obj_aio_completion_cb: io completion ofs=" << ofs
<< " len=" << len
<< dendl
;
10608 d
->throttle
.put(len
);
10610 r
= rados_aio_get_return_value(c
);
10612 ldout(cct
, 0) << "ERROR: got unexpected error when trying to read object: " << r
<< dendl
;
10613 d
->set_cancelled(r
);
10617 if (d
->is_cancelled()) {
10621 d
->data_lock
.Lock();
10623 r
= d
->get_complete_ios(ofs
, bl_list
);
10628 d
->read_list
.splice(d
->read_list
.end(), bl_list
);
10631 d
->data_lock
.Unlock();
10637 int RGWRados::flush_read_list(struct get_obj_data
*d
)
10639 d
->data_lock
.Lock();
10640 list
<bufferlist
> l
;
10641 l
.swap(d
->read_list
);
10643 d
->read_list
.clear();
10645 d
->data_lock
.Unlock();
10649 list
<bufferlist
>::iterator iter
;
10650 for (iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
10651 bufferlist
& bl
= *iter
;
10652 r
= d
->client_cb
->handle_data(bl
, 0, bl
.length());
10654 dout(0) << "ERROR: flush_read_list(): d->client_cb->handle_data() returned " << r
<< dendl
;
10659 d
->data_lock
.Lock();
10662 d
->set_cancelled(r
);
10664 d
->data_lock
.Unlock();
10668 int RGWRados::get_obj_iterate_cb(RGWObjectCtx
*ctx
, RGWObjState
*astate
,
10669 const RGWBucketInfo
& bucket_info
,
10670 const rgw_obj
& obj
,
10671 const rgw_raw_obj
& read_obj
,
10673 off_t read_ofs
, off_t len
,
10674 bool is_head_obj
, void *arg
)
10676 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
10677 ObjectReadOperation op
;
10678 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10686 /* only when reading from the head object do we need to do the atomic test */
10687 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &astate
);
10692 obj_ofs
< astate
->data
.length()) {
10693 unsigned chunk_len
= min((uint64_t)astate
->data
.length() - obj_ofs
, (uint64_t)len
);
10695 d
->data_lock
.Lock();
10696 r
= d
->client_cb
->handle_data(astate
->data
, obj_ofs
, chunk_len
);
10697 d
->data_lock
.Unlock();
10702 d
->total_read
+= chunk_len
;
10706 read_ofs
+= chunk_len
;
10707 obj_ofs
+= chunk_len
;
10713 d
->throttle
.get(len
);
10714 if (d
->is_cancelled()) {
10715 return d
->get_err_code();
10718 /* add io after we check that we're not cancelled, otherwise we're going to have trouble
10721 d
->add_io(obj_ofs
, len
, &pbl
, &c
);
10723 ldout(cct
, 20) << "rados->get_obj_iterate_cb oid=" << read_obj
.oid
<< " obj-ofs=" << obj_ofs
<< " read_ofs=" << read_ofs
<< " len=" << len
<< dendl
;
10724 op
.read(read_ofs
, len
, pbl
, NULL
);
10726 librados::IoCtx
io_ctx(d
->io_ctx
);
10727 io_ctx
.locator_set_key(read_obj
.loc
);
10729 r
= io_ctx
.aio_operate(read_obj
.oid
, c
, &op
, NULL
);
10731 ldout(cct
, 0) << "rados->aio_operate r=" << r
<< dendl
;
10735 // Flush data to client if there is any
10736 r
= flush_read_list(d
);
10743 ldout(cct
, 20) << "cancelling io r=" << r
<< " obj_ofs=" << obj_ofs
<< dendl
;
10744 d
->set_cancelled(r
);
10745 d
->cancel_io(obj_ofs
);
10750 int RGWRados::Object::Read::iterate(int64_t ofs
, int64_t end
, RGWGetDataCB
*cb
)
10752 RGWRados
*store
= source
->get_store();
10753 CephContext
*cct
= store
->ctx();
10755 struct get_obj_data
*data
= new get_obj_data(cct
);
10758 RGWObjectCtx
& obj_ctx
= source
->get_ctx();
10760 data
->rados
= store
;
10761 data
->io_ctx
.dup(state
.io_ctx
);
10762 data
->client_cb
= cb
;
10764 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
);
10766 data
->cancel_all_io();
10771 r
= data
->wait_next_io(&done
);
10773 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10774 data
->cancel_all_io();
10777 r
= store
->flush_read_list(data
);
10779 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10780 data
->cancel_all_io();
10790 int RGWRados::iterate_obj(RGWObjectCtx
& obj_ctx
,
10791 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
10792 off_t ofs
, off_t end
,
10793 uint64_t max_chunk_size
,
10794 int (*iterate_obj_cb
)(const RGWBucketInfo
&, const rgw_obj
& obj
,
10795 const rgw_raw_obj
&, off_t
, off_t
, off_t
, bool,
10796 RGWObjState
*, void *),
10799 rgw_raw_obj head_obj
;
10800 rgw_raw_obj read_obj
;
10801 uint64_t read_ofs
= ofs
;
10803 bool reading_from_head
= true;
10804 RGWObjState
*astate
= NULL
;
10806 obj_to_raw(bucket_info
.placement_rule
, obj
, &head_obj
);
10808 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &astate
, false);
10816 len
= end
- ofs
+ 1;
10818 if (astate
->has_manifest
) {
10819 /* now get the relevant object stripe */
10820 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10822 RGWObjManifest::obj_iterator obj_end
= astate
->manifest
.obj_end();
10824 for (; iter
!= obj_end
&& ofs
<= end
; ++iter
) {
10825 off_t stripe_ofs
= iter
.get_stripe_ofs();
10826 off_t next_stripe_ofs
= stripe_ofs
+ iter
.get_stripe_size();
10828 while (ofs
< next_stripe_ofs
&& ofs
<= end
) {
10829 read_obj
= iter
.get_location().get_raw_obj(this);
10830 uint64_t read_len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10831 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10833 if (read_len
> max_chunk_size
) {
10834 read_len
= max_chunk_size
;
10837 reading_from_head
= (read_obj
== head_obj
);
10838 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, read_ofs
, read_len
, reading_from_head
, astate
, arg
);
10848 while (ofs
<= end
) {
10849 read_obj
= head_obj
;
10850 uint64_t read_len
= min(len
, max_chunk_size
);
10852 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, ofs
, read_len
, reading_from_head
, astate
, arg
);
10865 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectWriteOperation
*op
)
10868 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10873 return ref
.ioctx
.operate(ref
.oid
, op
);
10876 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectReadOperation
*op
)
10879 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10886 return ref
.ioctx
.operate(ref
.oid
, op
, &outbl
);
10889 int RGWRados::olh_init_modification_impl(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, string
*op_tag
)
10891 ObjectWriteOperation op
;
10893 assert(olh_obj
.key
.instance
.empty());
10895 bool has_tag
= (state
.exists
&& has_olh_tag(state
.attrset
));
10897 if (!state
.exists
) {
10900 op
.assert_exists();
10901 struct timespec mtime_ts
= real_clock::to_timespec(state
.mtime
);
10902 op
.mtime2(&mtime_ts
);
10906 * 3 possible cases: olh object doesn't exist, it exists as an olh, it exists as a regular object.
10907 * If it exists as a regular object we'll need to transform it into an olh. We'll do it in two
10908 * steps, first change its tag and set the olh pending attrs. Once write is done we'll need to
10909 * truncate it, remove extra attrs, and send it to the garbage collection. The bucket index olh
10910 * log will reflect that.
10912 * Need to generate separate olh and obj tags, as olh can be colocated with object data. obj_tag
10913 * is used for object data instance, olh_tag for olh instance.
10916 /* guard against racing writes */
10917 bucket_index_guard_olh_op(state
, op
);
10923 int ret
= gen_rand_alphanumeric_lower(cct
, &obj_tag
, 32);
10925 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10929 bl
.append(obj_tag
.c_str(), obj_tag
.size());
10930 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
10932 state
.attrset
[RGW_ATTR_ID_TAG
] = bl
;
10933 state
.obj_tag
= bl
;
10937 ret
= gen_rand_alphanumeric_lower(cct
, &olh_tag
, 32);
10939 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10943 olh_bl
.append(olh_tag
.c_str(), olh_tag
.size());
10944 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, olh_bl
);
10946 state
.attrset
[RGW_ATTR_OLH_ID_TAG
] = olh_bl
;
10947 state
.olh_tag
= olh_bl
;
10948 state
.is_olh
= true;
10951 op
.setxattr(RGW_ATTR_OLH_VER
, verbl
);
10955 RGWOLHPendingInfo pending_info
;
10956 pending_info
.time
= real_clock::now();
10957 ::encode(pending_info
, bl
);
10959 #define OLH_PENDING_TAG_LEN 32
10960 /* tag will start with current time epoch, this so that entries are sorted by time */
10962 utime_t
ut(pending_info
.time
);
10963 snprintf(buf
, sizeof(buf
), "%016llx", (unsigned long long)ut
.sec());
10967 int ret
= gen_rand_alphanumeric_lower(cct
, &s
, OLH_PENDING_TAG_LEN
- op_tag
->size());
10969 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10974 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
10975 attr_name
.append(*op_tag
);
10977 op
.setxattr(attr_name
.c_str(), bl
);
10979 ret
= obj_operate(bucket_info
, olh_obj
, &op
);
10984 state
.exists
= true;
10985 state
.attrset
[attr_name
] = bl
;
10990 int RGWRados::olh_init_modification(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj
, string
*op_tag
)
10994 ret
= olh_init_modification_impl(bucket_info
, state
, obj
, op_tag
);
10995 if (ret
== -EEXIST
) {
11002 int RGWRados::guard_reshard(BucketShard
*bs
, const rgw_obj
& obj_instance
, std::function
<int(BucketShard
*)> call
)
11005 const rgw_obj
*pobj
= &obj_instance
;
11008 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
11009 r
= bs
->init(pobj
->bucket
, *pobj
);
11011 ldout(cct
, 5) << "bs.init() returned ret=" << r
<< dendl
;
11015 if (r
!= -ERR_BUSY_RESHARDING
) {
11018 ldout(cct
, 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
11019 string new_bucket_id
;
11020 r
= block_while_resharding(bs
, &new_bucket_id
);
11021 if (r
== -ERR_BUSY_RESHARDING
) {
11027 ldout(cct
, 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
11028 i
= 0; /* resharding is finished, make sure we can retry */
11031 obj
.bucket
.update_bucket_id(new_bucket_id
);
11042 int RGWRados::block_while_resharding(RGWRados::BucketShard
*bs
, string
*new_bucket_id
)
11044 std::shared_ptr
<RGWReshardWait
> waiter
= reshard_wait
;
11046 return waiter
->block_while_resharding(bs
, new_bucket_id
);
11049 int RGWRados::bucket_index_link_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& olh_state
, const rgw_obj
& obj_instance
,
11050 bool delete_marker
,
11051 const string
& op_tag
,
11052 struct rgw_bucket_dir_entry_meta
*meta
,
11053 uint64_t olh_epoch
,
11054 real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*_zones_trace
)
11057 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11062 rgw_zone_set zones_trace
;
11063 if (_zones_trace
) {
11064 zones_trace
= *_zones_trace
;
11066 zones_trace
.insert(get_zone().id
);
11069 BucketShard
bs(this);
11071 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
11072 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11073 librados::ObjectWriteOperation op
;
11074 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11075 return cls_rgw_bucket_link_olh(bs
->index_ctx
, op
,
11076 bs
->bucket_obj
, key
, olh_state
.olh_tag
, delete_marker
, op_tag
, meta
, olh_epoch
,
11077 unmod_since
, high_precision_time
,
11078 get_zone().log_data
, zones_trace
);
11081 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
11088 void RGWRados::bucket_index_guard_olh_op(RGWObjState
& olh_state
, ObjectOperation
& op
)
11090 ldout(cct
, 20) << __func__
<< "(): olh_state.olh_tag=" << string(olh_state
.olh_tag
.c_str(), olh_state
.olh_tag
.length()) << dendl
;
11091 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_state
.olh_tag
);
11094 int RGWRados::bucket_index_unlink_instance(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj_instance
,
11095 const string
& op_tag
, const string
& olh_tag
, uint64_t olh_epoch
, rgw_zone_set
*_zones_trace
)
11098 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11103 rgw_zone_set zones_trace
;
11104 if (_zones_trace
) {
11105 zones_trace
= *_zones_trace
;
11107 zones_trace
.insert(get_zone().id
);
11109 BucketShard
bs(this);
11111 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
11112 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11113 librados::ObjectWriteOperation op
;
11114 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11115 return cls_rgw_bucket_unlink_instance(bs
->index_ctx
, op
, bs
->bucket_obj
, key
, op_tag
,
11116 olh_tag
, olh_epoch
, get_zone().log_data
, zones_trace
);
11119 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
11126 int RGWRados::bucket_index_read_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
,
11127 const rgw_obj
& obj_instance
, uint64_t ver_marker
,
11128 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > *log
,
11129 bool *is_truncated
)
11132 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11137 BucketShard
bs(this);
11138 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11140 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11144 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11146 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11148 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11149 ObjectReadOperation op
;
11150 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11151 return cls_rgw_get_olh_log(bs
->index_ctx
, bs
->bucket_obj
, op
,
11152 key
, ver_marker
, olh_tag
, log
, is_truncated
);
11155 ldout(cct
, 20) << "cls_rgw_get_olh_log() returned r=" << r
<< dendl
;
11162 int RGWRados::bucket_index_trim_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
, uint64_t ver
)
11165 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11170 BucketShard
bs(this);
11171 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11173 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11177 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11179 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11181 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11182 ObjectWriteOperation op
;
11183 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11184 cls_rgw_trim_olh_log(op
, key
, ver
, olh_tag
);
11185 return pbs
->index_ctx
.operate(pbs
->bucket_obj
, &op
);
11188 ldout(cct
, 20) << "cls_rgw_trim_olh_log() returned r=" << ret
<< dendl
;
11195 int RGWRados::bucket_index_clear_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
)
11198 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11203 BucketShard
bs(this);
11205 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11207 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11209 int ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11210 ObjectWriteOperation op
;
11211 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11212 return cls_rgw_clear_olh(pbs
->index_ctx
, op
, pbs
->bucket_obj
, key
, olh_tag
);
11215 ldout(cct
, 5) << "cls_rgw_clear_olh() returned ret=" << ret
<< dendl
;
11222 int RGWRados::apply_olh_log(RGWObjectCtx
& obj_ctx
, RGWObjState
& state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
11223 bufferlist
& olh_tag
, map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >& log
,
11224 uint64_t *plast_ver
, rgw_zone_set
* zones_trace
)
11230 librados::ObjectWriteOperation op
;
11232 uint64_t last_ver
= log
.rbegin()->first
;
11233 *plast_ver
= last_ver
;
11235 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >::iterator iter
= log
.begin();
11237 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11238 op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11240 struct timespec mtime_ts
= real_clock::to_timespec(state
.mtime
);
11241 op
.mtime2(&mtime_ts
);
11243 bool need_to_link
= false;
11244 cls_rgw_obj_key key
;
11245 bool delete_marker
= false;
11246 list
<cls_rgw_obj_key
> remove_instances
;
11247 bool need_to_remove
= false;
11249 for (iter
= log
.begin(); iter
!= log
.end(); ++iter
) {
11250 vector
<rgw_bucket_olh_log_entry
>::iterator viter
= iter
->second
.begin();
11251 for (; viter
!= iter
->second
.end(); ++viter
) {
11252 rgw_bucket_olh_log_entry
& entry
= *viter
;
11254 ldout(cct
, 20) << "olh_log_entry: op=" << (int)entry
.op
11255 << " key=" << entry
.key
.name
<< "[" << entry
.key
.instance
<< "] "
11256 << (entry
.delete_marker
? "(delete)" : "") << dendl
;
11257 switch (entry
.op
) {
11258 case CLS_RGW_OLH_OP_REMOVE_INSTANCE
:
11259 remove_instances
.push_back(entry
.key
);
11261 case CLS_RGW_OLH_OP_LINK_OLH
:
11262 need_to_link
= true;
11263 need_to_remove
= false;
11265 delete_marker
= entry
.delete_marker
;
11267 case CLS_RGW_OLH_OP_UNLINK_OLH
:
11268 need_to_remove
= true;
11269 need_to_link
= false;
11272 ldout(cct
, 0) << "ERROR: apply_olh_log: invalid op: " << (int)entry
.op
<< dendl
;
11275 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
11276 attr_name
.append(entry
.op_tag
);
11277 op
.rmxattr(attr_name
.c_str());
11282 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
11287 const rgw_bucket
& bucket
= obj
.bucket
;
11289 if (need_to_link
) {
11290 rgw_obj
target(bucket
, key
);
11292 info
.target
= target
;
11293 info
.removed
= delete_marker
;
11295 ::encode(info
, bl
);
11296 op
.setxattr(RGW_ATTR_OLH_INFO
, bl
);
11299 /* first remove object instances */
11300 for (list
<cls_rgw_obj_key
>::iterator liter
= remove_instances
.begin();
11301 liter
!= remove_instances
.end(); ++liter
) {
11302 cls_rgw_obj_key
& key
= *liter
;
11303 rgw_obj
obj_instance(bucket
, key
);
11304 int ret
= delete_obj(obj_ctx
, bucket_info
, obj_instance
, 0, RGW_BILOG_FLAG_VERSIONED_OP
, ceph::real_time(), zones_trace
);
11305 if (ret
< 0 && ret
!= -ENOENT
) {
11306 ldout(cct
, 0) << "ERROR: delete_obj() returned " << ret
<< " obj_instance=" << obj_instance
<< dendl
;
11311 /* update olh object */
11312 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11313 if (r
== -ECANCELED
) {
11317 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11321 r
= bucket_index_trim_olh_log(bucket_info
, state
, obj
, last_ver
);
11323 ldout(cct
, 0) << "ERROR: could not trim olh log, r=" << r
<< dendl
;
11327 if (need_to_remove
) {
11328 ObjectWriteOperation rm_op
;
11330 rm_op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11331 rm_op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11332 cls_obj_check_prefix_exist(rm_op
, RGW_ATTR_OLH_PENDING_PREFIX
, true); /* fail if found one of these, pending modification */
11335 r
= ref
.ioctx
.operate(ref
.oid
, &rm_op
);
11336 if (r
== -ECANCELED
) {
11337 return 0; /* someone else won this race */
11340 * only clear if was successful, otherwise we might clobber pending operations on this object
11342 r
= bucket_index_clear_olh(bucket_info
, state
, obj
);
11344 ldout(cct
, 0) << "ERROR: could not clear bucket index olh entries r=" << r
<< dendl
;
11354 * read olh log and apply it
11356 int RGWRados::update_olh(RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_zone_set
*zones_trace
)
11358 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > log
;
11360 uint64_t ver_marker
= 0;
11363 int ret
= bucket_index_read_olh_log(bucket_info
, *state
, obj
, ver_marker
, &log
, &is_truncated
);
11367 ret
= apply_olh_log(obj_ctx
, *state
, bucket_info
, obj
, state
->olh_tag
, log
, &ver_marker
, zones_trace
);
11371 } while (is_truncated
);
11376 int RGWRados::set_olh(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
, bool delete_marker
, rgw_bucket_dir_entry_meta
*meta
,
11377 uint64_t olh_epoch
, real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*zones_trace
)
11381 rgw_obj olh_obj
= target_obj
;
11382 olh_obj
.key
.instance
.clear();
11384 RGWObjState
*state
= NULL
;
11389 #define MAX_ECANCELED_RETRY 100
11390 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11391 if (ret
== -ECANCELED
) {
11392 obj_ctx
.obj
.invalidate(olh_obj
);
11395 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11400 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11402 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11403 if (ret
== -ECANCELED
) {
11408 ret
= bucket_index_link_olh(bucket_info
, *state
, target_obj
, delete_marker
, op_tag
, meta
, olh_epoch
, unmod_since
, high_precision_time
, zones_trace
);
11410 ldout(cct
, 20) << "bucket_index_link_olh() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11411 if (ret
== -ECANCELED
) {
11419 if (i
== MAX_ECANCELED_RETRY
) {
11420 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11424 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11425 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11429 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11436 int RGWRados::unlink_obj_instance(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
,
11437 uint64_t olh_epoch
, rgw_zone_set
*zones_trace
)
11441 rgw_obj olh_obj
= target_obj
;
11442 olh_obj
.key
.instance
.clear();
11444 RGWObjState
*state
= NULL
;
11449 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11450 if (ret
== -ECANCELED
) {
11451 obj_ctx
.obj
.invalidate(olh_obj
);
11454 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11458 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11460 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11461 if (ret
== -ECANCELED
) {
11467 string
olh_tag(state
->olh_tag
.c_str(), state
->olh_tag
.length());
11469 ret
= bucket_index_unlink_instance(bucket_info
, target_obj
, op_tag
, olh_tag
, olh_epoch
, zones_trace
);
11471 ldout(cct
, 20) << "bucket_index_unlink_instance() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11472 if (ret
== -ECANCELED
) {
11480 if (i
== MAX_ECANCELED_RETRY
) {
11481 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11485 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
, zones_trace
);
11486 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11490 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11497 void RGWRados::gen_rand_obj_instance_name(rgw_obj
*target_obj
)
11499 #define OBJ_INSTANCE_LEN 32
11500 char buf
[OBJ_INSTANCE_LEN
+ 1];
11502 gen_rand_alphanumeric_no_underscore(cct
, buf
, OBJ_INSTANCE_LEN
); /* don't want it to get url escaped,
11503 no underscore for instance name due to the way we encode the raw keys */
11505 target_obj
->key
.set_instance(buf
);
11508 static void filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
11509 map
<string
, bufferlist
> *attrset
)
11512 map
<string
, bufferlist
>::iterator iter
;
11513 for (iter
= unfiltered_attrset
.lower_bound(check_prefix
);
11514 iter
!= unfiltered_attrset
.end(); ++iter
) {
11515 if (!boost::algorithm::starts_with(iter
->first
, check_prefix
))
11517 (*attrset
)[iter
->first
] = iter
->second
;
11521 int RGWRados::get_olh(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWOLHInfo
*olh
)
11523 map
<string
, bufferlist
> unfiltered_attrset
;
11525 ObjectReadOperation op
;
11526 op
.getxattrs(&unfiltered_attrset
, NULL
);
11529 int r
= obj_operate(bucket_info
, obj
, &op
);
11534 map
<string
, bufferlist
> attrset
;
11536 filter_attrset(unfiltered_attrset
, RGW_ATTR_OLH_PREFIX
, &attrset
);
11538 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_OLH_INFO
);
11539 if (iter
== attrset
.end()) { /* not an olh */
11544 bufferlist::iterator biter
= iter
->second
.begin();
11545 ::decode(*olh
, biter
);
11546 } catch (buffer::error
& err
) {
11547 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11554 void RGWRados::check_pending_olh_entries(map
<string
, bufferlist
>& pending_entries
,
11555 map
<string
, bufferlist
> *rm_pending_entries
)
11557 map
<string
, bufferlist
>::iterator iter
= pending_entries
.begin();
11559 real_time now
= real_clock::now();
11561 while (iter
!= pending_entries
.end()) {
11562 bufferlist::iterator biter
= iter
->second
.begin();
11563 RGWOLHPendingInfo pending_info
;
11565 ::decode(pending_info
, biter
);
11566 } catch (buffer::error
& err
) {
11567 /* skipping bad entry, we could remove it but it might hide a bug */
11568 ldout(cct
, 0) << "ERROR: failed to decode pending entry " << iter
->first
<< dendl
;
11573 map
<string
, bufferlist
>::iterator cur_iter
= iter
;
11575 if (now
- pending_info
.time
>= make_timespan(cct
->_conf
->rgw_olh_pending_timeout_sec
)) {
11576 (*rm_pending_entries
)[cur_iter
->first
] = cur_iter
->second
;
11577 pending_entries
.erase(cur_iter
);
11579 /* entries names are sorted by time (rounded to a second) */
11585 int RGWRados::remove_olh_pending_entries(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, map
<string
, bufferlist
>& pending_attrs
)
11587 ObjectWriteOperation op
;
11589 bucket_index_guard_olh_op(state
, op
);
11591 for (map
<string
, bufferlist
>::iterator iter
= pending_attrs
.begin(); iter
!= pending_attrs
.end(); ++iter
) {
11592 op
.rmxattr(iter
->first
.c_str());
11596 int r
= get_obj_head_ref(bucket_info
, olh_obj
, &ref
);
11601 /* update olh object */
11602 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11603 if (r
== -ENOENT
|| r
== -ECANCELED
) {
11604 /* raced with some other change, shouldn't sweat about it */
11608 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11615 int RGWRados::follow_olh(const RGWBucketInfo
& bucket_info
, RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const rgw_obj
& olh_obj
, rgw_obj
*target
)
11617 map
<string
, bufferlist
> pending_entries
;
11618 filter_attrset(state
->attrset
, RGW_ATTR_OLH_PENDING_PREFIX
, &pending_entries
);
11620 map
<string
, bufferlist
> rm_pending_entries
;
11621 check_pending_olh_entries(pending_entries
, &rm_pending_entries
);
11623 if (!rm_pending_entries
.empty()) {
11624 int ret
= remove_olh_pending_entries(bucket_info
, *state
, olh_obj
, rm_pending_entries
);
11626 ldout(cct
, 20) << "ERROR: rm_pending_entries returned ret=" << ret
<< dendl
;
11630 if (!pending_entries
.empty()) {
11631 ldout(cct
, 20) << __func__
<< "(): found pending entries, need to update_olh() on bucket=" << olh_obj
.bucket
<< dendl
;
11633 int ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11639 map
<string
, bufferlist
>::iterator iter
= state
->attrset
.find(RGW_ATTR_OLH_INFO
);
11640 assert(iter
!= state
->attrset
.end());
11643 bufferlist::iterator biter
= iter
->second
.begin();
11644 ::decode(olh
, biter
);
11645 } catch (buffer::error
& err
) {
11646 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11654 *target
= olh
.target
;
11659 int RGWRados::raw_obj_stat(rgw_raw_obj
& obj
, uint64_t *psize
, real_time
*pmtime
, uint64_t *epoch
,
11660 map
<string
, bufferlist
> *attrs
, bufferlist
*first_chunk
,
11661 RGWObjVersionTracker
*objv_tracker
)
11664 int r
= get_raw_obj_ref(obj
, &ref
);
11669 map
<string
, bufferlist
> unfiltered_attrset
;
11671 struct timespec mtime_ts
;
11673 ObjectReadOperation op
;
11674 if (objv_tracker
) {
11675 objv_tracker
->prepare_op_for_read(&op
);
11678 op
.getxattrs(&unfiltered_attrset
, NULL
);
11680 if (psize
|| pmtime
) {
11681 op
.stat2(&size
, &mtime_ts
, NULL
);
11684 op
.read(0, cct
->_conf
->rgw_max_chunk_size
, first_chunk
, NULL
);
11687 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &outbl
);
11690 *epoch
= ref
.ioctx
.get_last_version();
11699 *pmtime
= ceph::real_clock::from_timespec(mtime_ts
);
11701 filter_attrset(unfiltered_attrset
, RGW_ATTR_PREFIX
, attrs
);
11707 int RGWRados::get_bucket_stats(RGWBucketInfo
& bucket_info
, int shard_id
, string
*bucket_ver
, string
*master_ver
,
11708 map
<RGWObjCategory
, RGWStorageStats
>& stats
, string
*max_marker
, bool *syncstopped
)
11710 map
<string
, rgw_bucket_dir_header
> headers
;
11711 map
<int, string
> bucket_instance_ids
;
11712 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11717 assert(headers
.size() == bucket_instance_ids
.size());
11719 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11720 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11721 BucketIndexShardsManager ver_mgr
;
11722 BucketIndexShardsManager master_ver_mgr
;
11723 BucketIndexShardsManager marker_mgr
;
11725 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11726 accumulate_raw_stats(iter
->second
, stats
);
11727 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.ver
);
11728 ver_mgr
.add(viter
->first
, string(buf
));
11729 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.master_ver
);
11730 master_ver_mgr
.add(viter
->first
, string(buf
));
11731 if (shard_id
>= 0) {
11732 *max_marker
= iter
->second
.max_marker
;
11734 marker_mgr
.add(viter
->first
, iter
->second
.max_marker
);
11736 if (syncstopped
!= NULL
)
11737 *syncstopped
= iter
->second
.syncstopped
;
11739 ver_mgr
.to_string(bucket_ver
);
11740 master_ver_mgr
.to_string(master_ver
);
11741 if (shard_id
< 0) {
11742 marker_mgr
.to_string(max_marker
);
11747 int RGWRados::get_bi_log_status(RGWBucketInfo
& bucket_info
, int shard_id
,
11748 map
<int, string
>& markers
)
11750 map
<string
, rgw_bucket_dir_header
> headers
;
11751 map
<int, string
> bucket_instance_ids
;
11752 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11756 assert(headers
.size() == bucket_instance_ids
.size());
11758 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11759 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11761 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11762 if (shard_id
>= 0) {
11763 markers
[shard_id
] = iter
->second
.max_marker
;
11765 markers
[viter
->first
] = iter
->second
.max_marker
;
11771 class RGWGetBucketStatsContext
: public RGWGetDirHeader_CB
{
11772 RGWGetBucketStats_CB
*cb
;
11774 map
<RGWObjCategory
, RGWStorageStats
> stats
;
11780 RGWGetBucketStatsContext(RGWGetBucketStats_CB
*_cb
, uint32_t _pendings
)
11781 : cb(_cb
), pendings(_pendings
), stats(), ret_code(0), should_cb(true),
11782 lock("RGWGetBucketStatsContext") {}
11784 void handle_response(int r
, rgw_bucket_dir_header
& header
) override
{
11785 Mutex::Locker
l(lock
);
11788 accumulate_raw_stats(header
, stats
);
11793 // Are we all done?
11794 if (--pendings
== 0) {
11796 cb
->set_response(&stats
);
11798 cb
->handle_response(ret_code
);
11805 Mutex::Locker
l(lock
);
11810 int RGWRados::get_bucket_stats_async(RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetBucketStats_CB
*ctx
)
11813 RGWGetBucketStatsContext
*get_ctx
= new RGWGetBucketStatsContext(ctx
, bucket_info
.num_shards
? : 1);
11815 int r
= cls_bucket_head_async(bucket_info
, shard_id
, get_ctx
, &num_aio
);
11819 get_ctx
->unset_cb();
11826 class RGWGetUserStatsContext
: public RGWGetUserHeader_CB
{
11827 RGWGetUserStats_CB
*cb
;
11830 explicit RGWGetUserStatsContext(RGWGetUserStats_CB
* const cb
)
11833 void handle_response(int r
, cls_user_header
& header
) override
{
11834 const cls_user_stats
& hs
= header
.stats
;
11836 RGWStorageStats stats
;
11838 stats
.size
= hs
.total_bytes
;
11839 stats
.size_rounded
= hs
.total_bytes_rounded
;
11840 stats
.num_objects
= hs
.total_entries
;
11842 cb
->set_response(stats
);
11845 cb
->handle_response(r
);
11851 int RGWRados::get_user_stats(const rgw_user
& user
, RGWStorageStats
& stats
)
11853 string user_str
= user
.to_str();
11855 cls_user_header header
;
11856 int r
= cls_user_get_header(user_str
, &header
);
11860 const cls_user_stats
& hs
= header
.stats
;
11862 stats
.size
= hs
.total_bytes
;
11863 stats
.size_rounded
= hs
.total_bytes_rounded
;
11864 stats
.num_objects
= hs
.total_entries
;
11869 int RGWRados::get_user_stats_async(const rgw_user
& user
, RGWGetUserStats_CB
*ctx
)
11871 string user_str
= user
.to_str();
11873 RGWGetUserStatsContext
*get_ctx
= new RGWGetUserStatsContext(ctx
);
11874 int r
= cls_user_get_header_async(user_str
, get_ctx
);
11884 void RGWRados::get_bucket_meta_oid(const rgw_bucket
& bucket
, string
& oid
)
11886 oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ bucket
.get_key(':');
11889 void RGWRados::get_bucket_instance_obj(const rgw_bucket
& bucket
, rgw_raw_obj
& obj
)
11891 if (!bucket
.oid
.empty()) {
11892 obj
.init(get_zone_params().domain_root
, bucket
.oid
);
11895 get_bucket_meta_oid(bucket
, oid
);
11896 obj
.init(get_zone_params().domain_root
, oid
);
11900 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const string
& meta_key
, RGWBucketInfo
& info
,
11901 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11903 size_t pos
= meta_key
.find(':');
11904 if (pos
== string::npos
) {
11907 string oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ meta_key
;
11908 rgw_bucket_instance_key_to_oid(oid
);
11910 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11913 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const rgw_bucket
& bucket
, RGWBucketInfo
& info
,
11914 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11917 if (bucket
.oid
.empty()) {
11918 get_bucket_meta_oid(bucket
, oid
);
11923 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11926 int RGWRados::get_bucket_instance_from_oid(RGWObjectCtx
& obj_ctx
, const string
& oid
, RGWBucketInfo
& info
,
11927 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
,
11928 rgw_cache_entry_info
*cache_info
,
11929 boost::optional
<obj_version
> refresh_version
)
11931 ldout(cct
, 20) << "reading from " << get_zone_params().domain_root
<< ":" << oid
<< dendl
;
11935 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
,
11936 oid
, epbl
, &info
.objv_tracker
, pmtime
, pattrs
,
11937 cache_info
, refresh_version
);
11942 bufferlist::iterator iter
= epbl
.begin();
11944 ::decode(info
, iter
);
11945 } catch (buffer::error
& err
) {
11946 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11949 info
.bucket
.oid
= oid
;
11953 int RGWRados::get_bucket_entrypoint_info(RGWObjectCtx
& obj_ctx
,
11954 const string
& tenant_name
,
11955 const string
& bucket_name
,
11956 RGWBucketEntryPoint
& entry_point
,
11957 RGWObjVersionTracker
*objv_tracker
,
11959 map
<string
, bufferlist
> *pattrs
,
11960 rgw_cache_entry_info
*cache_info
,
11961 boost::optional
<obj_version
> refresh_version
)
11964 string bucket_entry
;
11966 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
11967 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
,
11968 bucket_entry
, bl
, objv_tracker
, pmtime
, pattrs
,
11969 cache_info
, refresh_version
);
11974 bufferlist::iterator iter
= bl
.begin();
11976 ::decode(entry_point
, iter
);
11977 } catch (buffer::error
& err
) {
11978 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11984 int RGWRados::convert_old_bucket_info(RGWObjectCtx
& obj_ctx
,
11985 const string
& tenant_name
,
11986 const string
& bucket_name
)
11988 RGWBucketEntryPoint entry_point
;
11989 real_time ep_mtime
;
11990 RGWObjVersionTracker ot
;
11991 map
<string
, bufferlist
> attrs
;
11992 RGWBucketInfo info
;
11994 ldout(cct
, 10) << "RGWRados::convert_old_bucket_info(): bucket=" << bucket_name
<< dendl
;
11996 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, entry_point
, &ot
, &ep_mtime
, &attrs
);
11998 ldout(cct
, 0) << "ERROR: get_bucket_entrypoint_info() returned " << ret
<< " bucket=" << bucket_name
<< dendl
;
12002 if (!entry_point
.has_bucket_info
) {
12003 /* already converted! */
12007 info
= entry_point
.old_bucket_info
;
12008 info
.bucket
.oid
= bucket_name
;
12009 info
.ep_objv
= ot
.read_version
;
12011 ot
.generate_new_write_ver(cct
);
12013 ret
= put_linked_bucket_info(info
, false, ep_mtime
, &ot
.write_version
, &attrs
, true);
12015 ldout(cct
, 0) << "ERROR: failed to put_linked_bucket_info(): " << ret
<< dendl
;
12022 int RGWRados::_get_bucket_info(RGWObjectCtx
& obj_ctx
,
12023 const string
& tenant
,
12024 const string
& bucket_name
,
12025 RGWBucketInfo
& info
,
12027 map
<string
, bufferlist
> *pattrs
,
12028 boost::optional
<obj_version
> refresh_version
)
12030 bucket_info_entry e
;
12031 string bucket_entry
;
12032 rgw_make_bucket_entry_name(tenant
, bucket_name
, bucket_entry
);
12035 if (binfo_cache
->find(bucket_entry
, &e
)) {
12036 if (refresh_version
&&
12037 e
.info
.objv_tracker
.read_version
.compare(&(*refresh_version
))) {
12038 lderr(cct
) << "WARNING: The bucket info cache is inconsistent. This is "
12039 << "a failure that should be debugged. I am a nice machine, "
12040 << "so I will try to recover." << dendl
;
12041 binfo_cache
->invalidate(bucket_entry
);
12051 RGWBucketEntryPoint entry_point
;
12052 real_time ep_mtime
;
12053 RGWObjVersionTracker ot
;
12054 rgw_cache_entry_info entry_cache_info
;
12055 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant
, bucket_name
,
12056 entry_point
, &ot
, &ep_mtime
, pattrs
,
12057 &entry_cache_info
, refresh_version
);
12059 /* only init these fields */
12060 info
.bucket
.tenant
= tenant
;
12061 info
.bucket
.name
= bucket_name
;
12065 if (entry_point
.has_bucket_info
) {
12066 info
= entry_point
.old_bucket_info
;
12067 info
.bucket
.oid
= bucket_name
;
12068 info
.bucket
.tenant
= tenant
;
12069 info
.ep_objv
= ot
.read_version
;
12070 ldout(cct
, 20) << "rgw_get_bucket_info: old bucket info, bucket=" << info
.bucket
<< " owner " << info
.owner
<< dendl
;
12074 /* data is in the bucket instance object, we need to get attributes from there, clear everything
12081 ldout(cct
, 20) << "rgw_get_bucket_info: bucket instance: " << entry_point
.bucket
<< dendl
;
12084 /* read bucket instance info */
12087 get_bucket_meta_oid(entry_point
.bucket
, oid
);
12089 rgw_cache_entry_info cache_info
;
12091 ret
= get_bucket_instance_from_oid(obj_ctx
, oid
, e
.info
, &e
.mtime
, &e
.attrs
,
12092 &cache_info
, refresh_version
);
12093 e
.info
.ep_objv
= ot
.read_version
;
12096 lderr(cct
) << "ERROR: get_bucket_instance_from_oid failed: " << ret
<< dendl
;
12097 info
.bucket
.tenant
= tenant
;
12098 info
.bucket
.name
= bucket_name
;
12099 // XXX and why return anything in case of an error anyway?
12108 list
<rgw_cache_entry_info
*> cache_info_entries
;
12109 cache_info_entries
.push_back(&entry_cache_info
);
12110 cache_info_entries
.push_back(&cache_info
);
12113 /* chain to both bucket entry point and bucket instance */
12114 if (!binfo_cache
->put(this, bucket_entry
, &e
, cache_info_entries
)) {
12115 ldout(cct
, 20) << "couldn't put binfo cache entry, might have raced with data changes" << dendl
;
12118 if (refresh_version
&&
12119 refresh_version
->compare(&info
.objv_tracker
.read_version
)) {
12120 lderr(cct
) << "WARNING: The OSD has the same version I have. Something may "
12121 << "have gone squirrelly. An administrator may have forced a "
12122 << "change; otherwise there is a problem somewhere." << dendl
;
12128 int RGWRados::get_bucket_info(RGWObjectCtx
& obj_ctx
,
12129 const string
& tenant
, const string
& bucket_name
,
12130 RGWBucketInfo
& info
,
12131 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
12133 return _get_bucket_info(obj_ctx
, tenant
, bucket_name
, info
, pmtime
,
12134 pattrs
, boost::none
);
12137 int RGWRados::try_refresh_bucket_info(RGWBucketInfo
& info
,
12138 ceph::real_time
*pmtime
,
12139 map
<string
, bufferlist
> *pattrs
)
12141 RGWObjectCtx
obj_ctx(this);
12143 return _get_bucket_info(obj_ctx
, info
.bucket
.tenant
, info
.bucket
.name
,
12144 info
, pmtime
, pattrs
, info
.objv_tracker
.read_version
);
12147 int RGWRados::put_bucket_entrypoint_info(const string
& tenant_name
, const string
& bucket_name
, RGWBucketEntryPoint
& entry_point
,
12148 bool exclusive
, RGWObjVersionTracker
& objv_tracker
, real_time mtime
,
12149 map
<string
, bufferlist
> *pattrs
)
12152 ::encode(entry_point
, epbl
);
12153 string bucket_entry
;
12154 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
12155 return rgw_bucket_store_info(this, bucket_entry
, epbl
, exclusive
, pattrs
, &objv_tracker
, mtime
);
12158 int RGWRados::put_bucket_instance_info(RGWBucketInfo
& info
, bool exclusive
,
12159 real_time mtime
, map
<string
, bufferlist
> *pattrs
)
12161 info
.has_instance_obj
= true;
12164 ::encode(info
, bl
);
12166 string key
= info
.bucket
.get_key(); /* when we go through meta api, we don't use oid directly */
12167 int ret
= rgw_bucket_instance_store_info(this, key
, bl
, exclusive
, pattrs
, &info
.objv_tracker
, mtime
);
12168 if (ret
== -EEXIST
) {
12169 /* well, if it's exclusive we shouldn't overwrite it, because we might race with another
12170 * bucket operation on this specific bucket (e.g., being synced from the master), but
12171 * since bucket instace meta object is unique for this specific bucket instace, we don't
12172 * need to return an error.
12173 * A scenario where we'd get -EEXIST here, is in a multi-zone config, we're not on the
12174 * master, creating a bucket, sending bucket creation to the master, we create the bucket
12175 * locally, while in the sync thread we sync the new bucket.
12182 int RGWRados::put_linked_bucket_info(RGWBucketInfo
& info
, bool exclusive
, real_time mtime
, obj_version
*pep_objv
,
12183 map
<string
, bufferlist
> *pattrs
, bool create_entry_point
)
12185 bool create_head
= !info
.has_instance_obj
|| create_entry_point
;
12187 int ret
= put_bucket_instance_info(info
, exclusive
, mtime
, pattrs
);
12193 return 0; /* done! */
12195 RGWBucketEntryPoint entry_point
;
12196 entry_point
.bucket
= info
.bucket
;
12197 entry_point
.owner
= info
.owner
;
12198 entry_point
.creation_time
= info
.creation_time
;
12199 entry_point
.linked
= true;
12200 RGWObjVersionTracker ot
;
12201 if (pep_objv
&& !pep_objv
->tag
.empty()) {
12202 ot
.write_version
= *pep_objv
;
12204 ot
.generate_new_write_ver(cct
);
12206 *pep_objv
= ot
.write_version
;
12209 ret
= put_bucket_entrypoint_info(info
.bucket
.tenant
, info
.bucket
.name
, entry_point
, exclusive
, ot
, mtime
, NULL
);
12216 int RGWRados::omap_get_vals(rgw_raw_obj
& obj
, bufferlist
& header
, const string
& marker
, uint64_t count
, std::map
<string
, bufferlist
>& m
)
12219 int r
= get_raw_obj_ref(obj
, &ref
);
12224 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, marker
, count
, &m
);
12232 int RGWRados::omap_get_all(rgw_raw_obj
& obj
, bufferlist
& header
,
12233 std::map
<string
, bufferlist
>& m
)
12236 int r
= get_raw_obj_ref(obj
, &ref
);
12241 #define MAX_OMAP_GET_ENTRIES 1024
12242 const int count
= MAX_OMAP_GET_ENTRIES
;
12243 string start_after
;
12246 std::map
<string
, bufferlist
> t
;
12247 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, start_after
, count
, &t
);
12254 start_after
= t
.rbegin()->first
;
12255 m
.insert(t
.begin(), t
.end());
12260 int RGWRados::omap_set(rgw_raw_obj
& obj
, const std::string
& key
, bufferlist
& bl
)
12263 int r
= get_raw_obj_ref(obj
, &ref
);
12267 ldout(cct
, 15) << "omap_set obj=" << obj
<< " key=" << key
<< dendl
;
12269 map
<string
, bufferlist
> m
;
12272 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12277 int RGWRados::omap_set(rgw_raw_obj
& obj
, std::map
<std::string
, bufferlist
>& m
)
12280 int r
= get_raw_obj_ref(obj
, &ref
);
12285 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12290 int RGWRados::omap_del(rgw_raw_obj
& obj
, const std::string
& key
)
12293 int r
= get_raw_obj_ref(obj
, &ref
);
12301 r
= ref
.ioctx
.omap_rm_keys(ref
.oid
, k
);
12305 int RGWRados::update_containers_stats(map
<string
, RGWBucketEnt
>& m
)
12307 RGWObjectCtx
obj_ctx(this);
12309 map
<string
, RGWBucketEnt
>::iterator iter
;
12310 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
12311 RGWBucketEnt
& ent
= iter
->second
;
12312 rgw_bucket
& bucket
= ent
.bucket
;
12315 ent
.size_rounded
= 0;
12317 map
<string
, rgw_bucket_dir_header
> headers
;
12319 RGWBucketInfo bucket_info
;
12320 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
12325 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
12329 map
<string
, rgw_bucket_dir_header
>::iterator hiter
= headers
.begin();
12330 for (; hiter
!= headers
.end(); ++hiter
) {
12331 RGWObjCategory category
= main_category
;
12332 map
<uint8_t, struct rgw_bucket_category_stats
>::iterator iter
= (hiter
->second
.stats
).find((uint8_t)category
);
12333 if (iter
!= hiter
->second
.stats
.end()) {
12334 struct rgw_bucket_category_stats
& stats
= iter
->second
;
12335 ent
.count
+= stats
.num_entries
;
12336 ent
.size
+= stats
.total_size
;
12337 ent
.size_rounded
+= stats
.total_size_rounded
;
12341 // fill in placement_rule from the bucket instance for use in swift's
12342 // per-storage policy statistics
12343 ent
.placement_rule
= std::move(bucket_info
.placement_rule
);
12349 int RGWRados::append_async(rgw_raw_obj
& obj
, size_t size
, bufferlist
& bl
)
12352 int r
= get_raw_obj_ref(obj
, &ref
);
12356 librados::Rados
*rad
= get_rados_handle();
12357 librados::AioCompletion
*completion
= rad
->aio_create_completion(NULL
, NULL
, NULL
);
12359 r
= ref
.ioctx
.aio_append(ref
.oid
, completion
, bl
, size
);
12360 completion
->release();
12364 int RGWRados::distribute(const string
& key
, bufferlist
& bl
)
12367 * we were called before watch was initialized. This can only happen if we're updating some system
12368 * config object (e.g., zone info) during init. Don't try to distribute the cache info for these
12369 * objects, they're currently only read on startup anyway.
12371 if (!watch_initialized
)
12375 pick_control_oid(key
, notify_oid
);
12377 ldout(cct
, 10) << "distributing notification oid=" << notify_oid
<< " bl.length()=" << bl
.length() << dendl
;
12378 return control_pool_ctx
.notify2(notify_oid
, bl
, 0, NULL
);
12381 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, RGWPoolIterCtx
& ctx
)
12383 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12384 librados::NObjectIterator
& iter
= ctx
.iter
;
12386 int r
= open_pool_ctx(pool
, io_ctx
);
12390 iter
= io_ctx
.nobjects_begin();
12395 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, const string
& cursor
, RGWPoolIterCtx
& ctx
)
12397 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12398 librados::NObjectIterator
& iter
= ctx
.iter
;
12400 int r
= open_pool_ctx(pool
, io_ctx
);
12404 librados::ObjectCursor oc
;
12405 if (!oc
.from_str(cursor
)) {
12406 ldout(cct
, 10) << "failed to parse cursor: " << cursor
<< dendl
;
12410 iter
= io_ctx
.nobjects_begin(oc
);
12415 string
RGWRados::pool_iterate_get_cursor(RGWPoolIterCtx
& ctx
)
12417 return ctx
.iter
.get_cursor().to_str();
12420 int RGWRados::pool_iterate(RGWPoolIterCtx
& ctx
, uint32_t num
, vector
<rgw_bucket_dir_entry
>& objs
,
12421 bool *is_truncated
, RGWAccessListFilter
*filter
)
12423 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12424 librados::NObjectIterator
& iter
= ctx
.iter
;
12426 if (iter
== io_ctx
.nobjects_end())
12431 for (i
= 0; i
< num
&& iter
!= io_ctx
.nobjects_end(); ++i
, ++iter
) {
12432 rgw_bucket_dir_entry e
;
12434 string oid
= iter
->get_oid();
12435 ldout(cct
, 20) << "RGWRados::pool_iterate: got " << oid
<< dendl
;
12437 // fill it in with initial values; we may correct later
12438 if (filter
&& !filter
->filter(oid
, oid
))
12446 *is_truncated
= (iter
!= io_ctx
.nobjects_end());
12448 return objs
.size();
12450 struct RGWAccessListFilterPrefix
: public RGWAccessListFilter
{
12453 explicit RGWAccessListFilterPrefix(const string
& _prefix
) : prefix(_prefix
) {}
12454 bool filter(string
& name
, string
& key
) override
{
12455 return (prefix
.compare(key
.substr(0, prefix
.size())) == 0);
12459 int RGWRados::list_raw_objects_init(const rgw_pool
& pool
, const string
& marker
, RGWListRawObjsCtx
*ctx
)
12461 if (!ctx
->initialized
) {
12462 int r
= pool_iterate_begin(pool
, marker
, ctx
->iter_ctx
);
12464 ldout(cct
, 10) << "failed to list objects pool_iterate_begin() returned r=" << r
<< dendl
;
12467 ctx
->initialized
= true;
12472 int RGWRados::list_raw_objects_next(const string
& prefix_filter
, int max
,
12473 RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12474 bool *is_truncated
)
12476 if (!ctx
.initialized
) {
12479 RGWAccessListFilterPrefix
filter(prefix_filter
);
12480 vector
<rgw_bucket_dir_entry
> objs
;
12481 int r
= pool_iterate(ctx
.iter_ctx
, max
, objs
, is_truncated
, &filter
);
12484 ldout(cct
, 10) << "failed to list objects pool_iterate returned r=" << r
<< dendl
;
12488 vector
<rgw_bucket_dir_entry
>::iterator iter
;
12489 for (iter
= objs
.begin(); iter
!= objs
.end(); ++iter
) {
12490 oids
.push_back(iter
->key
.name
);
12493 return oids
.size();
12496 int RGWRados::list_raw_objects(const rgw_pool
& pool
, const string
& prefix_filter
,
12497 int max
, RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12498 bool *is_truncated
)
12500 if (!ctx
.initialized
) {
12501 int r
= list_raw_objects_init(pool
, string(), &ctx
);
12507 return list_raw_objects_next(prefix_filter
, max
, ctx
, oids
, is_truncated
);
12510 string
RGWRados::list_raw_objs_get_cursor(RGWListRawObjsCtx
& ctx
)
12512 return pool_iterate_get_cursor(ctx
.iter_ctx
);
12515 int RGWRados::list_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& marker
, uint32_t max
,
12516 std::list
<rgw_bi_log_entry
>& result
, bool *truncated
)
12518 ldout(cct
, 20) << __func__
<< ": " << bucket_info
.bucket
<< " marker " << marker
<< " shard_id=" << shard_id
<< " max " << max
<< dendl
;
12521 librados::IoCtx index_ctx
;
12522 map
<int, string
> oids
;
12523 map
<int, cls_rgw_bi_log_list_ret
> bi_log_lists
;
12524 map
<int, string
> bucket_instance_ids
;
12525 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, &bucket_instance_ids
);
12529 BucketIndexShardsManager marker_mgr
;
12530 bool has_shards
= (oids
.size() > 1 || shard_id
>= 0);
12531 // If there are multiple shards for the bucket index object, the marker
12532 // should have the pattern '{shard_id_1}#{shard_marker_1},{shard_id_2}#
12533 // {shard_marker_2}...', if there is no sharding, the bi_log_list should
12534 // only contain one record, and the key is the bucket instance id.
12535 r
= marker_mgr
.from_string(marker
, shard_id
);
12539 r
= CLSRGWIssueBILogList(index_ctx
, marker_mgr
, max
, oids
, bi_log_lists
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12543 map
<int, list
<rgw_bi_log_entry
>::iterator
> vcurrents
;
12544 map
<int, list
<rgw_bi_log_entry
>::iterator
> vends
;
12546 *truncated
= false;
12548 map
<int, cls_rgw_bi_log_list_ret
>::iterator miter
= bi_log_lists
.begin();
12549 for (; miter
!= bi_log_lists
.end(); ++miter
) {
12550 int shard_id
= miter
->first
;
12551 vcurrents
[shard_id
] = miter
->second
.entries
.begin();
12552 vends
[shard_id
] = miter
->second
.entries
.end();
12554 *truncated
= (*truncated
|| miter
->second
.truncated
);
12559 bool has_more
= true;
12560 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator viter
;
12561 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator eiter
;
12562 while (total
< max
&& has_more
) {
12565 viter
= vcurrents
.begin();
12566 eiter
= vends
.begin();
12568 for (; total
< max
&& viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12569 assert (eiter
!= vends
.end());
12571 int shard_id
= viter
->first
;
12572 list
<rgw_bi_log_entry
>::iterator
& liter
= viter
->second
;
12574 if (liter
== eiter
->second
){
12577 rgw_bi_log_entry
& entry
= *(liter
);
12580 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
12582 build_bucket_index_marker(buf
, entry
.id
, &tmp_id
);
12583 entry
.id
.swap(tmp_id
);
12585 marker_mgr
.add(shard_id
, entry
.id
);
12586 result
.push_back(entry
);
12594 for (viter
= vcurrents
.begin(), eiter
= vends
.begin(); viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12595 assert (eiter
!= vends
.end());
12596 *truncated
= (*truncated
|| (viter
->second
!= eiter
->second
));
12600 // Refresh marker, if there are multiple shards, the output will look like
12601 // '{shard_oid_1}#{shard_marker_1},{shard_oid_2}#{shard_marker_2}...',
12602 // if there is no sharding, the simply marker (without oid) is returned
12604 marker_mgr
.to_string(&marker
);
12606 if (!result
.empty()) {
12607 marker
= result
.rbegin()->id
;
12614 int RGWRados::trim_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& start_marker
, string
& end_marker
)
12616 librados::IoCtx index_ctx
;
12617 map
<int, string
> bucket_objs
;
12619 BucketIndexShardsManager start_marker_mgr
;
12620 BucketIndexShardsManager end_marker_mgr
;
12622 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12627 r
= start_marker_mgr
.from_string(start_marker
, shard_id
);
12632 r
= end_marker_mgr
.from_string(end_marker
, shard_id
);
12637 return CLSRGWIssueBILogTrim(index_ctx
, start_marker_mgr
, end_marker_mgr
, bucket_objs
,
12638 cct
->_conf
->rgw_bucket_index_max_aio
)();
12643 int RGWRados::resync_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12645 librados::IoCtx index_ctx
;
12646 map
<int, string
> bucket_objs
;
12647 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12651 return CLSRGWIssueResyncBucketBILog(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12654 int RGWRados::stop_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12656 librados::IoCtx index_ctx
;
12657 map
<int, string
> bucket_objs
;
12658 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12662 return CLSRGWIssueBucketBILogStop(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12665 int RGWRados::bi_get_instance(const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, rgw_bucket_dir_entry
*dirent
)
12668 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
12673 rgw_cls_bi_entry bi_entry
;
12674 r
= bi_get(obj
.bucket
, obj
, InstanceIdx
, &bi_entry
);
12675 if (r
< 0 && r
!= -ENOENT
) {
12676 ldout(cct
, 0) << "ERROR: bi_get() returned r=" << r
<< dendl
;
12681 bufferlist::iterator iter
= bi_entry
.data
.begin();
12683 ::decode(*dirent
, iter
);
12684 } catch (buffer::error
& err
) {
12685 ldout(cct
, 0) << "ERROR: failed to decode bi_entry()" << dendl
;
12692 int RGWRados::bi_get(rgw_bucket
& bucket
, rgw_obj
& obj
, BIIndexType index_type
, rgw_cls_bi_entry
*entry
)
12694 BucketShard
bs(this);
12695 int ret
= bs
.init(bucket
, obj
);
12697 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12701 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12703 ret
= cls_rgw_bi_get(bs
.index_ctx
, bs
.bucket_obj
, index_type
, key
, entry
);
12710 void RGWRados::bi_put(ObjectWriteOperation
& op
, BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12712 cls_rgw_bi_put(op
, bs
.bucket_obj
, entry
);
12715 int RGWRados::bi_put(BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12717 int ret
= cls_rgw_bi_put(bs
.index_ctx
, bs
.bucket_obj
, entry
);
12724 int RGWRados::bi_put(rgw_bucket
& bucket
, rgw_obj
& obj
, rgw_cls_bi_entry
& entry
)
12726 BucketShard
bs(this);
12727 int ret
= bs
.init(bucket
, obj
);
12729 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12733 return bi_put(bs
, entry
);
12736 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
)
12738 rgw_obj
obj(bucket
, obj_name
);
12739 BucketShard
bs(this);
12740 int ret
= bs
.init(bucket
, obj
);
12742 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12746 ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, obj_name
, marker
, max
, entries
, is_truncated
);
12747 if (ret
== -ENOENT
) {
12748 *is_truncated
= false;
12756 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
)
12758 int ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, filter_obj
, marker
, max
, entries
, is_truncated
);
12765 int RGWRados::bi_remove(BucketShard
& bs
)
12767 int ret
= bs
.index_ctx
.remove(bs
.bucket_obj
);
12768 if (ret
== -ENOENT
) {
12772 ldout(cct
, 5) << "bs.index_ctx.remove(" << bs
.bucket_obj
<< ") returned ret=" << ret
<< dendl
;
12779 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
)
12781 BucketShard
bs(this);
12782 int ret
= bs
.init(bucket
, shard_id
);
12784 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12788 return bi_list(bs
, filter_obj
, marker
, max
, entries
, is_truncated
);
12791 int RGWRados::gc_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12793 return gc_pool_ctx
.operate(oid
, op
);
12796 int RGWRados::gc_aio_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12798 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
12799 int r
= gc_pool_ctx
.aio_operate(oid
, c
, op
);
12804 int RGWRados::gc_operate(string
& oid
, librados::ObjectReadOperation
*op
, bufferlist
*pbl
)
12806 return gc_pool_ctx
.operate(oid
, op
, pbl
);
12809 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
)
12811 return gc
->list(index
, marker
, max
, expired_only
, result
, truncated
);
12814 int RGWRados::process_gc()
12816 return gc
->process();
12819 int RGWRados::list_lc_progress(const string
& marker
, uint32_t max_entries
, map
<string
, int> *progress_map
)
12821 return lc
->list_lc_progress(marker
, max_entries
, progress_map
);
12824 int RGWRados::process_lc()
12826 return lc
->process();
12829 int RGWRados::process_expire_objects()
12831 obj_expirer
->inspect_all_shards(utime_t(), ceph_clock_now());
12835 int RGWRados::cls_rgw_init_index(librados::IoCtx
& index_ctx
, librados::ObjectWriteOperation
& op
, string
& oid
)
12838 cls_rgw_bucket_init(op
);
12839 return index_ctx
.operate(oid
, &op
);
12842 int RGWRados::cls_obj_prepare_op(BucketShard
& bs
, RGWModifyOp op
, string
& tag
,
12843 rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12845 rgw_zone_set zones_trace
;
12846 if (_zones_trace
) {
12847 zones_trace
= *_zones_trace
;
12850 zones_trace
.insert(get_zone().id
);
12853 ObjectWriteOperation o
;
12854 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12855 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12856 cls_rgw_bucket_prepare_op(o
, op
, tag
, key
, obj
.key
.get_loc(), get_zone().log_data
, bilog_flags
, zones_trace
);
12857 return bs
.index_ctx
.operate(bs
.bucket_obj
, &o
);
12860 int RGWRados::cls_obj_complete_op(BucketShard
& bs
, const rgw_obj
& obj
, RGWModifyOp op
, string
& tag
,
12861 int64_t pool
, uint64_t epoch
,
12862 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12863 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12865 ObjectWriteOperation o
;
12866 rgw_bucket_dir_entry_meta dir_meta
;
12867 dir_meta
= ent
.meta
;
12868 dir_meta
.category
= category
;
12870 rgw_bucket_entry_ver ver
;
12873 cls_rgw_obj_key
key(ent
.key
.name
, ent
.key
.instance
);
12874 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12875 cls_rgw_bucket_complete_op(o
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12876 get_zone().log_data
, bilog_flags
, _zones_trace
);
12877 complete_op_data
*arg
;
12878 index_completion_manager
->create_completion(obj
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12879 get_zone().log_data
, bilog_flags
, _zones_trace
, &arg
);
12880 librados::AioCompletion
*completion
= arg
->rados_completion
;
12881 int ret
= bs
.index_ctx
.aio_operate(bs
.bucket_obj
, arg
->rados_completion
, &o
);
12882 completion
->release(); /* can't reference arg here, as it might have already been released */
12886 int RGWRados::cls_obj_complete_add(BucketShard
& bs
, const rgw_obj
& obj
, string
& tag
,
12887 int64_t pool
, uint64_t epoch
,
12888 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12889 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12891 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_ADD
, tag
, pool
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
12894 int RGWRados::cls_obj_complete_del(BucketShard
& bs
, string
& tag
,
12895 int64_t pool
, uint64_t epoch
,
12897 real_time
& removed_mtime
,
12898 list
<rgw_obj_index_key
> *remove_objs
,
12899 uint16_t bilog_flags
,
12900 rgw_zone_set
*zones_trace
)
12902 rgw_bucket_dir_entry ent
;
12903 ent
.meta
.mtime
= removed_mtime
;
12904 obj
.key
.get_index_key(&ent
.key
);
12905 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
);
12908 int RGWRados::cls_obj_complete_cancel(BucketShard
& bs
, string
& tag
, rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12910 rgw_bucket_dir_entry ent
;
12911 obj
.key
.get_index_key(&ent
.key
);
12912 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
);
12915 int RGWRados::cls_obj_set_bucket_tag_timeout(RGWBucketInfo
& bucket_info
, uint64_t timeout
)
12917 librados::IoCtx index_ctx
;
12918 map
<int, string
> bucket_objs
;
12919 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
12923 return CLSRGWIssueSetTagTimeout(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
, timeout
)();
12926 int RGWRados::cls_bucket_list(RGWBucketInfo
& bucket_info
, int shard_id
, rgw_obj_index_key
& start
, const string
& prefix
,
12927 uint32_t num_entries
, bool list_versions
, map
<string
, rgw_bucket_dir_entry
>& m
,
12928 bool *is_truncated
, rgw_obj_index_key
*last_entry
,
12929 bool (*force_check_filter
)(const string
& name
))
12931 ldout(cct
, 10) << "cls_bucket_list " << bucket_info
.bucket
<< " start " << start
.name
<< "[" << start
.instance
<< "] num_entries " << num_entries
<< dendl
;
12933 librados::IoCtx index_ctx
;
12934 // key - oid (for different shards if there is any)
12935 // value - list result for the corresponding oid (shard), it is filled by the AIO callback
12936 map
<int, string
> oids
;
12937 map
<int, struct rgw_cls_list_ret
> list_results
;
12938 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
);
12942 cls_rgw_obj_key
start_key(start
.name
, start
.instance
);
12943 r
= CLSRGWIssueBucketList(index_ctx
, start_key
, prefix
, num_entries
, list_versions
,
12944 oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12948 // Create a list of iterators that are used to iterate each shard
12949 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vcurrents(list_results
.size());
12950 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vends(list_results
.size());
12951 vector
<string
> vnames(list_results
.size());
12952 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
12953 *is_truncated
= false;
12954 for (; iter
!= list_results
.end(); ++iter
) {
12955 vcurrents
.push_back(iter
->second
.dir
.m
.begin());
12956 vends
.push_back(iter
->second
.dir
.m
.end());
12957 vnames
.push_back(oids
[iter
->first
]);
12958 *is_truncated
= (*is_truncated
|| iter
->second
.is_truncated
);
12961 // Create a map to track the next candidate entry from each shard, if the entry
12962 // from a specified shard is selected/erased, the next entry from that shard will
12963 // be inserted for next round selection
12964 map
<string
, size_t> candidates
;
12965 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
12966 if (vcurrents
[i
] != vends
[i
]) {
12967 candidates
[vcurrents
[i
]->first
] = i
;
12971 map
<string
, bufferlist
> updates
;
12972 uint32_t count
= 0;
12973 while (count
< num_entries
&& !candidates
.empty()) {
12975 // Select the next one
12976 int pos
= candidates
.begin()->second
;
12977 const string
& name
= vcurrents
[pos
]->first
;
12978 struct rgw_bucket_dir_entry
& dirent
= vcurrents
[pos
]->second
;
12980 bool force_check
= force_check_filter
&&
12981 force_check_filter(dirent
.key
.name
);
12982 if ((!dirent
.exists
&& !dirent
.is_delete_marker()) ||
12983 !dirent
.pending_map
.empty() ||
12985 /* there are uncommitted ops. We need to check the current state,
12986 * and if the tags are old we need to do cleanup as well. */
12987 librados::IoCtx sub_ctx
;
12988 sub_ctx
.dup(index_ctx
);
12989 r
= check_disk_state(sub_ctx
, bucket_info
, dirent
, dirent
, updates
[vnames
[pos
]]);
12990 if (r
< 0 && r
!= -ENOENT
) {
12995 ldout(cct
, 10) << "RGWRados::cls_bucket_list: got " << dirent
.key
.name
<< "[" << dirent
.key
.instance
<< "]" << dendl
;
12996 m
[name
] = std::move(dirent
);
13000 // Refresh the candidates map
13001 candidates
.erase(candidates
.begin());
13003 if (vcurrents
[pos
] != vends
[pos
]) {
13004 candidates
[vcurrents
[pos
]->first
] = pos
;
13008 // Suggest updates if there is any
13009 map
<string
, bufferlist
>::iterator miter
= updates
.begin();
13010 for (; miter
!= updates
.end(); ++miter
) {
13011 if (miter
->second
.length()) {
13012 ObjectWriteOperation o
;
13013 cls_rgw_suggest_changes(o
, miter
->second
);
13014 // we don't care if we lose suggested updates, send them off blindly
13015 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
13016 index_ctx
.aio_operate(miter
->first
, c
, &o
);
13021 // Check if all the returned entries are consumed or not
13022 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
13023 if (vcurrents
[i
] != vends
[i
])
13024 *is_truncated
= true;
13027 *last_entry
= m
.rbegin()->first
;
13032 int RGWRados::cls_obj_usage_log_add(const string
& oid
, rgw_usage_log_info
& info
)
13034 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13037 int r
= get_raw_obj_ref(obj
, &ref
);
13042 ObjectWriteOperation op
;
13043 cls_rgw_usage_log_add(op
, info
);
13045 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13049 int RGWRados::cls_obj_usage_log_read(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
, uint32_t max_entries
,
13050 string
& read_iter
, map
<rgw_user_bucket
, rgw_usage_log_entry
>& usage
, bool *is_truncated
)
13052 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13055 int r
= get_raw_obj_ref(obj
, &ref
);
13060 *is_truncated
= false;
13062 r
= cls_rgw_usage_log_read(ref
.ioctx
, ref
.oid
, user
, start_epoch
, end_epoch
,
13063 max_entries
, read_iter
, usage
, is_truncated
);
13068 int RGWRados::cls_obj_usage_log_trim(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
)
13070 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
13073 int r
= get_raw_obj_ref(obj
, &ref
);
13078 r
= cls_rgw_usage_log_trim(ref
.ioctx
, ref
.oid
, user
, start_epoch
, end_epoch
);
13082 int RGWRados::remove_objs_from_index(RGWBucketInfo
& bucket_info
, list
<rgw_obj_index_key
>& oid_list
)
13084 librados::IoCtx index_ctx
;
13087 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
13089 int r
= open_bucket_index(bucket_info
, index_ctx
, dir_oid
);
13093 bufferlist updates
;
13095 for (auto iter
= oid_list
.begin(); iter
!= oid_list
.end(); ++iter
) {
13096 rgw_bucket_dir_entry entry
;
13098 dout(2) << "RGWRados::remove_objs_from_index bucket=" << bucket_info
.bucket
<< " obj=" << entry
.key
.name
<< ":" << entry
.key
.instance
<< dendl
;
13099 entry
.ver
.epoch
= (uint64_t)-1; // ULLONG_MAX, needed to that objclass doesn't skip out request
13100 updates
.append(CEPH_RGW_REMOVE
| suggest_flag
);
13101 ::encode(entry
, updates
);
13106 r
= index_ctx
.exec(dir_oid
, RGW_CLASS
, RGW_DIR_SUGGEST_CHANGES
, updates
, out
);
13111 int RGWRados::check_disk_state(librados::IoCtx io_ctx
,
13112 const RGWBucketInfo
& bucket_info
,
13113 rgw_bucket_dir_entry
& list_state
,
13114 rgw_bucket_dir_entry
& object
,
13115 bufferlist
& suggested_updates
)
13117 const rgw_bucket
& bucket
= bucket_info
.bucket
;
13118 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
13122 rgw_obj
obj(bucket
, list_state
.key
);
13125 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
13127 if (loc
!= list_state
.locator
) {
13128 ldout(cct
, 0) << "WARNING: generated locator (" << loc
<< ") is different from listed locator (" << list_state
.locator
<< ")" << dendl
;
13131 io_ctx
.locator_set_key(list_state
.locator
);
13133 RGWObjState
*astate
= NULL
;
13134 RGWObjectCtx
rctx(this);
13135 int r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
13139 list_state
.pending_map
.clear(); // we don't need this and it inflates size
13140 if (!astate
->exists
) {
13141 /* object doesn't exist right now -- hopefully because it's
13142 * marked as !exists and got deleted */
13143 if (list_state
.exists
) {
13144 /* FIXME: what should happen now? Work out if there are any
13145 * non-bad ways this could happen (there probably are, but annoying
13148 // encode a suggested removal of that key
13149 list_state
.ver
.epoch
= io_ctx
.get_last_version();
13150 list_state
.ver
.pool
= io_ctx
.get_id();
13151 cls_rgw_encode_suggestion(CEPH_RGW_REMOVE
, list_state
, suggested_updates
);
13156 string content_type
;
13159 object
.meta
.size
= astate
->size
;
13160 object
.meta
.accounted_size
= astate
->accounted_size
;
13161 object
.meta
.mtime
= astate
->mtime
;
13163 map
<string
, bufferlist
>::iterator iter
= astate
->attrset
.find(RGW_ATTR_ETAG
);
13164 if (iter
!= astate
->attrset
.end()) {
13165 etag
= iter
->second
.c_str();
13167 iter
= astate
->attrset
.find(RGW_ATTR_CONTENT_TYPE
);
13168 if (iter
!= astate
->attrset
.end()) {
13169 content_type
= iter
->second
.c_str();
13171 iter
= astate
->attrset
.find(RGW_ATTR_ACL
);
13172 if (iter
!= astate
->attrset
.end()) {
13173 r
= decode_policy(iter
->second
, &owner
);
13175 dout(0) << "WARNING: could not decode policy for object: " << obj
<< dendl
;
13179 if (astate
->has_manifest
) {
13180 RGWObjManifest::obj_iterator miter
;
13181 RGWObjManifest
& manifest
= astate
->manifest
;
13182 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
13183 const rgw_raw_obj
& raw_loc
= miter
.get_location().get_raw_obj(this);
13185 rgw_raw_obj_to_obj(manifest
.get_obj().bucket
, raw_loc
, &loc
);
13187 if (loc
.key
.ns
== RGW_OBJ_NS_MULTIPART
) {
13188 dout(10) << "check_disk_state(): removing manifest part from index: " << loc
<< dendl
;
13189 r
= delete_obj_index(loc
);
13191 dout(0) << "WARNING: delete_obj_index() returned r=" << r
<< dendl
;
13197 object
.meta
.etag
= etag
;
13198 object
.meta
.content_type
= content_type
;
13199 object
.meta
.owner
= owner
.get_id().to_str();
13200 object
.meta
.owner_display_name
= owner
.get_display_name();
13202 // encode suggested updates
13203 list_state
.ver
.pool
= io_ctx
.get_id();
13204 list_state
.ver
.epoch
= astate
->epoch
;
13205 list_state
.meta
.size
= object
.meta
.size
;
13206 list_state
.meta
.accounted_size
= object
.meta
.accounted_size
;
13207 list_state
.meta
.mtime
= object
.meta
.mtime
;
13208 list_state
.meta
.category
= main_category
;
13209 list_state
.meta
.etag
= etag
;
13210 list_state
.meta
.content_type
= content_type
;
13211 if (astate
->obj_tag
.length() > 0)
13212 list_state
.tag
= astate
->obj_tag
.c_str();
13213 list_state
.meta
.owner
= owner
.get_id().to_str();
13214 list_state
.meta
.owner_display_name
= owner
.get_display_name();
13216 list_state
.exists
= true;
13217 cls_rgw_encode_suggestion(CEPH_RGW_UPDATE
| suggest_flag
, list_state
, suggested_updates
);
13221 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
)
13223 librados::IoCtx index_ctx
;
13224 map
<int, string
> oids
;
13225 map
<int, struct rgw_cls_list_ret
> list_results
;
13226 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, list_results
, shard_id
, bucket_instance_ids
);
13230 r
= CLSRGWIssueGetDirHeader(index_ctx
, oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
13234 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
13235 for(; iter
!= list_results
.end(); ++iter
) {
13236 headers
[oids
[iter
->first
]] = iter
->second
.dir
.header
;
13241 int RGWRados::cls_bucket_head_async(const RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetDirHeader_CB
*ctx
, int *num_aio
)
13243 librados::IoCtx index_ctx
;
13244 map
<int, string
> bucket_objs
;
13245 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
13249 map
<int, string
>::iterator iter
= bucket_objs
.begin();
13250 for (; iter
!= bucket_objs
.end(); ++iter
) {
13251 r
= cls_rgw_get_dir_header_async(index_ctx
, iter
->second
, static_cast<RGWGetDirHeader_CB
*>(ctx
->get()));
13262 int RGWRados::cls_user_get_header(const string
& user_id
, cls_user_header
*header
)
13264 string buckets_obj_id
;
13265 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13266 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13269 int r
= get_raw_obj_ref(obj
, &ref
);
13274 librados::ObjectReadOperation op
;
13276 ::cls_user_get_header(op
, header
, &rc
);
13278 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13287 int RGWRados::cls_user_reset_stats(const string
& user_id
)
13289 string buckets_obj_id
;
13290 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13291 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13294 int r
= get_raw_obj_ref(obj
, &ref
);
13299 librados::ObjectWriteOperation op
;
13300 ::cls_user_reset_stats(op
);
13301 return ref
.ioctx
.operate(ref
.oid
, &op
);
13304 int RGWRados::cls_user_get_header_async(const string
& user_id
, RGWGetUserHeader_CB
*ctx
)
13306 string buckets_obj_id
;
13307 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13308 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13311 int r
= get_raw_obj_ref(obj
, &ref
);
13316 r
= ::cls_user_get_header_async(ref
.ioctx
, ref
.oid
, ctx
);
13323 int RGWRados::cls_user_sync_bucket_stats(rgw_raw_obj
& user_obj
, const RGWBucketInfo
& bucket_info
)
13325 map
<string
, struct rgw_bucket_dir_header
> headers
;
13326 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13328 ldout(cct
, 20) << "cls_bucket_header() returned " << r
<< dendl
;
13332 cls_user_bucket_entry entry
;
13334 bucket_info
.bucket
.convert(&entry
.bucket
);
13336 for (const auto& hiter
: headers
) {
13337 for (const auto& iter
: hiter
.second
.stats
) {
13338 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13339 entry
.size
+= header_stats
.total_size
;
13340 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13341 entry
.count
+= header_stats
.num_entries
;
13345 list
<cls_user_bucket_entry
> entries
;
13346 entries
.push_back(entry
);
13348 r
= cls_user_update_buckets(user_obj
, entries
, false);
13350 ldout(cct
, 20) << "cls_user_update_buckets() returned " << r
<< dendl
;
13357 int RGWRados::cls_user_get_bucket_stats(const rgw_bucket
& bucket
, cls_user_bucket_entry
& entry
)
13359 map
<string
, struct rgw_bucket_dir_header
> headers
;
13360 RGWBucketInfo bucket_info
;
13361 RGWObjectCtx
obj_ctx(this);
13362 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
13367 ret
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13369 ldout(cct
, 20) << "cls_bucket_header() returned " << ret
<< dendl
;
13373 bucket
.convert(&entry
.bucket
);
13375 for (const auto& hiter
: headers
) {
13376 for (const auto& iter
: hiter
.second
.stats
) {
13377 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13378 entry
.size
+= header_stats
.total_size
;
13379 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13380 entry
.count
+= header_stats
.num_entries
;
13387 int RGWRados::cls_user_list_buckets(rgw_raw_obj
& obj
,
13388 const string
& in_marker
,
13389 const string
& end_marker
,
13390 const int max_entries
,
13391 list
<cls_user_bucket_entry
>& entries
,
13392 string
* const out_marker
,
13393 bool * const truncated
)
13396 int r
= get_raw_obj_ref(obj
, &ref
);
13401 librados::ObjectReadOperation op
;
13404 cls_user_bucket_list(op
, in_marker
, end_marker
, max_entries
, entries
, out_marker
, truncated
, &rc
);
13406 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13415 int RGWRados::cls_user_update_buckets(rgw_raw_obj
& obj
, list
<cls_user_bucket_entry
>& entries
, bool add
)
13418 int r
= get_raw_obj_ref(obj
, &ref
);
13423 librados::ObjectWriteOperation op
;
13424 cls_user_set_buckets(op
, entries
, add
);
13425 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13432 int RGWRados::complete_sync_user_stats(const rgw_user
& user_id
)
13434 string buckets_obj_id
;
13435 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13436 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13437 return cls_user_complete_stats_sync(obj
);
13440 int RGWRados::cls_user_complete_stats_sync(rgw_raw_obj
& obj
)
13443 int r
= get_raw_obj_ref(obj
, &ref
);
13448 librados::ObjectWriteOperation op
;
13449 ::cls_user_complete_stats_sync(op
);
13450 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13457 int RGWRados::cls_user_add_bucket(rgw_raw_obj
& obj
, const cls_user_bucket_entry
& entry
)
13459 list
<cls_user_bucket_entry
> l
;
13460 l
.push_back(entry
);
13462 return cls_user_update_buckets(obj
, l
, true);
13465 int RGWRados::cls_user_remove_bucket(rgw_raw_obj
& obj
, const cls_user_bucket
& bucket
)
13468 int r
= get_system_obj_ref(obj
, &ref
);
13473 librados::ObjectWriteOperation op
;
13474 ::cls_user_remove_bucket(op
, bucket
);
13475 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13482 int RGWRados::check_bucket_shards(const RGWBucketInfo
& bucket_info
, const rgw_bucket
& bucket
,
13483 RGWQuotaInfo
& bucket_quota
)
13485 if (!cct
->_conf
->rgw_dynamic_resharding
) {
13489 bool need_resharding
= false;
13490 int num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13491 uint32_t suggested_num_shards
;
13493 int ret
= quota_handler
->check_bucket_shards((uint64_t)cct
->_conf
->rgw_max_objs_per_shard
,
13494 num_source_shards
, bucket_info
.owner
, bucket
, bucket_quota
,
13495 1, need_resharding
, &suggested_num_shards
);
13500 if (need_resharding
) {
13501 ldout(cct
, 20) << __func__
<< " bucket " << bucket
.name
<< " need resharding " <<
13502 " old num shards " << bucket_info
.num_shards
<< " new num shards " << suggested_num_shards
<<
13504 return add_bucket_to_reshard(bucket_info
, suggested_num_shards
);
13510 int RGWRados::add_bucket_to_reshard(const RGWBucketInfo
& bucket_info
, uint32_t new_num_shards
)
13512 RGWReshard
reshard(this);
13514 uint32_t num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13516 new_num_shards
= min(new_num_shards
, get_max_bucket_shards());
13517 if (new_num_shards
<= num_source_shards
) {
13518 ldout(cct
, 20) << "not resharding bucket name=" << bucket_info
.bucket
.name
<< ", orig_num=" << num_source_shards
<< ", new_num_shards=" << new_num_shards
<< dendl
;
13522 cls_rgw_reshard_entry entry
;
13523 entry
.time
= real_clock::now();
13524 entry
.tenant
= bucket_info
.owner
.tenant
;
13525 entry
.bucket_name
= bucket_info
.bucket
.name
;
13526 entry
.bucket_id
= bucket_info
.bucket
.bucket_id
;
13527 entry
.old_num_shards
= num_source_shards
;
13528 entry
.new_num_shards
= new_num_shards
;
13530 return reshard
.add(entry
);
13533 int RGWRados::check_quota(const rgw_user
& bucket_owner
, rgw_bucket
& bucket
,
13534 RGWQuotaInfo
& user_quota
, RGWQuotaInfo
& bucket_quota
, uint64_t obj_size
)
13536 return quota_handler
->check_quota(bucket_owner
, bucket
, user_quota
, bucket_quota
, 1, obj_size
);
13539 void RGWRados::get_bucket_index_objects(const string
& bucket_oid_base
,
13540 uint32_t num_shards
, map
<int, string
>& bucket_objects
, int shard_id
)
13543 bucket_objects
[0] = bucket_oid_base
;
13545 char buf
[bucket_oid_base
.size() + 32];
13546 if (shard_id
< 0) {
13547 for (uint32_t i
= 0; i
< num_shards
; ++i
) {
13548 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), i
);
13549 bucket_objects
[i
] = buf
;
13552 if ((uint32_t)shard_id
> num_shards
) {
13555 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13556 bucket_objects
[shard_id
] = buf
;
13561 void RGWRados::get_bucket_instance_ids(const RGWBucketInfo
& bucket_info
, int shard_id
, map
<int, string
> *result
)
13563 const rgw_bucket
& bucket
= bucket_info
.bucket
;
13564 string plain_id
= bucket
.name
+ ":" + bucket
.bucket_id
;
13565 if (!bucket_info
.num_shards
) {
13566 (*result
)[0] = plain_id
;
13569 if (shard_id
< 0) {
13570 for (uint32_t i
= 0; i
< bucket_info
.num_shards
; ++i
) {
13571 snprintf(buf
, sizeof(buf
), ":%d", i
);
13572 (*result
)[i
] = plain_id
+ buf
;
13575 if ((uint32_t)shard_id
> bucket_info
.num_shards
) {
13578 snprintf(buf
, sizeof(buf
), ":%d", shard_id
);
13579 (*result
)[shard_id
] = plain_id
+ buf
;
13584 int RGWRados::get_target_shard_id(const RGWBucketInfo
& bucket_info
, const string
& obj_key
,
13588 switch (bucket_info
.bucket_index_shard_hash_type
) {
13589 case RGWBucketInfo::MOD
:
13590 if (!bucket_info
.num_shards
) {
13595 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13596 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13597 sid
= rgw_shards_mod(sid2
, bucket_info
.num_shards
);
13599 *shard_id
= (int)sid
;
13609 void RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, uint32_t num_shards
,
13610 int shard_id
, string
*bucket_obj
)
13613 // By default with no sharding, we use the bucket oid as itself
13614 (*bucket_obj
) = bucket_oid_base
;
13616 char buf
[bucket_oid_base
.size() + 32];
13617 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13618 (*bucket_obj
) = buf
;
13622 int RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, const string
& obj_key
,
13623 uint32_t num_shards
, RGWBucketInfo::BIShardsHashType hash_type
, string
*bucket_obj
, int *shard_id
)
13626 switch (hash_type
) {
13627 case RGWBucketInfo::MOD
:
13629 // By default with no sharding, we use the bucket oid as itself
13630 (*bucket_obj
) = bucket_oid_base
;
13635 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13636 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13637 sid
= rgw_shards_mod(sid2
, num_shards
);
13638 char buf
[bucket_oid_base
.size() + 32];
13639 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), sid
);
13640 (*bucket_obj
) = buf
;
13642 *shard_id
= (int)sid
;
13652 void RGWStateLog::oid_str(int shard
, string
& oid
) {
13653 oid
= RGW_STATELOG_OBJ_PREFIX
+ module_name
+ ".";
13655 snprintf(buf
, sizeof(buf
), "%d", shard
);
13659 int RGWStateLog::get_shard_num(const string
& object
) {
13660 uint32_t val
= ceph_str_hash_linux(object
.c_str(), object
.length());
13661 return val
% num_shards
;
13664 string
RGWStateLog::get_oid(const string
& object
) {
13665 int shard
= get_shard_num(object
);
13667 oid_str(shard
, oid
);
13671 int RGWStateLog::open_ioctx(librados::IoCtx
& ioctx
) {
13673 store
->get_log_pool(pool
);
13674 int r
= rgw_init_ioctx(store
->get_rados_handle(), pool
, ioctx
);
13676 lderr(store
->ctx()) << "ERROR: could not open rados pool" << dendl
;
13682 int RGWStateLog::store_entry(const string
& client_id
, const string
& op_id
, const string
& object
,
13683 uint32_t state
, bufferlist
*bl
, uint32_t *check_state
)
13685 if (client_id
.empty() ||
13688 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13691 librados::IoCtx ioctx
;
13692 int r
= open_ioctx(ioctx
);
13696 string oid
= get_oid(object
);
13698 librados::ObjectWriteOperation op
;
13700 cls_statelog_check_state(op
, client_id
, op_id
, object
, *check_state
);
13702 utime_t ts
= ceph_clock_now();
13704 cls_statelog_add(op
, client_id
, op_id
, object
, ts
, state
, (bl
? *bl
: nobl
));
13705 r
= ioctx
.operate(oid
, &op
);
13713 int RGWStateLog::remove_entry(const string
& client_id
, const string
& op_id
, const string
& object
)
13715 if (client_id
.empty() ||
13718 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13721 librados::IoCtx ioctx
;
13722 int r
= open_ioctx(ioctx
);
13726 string oid
= get_oid(object
);
13728 librados::ObjectWriteOperation op
;
13729 cls_statelog_remove_by_object(op
, object
, op_id
);
13730 r
= ioctx
.operate(oid
, &op
);
13738 void RGWStateLog::init_list_entries(const string
& client_id
, const string
& op_id
, const string
& object
,
13741 list_state
*state
= new list_state
;
13742 state
->client_id
= client_id
;
13743 state
->op_id
= op_id
;
13744 state
->object
= object
;
13745 if (object
.empty()) {
13746 state
->cur_shard
= 0;
13747 state
->max_shard
= num_shards
- 1;
13749 state
->cur_shard
= state
->max_shard
= get_shard_num(object
);
13751 *handle
= (void *)state
;
13754 int RGWStateLog::list_entries(void *handle
, int max_entries
,
13755 list
<cls_statelog_entry
>& entries
,
13758 list_state
*state
= static_cast<list_state
*>(handle
);
13760 librados::IoCtx ioctx
;
13761 int r
= open_ioctx(ioctx
);
13767 for (; state
->cur_shard
<= state
->max_shard
&& max_entries
> 0; ++state
->cur_shard
) {
13769 oid_str(state
->cur_shard
, oid
);
13771 librados::ObjectReadOperation op
;
13772 list
<cls_statelog_entry
> ents
;
13774 cls_statelog_list(op
, state
->client_id
, state
->op_id
, state
->object
, state
->marker
,
13775 max_entries
, ents
, &state
->marker
, &truncated
);
13777 r
= ioctx
.operate(oid
, &op
, &ibl
);
13778 if (r
== -ENOENT
) {
13783 ldout(store
->ctx(), 0) << "cls_statelog_list returned " << r
<< dendl
;
13788 state
->marker
.clear();
13791 max_entries
-= ents
.size();
13793 entries
.splice(entries
.end(), ents
);
13799 *done
= (state
->cur_shard
> state
->max_shard
);
13804 void RGWStateLog::finish_list_entries(void *handle
)
13806 list_state
*state
= static_cast<list_state
*>(handle
);
13810 void RGWStateLog::dump_entry(const cls_statelog_entry
& entry
, Formatter
*f
)
13812 f
->open_object_section("statelog_entry");
13813 f
->dump_string("client_id", entry
.client_id
);
13814 f
->dump_string("op_id", entry
.op_id
);
13815 f
->dump_string("object", entry
.object
);
13816 entry
.timestamp
.gmtime_nsec(f
->dump_stream("timestamp"));
13817 if (!dump_entry_internal(entry
, f
)) {
13818 f
->dump_int("state", entry
.state
);
13820 f
->close_section();
13823 RGWOpState::RGWOpState(RGWRados
*_store
) : RGWStateLog(_store
, _store
->ctx()->_conf
->rgw_num_zone_opstate_shards
, string("obj_opstate"))
13827 bool RGWOpState::dump_entry_internal(const cls_statelog_entry
& entry
, Formatter
*f
)
13830 switch ((OpState
)entry
.state
) {
13831 case OPSTATE_UNKNOWN
:
13834 case OPSTATE_IN_PROGRESS
:
13837 case OPSTATE_COMPLETE
:
13840 case OPSTATE_ERROR
:
13843 case OPSTATE_ABORT
:
13846 case OPSTATE_CANCELLED
:
13852 f
->dump_string("state", s
);
13856 int RGWOpState::state_from_str(const string
& s
, OpState
*state
)
13858 if (s
== "unknown") {
13859 *state
= OPSTATE_UNKNOWN
;
13860 } else if (s
== "in-progress") {
13861 *state
= OPSTATE_IN_PROGRESS
;
13862 } else if (s
== "complete") {
13863 *state
= OPSTATE_COMPLETE
;
13864 } else if (s
== "error") {
13865 *state
= OPSTATE_ERROR
;
13866 } else if (s
== "abort") {
13867 *state
= OPSTATE_ABORT
;
13868 } else if (s
== "cancelled") {
13869 *state
= OPSTATE_CANCELLED
;
13877 int RGWOpState::set_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13879 uint32_t s
= (uint32_t)state
;
13880 return store_entry(client_id
, op_id
, object
, s
, NULL
, NULL
);
13883 int RGWOpState::renew_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13885 uint32_t s
= (uint32_t)state
;
13886 return store_entry(client_id
, op_id
, object
, s
, NULL
, &s
);
13889 RGWOpStateSingleOp::RGWOpStateSingleOp(RGWRados
*store
, const string
& cid
, const string
& oid
,
13890 const string
& obj
) : os(store
), client_id(cid
), op_id(oid
), object(obj
)
13892 cct
= store
->ctx();
13893 cur_state
= RGWOpState::OPSTATE_UNKNOWN
;
13896 int RGWOpStateSingleOp::set_state(RGWOpState::OpState state
) {
13897 last_update
= real_clock::now();
13899 return os
.set_state(client_id
, op_id
, object
, state
);
13902 int RGWOpStateSingleOp::renew_state() {
13903 real_time now
= real_clock::now();
13905 int rate_limit_sec
= cct
->_conf
->rgw_opstate_ratelimit_sec
;
13907 if (rate_limit_sec
&& now
- last_update
< make_timespan(rate_limit_sec
)) {
13912 return os
.renew_state(client_id
, op_id
, object
, cur_state
);
13916 uint64_t RGWRados::instance_id()
13918 return get_rados_handle()->get_instance_id();
13921 uint64_t RGWRados::next_bucket_id()
13923 Mutex::Locker
l(bucket_id_lock
);
13924 return ++max_bucket_id
;
13927 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
)
13929 int use_cache
= cct
->_conf
->rgw_cache_enabled
;
13930 RGWRados
*store
= NULL
;
13932 store
= new RGWRados
;
13934 store
= new RGWCache
<RGWRados
>;
13937 if (store
->initialize(cct
, use_gc_thread
, use_lc_thread
, quota_threads
, run_sync_thread
, run_reshard_thread
) < 0) {
13945 RGWRados
*RGWStoreManager::init_raw_storage_provider(CephContext
*cct
)
13947 RGWRados
*store
= NULL
;
13948 store
= new RGWRados
;
13950 store
->set_context(cct
);
13952 if (store
->init_rados() < 0) {
13960 void RGWStoreManager::close_storage(RGWRados
*store
)
13970 librados::Rados
* RGWRados::get_rados_handle()
13972 if (rados
.size() == 1) {
13975 handle_lock
.get_read();
13976 pthread_t id
= pthread_self();
13977 std::map
<pthread_t
, int>:: iterator it
= rados_map
.find(id
);
13979 if (it
!= rados_map
.end()) {
13980 handle_lock
.put_read();
13981 return &rados
[it
->second
];
13983 handle_lock
.put_read();
13984 handle_lock
.get_write();
13985 const uint32_t handle
= next_rados_handle
;
13986 rados_map
[id
] = handle
;
13987 if (++next_rados_handle
== rados
.size()) {
13988 next_rados_handle
= 0;
13990 handle_lock
.put_write();
13991 return &rados
[handle
];
13996 int RGWRados::delete_raw_obj_aio(const rgw_raw_obj
& obj
, list
<librados::AioCompletion
*>& handles
)
13999 int ret
= get_raw_obj_ref(obj
, &ref
);
14001 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
14005 ObjectWriteOperation op
;
14006 list
<string
> prefixes
;
14007 cls_rgw_remove_obj(op
, prefixes
);
14009 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
14010 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
14012 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
14017 handles
.push_back(c
);
14022 int RGWRados::delete_obj_aio(const rgw_obj
& obj
,
14023 RGWBucketInfo
& bucket_info
, RGWObjState
*astate
,
14024 list
<librados::AioCompletion
*>& handles
, bool keep_index_consistent
)
14027 int ret
= get_obj_head_ref(bucket_info
, obj
, &ref
);
14029 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
14033 if (keep_index_consistent
) {
14034 RGWRados::Bucket
bop(this, bucket_info
);
14035 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
14037 ret
= index_op
.prepare(CLS_RGW_OP_DEL
, &astate
->write_tag
);
14039 lderr(cct
) << "ERROR: failed to prepare index op with ret=" << ret
<< dendl
;
14044 ObjectWriteOperation op
;
14045 list
<string
> prefixes
;
14046 cls_rgw_remove_obj(op
, prefixes
);
14048 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
14049 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
14051 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
14056 handles
.push_back(c
);
14058 if (keep_index_consistent
) {
14059 ret
= delete_obj_index(obj
);
14061 lderr(cct
) << "ERROR: failed to delete obj index with ret=" << ret
<< dendl
;
14068 int rgw_compression_info_from_attrset(map
<string
, bufferlist
>& attrs
, bool& need_decompress
, RGWCompressionInfo
& cs_info
) {
14069 map
<string
, bufferlist
>::iterator value
= attrs
.find(RGW_ATTR_COMPRESSION
);
14070 if (value
!= attrs
.end()) {
14071 bufferlist::iterator bliter
= value
->second
.begin();
14073 ::decode(cs_info
, bliter
);
14074 } catch (buffer::error
& err
) {
14077 if (cs_info
.blocks
.size() == 0) {
14080 if (cs_info
.compression_type
!= "none")
14081 need_decompress
= true;
14083 need_decompress
= false;
14086 need_decompress
= false;
14091 bool RGWRados::call(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
14094 if (command
== "cache list") {
14095 boost::optional
<std::string
> filter
;
14096 auto i
= cmdmap
.find("filter");
14097 if (i
!= cmdmap
.cend()) {
14098 filter
= boost::get
<std::string
>(i
->second
);
14100 std::unique_ptr
<Formatter
> f(ceph::Formatter::create(format
, "table"));
14102 f
->open_array_section("cache_entries");
14103 call_list(filter
, f
.get());
14104 f
->close_section();
14108 out
.append("Unable to create Formatter.\n");
14111 } else if (command
== "cache inspect") {
14112 std::unique_ptr
<Formatter
> f(ceph::Formatter::create(format
, "json-pretty"));
14114 const auto& target
= boost::get
<std::string
>(cmdmap
["target"]);
14115 if (call_inspect(target
, f
.get())) {
14119 out
.append(string("Unable to find entry ") + target
+ string(".\n"));
14123 out
.append("Unable to create Formatter.\n");
14126 } else if (command
== "cache erase") {
14127 const auto& target
= boost::get
<std::string
>(cmdmap
["target"]);
14128 if (call_erase(target
)) {
14131 out
.append(string("Unable to find entry ") + target
+ string(".\n"));
14134 } else if (command
== "cache zap") {
14141 void RGWRados::call_list(const boost::optional
<std::string
>&,
14147 bool RGWRados::call_inspect(const std::string
&, Formatter
*)
14152 bool RGWRados::call_erase(const std::string
&) {
14156 void RGWRados::call_zap() {