-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBrancherHypernodeManager.php
More file actions
356 lines (320 loc) · 13 KB
/
BrancherHypernodeManager.php
File metadata and controls
356 lines (320 loc) · 13 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
declare(strict_types=1);
namespace Hypernode\Deploy\Brancher;
use Hypernode\Api\Exception\HypernodeApiClientException;
use Hypernode\Api\Exception\HypernodeApiServerException;
use Hypernode\Api\Exception\ResponseException;
use Hypernode\Api\HypernodeClient;
use Hypernode\Api\HypernodeClientFactory;
use Hypernode\Api\Resource\Logbook\Flow;
use Hypernode\Deploy\Exception\CreateBrancherHypernodeFailedException;
use Hypernode\Deploy\Exception\TimeoutException;
use Psr\Log\LoggerInterface;
class BrancherHypernodeManager
{
/**
* Relevant flow names to poll for delivery
*
* @var string[]
*/
public const RELEVANT_FLOW_NAMES = ['ensure_app', 'ensure_copied_app'];
public const PRE_POLL_SUCCESS_COUNT = 3;
public const PRE_POLL_FAIL_COUNT = 5;
private LoggerInterface $log;
private HypernodeClient $hypernodeClient;
private SshPoller $sshPoller;
public function __construct(
LoggerInterface $log,
?HypernodeClient $hypernodeClient = null,
?SshPoller $sshPoller = null
) {
$this->log = $log;
$this->hypernodeClient = $hypernodeClient
?? HypernodeClientFactory::create(getenv('HYPERNODE_API_TOKEN') ?: '');
$this->sshPoller = $sshPoller ?? new SshPoller();
}
/**
* Query brancher instances for given Hypernode and return the Brancher instance names.
*
* @param string $hypernode The parent hypernode to query the Brancher instances from
* @param string[] $labels Labels to match against, may be empty
* @return string[] The found Brancher instance names
* @throws ResponseException
*/
public function queryBrancherHypernodes(string $hypernode, array $labels = []): array
{
$result = [];
$hypernodes = $this->hypernodeClient->app->getList([
'parent' => $hypernode,
'type' => 'brancher',
'destroyed' => 'False',
]);
foreach ($hypernodes as $brancher) {
$match = true;
foreach ($labels as $label) {
if (!in_array($label, $brancher->labels)) {
$match = false;
break;
}
}
if ($match) {
$result[] = $brancher->name;
}
}
return $result;
}
/**
* Query brancher instances for the given Hypernode and label and return the
* most recent Brancher instance name.
*
* @param string $hypernode The parent hypernode to query the Brancher instances from
* @param string[] $labels Labels to match against, may be empty
* @return string|null The found Brancher instance name, or null if none was found
*/
public function reuseExistingBrancherHypernode(string $hypernode, array $labels = []): ?string
{
try {
$brancherHypernodes = $this->queryBrancherHypernodes($hypernode, $labels);
if (count($brancherHypernodes) > 0) {
// Return the last brancher Hypernode, which is the most recently created one
return $brancherHypernodes[count($brancherHypernodes) - 1];
}
} catch (ResponseException $e) {
$this->log->error(
sprintf(
'Got an API exception (code %d) while querying for existing brancher Hypernodes for Hypernode %s with labels (%s).',
$e->getCode(),
$hypernode,
implode(', ', $labels)
)
);
}
return null;
}
/**
* Create brancher Hypernode instance for given Hypernode.
*
* @param string $hypernode Name of the Hypernode
* @param string[] $data Extra data to be applied to brancher instance
* @return string Name of the created brancher Hypernode
* @throws HypernodeApiClientException
* @throws HypernodeApiServerException
*/
public function createForHypernode(string $hypernode, array $data = []): string
{
return $this->hypernodeClient->brancherApp->create($hypernode, $data);
}
/**
* Wait for brancher Hypernode to become available.
*
* This method first attempts a quick SSH connectivity check. If the brancher is already
* reachable (e.g., when reusing an existing brancher), it returns early. Otherwise, it
* falls back to polling the API logbook for delivery status, then performs a final SSH
* reachability check.
*
* @param string $brancherHypernode Name of the brancher Hypernode
* @param int $timeout Maximum time to wait for availability
* @param int $reachabilityCheckCount Number of consecutive successful checks required
* @param int $reachabilityCheckInterval Seconds between reachability checks
* @return void
* @throws CreateBrancherHypernodeFailedException
* @throws HypernodeApiClientException
* @throws HypernodeApiServerException
* @throws TimeoutException
*/
public function waitForAvailability(
string $brancherHypernode,
int $timeout = 1500,
int $reachabilityCheckCount = 6,
int $reachabilityCheckInterval = 10
): void {
$latest = $this->sshPoller->microtime();
$timeElapsed = 0.0;
// Phase 1: SSH-first check, early return for reused delivered branchers
$this->log->info(
sprintf('Attempting SSH connectivity check for brancher Hypernode %s...', $brancherHypernode)
);
$isReachable = $this->pollSshConnectivity(
$brancherHypernode,
self::PRE_POLL_SUCCESS_COUNT,
self::PRE_POLL_FAIL_COUNT,
$reachabilityCheckInterval,
$timeElapsed,
$latest,
$timeout
);
if ($isReachable) {
$this->log->info(
sprintf('Brancher Hypernode %s is reachable!', $brancherHypernode)
);
return;
}
$this->log->info(
sprintf(
'SSH check inconclusive for brancher Hypernode %s, falling back to delivery check...',
$brancherHypernode
)
);
// Phase 2: Wait for delivery by polling the logbook
$resolved = false;
$interval = 3;
$allowedErrorWindow = 3;
$logbookStartTime = $timeElapsed;
while ($timeElapsed < $timeout) {
$now = $this->sshPoller->microtime();
$timeElapsed += $now - $latest;
$latest = $now;
try {
$flows = $this->hypernodeClient->logbook->getList($brancherHypernode);
$relevantFlows = array_filter(
$flows,
fn(Flow $flow) => in_array($flow->name, self::RELEVANT_FLOW_NAMES, true)
);
$failedFlows = array_filter($relevantFlows, fn(Flow $flow) => $flow->isReverted());
$completedFlows = array_filter($relevantFlows, fn(Flow $flow) => $flow->isComplete());
if (count($relevantFlows) > 0 && count($failedFlows) === count($relevantFlows)) {
throw new CreateBrancherHypernodeFailedException();
}
if ($relevantFlows && count($completedFlows) === count($relevantFlows)) {
$resolved = true;
break;
}
} catch (HypernodeApiClientException $e) {
// A 404 not found means there are no flows in the logbook yet, we should wait.
// Otherwise, there's an error, and it should be propagated.
if ($e->getCode() !== 404) {
throw $e;
} elseif (($timeElapsed - $logbookStartTime) < $allowedErrorWindow) {
// Sometimes we get an error where the logbook is not yet available, but it will be soon.
// We allow a small window for this to happen, and then we continue polling.
$this->log->info(
sprintf(
'Got an expected exception during the allowed error window of HTTP code %d, waiting for %s to become available.',
$e->getCode(),
$brancherHypernode
)
);
}
}
$this->sshPoller->sleep($interval);
}
if (!$resolved) {
throw new TimeoutException(
sprintf('Timed out waiting for brancher Hypernode %s to be delivered', $brancherHypernode)
);
}
$this->log->info(
sprintf(
'Brancher Hypernode %s was delivered. Now waiting for node to become reachable...',
$brancherHypernode
)
);
// Phase 3: Final SSH reachability check
$isReachable = $this->pollSshConnectivity(
$brancherHypernode,
$reachabilityCheckCount,
0, // No max failures, rely on timeout
$reachabilityCheckInterval,
$timeElapsed,
$latest,
$timeout
);
if (!$isReachable) {
throw new TimeoutException(
sprintf('Timed out waiting for brancher Hypernode %s to become reachable', $brancherHypernode)
);
}
$this->log->info(
sprintf('Brancher Hypernode %s became reachable!', $brancherHypernode)
);
}
/**
* Poll SSH connectivity until we get enough consecutive successes or hit a limit.
*
* @param string $brancherHypernode Hostname to check
* @param int $requiredConsecutiveSuccesses Number of consecutive successes required
* @param int $maxFailedAttempts Maximum failed attempts before giving up (0 = no limit, use timeout only)
* @param int $checkInterval Seconds between checks
* @param float $timeElapsed Reference to track elapsed time
* @param float $latest Reference to track latest timestamp
* @param int $timeout Maximum time allowed
* @return bool True if SSH check succeeded, false if we should fall back to other methods
*/
private function pollSshConnectivity(
string $brancherHypernode,
int $requiredConsecutiveSuccesses,
int $maxFailedAttempts,
int $checkInterval,
float &$timeElapsed,
float &$latest,
int $timeout
): bool {
$consecutiveSuccesses = 0;
$failedAttempts = 0;
while ($timeElapsed < $timeout) {
$now = $this->sshPoller->microtime();
$timeElapsed += $now - $latest;
$latest = $now;
// Check if we've hit the max failed attempts limit (0 = unlimited)
if ($maxFailedAttempts > 0 && $failedAttempts >= $maxFailedAttempts) {
return false;
}
if ($this->sshPoller->poll($brancherHypernode)) {
$consecutiveSuccesses++;
$this->log->info(
sprintf(
'Brancher Hypernode %s reachability check %d/%d succeeded.',
$brancherHypernode,
$consecutiveSuccesses,
$requiredConsecutiveSuccesses
)
);
if ($consecutiveSuccesses >= $requiredConsecutiveSuccesses) {
return true;
}
} else {
if ($consecutiveSuccesses > 0) {
$this->log->info(
sprintf(
'Brancher Hypernode %s reachability check failed, resetting counter (was at %d/%d).',
$brancherHypernode,
$consecutiveSuccesses,
$requiredConsecutiveSuccesses
)
);
}
$consecutiveSuccesses = 0;
$failedAttempts++;
}
$this->sshPoller->sleep($checkInterval);
}
return false;
}
/**
* Cancel one or multiple brancher Hypernodes.
*
* @param string ...$brancherHypernodes Name(s) of the brancher Hypernode(s)
* @throws HypernodeApiClientException
* @throws HypernodeApiServerException
*/
public function cancel(string ...$brancherHypernodes): void
{
foreach ($brancherHypernodes as $brancherHypernode) {
$this->log->info(sprintf('Stopping brancher Hypernode %s...', $brancherHypernode));
try {
$this->hypernodeClient->brancherApp->cancel($brancherHypernode);
} catch (HypernodeApiClientException $e) {
// If the brancher is already cancelled or not found, that's fine -
// our goal was to cancel it anyway
if ($e->getCode() === 404 || str_contains($e->getMessage(), 'has already been cancelled')) {
$this->log->info(sprintf(
'Brancher Hypernode %s was already cancelled or not found, skipping.',
$brancherHypernode
));
continue;
}
throw $e;
}
}
}
}