forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBedrockConflictMetrics.cpp
More file actions
114 lines (97 loc) · 4.08 KB
/
BedrockConflictMetrics.cpp
File metadata and controls
114 lines (97 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "BedrockConflictMetrics.h"
// Initialize non-const static variables.
recursive_mutex BedrockConflictMetrics::_mutex;
map<string, BedrockConflictMetrics> BedrockConflictMetrics::_conflictInfoMap;
double BedrockConflictMetrics::_fraction = 0.10;
int BedrockConflictMetrics::_threshold = _fraction * COMMAND_COUNT;
BedrockConflictMetrics::BedrockConflictMetrics(const string& commandName) :
_commandName(commandName)
{ }
void BedrockConflictMetrics::success() {
++_totalSuccessCount;
_results.reset(_resultsPtr);
++_resultsPtr;
_resultsPtr %= COMMAND_COUNT;
}
void BedrockConflictMetrics::conflict() {
++_totalConflictCount;
_results.set(_resultsPtr);
++_resultsPtr;
_resultsPtr %= COMMAND_COUNT;
}
int BedrockConflictMetrics::recentSuccessCount() {
return COMMAND_COUNT - _results.count();
}
int BedrockConflictMetrics::recentConflictCount() {
return _results.count();
}
void BedrockConflictMetrics::recordConflict(const string& commandName) {
SAUTOLOCK(_mutex);
// Look up, and create if necessary, the info object for this command.
auto it = _conflictInfoMap.find(commandName);
if (it == _conflictInfoMap.end()) {
// We need to insert this command.
auto itPair = _conflictInfoMap.emplace(make_pair(commandName, commandName));
it = itPair.first;
}
it->second.conflict();
SINFO("Multi-write conflict recorded for " << commandName);
}
void BedrockConflictMetrics::recordSuccess(const string& commandName) {
SAUTOLOCK(_mutex);
// Look up, and create if necessary, the info object for this command.
auto it = _conflictInfoMap.find(commandName);
if (it == _conflictInfoMap.end()) {
// We need to insert this command.
auto itPair = _conflictInfoMap.emplace(make_pair(commandName, commandName));
it = itPair.first;
}
it->second.success();
SINFO("Multi-write success recorded for " << commandName);
}
bool BedrockConflictMetrics::multiWriteOK(const string& commandName) {
SAUTOLOCK(_mutex);
// Look up the command in the list of commands.
auto it = _conflictInfoMap.find(commandName);
if (it == _conflictInfoMap.end()) {
// If we don't find it, we assume it's OK to multi-write.
SINFO("Multi-write command '" << commandName << "' not tracked in BedrockConflictMetrics. Assuming OK.");
return true;
}
// Otherwise, we check to see if it's recent conflict count is too high for multi-write.
auto& metric = it->second;
int conflicts = metric.recentConflictCount();
uint64_t totalAttempts = metric._totalConflictCount + metric._totalSuccessCount;
bool result = conflicts < _threshold;
string resultString = result ? "OK" : "DENIED";
SINFO("Multi-write " << resultString << " for command '" << commandName << "' recent conflicts: "
<< conflicts << "/" << min((uint64_t)COMMAND_COUNT, totalAttempts) << ".");
// And now that we know whether or not we can multi-write this, see if that's different than the last time we
// checked for this command, so we can do extra logging if so.
if (result != metric._lastCheckOK) {
SINFO("Multi-write changing to " << resultString << " for command '" << commandName
<< "' recent conflicts: " << conflicts << "/" << min((uint64_t)COMMAND_COUNT, totalAttempts)
<< ", total conflicts: " << metric._totalConflictCount << "/" << totalAttempts << ".");
}
metric._lastCheckOK = result;
return result;
}
string BedrockConflictMetrics::getMultiWriteDeniedCommands() {
SAUTOLOCK(_mutex);
set<string> commands;
for (auto& pair : _conflictInfoMap) {
auto& metric = pair.second;
if (metric.recentConflictCount() >= _threshold) {
commands.insert(metric._commandName);
}
}
return SComposeList(commands);
}
void BedrockConflictMetrics::setFraction(double fraction) {
SAUTOLOCK(_mutex);
if (fraction > 0.0 && fraction < 1.0) {
_fraction = fraction;
_threshold = _fraction * COMMAND_COUNT;
SINFO("Multi-write conflict limit fraction set to " << _fraction << ".");
}
}