-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathSession.php
More file actions
482 lines (416 loc) · 17.6 KB
/
Copy pathSession.php
File metadata and controls
482 lines (416 loc) · 17.6 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\Bolt;
use Bolt\error\ConnectException as BoltConnectException;
use Exception;
use Laudis\Neo4j\Common\GeneratorHelper;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Contracts\ConnectionInterface;
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
use Laudis\Neo4j\Contracts\SessionInterface;
use Laudis\Neo4j\Contracts\TransactionInterface;
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
use Laudis\Neo4j\Databags\Bookmark;
use Laudis\Neo4j\Databags\BookmarkHolder;
use Laudis\Neo4j\Databags\Neo4jError;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\Databags\Statement;
use Laudis\Neo4j\Databags\SummarizedResult;
use Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Enum\AccessMode;
use Laudis\Neo4j\Exception\Neo4jException;
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
use Laudis\Neo4j\Neo4j\Neo4jConnectionPool;
use Laudis\Neo4j\NoOpBookmarkManager;
use Laudis\Neo4j\Types\CypherList;
use Psr\Log\LogLevel;
use Throwable;
/**
* A session using bolt connections.
*/
final class Session implements SessionInterface
{
private const ROLLBACK_CLASSIFICATIONS = ['ClientError', 'DatabaseError'];
/** @var list<BoltConnection> */
private array $usedConnections = [];
private readonly BookmarkHolder $bookmarkHolder;
private readonly SessionBookmarkTracker $bookmarkTracker;
private readonly SessionConfiguration $sessionConfig;
/**
* @param ConnectionPool|Neo4jConnectionPool $pool
*/
public function __construct(
SessionConfiguration $config,
private readonly ConnectionPoolInterface $pool,
/**
* @psalm-readonly
*/
private readonly SummarizedResultFormatter $formatter,
) {
$bookmarkManager = $config->getBookmarkManager() ?? NoOpBookmarkManager::instance();
$this->bookmarkHolder = new BookmarkHolder(Bookmark::from($config->getBookmarks()));
$this->bookmarkTracker = new SessionBookmarkTracker(
$this->bookmarkHolder,
$bookmarkManager,
$config->getBookmarks(),
);
$this->bookmarkHolder->onServerBookmark($this->bookmarkTracker->handleNewBookmark(...));
$this->sessionConfig = $config->withRoutingBookmarks($this->bookmarkTracker->getRediscoveryBookmarkValues());
}
/**
* @param iterable<Statement> $statements
*
* @return CypherList<SummarizedResult>
*/
public function runStatements(iterable $statements, ?TransactionConfiguration $config = null): CypherList
{
$tbr = [];
$this->getLogger()?->log(LogLevel::INFO, 'Running statements', ['statements' => $statements]);
$config = $this->mergeTsxConfig($config);
foreach ($statements as $statement) {
$tbr[] = $this->executeStatementWithRetry($statement, $config);
}
return new CypherList($tbr);
}
/**
* @param iterable<Statement>|null $statements
*/
public function openTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
{
return $this->beginTransaction($statements, $this->mergeTsxConfig($config));
}
public function runStatement(Statement $statement, ?TransactionConfiguration $config = null): SummarizedResult
{
return $this->runStatements([$statement], $config)->first();
}
public function run(string $statement, iterable $parameters = [], ?TransactionConfiguration $config = null): SummarizedResult
{
return $this->runStatement(new Statement($statement, $parameters), $config);
}
/**
* @param array<string, mixed> $parameters
*/
public function executeQuery(string $cypher, array $parameters = []): SummarizedResult
{
$statement = new Statement($cypher, $parameters);
return $this->retry(
static function (TransactionInterface $tx) use ($statement): SummarizedResult {
$result = $tx->runStatement($statement);
$result->preload();
return $result;
},
$this->mergeTsxConfig(null),
$this->sessionConfig,
TelemetryAPIEnum::EXECUTE_QUERY,
);
}
public function writeTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
{
$this->getLogger()?->log(LogLevel::INFO, 'Beginning write transaction', ['config' => $config]);
$config = $this->mergeTsxConfig($config);
return $this->retry(
$tsxHandler,
$config,
$this->sessionConfig->withAccessMode(AccessMode::WRITE()),
TelemetryAPIEnum::TRANSACTION_FUNCTION
);
}
public function readTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
{
$this->getLogger()?->log(LogLevel::INFO, 'Beginning read transaction', ['config' => $config]);
$config = $this->mergeTsxConfig($config);
return $this->retry(
$tsxHandler,
$config,
$this->sessionConfig->withAccessMode(AccessMode::READ()),
TelemetryAPIEnum::TRANSACTION_FUNCTION
);
}
/**
* @template U
*
* @param callable(TransactionInterface):U $tsxHandler
*
* @return U
*/
private function retry(
callable $tsxHandler,
TransactionConfiguration $config,
SessionConfiguration $sessionConfig,
TelemetryAPIEnum $telemetryApi,
) {
$transaction = null;
$maxRetries = 3;
$error = null;
while ($maxRetries > 0) {
--$maxRetries;
try {
if ($transaction === null) {
$transaction = $this->startTransaction($config, $sessionConfig, $telemetryApi);
}
$tbr = $tsxHandler($transaction);
$transaction->commit();
return $tbr;
} catch (Neo4jException $e) {
if ($e->getClassification() === 'TransientError' && $transaction instanceof BoltUnmanagedTransaction) {
usleep(100_000); // transient errors mean we have to retry later. For now we wait 100 ms, later we'll make this configurable, non-blocking
$error = $e;
continue;
}
$transaction = null;
if ($e->getTitle() === 'NotALeader' || $e->getNeo4jCode() === 'Neo.ClientError.Cluster.NotALeader') {
if ($this->pool instanceof Neo4jConnectionPool) {
$this->pool->clearRoutingTable($this->sessionConfig);
}
$error = $e;
continue;
}
throw $e;
} catch (Throwable $e) {
if ($this->isConnectionError($e)) {
$transaction = null;
$error = $e;
continue;
}
throw $e;
}
}
throw new Exception('Failed to execute transaction', 0, $error);
}
/**
* Check if the exception is a connection-related error (network/socket/timeout).
* NotALeader is a routing error, not a connection error - the connection is fine.
*/
private function isConnectionError(Throwable $e): bool
{
if ($e instanceof BoltConnectException) {
return true;
}
$message = strtolower($e->getMessage());
return str_contains($message, 'interrupted system call')
|| str_contains($message, 'broken pipe')
|| str_contains($message, 'connection reset')
|| str_contains($message, 'connection timeout')
|| str_contains($message, 'connection closed')
|| str_contains($message, 'connection refused')
|| str_contains($message, 'i/o error');
}
/**
* Check if the exception should trigger a routing table clear.
*/
private function shouldClearRoutingTable(Neo4jException $e): bool
{
$message = strtolower($e->getMessage());
$title = $e->getTitle();
return str_contains($message, 'interrupted system call')
|| str_contains($message, 'broken pipe')
|| str_contains($message, 'connection reset')
|| str_contains($message, 'connection timeout')
|| str_contains($message, 'connection closed')
|| $e->getNeo4jCode() === 'Neo.ClientError.Cluster.NotALeader'
|| $title === 'NotALeader';
}
/**
* Execute a statement with automatic retry on connection errors.
* Retries up to 3 times on connection failures, clearing routing table between attempts.
*
* @param Statement $statement The statement to execute
* @param TransactionConfiguration $config Transaction configuration
*
* @return SummarizedResult The result of the statement
*/
/**
* Execute instant transaction (session.run) with automatic retry on connection/routing errors.
*
* PURPOSE:
* - Handles transient failures transparently to user: connection timeouts, server unavailable, etc.
* - Supports cluster failover: when server goes down, clears routing table and retries on different node
* - Distinguishes errors: retries on connection/routing issues but fails immediately on client errors (syntax, auth)
* - Improves reliability: 3 retry attempts with fresh routing table each time = high availability
*
* EXAMPLE: User calls session.run("CREATE (n)") during cluster failover:
* Attempt 1: Node A is leader → "NotALeader" (stepping down) → Clear routing table
* Attempt 2: Node B elected leader → "Connection timeout" (election in progress) → Retry
* Attempt 3: Cluster stable → Query succeeds
* User sees: Query succeeded transparently (no exception, no manual retry needed)
*
* WHY THIS IS CRITICAL FOR DRIVERS:
* - All Neo4j drivers (Java, Python, JavaScript) have this pattern
* - Without it: user must manually retry or wrap every session.run() in try-catch
* - With it: driver handles recovery automatically = better UX and reliability
*/
private function executeStatementWithRetry(Statement $statement, TransactionConfiguration $config): SummarizedResult
{ // Retry instant transactions up to 3 times on connection/routing errors; catch distinguishes retryable errors from client errors (syntax, auth) and clears routing table for cluster failover.
$maxRetries = 3;
$retries = 0;
while ($retries < $maxRetries) {
try {
return $this->beginInstantTransaction($this->sessionConfig, $config)->runStatement($statement);
} catch (Neo4jException $e) {
if (!$this->shouldClearRoutingTable($e)) {
throw $e;
}
$this->handleStatementRetry($retries, $maxRetries, $e);
} catch (Throwable $e) {
if (!$this->isConnectionError($e)) {
throw $e;
}
$this->handleStatementRetry($retries, $maxRetries, $e);
}
}
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.General', 'Statement execution failed after maximum retries')]);
}
/**
* Handle retry logic for statement execution - clear routing and increment counter.
* Throws the exception if max retries exceeded.
*/
private function handleStatementRetry(int &$retries, int $maxRetries, Throwable $e): void
{
$this->getLogger()?->log(LogLevel::WARNING, 'Connection error in instant transaction, retrying', [
'error' => $e->getMessage(),
'retry' => $retries + 1,
]);
$this->pool->close();
++$retries;
if ($retries >= $maxRetries) {
throw $e;
}
}
public function transaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
{
return $this->writeTransaction($tsxHandler, $config);
}
/**
* @param iterable<Statement> $statements
*/
public function beginTransaction(?iterable $statements = null, ?TransactionConfiguration $config = null): UnmanagedTransactionInterface
{
$this->getLogger()?->log(LogLevel::INFO, 'Beginning transaction', ['statements' => $statements, 'config' => $config]);
$config = $this->mergeTsxConfig($config);
$tsx = $this->startTransaction($config, $this->sessionConfig, TelemetryAPIEnum::UNMANAGED_TRANSACTION);
$tsx->runStatements($statements ?? []);
return $tsx;
}
public function beginReadTransaction(?TransactionConfiguration $config = null): UnmanagedTransactionInterface
{
$config = $this->mergeTsxConfig($config);
return $this->startTransaction($config, $this->sessionConfig->withAccessMode(AccessMode::READ()), TelemetryAPIEnum::TRANSACTION_FUNCTION);
}
public function beginWriteTransaction(?TransactionConfiguration $config = null): UnmanagedTransactionInterface
{
$config = $this->mergeTsxConfig($config);
return $this->startTransaction($config, $this->sessionConfig->withAccessMode(AccessMode::WRITE()), TelemetryAPIEnum::TRANSACTION_FUNCTION);
}
/**
* @return UnmanagedTransactionInterface
*/
private function beginInstantTransaction(
SessionConfiguration $config,
TransactionConfiguration $tsxConfig,
): TransactionInterface {
$this->getLogger()?->log(LogLevel::INFO, 'Starting instant transaction', ['config' => $tsxConfig]);
$connection = $this->acquireConnection($tsxConfig, $config);
/** @var ConnectionPoolInterface<ConnectionInterface>|null $pool */
$pool = $this->pool;
return new BoltUnmanagedTransaction(
$this->sessionConfig->getDatabase(),
$this->formatter,
$connection,
$this->sessionConfig,
$tsxConfig,
$this->bookmarkHolder,
new BoltMessageFactory($connection, $this->getLogger()),
true,
TelemetryAPIEnum::SESSION_RUN,
$pool,
false,
$this->bookmarkTracker,
);
}
/**
* @throws Exception
*/
private function acquireConnection(TransactionConfiguration $config, SessionConfiguration $sessionConfig): BoltConnection
{
$this->getLogger()?->log(LogLevel::INFO, 'Acquiring connection', ['config' => $config, 'sessionConfig' => $sessionConfig]);
$connectionGenerator = $this->pool->acquire($sessionConfig);
/**
* @var BoltConnection $connection
*
* @psalm-suppress UnnecessaryVarAnnotation
*/
$connection = GeneratorHelper::getReturnFromGenerator($connectionGenerator);
// We try and let the server do the timeout management.
// Since the client should not run indefinitely, we just add the client side by two, just in case
$timeout = $config->getTimeout();
if ($timeout !== null) {
$timeout = ($timeout < 30) ? 30 : $timeout;
$connection->setTimeout($timeout + 2);
}
$this->usedConnections[] = $connection;
return $connection;
}
private function startTransaction(TransactionConfiguration $config, SessionConfiguration $sessionConfig, TelemetryAPIEnum $telemetryApi): UnmanagedTransactionInterface
{
$this->getLogger()?->log(LogLevel::INFO, 'Starting transaction', ['config' => $config, 'sessionConfig' => $sessionConfig]);
$connection = $this->acquireConnection($config, $sessionConfig);
// Defer BEGIN to first run/commit/rollback. The driver does not support OPT_EAGER_TX_BEGIN,
// so BEGIN is sent on first run/commit/rollback. This matches test_disconnect_on_tx_begin,
// which expects the error at "after run" when the stub disconnects on BEGIN.
/** @var ConnectionPoolInterface<ConnectionInterface>|null $pool */
$pool = $this->pool;
return new BoltUnmanagedTransaction(
$this->sessionConfig->getDatabase(),
$this->formatter,
$connection,
$this->sessionConfig,
$config,
$this->bookmarkHolder,
new BoltMessageFactory($connection, $this->getLogger()),
false,
$telemetryApi,
$pool,
false,
$this->bookmarkTracker,
);
}
/**
* Clean up a connection that failed during BEGIN or other initialization.
* Resets the connection if it's in FAILED state and releases it back to the pool.
*/
private function cleanupFailedConnection(BoltConnection $connection): void
{
if ($connection->getServerState() === 'FAILED') {
$connection->reset();
}
// Release connection back to pool for reuse
$this->pool->release($connection);
}
private function mergeTsxConfig(?TransactionConfiguration $config): TransactionConfiguration
{
return TransactionConfiguration::default()->merge($config);
}
public function getLastBookmark(): Bookmark
{
return $this->bookmarkHolder->getBookmark();
}
public function close(): void
{
foreach ($this->usedConnections as $connection) {
$connection->discardUnconsumedResults();
}
$this->usedConnections = [];
}
private function getLogger(): ?Neo4jLogger
{
return $this->pool->getLogger();
}
}