| 215 | } |
| 216 | |
| 217 | bool findBestPolicySet(std::vector<LocalityEntry>& bestResults, |
| 218 | Reference<LocalitySet>& localitySet, |
| 219 | Reference<IReplicationPolicy> const& policy, |
| 220 | unsigned int nMinItems, |
| 221 | unsigned int nSelectTests, |
| 222 | unsigned int nPolicyTests) { |
| 223 | |
| 224 | bool bestFound = false; |
| 225 | |
| 226 | // Specialization for policies of shape: |
| 227 | // - PolicyOne() |
| 228 | // - PolicyAcross(,"zoneId",PolicyOne()) |
| 229 | // - TODO: More specializations for common policies |
| 230 | if (policy->name() == "One") { |
| 231 | bestFound = true; |
| 232 | int count = 0; |
| 233 | auto& mutableEntries = localitySet->getMutableEntries(); |
| 234 | deterministicRandom()->randomShuffle(mutableEntries); |
| 235 | for (auto const& entry : mutableEntries) { |
| 236 | bestResults.push_back(entry); |
| 237 | if (++count == nMinItems) |
| 238 | break; |
| 239 | } |
| 240 | } else if (policy->name() == "Across") { |
| 241 | PolicyAcross* pa = (PolicyAcross*)policy.getPtr(); |
| 242 | std::set<std::string> attributeKeys; |
| 243 | pa->attributeKeys(&attributeKeys); |
| 244 | if (pa->embeddedPolicyName() == "One" && attributeKeys.size() == 1 && |
| 245 | *attributeKeys.begin() == "zoneid" // This algorithm can actually apply to any field |
| 246 | ) { |
| 247 | bestFound = findBestPolicySetSimple(pa->getCount(), localitySet, bestResults, nMinItems); |
| 248 | if (bestFound && g_network->isSimulated()) { |
| 249 | std::vector<LocalityEntry> oldBest; |
| 250 | auto oldBestFound = |
| 251 | findBestPolicySetExpensive(oldBest, localitySet, policy, nMinItems, nSelectTests, nPolicyTests); |
| 252 | if (!oldBestFound) { |
| 253 | TraceEvent(SevError, "FBPSMissmatch").detail("Policy", policy->info()); |
| 254 | } else { |
| 255 | ASSERT(mostUsedZoneCount(localitySet, bestResults) <= mostUsedZoneCount(localitySet, oldBest)); |
| 256 | } |
| 257 | } |
| 258 | } else { |
| 259 | bestFound = |
| 260 | findBestPolicySetExpensive(bestResults, localitySet, policy, nMinItems, nSelectTests, nPolicyTests); |
| 261 | } |
| 262 | } else { |
| 263 | bestFound = findBestPolicySetExpensive(bestResults, localitySet, policy, nMinItems, nSelectTests, nPolicyTests); |
| 264 | } |
| 265 | return bestFound; |
| 266 | } |
| 267 | |
| 268 | bool findBestUniquePolicySet(std::vector<LocalityEntry>& bestResults, |
| 269 | Reference<LocalitySet>& localitySet, |
no test coverage detected