Skip to content
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

PHPORM-56 Replace Collection proxy class with Driver monitoring #3137

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 5, 2024

Fix PHPORM-56

Use MongoDB driver monitoring feature to log all commands.

Benefit: any 3rd party class that accepts a MongoDB\Collection instance can be used and still having query logs in Laravel.

Upgrade guide:

  • Change type hints from MongoDB\Laravel\Collection to MongoDB\Collection.
- use MongoDB\Laravel\Collection;
+ use MongoDB\Collection;

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file

@github-actions github-actions bot added the docs label Sep 5, 2024
@GromNaN GromNaN force-pushed the PHPORM-56 branch 3 times, most recently from 6db1052 to 0f2f584 Compare September 5, 2024 16:04
@GromNaN GromNaN marked this pull request as ready for review September 5, 2024 16:05
@GromNaN GromNaN requested review from a team as code owners September 5, 2024 16:05
@GromNaN GromNaN force-pushed the PHPORM-56 branch 2 times, most recently from a187abc to 2b02660 Compare September 5, 2024 16:13
@@ -30,7 +30,7 @@
"illuminate/database": "^11",
"illuminate/events": "^11",
"illuminate/support": "^11",
"mongodb/mongodb": "^1.15"
"mongodb/mongodb": "^1.18"
Copy link
Member Author

Choose a reason for hiding this comment

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

@GromNaN GromNaN added this to the 5.0 milestone Sep 6, 2024
tests/ConnectionTest.php Outdated Show resolved Hide resolved
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
* Remove `MongoFailedJobProvider`, replaced by Laravel `DatabaseFailedJobProvider` by @GromNaN in [#3122](https://github.com/mongodb/laravel-mongodb/pull/3122)
* Remove custom `PasswordResetServiceProvider`, use the default `DatabaseTokenRepository` by @GromNaN in [#3124](https://github.com/mongodb/laravel-mongodb/pull/3124)
* Remove `Blueprint::background()` method by @GromNaN in [#3132](https://github.com/mongodb/laravel-mongodb/pull/3132)
* Replace `Collection` proxy class with Driver monitoring by @GromNaN in [#3137]((https://github.com/mongodb/laravel-mongodb/pull/3137)
Copy link
Member

Choose a reason for hiding this comment

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

Might apply to other BC breaks as well, but do we want to leverage deprecations in 1.x to announce the break?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can mark the class as deprecated in 1.x. Unless we add an option to switch the monitoring feature from the proxy class to the monitoring events, there will be no way to disable the deprecation.

Copy link
Member

Choose a reason for hiding this comment

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

Got it - not sure we need to do this, just wanted to check 👍


$command = [];
foreach (get_object_vars($startedEvent->getCommand()) as $key => $value) {
if ($key[0] !== '$' && ! in_array($key, ['lsid', 'txnNumber'])) {
Copy link
Member

Choose a reason for hiding this comment

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

I never checked the performance of this, but why not use str_starts_with?

Copy link
Member Author

@GromNaN GromNaN Sep 6, 2024

Choose a reason for hiding this comment

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

The array key is never empty, and I need to check only 1 char. This skips 1 method call.

public function benchStrStartsWith()
{
    $key = '$db';
    return str_starts_with($key, '$');
}

public function benchStrictCmp()
{
    $key = '$db';
    return $key[0] === '$';
}
benchStrStartsWith......................I2 - Mo0.185μs (±0.26%)
benchStrictCmp..........................I2 - Mo0.135μs (±0.60%)

Co-authored-by: Andreas Braun <git@alcaeus.org>
@GromNaN GromNaN removed the request for review from jmikola September 6, 2024 09:37
@GromNaN GromNaN merged commit 74f219c into mongodb:5.0 Sep 6, 2024
15 checks passed
@GromNaN GromNaN deleted the PHPORM-56 branch September 6, 2024 12:35
@@ -198,6 +203,8 @@ public function ping(): void
/** @inheritdoc */
public function disconnect()
{
$this->connection?->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
Copy link
Member

Choose a reason for hiding this comment

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

Unregistering the subscriber seems correct, but I'm more generally curious how the disconnect() method is used in Laravel. Wouldn't it be misleading to users given that PHPC persists the connections by default?

I assume Laravel never uses the disableClientPersistence driver option.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm just making sure that the objects are deleted without requiring a garbage collector cycle.

Copy link
Member

Choose a reason for hiding this comment

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

I understand why you made the additions to this method. It was a more general question about what the purpose of disconnect() was for the Laravel MongoDB library. Is it something we're obligated to implement for Eloquent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants