Skip to content

Commit 3f3a273

Browse files
committed
Add retry for qeury execution after catch of not primary execption or interrupted execption durin primary switch process
1 parent 0b03010 commit 3f3a273

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

Dockerfile

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG PHP_VERSION=8.0
2-
ARG COMPOSER_VERSION=2.0
2+
ARG COMPOSER_VERSION=2.5.4
33

44
FROM composer:${COMPOSER_VERSION}
55
FROM php:${PHP_VERSION}-cli
@@ -13,3 +13,11 @@ RUN apt-get update && \
1313
COPY --from=composer /usr/bin/composer /usr/local/bin/composer
1414

1515
WORKDIR /code
16+
17+
COPY composer.* ./
18+
19+
COPY ./ ./
20+
21+
RUN composer install
22+
23+
CMD ["./vendor/bin/phpunit"]

docker-compose.yml

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,29 @@ version: '3'
33
services:
44
tests:
55
container_name: tests
6+
tty: true
67
build:
78
context: .
89
dockerfile: Dockerfile
910
volumes:
1011
- .:/code
1112
working_dir: /code
1213
depends_on:
13-
- mongodb
14-
- mysql
14+
- mongodb
15+
- mysql
1516

1617
mysql:
1718
container_name: mysql
18-
image: mysql:5.7
19+
image: mysql:8.0
1920
ports:
20-
- 3306:3306
21+
- "3306:3306"
2122
environment:
2223
MYSQL_ROOT_PASSWORD:
2324
MYSQL_DATABASE: unittest
2425
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
25-
logging:
26-
driver: none
2726

2827
mongodb:
2928
container_name: mongodb
30-
image: mongo
29+
image: mongo:latest
3130
ports:
32-
- 27017:27017
33-
logging:
34-
driver: none
31+
- "27017:27017"

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</testsuite>
3939
</testsuites>
4040
<php>
41-
<env name="MONGODB_URI" value="mongodb://127.0.0.1/" />
41+
<env name="MONGODB_URI" value="mongodb://mongodb:27017/"/>
4242
<env name="MONGODB_DATABASE" value="unittest"/>
4343
<env name="MYSQL_HOST" value="mysql"/>
4444
<env name="MYSQL_PORT" value="3306"/>

src/Collection.php

+27-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jenssegers\Mongodb;
44

55
use Exception;
6+
use Illuminate\Support\Facades\Log;
67
use MongoDB\BSON\ObjectID;
78
use MongoDB\Collection as MongoCollection;
89

@@ -23,8 +24,8 @@ class Collection
2324
protected $collection;
2425

2526
/**
26-
* @param Connection $connection
27-
* @param MongoCollection $collection
27+
* @param Connection $connection
28+
* @param MongoCollection $collection
2829
*/
2930
public function __construct(Connection $connection, MongoCollection $collection)
3031
{
@@ -35,14 +36,33 @@ public function __construct(Connection $connection, MongoCollection $collection)
3536
/**
3637
* Handle dynamic method calls.
3738
*
38-
* @param string $method
39-
* @param array $parameters
39+
* @param string $method
40+
* @param array $parameters
4041
* @return mixed
4142
*/
4243
public function __call(string $method, array $parameters)
4344
{
4445
$start = microtime(true);
45-
$result = $this->collection->$method(...$parameters);
46+
try {
47+
$result = $this->collection->$method(...$parameters);
48+
} catch (Exception $e) {
49+
// 11602 - operation was interrupted
50+
// 10107 - not primary
51+
if ($e->getCode() == 11602 || $e->getCode() == 10107) {
52+
Log::info(
53+
'Query execution was interrupted and will be retried',
54+
['code' => $e->getCode(), 'mess' => $e->getMessage(), 'params' => $parameters]
55+
);
56+
} else {
57+
Log::info('Unexpected error', [
58+
'mess' => $e->getMessage(),
59+
'code' => $e->getCode(),
60+
'file' => $e->getFile(),
61+
'line' => $e->getLine(),
62+
]);
63+
}
64+
$result = $this->collection->$method(...$parameters);
65+
}
4666

4767
// Once we have run the query we will calculate the time that it took to run and
4868
// then log the query, bindings, and execution time so we will report them on
@@ -54,7 +74,7 @@ public function __call(string $method, array $parameters)
5474
// Convert the query parameters to a json string.
5575
array_walk_recursive($parameters, function (&$item, $key) {
5676
if ($item instanceof ObjectID) {
57-
$item = (string) $item;
77+
$item = (string)$item;
5878
}
5979
});
6080

@@ -67,7 +87,7 @@ public function __call(string $method, array $parameters)
6787
}
6888
}
6989

70-
$queryString = $this->collection->getCollectionName().'.'.$method.'('.implode(',', $query).')';
90+
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
7191

7292
$this->connection->logQuery($queryString, [], $time);
7393

0 commit comments

Comments
 (0)