-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolling_client.php
More file actions
233 lines (196 loc) · 6.65 KB
/
Copy pathpolling_client.php
File metadata and controls
233 lines (196 loc) · 6.65 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
<?php
declare(strict_types=1);
pathlockd_polling_demo($argv);
function pathlockd_polling_demo(array $argv): void
{
$base = getenv('PATHLOCKD_WEB_URL') ?: 'https://localhost:8443';
$client = new PathlockdClient($base, verifyTls: false);
$client->health();
echo "connected to pathlockd at $base\n";
$owner = 'php-worker-1';
$path = 'mutex:/critical-section';
$ttlMs = 30000;
$queueTtlMs = 60000;
$client->releaseAll(['ownerId' => $owner]);
$fence = acquire_with_polling($client, $owner, $path, $ttlMs, $queueTtlMs);
if ($fence === null) {
fwrite(STDERR, "acquire failed — aborting\n");
exit(1);
}
echo "[$owner] holding lock (fence=$fence)\n";
$lastRenew = time();
$renewInterval = (int)($ttlMs / 1000 / 3);
for ($i = 1; $i <= 3; $i++) {
if (time() - $lastRenew >= $renewInterval) {
$r = $client->renew(['ownerId' => $owner, 'ttlMs' => $ttlMs]);
if ($r['status'] !== 0) {
fwrite(STDERR, "[$owner] renew LOST: " . json_encode($r) . "\n");
exit(1);
}
$lastRenew = time();
echo "[$owner] renewed\n";
}
if (!$client->isOwnerAlive(['ownerId' => $owner])['alive']) {
fwrite(STDERR, "[$owner] preempted (isOwnerAlive=false) — aborting\n");
exit(1);
}
$assert = $client->assertFencing([
'ownerId' => $owner,
'fencingToken' => $fence,
'paths' => [$path],
]);
if ($assert['status'] !== 0) {
fwrite(STDERR, "[$owner] fencing failed on {$assert['path']} "
. "({$assert['reason']}) — aborting write\n");
exit(1);
}
echo "[$owner] write #$i (fence OK)\n";
sleep(1);
}
$client->release([
'ownerId' => $owner,
'requests' => [['path' => $path, 'mode' => 0]],
]);
echo "[$owner] released. polling demo complete.\n";
}
function acquire_with_polling(
PathlockdClient $client,
string $owner,
string $path,
int $ttlMs,
int $queueTtlMs,
): ?int {
echo "[$owner] acquiring $path ...\n";
$resp = $client->acquire([
'ownerId' => $owner,
'ttlMs' => $ttlMs,
'requests' => [['path' => $path, 'mode' => 0]],
'queueTtlMs' => $queueTtlMs,
]);
if ($resp['status'] === 0) {
return $resp['fencingToken'];
}
if ($resp['status'] !== 3) {
fwrite(STDERR, "[$owner] acquire failed: " . json_encode($resp) . "\n");
return null;
}
$blocker = $resp['owner'] ?? '?';
$reason = $resp['reason'] ?? '?';
echo "[$owner] QUEUED behind $blocker ($reason); polling listOwnerLocks ...\n";
$deadline = time() + (int)($queueTtlMs / 1000);
while (time() < $deadline) {
usleep(500_000);
$alive = $client->isOwnerAlive(['ownerId' => $owner]);
if (!$alive['alive']) {
fwrite(STDERR, "[$owner] preempted while queued — aborting\n");
return null;
}
$locks = $client->listOwnerLocks(['ownerId' => $owner]);
foreach ($locks['locks'] as $lock) {
if ($lock['path'] === $path) {
echo "[$owner] path appears in held set — re-issuing acquire\n";
$resp = $client->acquire([
'ownerId' => $owner,
'ttlMs' => $ttlMs,
'requests' => [['path' => $path, 'mode' => 0]],
]);
if ($resp['status'] === 0) {
return $resp['fencingToken'];
}
fwrite(STDERR, "[$owner] re-acquire failed: "
. json_encode($resp) . "\n");
return null;
}
}
}
fwrite(STDERR, "[$owner] queue TTL lapsed — abandoning\n");
return null;
}
class PathlockdClient
{
private string $baseUrl;
private bool $verifyTls;
public function __construct(string $baseUrl, bool $verifyTls = true)
{
$this->baseUrl = rtrim($baseUrl, '/');
$this->verifyTls = $verifyTls;
}
public function health(): array
{
return $this->request('GET', '/v1/health');
}
public function acquire(array $body): array
{
return $this->request('POST', '/v1/acquire', $body);
}
public function release(array $body): array
{
return $this->request('POST', '/v1/release', $body);
}
public function releaseAll(array $body): array
{
return $this->request('POST', '/v1/releaseAll', $body);
}
public function renew(array $body): array
{
return $this->request('POST', '/v1/renew', $body);
}
public function assertFencing(array $body): array
{
return $this->request('POST', '/v1/assertFencing', $body);
}
public function listOwnerLocks(array $body): array
{
return $this->request('POST', '/v1/listOwnerLocks', $body);
}
public function inspectPath(string $path): array
{
return $this->request('POST', '/v1/inspectPath', ['path' => $path]);
}
public function isOwnerAlive(array $body): array
{
return $this->request('POST', '/v1/isOwnerAlive', $body);
}
private function request(string $method, string $path, ?array $body = null): array
{
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 30,
]);
if (!$this->verifyTls) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
}
$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($err !== '') {
throw new RuntimeException("cURL error: $err");
}
$data = json_decode($raw, true) ?? [];
if ($status !== 200) {
$code = $data['error']['code'] ?? 'UNKNOWN';
$msg = $data['error']['message'] ?? '';
throw new PathlockdError($code, $msg, $status);
}
return $data;
}
}
class PathlockdError extends Exception
{
public string $errorCode;
public int $httpStatus;
public function __construct(string $code, string $message, int $httpStatus)
{
parent::__construct("$code: $message");
$this->errorCode = $code;
$this->httpStatus = $httpStatus;
}
}