Line data Source code
1 : #include "source/common/upstream/subset_lb_config.h" 2 : 3 : #include "source/common/config/utility.h" 4 : 5 : namespace Envoy { 6 : namespace Upstream { 7 : 8 : SubsetSelectorImpl::SubsetSelectorImpl( 9 : const Protobuf::RepeatedPtrField<std::string>& selector_keys, 10 : envoy::config::cluster::v3::Cluster::LbSubsetConfig::LbSubsetSelector:: 11 : LbSubsetSelectorFallbackPolicy fallback_policy, 12 : const Protobuf::RepeatedPtrField<std::string>& fallback_keys_subset, 13 : bool single_host_per_subset) 14 : : selector_keys_(selector_keys.begin(), selector_keys.end()), 15 : fallback_keys_subset_(fallback_keys_subset.begin(), fallback_keys_subset.end()), 16 0 : fallback_policy_(fallback_policy), single_host_per_subset_(single_host_per_subset) { 17 : 18 0 : if (fallback_policy_ != 19 0 : envoy::config::cluster::v3::Cluster::LbSubsetConfig::LbSubsetSelector::KEYS_SUBSET) { 20 : // defining fallback_keys_subset_ for a fallback policy other than KEYS_SUBSET doesn't have 21 : // any effect and it is probably a user mistake. We should let the user know about it. 22 0 : if (!fallback_keys_subset_.empty()) { 23 0 : throwEnvoyExceptionOrPanic( 24 0 : "fallback_keys_subset can be set only for KEYS_SUBSET fallback_policy"); 25 0 : } 26 0 : return; 27 0 : } 28 : 29 : // if KEYS_SUBSET fallback policy is selected, fallback_keys_subset must not be empty, because 30 : // it would be the same as not defining fallback policy at all (global fallback policy would be 31 : // used) 32 0 : if (fallback_keys_subset_.empty()) { 33 0 : throwEnvoyExceptionOrPanic("fallback_keys_subset cannot be empty"); 34 0 : } 35 : 36 : // We allow only for a fallback to a subset of the selector keys because this is probably the 37 : // only use case that makes sense (fallback from more specific selector to less specific 38 : // selector). Potentially we can relax this constraint in the future if there will be a use case 39 : // for this. 40 0 : if (!std::includes(selector_keys_.begin(), selector_keys_.end(), fallback_keys_subset_.begin(), 41 0 : fallback_keys_subset_.end())) { 42 0 : throwEnvoyExceptionOrPanic("fallback_keys_subset must be a subset of selector keys"); 43 0 : } 44 : 45 : // Enforce that the fallback_keys_subset_ set is smaller than the selector_keys_ set. Otherwise 46 : // we could end up with a infinite recursion of SubsetLoadBalancer::chooseHost(). 47 0 : if (selector_keys_.size() == fallback_keys_subset_.size()) { 48 0 : throwEnvoyExceptionOrPanic("fallback_keys_subset cannot be equal to keys"); 49 0 : } 50 0 : } 51 : 52 : } // namespace Upstream 53 : } // namespace Envoy