Skip to content

PHPLIB-722: Ignore writeConcern option for read-only aggregations #1100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ public function __construct(string $databaseName, ?string $collectionName, array
unset($options['batchSize']);
}

/* Ignore batchSize for writes, since no documents are returned and a
* batchSize of zero could prevent the pipeline from executing. */
if ($this->isWrite) {
/* Ignore batchSize for writes, since no documents are returned and
* a batchSize of zero could prevent the pipeline from executing. */
unset($options['batchSize']);
} else {
unset($options['writeConcern']);
}

$this->databaseName = $databaseName;
Expand Down Expand Up @@ -366,16 +368,12 @@ private function executeCommand(Server $server, Command $command): Cursor
{
$options = [];

foreach (['readConcern', 'readPreference', 'session'] as $option) {
foreach (['readConcern', 'readPreference', 'session', 'writeConcern'] as $option) {
if (isset($this->options[$option])) {
$options[$option] = $this->options[$option];
}
}

if ($this->isWrite && isset($this->options['writeConcern'])) {
$options['writeConcern'] = $this->options['writeConcern'];
}

if (! $this->isWrite) {
return $server->executeReadCommand($this->databaseName, $command, $options);
}
Expand Down
80 changes: 45 additions & 35 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use function call_user_func;
use function is_scalar;
use function json_encode;
use function strchr;
use function str_contains;
use function usort;
use function version_compare;

Expand Down Expand Up @@ -434,16 +434,30 @@ public function testMapReduce(): void
public function collectionMethodClosures()
{
return [
[
'read-only aggregate' => [
Copy link
Member Author

@GromNaN GromNaN Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging is simplified when the data provider uses names. Even more so because of the filter applied in collectionReadMethodClosures and collectionWriteMethodClosures.

function ($collection, $session, $options = []): void {
$collection->aggregate(
[['$match' => ['_id' => ['$lt' => 3]]]],
['session' => $session] + $options
);
}, 'r',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pipeline is actually read-only. I added a write pipeline as @jmikola recommended.

],

/* Disabled, as write aggregations are not supported in transactions
'read-write aggregate' => [
function ($collection, $session, $options = []): void {
$collection->aggregate(
[
['$match' => ['_id' => ['$lt' => 3]]],
['$merge' => $collection . '_out'],
],
['session' => $session] + $options
);
}, 'rw',
],
*/

[
'bulkWrite insertOne' => [
function ($collection, $session, $options = []): void {
$collection->bulkWrite(
[['insertOne' => [['test' => 'foo']]]],
Expand All @@ -453,7 +467,7 @@ function ($collection, $session, $options = []): void {
],

/* Disabled, as count command can't be used in transactions
[
'count' => [
function($collection, $session, $options = []) {
$collection->count(
[],
Expand All @@ -463,7 +477,7 @@ function($collection, $session, $options = []) {
],
*/

[
'countDocuments' => [
function ($collection, $session, $options = []): void {
$collection->countDocuments(
[],
Expand All @@ -473,7 +487,7 @@ function ($collection, $session, $options = []): void {
],

/* Disabled, as it's illegal to use createIndex command in transactions
[
'createIndex' => [
function($collection, $session, $options = []) {
$collection->createIndex(
['test' => 1],
Expand All @@ -483,7 +497,7 @@ function($collection, $session, $options = []) {
],
*/

[
'deleteMany' => [
function ($collection, $session, $options = []): void {
$collection->deleteMany(
['test' => 'foo'],
Expand All @@ -492,7 +506,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'deleteOne' => [
function ($collection, $session, $options = []): void {
$collection->deleteOne(
['test' => 'foo'],
Expand All @@ -501,7 +515,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'distinct' => [
function ($collection, $session, $options = []): void {
$collection->distinct(
'_id',
Expand All @@ -512,7 +526,7 @@ function ($collection, $session, $options = []): void {
],

/* Disabled, as it's illegal to use drop command in transactions
[
'drop' => [
function($collection, $session, $options = []) {
$collection->drop(
['session' => $session] + $options
Expand All @@ -522,7 +536,7 @@ function($collection, $session, $options = []) {
*/

/* Disabled, as it's illegal to use dropIndexes command in transactions
[
'dropIndex' => [
function($collection, $session, $options = []) {
$collection->dropIndex(
'_id_1',
Expand All @@ -532,7 +546,7 @@ function($collection, $session, $options = []) {
], */

/* Disabled, as it's illegal to use dropIndexes command in transactions
[
'dropIndexes' => [
function($collection, $session, $options = []) {
$collection->dropIndexes(
['session' => $session] + $options
Expand All @@ -542,7 +556,7 @@ function($collection, $session, $options = []) {
*/

/* Disabled, as count command can't be used in transactions
[
'estimatedDocumentCount' => [
function($collection, $session, $options = []) {
$collection->estimatedDocumentCount(
['session' => $session] + $options
Expand All @@ -551,7 +565,7 @@ function($collection, $session, $options = []) {
],
*/

[
'find' => [
function ($collection, $session, $options = []): void {
$collection->find(
['test' => 'foo'],
Expand All @@ -560,7 +574,7 @@ function ($collection, $session, $options = []): void {
}, 'r',
],

[
'findOne' => [
function ($collection, $session, $options = []): void {
$collection->findOne(
['test' => 'foo'],
Expand All @@ -569,7 +583,7 @@ function ($collection, $session, $options = []): void {
}, 'r',
],

[
'findOneAndDelete' => [
function ($collection, $session, $options = []): void {
$collection->findOneAndDelete(
['test' => 'foo'],
Expand All @@ -578,7 +592,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'findOneAndReplace' => [
function ($collection, $session, $options = []): void {
$collection->findOneAndReplace(
['test' => 'foo'],
Expand All @@ -588,7 +602,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'findOneAndUpdate' => [
function ($collection, $session, $options = []): void {
$collection->findOneAndUpdate(
['test' => 'foo'],
Expand All @@ -598,7 +612,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'insertMany' => [
function ($collection, $session, $options = []): void {
$collection->insertMany(
[
Expand All @@ -610,7 +624,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'insertOne' => [
function ($collection, $session, $options = []): void {
$collection->insertOne(
['test' => 'foo'],
Expand All @@ -620,7 +634,7 @@ function ($collection, $session, $options = []): void {
],

/* Disabled, as it's illegal to use listIndexes command in transactions
[
'listIndexes' => [
function($collection, $session, $options = []) {
$collection->listIndexes(
['session' => $session] + $options
Expand All @@ -630,7 +644,7 @@ function($collection, $session, $options = []) {
*/

/* Disabled, as it's illegal to use mapReduce command in transactions
[
'mapReduce' => [
function($collection, $session, $options = []) {
$collection->mapReduce(
new \MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }'),
Expand All @@ -642,7 +656,7 @@ function($collection, $session, $options = []) {
],
*/

[
'replaceOne' => [
function ($collection, $session, $options = []): void {
$collection->replaceOne(
['test' => 'foo'],
Expand All @@ -652,7 +666,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'updateMany' => [
function ($collection, $session, $options = []): void {
$collection->updateMany(
['test' => 'foo'],
Expand All @@ -662,7 +676,7 @@ function ($collection, $session, $options = []): void {
}, 'w',
],

[
'updateOne' => [
function ($collection, $session, $options = []): void {
$collection->updateOne(
['test' => 'foo'],
Expand All @@ -673,7 +687,7 @@ function ($collection, $session, $options = []): void {
],

/* Disabled, as it's illegal to use change streams in transactions
[
'watch' => [
function($collection, $session, $options = []) {
$collection->watch(
[],
Expand All @@ -685,32 +699,28 @@ function($collection, $session, $options = []) {
];
}

public function collectionReadMethodClosures()
public function collectionReadMethodClosures(): array
{
return array_filter(
$this->collectionMethodClosures(),
function ($rw) {
if (strchr($rw[1], 'r') !== false) {
return true;
}
return str_contains($rw[1], 'r');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small refactoring to use the PHP 8 function, provided by the polyfill for older versions.

}
);
}

public function collectionWriteMethodClosures()
public function collectionWriteMethodClosures(): array
{
return array_filter(
$this->collectionMethodClosures(),
function ($rw) {
if (strchr($rw[1], 'w') !== false) {
return true;
}
return str_contains($rw[1], 'w');
}
);
}

/** @dataProvider collectionMethodClosures */
public function testMethodDoesNotInheritReadWriteConcernInTranasaction(Closure $method): void
public function testMethodDoesNotInheritReadWriteConcernInTransaction(Closure $method): void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a typo

{
$this->skipIfTransactionsAreNotSupported();

Expand Down