MCPcopy Create free account
hub / github.com/apple/foundationdb / findBestPolicySetSimple

Function findBestPolicySetSimple

fdbrpc/ReplicationUtils.cpp:99–144  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99bool findBestPolicySetSimple(int targetUniqueValueCount,
100 Reference<LocalitySet>& logServerSet,
101 std::vector<LocalityEntry>& bestSet,
102 int desired) {
103 auto& mutableEntries = logServerSet->getMutableEntries();
104 // First make sure the current localitySet is able to fulfuill the policy
105 AttribKey indexKey = logServerSet->keyIndex("zoneid");
106 int uniqueValueCount = logServerSet->getKeyValueArray()[indexKey._id].size();
107
108 if (uniqueValueCount < targetUniqueValueCount) {
109 // logServerSet won't be able to fulfill the policy
110 return false;
111 }
112
113 std::map<AttribValue, std::vector<int>> entries;
114 for (int i = 0; i < mutableEntries.size(); i++) {
115 Optional<AttribValue> value = logServerSet->getRecord(mutableEntries[i]._id)->getValue(indexKey);
116 if (value.present()) {
117 entries[value.get()].push_back(i);
118 }
119 }
120
121 ASSERT_WE_THINK(uniqueValueCount == entries.size());
122 std::vector<std::vector<int>> randomizedEntries;
123 randomizedEntries.resize(entries.size());
124 for (auto it : entries) {
125 randomizedEntries.push_back(it.second);
126 }
127 deterministicRandom()->randomShuffle(randomizedEntries);
128
129 desired = std::max(desired, targetUniqueValueCount);
130 auto it = randomizedEntries.begin();
131 while (bestSet.size() < desired) {
132 if (it->size()) {
133 bestSet.push_back(mutableEntries[it->back()]);
134 it->pop_back();
135 }
136
137 ++it;
138 if (it == randomizedEntries.end()) {
139 it = randomizedEntries.begin();
140 }
141 }
142
143 return true;
144}
145
146bool findBestPolicySetExpensive(std::vector<LocalityEntry>& bestResults,
147 Reference<LocalitySet>& localitySet,

Callers 1

findBestPolicySetFunction · 0.85

Calls 14

deterministicRandomFunction · 0.85
keyIndexMethod · 0.80
getKeyValueArrayMethod · 0.80
randomShuffleMethod · 0.80
getMethod · 0.65
sizeMethod · 0.45
getValueMethod · 0.45
presentMethod · 0.45
push_backMethod · 0.45
resizeMethod · 0.45
beginMethod · 0.45
backMethod · 0.45

Tested by

no test coverage detected