Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/config/controller/services/PhabricatorConfigClusterDatabasesController.php
12262 views
1
<?php
2
3
final class PhabricatorConfigClusterDatabasesController
4
extends PhabricatorConfigServicesController {
5
6
public function handleRequest(AphrontRequest $request) {
7
$nav = $this->newNavigation('database-servers');
8
9
$title = pht('Database Servers');
10
$doc_href = PhabricatorEnv::getDoclink('Cluster: Databases');
11
$button = id(new PHUIButtonView())
12
->setIcon('fa-book')
13
->setHref($doc_href)
14
->setTag('a')
15
->setText(pht('Documentation'));
16
17
$header = $this->buildHeaderView($title, $button);
18
19
$database_status = $this->buildClusterDatabaseStatus();
20
$status = $this->buildConfigBoxView(pht('Status'), $database_status);
21
22
$crumbs = $this->newCrumbs()
23
->addTextCrumb($title);
24
25
$content = id(new PHUITwoColumnView())
26
->setHeader($header)
27
->setFooter($status);
28
29
return $this->newPage()
30
->setTitle($title)
31
->setCrumbs($crumbs)
32
->setNavigation($nav)
33
->appendChild($content);
34
}
35
36
private function buildClusterDatabaseStatus() {
37
$viewer = $this->getViewer();
38
39
$databases = PhabricatorDatabaseRef::queryAll();
40
$connection_map = PhabricatorDatabaseRef::getConnectionStatusMap();
41
$replica_map = PhabricatorDatabaseRef::getReplicaStatusMap();
42
Javelin::initBehavior('phabricator-tooltips');
43
44
$rows = array();
45
foreach ($databases as $database) {
46
$messages = array();
47
48
if ($database->getIsMaster()) {
49
$role_icon = id(new PHUIIconView())
50
->setIcon('fa-database sky')
51
->addSigil('has-tooltip')
52
->setMetadata(
53
array(
54
'tip' => pht('Master'),
55
));
56
} else {
57
$role_icon = id(new PHUIIconView())
58
->setIcon('fa-download')
59
->addSigil('has-tooltip')
60
->setMetadata(
61
array(
62
'tip' => pht('Replica'),
63
));
64
}
65
66
if ($database->getDisabled()) {
67
$conn_icon = 'fa-times';
68
$conn_color = 'grey';
69
$conn_label = pht('Disabled');
70
} else {
71
$status = $database->getConnectionStatus();
72
73
$info = idx($connection_map, $status, array());
74
$conn_icon = idx($info, 'icon');
75
$conn_color = idx($info, 'color');
76
$conn_label = idx($info, 'label');
77
78
if ($status === PhabricatorDatabaseRef::STATUS_OKAY) {
79
$latency = $database->getConnectionLatency();
80
$latency = (int)(1000000 * $latency);
81
$conn_label = pht('%s us', new PhutilNumber($latency));
82
}
83
}
84
85
$connection = array(
86
id(new PHUIIconView())->setIcon("{$conn_icon} {$conn_color}"),
87
' ',
88
$conn_label,
89
);
90
91
if ($database->getDisabled()) {
92
$replica_icon = 'fa-times';
93
$replica_color = 'grey';
94
$replica_label = pht('Disabled');
95
} else {
96
$status = $database->getReplicaStatus();
97
98
$info = idx($replica_map, $status, array());
99
$replica_icon = idx($info, 'icon');
100
$replica_color = idx($info, 'color');
101
$replica_label = idx($info, 'label');
102
103
if ($database->getIsMaster()) {
104
if ($status === PhabricatorDatabaseRef::REPLICATION_OKAY) {
105
$replica_icon = 'fa-database';
106
}
107
} else {
108
switch ($status) {
109
case PhabricatorDatabaseRef::REPLICATION_OKAY:
110
case PhabricatorDatabaseRef::REPLICATION_SLOW:
111
$delay = $database->getReplicaDelay();
112
if ($delay) {
113
$replica_label = pht('%ss Behind', new PhutilNumber($delay));
114
} else {
115
$replica_label = pht('Up to Date');
116
}
117
break;
118
}
119
}
120
}
121
122
$replication = array(
123
id(new PHUIIconView())->setIcon("{$replica_icon} {$replica_color}"),
124
' ',
125
$replica_label,
126
);
127
128
$health = $database->getHealthRecord();
129
$health_up = $health->getUpEventCount();
130
$health_down = $health->getDownEventCount();
131
132
if ($health->getIsHealthy()) {
133
$health_icon = id(new PHUIIconView())
134
->setIcon('fa-plus green');
135
} else {
136
$health_icon = id(new PHUIIconView())
137
->setIcon('fa-times red');
138
$messages[] = pht(
139
'UNHEALTHY: This database has failed recent health checks. Traffic '.
140
'will not be sent to it until it recovers.');
141
}
142
143
$health_count = pht(
144
'%s / %s',
145
new PhutilNumber($health_up),
146
new PhutilNumber($health_up + $health_down));
147
148
$health_status = array(
149
$health_icon,
150
' ',
151
$health_count,
152
);
153
154
$conn_message = $database->getConnectionMessage();
155
if ($conn_message) {
156
$messages[] = $conn_message;
157
}
158
159
$replica_message = $database->getReplicaMessage();
160
if ($replica_message) {
161
$messages[] = $replica_message;
162
}
163
164
$messages = phutil_implode_html(phutil_tag('br'), $messages);
165
166
$partition = null;
167
if ($database->getIsMaster()) {
168
if ($database->getIsDefaultPartition()) {
169
$partition = id(new PHUIIconView())
170
->setIcon('fa-circle sky')
171
->addSigil('has-tooltip')
172
->setMetadata(
173
array(
174
'tip' => pht('Default Partition'),
175
));
176
} else {
177
$map = $database->getApplicationMap();
178
if ($map) {
179
$list = implode(', ', $map);
180
} else {
181
$list = pht('Empty');
182
}
183
184
$partition = id(new PHUIIconView())
185
->setIcon('fa-adjust sky')
186
->addSigil('has-tooltip')
187
->setMetadata(
188
array(
189
'tip' => pht('Partition: %s', $list),
190
));
191
}
192
}
193
194
$rows[] = array(
195
$role_icon,
196
$partition,
197
$database->getHost(),
198
$database->getPort(),
199
$database->getUser(),
200
$connection,
201
$replication,
202
$health_status,
203
$messages,
204
);
205
}
206
207
208
$table = id(new AphrontTableView($rows))
209
->setNoDataString(
210
pht('This server is not configured in cluster mode.'))
211
->setHeaders(
212
array(
213
null,
214
null,
215
pht('Host'),
216
pht('Port'),
217
pht('User'),
218
pht('Connection'),
219
pht('Replication'),
220
pht('Health'),
221
pht('Messages'),
222
))
223
->setColumnClasses(
224
array(
225
null,
226
null,
227
null,
228
null,
229
null,
230
null,
231
null,
232
null,
233
'wide',
234
));
235
236
return $table;
237
}
238
239
}
240
241