forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPhabricatorClusterServiceHealthRecord.php
181 lines (141 loc) · 3.91 KB
/
PhabricatorClusterServiceHealthRecord.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class PhabricatorClusterServiceHealthRecord
extends Phobject {
private $cacheKey;
private $shouldCheck;
private $isHealthy;
private $upEventCount;
private $downEventCount;
public function __construct($cache_key) {
$this->cacheKey = $cache_key;
$this->readState();
}
/**
* Is the database currently healthy?
*/
public function getIsHealthy() {
return $this->isHealthy;
}
/**
* Should this request check database health?
*/
public function getShouldCheck() {
return $this->shouldCheck;
}
/**
* How many recent health checks were successful?
*/
public function getUpEventCount() {
return $this->upEventCount;
}
/**
* How many recent health checks failed?
*/
public function getDownEventCount() {
return $this->downEventCount;
}
/**
* Number of failures or successes we need to see in a row before we change
* the state.
*/
public function getRequiredEventCount() {
// NOTE: If you change this value, update the "Cluster: Databases" docs.
return 5;
}
/**
* Seconds to wait between health checks.
*/
public function getHealthCheckFrequency() {
// NOTE: If you change this value, update the "Cluster: Databases" docs.
return 3;
}
public function didHealthCheck($result) {
$now = microtime(true);
$check_frequency = $this->getHealthCheckFrequency();
$event_count = $this->getRequiredEventCount();
$record = $this->readHealthRecord();
$log = $record['log'];
foreach ($log as $key => $event) {
$when = idx($event, 'timestamp');
// If the log already has another nearby event, just ignore this one.
// We raced with another process and our result can just be thrown away.
if (($now - $when) <= $check_frequency) {
return $this;
}
}
$log[] = array(
'timestamp' => $now,
'up' => $result,
);
// Throw away older events which are now obsolete.
$log = array_slice($log, -$event_count);
$count_up = 0;
$count_down = 0;
foreach ($log as $event) {
if ($event['up']) {
$count_up++;
} else {
$count_down++;
}
}
// If all of the events are the same, change the state.
if ($count_up == $event_count) {
$record['up'] = true;
} else if ($count_down == $event_count) {
$record['up'] = false;
}
$record['log'] = $log;
$this->writeHealthRecord($record);
$this->isHealthy = $record['up'];
$this->shouldCheck = false;
$this->updateStatistics($record);
return $this;
}
private function readState() {
$now = microtime(true);
$check_frequency = $this->getHealthCheckFrequency();
$record = $this->readHealthRecord();
$last_check = $record['lastCheck'];
if (($now - $last_check) >= $check_frequency) {
$record['lastCheck'] = $now;
$this->writeHealthRecord($record);
$this->shouldCheck = true;
} else {
$this->shouldCheck = false;
}
$this->isHealthy = $record['up'];
$this->updateStatistics($record);
}
private function updateStatistics(array $record) {
$this->upEventCount = 0;
$this->downEventCount = 0;
foreach ($record['log'] as $event) {
if ($event['up']) {
$this->upEventCount++;
} else {
$this->downEventCount++;
}
}
}
public function getCacheKey() {
return $this->cacheKey;
}
private function readHealthRecord() {
$cache = PhabricatorCaches::getSetupCache();
$cache_key = $this->getCacheKey();
$health_record = $cache->getKey($cache_key);
if (!is_array($health_record)) {
$health_record = array(
'up' => true,
'lastCheck' => 0,
'log' => array(),
);
}
return $health_record;
}
private function writeHealthRecord(array $record) {
$cache = PhabricatorCaches::getSetupCache();
$cache_key = $this->getCacheKey();
$cache->setKey($cache_key, $record);
}
}