Skip to content

Commit

Permalink
split definition and implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Falkowski <[email protected]>
  • Loading branch information
MarcinFalkowski committed Nov 11, 2019
1 parent 280893a commit 10ba22d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
30 changes: 30 additions & 0 deletions source/common/upstream/load_balancer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -775,5 +775,35 @@ HostConstSharedPtr RandomLoadBalancer::chooseHostOnce(LoadBalancerContext* conte
return hosts_to_use[random_.random() % hosts_to_use.size()];
}

SubsetSelectorImpl::SubsetSelectorImpl(
const Protobuf::RepeatedPtrField<std::string>& selector_keys,
envoy::api::v2::Cluster::LbSubsetConfig::LbSubsetSelector::LbSubsetSelectorFallbackPolicy
fallback_policy,
const Protobuf::RepeatedPtrField<std::string>& fallback_keys_subset)
: selector_keys_(selector_keys.begin(), selector_keys.end()), fallback_policy_(fallback_policy),
fallback_keys_subset_(fallback_keys_subset.begin(), fallback_keys_subset.end()) {

if (fallback_policy_ != envoy::api::v2::Cluster::LbSubsetConfig::LbSubsetSelector::KEYS_SUBSET) {
if (!fallback_keys_subset_.empty()) {
throw EnvoyException("fallback_keys_subset can be set only for KEYS_SUBSET fallback_policy");
}
return;
}

if (fallback_keys_subset_.empty()) {
throw EnvoyException("fallback_keys_subset cannot be empty");
}

if (!std::includes(selector_keys_.begin(), selector_keys_.end(), fallback_keys_subset_.begin(),
fallback_keys_subset_.end())) {
throw EnvoyException("fallback_keys_subset must be a subset of selector keys");
}

// Enforce that the fallback_keys_subset_ set is smaller than the selector_keys_ set. Otherwise
// we could end up with a infinite recursion of SubsetLoadBalancer::chooseHost().
if (selector_keys_.size() == fallback_keys_subset_.size()) {
throw EnvoyException("fallback_keys_subset cannot be equal to keys");
}
}
} // namespace Upstream
} // namespace Envoy
30 changes: 1 addition & 29 deletions source/common/upstream/load_balancer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,35 +489,7 @@ class SubsetSelectorImpl : public SubsetSelector {
const Protobuf::RepeatedPtrField<std::string>& selector_keys,
envoy::api::v2::Cluster::LbSubsetConfig::LbSubsetSelector::LbSubsetSelectorFallbackPolicy
fallback_policy,
const Protobuf::RepeatedPtrField<std::string>& fallback_keys_subset)
: selector_keys_(selector_keys.begin(), selector_keys.end()),
fallback_policy_(fallback_policy),
fallback_keys_subset_(fallback_keys_subset.begin(), fallback_keys_subset.end()) {

if (fallback_policy_ !=
envoy::api::v2::Cluster::LbSubsetConfig::LbSubsetSelector::KEYS_SUBSET) {
if (!fallback_keys_subset_.empty()) {
throw EnvoyException(
"fallback_keys_subset can be set only for KEYS_SUBSET fallback_policy");
}
return;
}

if (fallback_keys_subset_.empty()) {
throw EnvoyException("fallback_keys_subset cannot be empty");
}

if (!std::includes(selector_keys_.begin(), selector_keys_.end(), fallback_keys_subset_.begin(),
fallback_keys_subset_.end())) {
throw EnvoyException("fallback_keys_subset must be a subset of selector keys");
}

// Enforce that the fallback_keys_subset_ set is smaller than the selector_keys_ set. Otherwise
// we could end up with a infinite recursion of SubsetLoadBalancer::chooseHost().
if (selector_keys_.size() == fallback_keys_subset_.size()) {
throw EnvoyException("fallback_keys_subset cannot be equal to keys");
}
}
const Protobuf::RepeatedPtrField<std::string>& fallback_keys_subset);

// SubsetSelector
const std::set<std::string>& selectorKeys() const override { return selector_keys_; }
Expand Down

0 comments on commit 10ba22d

Please sign in to comment.