From 2d314a2ff4f1dcf664b67f44d636e19e7266cbfc Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Tue, 22 Oct 2019 15:32:11 -0700 Subject: [PATCH] rgw: move code around Signed-off-by: Yehuda Sadeh --- src/rgw/CMakeLists.txt | 1 + src/rgw/rgw_bucket_sync.cc | 300 ------------------------------------ src/rgw/rgw_sync_policy.cc | 307 +++++++++++++++++++++++++++++++++++++ 3 files changed, 308 insertions(+), 300 deletions(-) create mode 100644 src/rgw/rgw_sync_policy.cc diff --git a/src/rgw/CMakeLists.txt b/src/rgw/CMakeLists.txt index 2addc4b172c1f..b6a2a7a071eb5 100644 --- a/src/rgw/CMakeLists.txt +++ b/src/rgw/CMakeLists.txt @@ -89,6 +89,7 @@ set(librgw_common_srcs rgw_sync_module_es_rest.cc rgw_sync_module_log.cc rgw_sync_module_pubsub.cc + rgw_sync_policy.cc rgw_pubsub_push.cc rgw_notify.cc rgw_notify_event_type.cc diff --git a/src/rgw/rgw_bucket_sync.cc b/src/rgw/rgw_bucket_sync.cc index ccf0d64a6889f..87592392f5cdd 100644 --- a/src/rgw/rgw_bucket_sync.cc +++ b/src/rgw/rgw_bucket_sync.cc @@ -10,306 +10,6 @@ #define dout_subsys ceph_subsys_rgw -string rgw_sync_bucket_entity::bucket_key() const -{ - return rgw_sync_bucket_entities::bucket_key(bucket); -} -void rgw_sync_bucket_entity::apply_bucket(std::optional b) -{ - if (!b) { - return; - } - - if (!bucket || - bucket->name.empty()) { - bucket = b; - } -} - -void rgw_sync_bucket_entities::add_zones(const std::vector& new_zones) { - for (auto& z : new_zones) { - if (z == "*") { - all_zones = true; - zones.reset(); - return; - } - - if (!zones) { - zones.emplace(); - } - - zones->insert(z); - } -} - -std::vector rgw_sync_bucket_entities::expand() const -{ - std::vector result; - rgw_bucket b = get_bucket(); - if (all_zones) { - rgw_sync_bucket_entity e; - e.all_zones = true; - e.bucket = b; - result.push_back(e); - return std::move(result); - } - - if (!zones) { - return result; - } - - for (auto& z : *zones) { - rgw_sync_bucket_entity e; - e.all_zones = false; - e.bucket = b; - e.zone = z; - result.push_back(e); - } - - return result; -} - -void rgw_sync_bucket_entities::remove_zones(const std::vector& rm_zones) { - all_zones = false; - - if (!zones) { - return; - } - - for (auto& z : rm_zones) { - zones->erase(z); - } -} - -static void set_bucket_field(std::optional source, string *field) { - if (!source) { - return; - } - if (source == "*") { - field->clear(); - return; - } - *field = *source; -} - -void rgw_sync_bucket_entities::set_bucket(std::optional tenant, - std::optional bucket_name, - std::optional bucket_id) -{ - if ((!bucket) && (tenant || bucket_name || bucket_id)) { - bucket.emplace(); - } - - set_bucket_field(tenant, &bucket->tenant); - set_bucket_field(bucket_name, &bucket->name); - set_bucket_field(bucket_id, &bucket->bucket_id); - - if (bucket->tenant.empty() && - bucket->name.empty() && - bucket->bucket_id.empty()) { - bucket.reset(); - } -} - -void rgw_sync_bucket_entities::remove_bucket(std::optional tenant, - std::optional bucket_name, - std::optional bucket_id) -{ - if (!bucket) { - return; - } - - if (tenant) { - bucket->tenant.clear(); - } - if (bucket_name) { - bucket->name.clear(); - } - if (bucket_id) { - bucket->bucket_id.clear(); - } - - if (bucket->tenant.empty() && - bucket->name.empty() && - bucket->bucket_id.empty()) { - bucket.reset(); - } -} - - -string rgw_sync_bucket_entities::bucket_key(std::optional b) -{ - if (!b) { - return string("*"); - } - - rgw_bucket _b = *b; - - if (_b.name.empty()) { - _b.name = "*"; - } - - return _b.get_key(); -} - - -bool rgw_sync_data_flow_group::find_symmetrical(const string& flow_id, bool create, rgw_sync_symmetric_group **flow_group) -{ - if (!symmetrical) { - if (!create) { - return false; - } - symmetrical.emplace(); - } - - for (auto& group : *symmetrical) { - if (flow_id == group.id) { - *flow_group = &group; - return true; - } - } - - if (!create) { - return false; - } - - auto& group = symmetrical->emplace_back(); - *flow_group = &group; - (*flow_group)->id = flow_id; - return true; -} - -void rgw_sync_data_flow_group::remove_symmetrical(const string& flow_id, std::optional > zones) -{ - if (!symmetrical) { - return; - } - - auto& groups = *symmetrical; - - auto iter = groups.begin(); - - for (; iter != groups.end(); ++iter) { - if (iter->id == flow_id) { - if (!zones) { - groups.erase(iter); - return; - } - break; - } - } - - if (iter == groups.end()) { - return; - } - - auto& flow_group = *iter; - - for (auto& z : *zones) { - flow_group.zones.erase(z); - } - - if (flow_group.zones.empty()) { - groups.erase(iter); - } -} - -bool rgw_sync_data_flow_group::find_directional(const string& source_zone, const string& dest_zone, bool create, rgw_sync_directional_rule **flow_group) -{ - if (!directional) { - if (!create) { - return false; - } - directional.emplace(); - } - - for (auto& rule : *directional) { - if (source_zone == rule.source_zone && - dest_zone == rule.dest_zone) { - *flow_group = &rule; - return true; - } - } - - if (!create) { - return false; - } - - auto& rule = directional->emplace_back(); - *flow_group = &rule; - - rule.source_zone = source_zone; - rule.dest_zone = dest_zone; - - return true; -} - -void rgw_sync_data_flow_group::remove_directional(const string& source_zone, const string& dest_zone) -{ - if (!directional) { - return; - } - - for (auto iter = directional->begin(); iter != directional->end(); ++iter) { - auto& rule = *iter; - if (source_zone == rule.source_zone && - dest_zone == rule.dest_zone) { - directional->erase(iter); - return; - } - } -} - -bool rgw_sync_policy_group::find_pipe(const string& pipe_id, bool create, rgw_sync_bucket_pipes **pipe) -{ - for (auto& p : pipes) { - if (pipe_id == p.id) { - *pipe = &p; - return true; - } - } - - if (!create) { - return false; - } - - auto& p = pipes.emplace_back(); - *pipe = &p; - p.id = pipe_id; - - return true; -} - -void rgw_sync_policy_group::remove_pipe(const string& pipe_id) -{ - for (auto iter = pipes.begin(); iter != pipes.end(); ++iter) { - if (pipe_id == iter->id) { - pipes.erase(iter); - return; - } - } -} - -std::vector rgw_sync_bucket_pipes::expand() const -{ - std::vector result; - - auto sources = source.expand(); - auto dests = dest.expand(); - - for (auto& s : sources) { - for (auto& d : dests) { - rgw_sync_bucket_pipe pipe; - pipe.source = std::move(s); - pipe.dest = std::move(d); - result.push_back(pipe); - } - } - - return result; -} - - static std::vector filter_relevant_pipes(const std::vector& pipes, const string& source_zone, const string& dest_zone) diff --git a/src/rgw/rgw_sync_policy.cc b/src/rgw/rgw_sync_policy.cc new file mode 100644 index 0000000000000..1cbf79179a178 --- /dev/null +++ b/src/rgw/rgw_sync_policy.cc @@ -0,0 +1,307 @@ + + +#include "rgw_common.h" +#include "rgw_sync_policy.h" + +#define dout_subsys ceph_subsys_rgw + + +string rgw_sync_bucket_entity::bucket_key() const +{ + return rgw_sync_bucket_entities::bucket_key(bucket); +} +void rgw_sync_bucket_entity::apply_bucket(std::optional b) +{ + if (!b) { + return; + } + + if (!bucket || + bucket->name.empty()) { + bucket = b; + } +} + +void rgw_sync_bucket_entities::add_zones(const std::vector& new_zones) { + for (auto& z : new_zones) { + if (z == "*") { + all_zones = true; + zones.reset(); + return; + } + + if (!zones) { + zones.emplace(); + } + + zones->insert(z); + } +} + +std::vector rgw_sync_bucket_entities::expand() const +{ + std::vector result; + rgw_bucket b = get_bucket(); + if (all_zones) { + rgw_sync_bucket_entity e; + e.all_zones = true; + e.bucket = b; + result.push_back(e); + return std::move(result); + } + + if (!zones) { + return result; + } + + for (auto& z : *zones) { + rgw_sync_bucket_entity e; + e.all_zones = false; + e.bucket = b; + e.zone = z; + result.push_back(e); + } + + return result; +} + +void rgw_sync_bucket_entities::remove_zones(const std::vector& rm_zones) { + all_zones = false; + + if (!zones) { + return; + } + + for (auto& z : rm_zones) { + zones->erase(z); + } +} + +static void set_bucket_field(std::optional source, string *field) { + if (!source) { + return; + } + if (source == "*") { + field->clear(); + return; + } + *field = *source; +} + +void rgw_sync_bucket_entities::set_bucket(std::optional tenant, + std::optional bucket_name, + std::optional bucket_id) +{ + if ((!bucket) && (tenant || bucket_name || bucket_id)) { + bucket.emplace(); + } + + set_bucket_field(tenant, &bucket->tenant); + set_bucket_field(bucket_name, &bucket->name); + set_bucket_field(bucket_id, &bucket->bucket_id); + + if (bucket->tenant.empty() && + bucket->name.empty() && + bucket->bucket_id.empty()) { + bucket.reset(); + } +} + +void rgw_sync_bucket_entities::remove_bucket(std::optional tenant, + std::optional bucket_name, + std::optional bucket_id) +{ + if (!bucket) { + return; + } + + if (tenant) { + bucket->tenant.clear(); + } + if (bucket_name) { + bucket->name.clear(); + } + if (bucket_id) { + bucket->bucket_id.clear(); + } + + if (bucket->tenant.empty() && + bucket->name.empty() && + bucket->bucket_id.empty()) { + bucket.reset(); + } +} + + +string rgw_sync_bucket_entities::bucket_key(std::optional b) +{ + if (!b) { + return string("*"); + } + + rgw_bucket _b = *b; + + if (_b.name.empty()) { + _b.name = "*"; + } + + return _b.get_key(); +} + +std::vector rgw_sync_bucket_pipes::expand() const +{ + std::vector result; + + auto sources = source.expand(); + auto dests = dest.expand(); + + for (auto& s : sources) { + for (auto& d : dests) { + rgw_sync_bucket_pipe pipe; + pipe.source = std::move(s); + pipe.dest = std::move(d); + result.push_back(pipe); + } + } + + return result; +} + + +bool rgw_sync_data_flow_group::find_symmetrical(const string& flow_id, bool create, rgw_sync_symmetric_group **flow_group) +{ + if (!symmetrical) { + if (!create) { + return false; + } + symmetrical.emplace(); + } + + for (auto& group : *symmetrical) { + if (flow_id == group.id) { + *flow_group = &group; + return true; + } + } + + if (!create) { + return false; + } + + auto& group = symmetrical->emplace_back(); + *flow_group = &group; + (*flow_group)->id = flow_id; + return true; +} + +void rgw_sync_data_flow_group::remove_symmetrical(const string& flow_id, std::optional > zones) +{ + if (!symmetrical) { + return; + } + + auto& groups = *symmetrical; + + auto iter = groups.begin(); + + for (; iter != groups.end(); ++iter) { + if (iter->id == flow_id) { + if (!zones) { + groups.erase(iter); + return; + } + break; + } + } + + if (iter == groups.end()) { + return; + } + + auto& flow_group = *iter; + + for (auto& z : *zones) { + flow_group.zones.erase(z); + } + + if (flow_group.zones.empty()) { + groups.erase(iter); + } +} + +bool rgw_sync_data_flow_group::find_directional(const string& source_zone, const string& dest_zone, bool create, rgw_sync_directional_rule **flow_group) +{ + if (!directional) { + if (!create) { + return false; + } + directional.emplace(); + } + + for (auto& rule : *directional) { + if (source_zone == rule.source_zone && + dest_zone == rule.dest_zone) { + *flow_group = &rule; + return true; + } + } + + if (!create) { + return false; + } + + auto& rule = directional->emplace_back(); + *flow_group = &rule; + + rule.source_zone = source_zone; + rule.dest_zone = dest_zone; + + return true; +} + +void rgw_sync_data_flow_group::remove_directional(const string& source_zone, const string& dest_zone) +{ + if (!directional) { + return; + } + + for (auto iter = directional->begin(); iter != directional->end(); ++iter) { + auto& rule = *iter; + if (source_zone == rule.source_zone && + dest_zone == rule.dest_zone) { + directional->erase(iter); + return; + } + } +} + + +bool rgw_sync_policy_group::find_pipe(const string& pipe_id, bool create, rgw_sync_bucket_pipes **pipe) +{ + for (auto& p : pipes) { + if (pipe_id == p.id) { + *pipe = &p; + return true; + } + } + + if (!create) { + return false; + } + + auto& p = pipes.emplace_back(); + *pipe = &p; + p.id = pipe_id; + + return true; +} + +void rgw_sync_policy_group::remove_pipe(const string& pipe_id) +{ + for (auto iter = pipes.begin(); iter != pipes.end(); ++iter) { + if (pipe_id == iter->id) { + pipes.erase(iter); + return; + } + } +} -- 2.39.5