Skip to content

PHPC-1411: Accept 64-bit integers for wTimeoutMS #1042

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
Dec 19, 2019

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Oct 11, 2019

@alcaeus alcaeus requested a review from jmikola October 11, 2019 10:36
@alcaeus alcaeus self-assigned this Oct 11, 2019
@alcaeus
Copy link
Member Author

alcaeus commented Nov 13, 2019

Updated now that CDRIVER-3396 has been fixed. Contains the single commit from #1060 until that PR is merged.

@alcaeus alcaeus force-pushed the phpc-1411 branch 2 times, most recently from 9d9dd51 to f1f6cb1 Compare November 25, 2019 15:28
@alcaeus alcaeus requested a review from jmikola November 25, 2019 17:55
@alcaeus alcaeus force-pushed the phpc-1411 branch 7 times, most recently from dabe6a4 to a9861a0 Compare December 3, 2019 14:04
@alcaeus alcaeus requested a review from jmikola December 3, 2019 14:25
@alcaeus
Copy link
Member Author

alcaeus commented Dec 3, 2019

@jmikola: updated the logic again after our discussion yesterday.

I've added a few new symbols:

  • The php_phongo_int64_to_zval function converts an int64_t to a zval, either as string or as a MongoDB\BSON\Int64 instance
  • The php_phongo_parse_int64 parses a string as 64-bit int; this was mainly done to avoid duplicating that logic even more
  • The ADD_ASSOC_INT64_COMPAT macro wraps a php_phongo_int64_to_zval and ADD_ASSOC_ZVAL call to avoid adding more compiler conditionals for PHP 5.

The wTimeout of a WriteConcern object is now always serialised as string and can also be unserialised as such. This makes sure that the timeout is truly portable between 32-bit and 64-bit architectures. bsonSerialize continues to return an Int64 object for the timeout.

@@ -358,7 +368,7 @@ static HashTable* php_phongo_write_concern_get_properties_hash(zval* object, boo
if (wtimeout != 0) {
zval z_wtimeout;

ZVAL_LONG(&z_wtimeout, wtimeout);
php_phongo_int64_to_zval(wtimeout, &z_wtimeout, is_bson TSRMLS_CC);
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to always call php_phongo_int64_to_zval here? php_phongo_write_concern_get_properties_hash is called from three places:

  • get_debug_info handler
  • get_properties handler
  • bsonSerialize() method

We know that bsonSerialize() needs to conditionally use an Int64 object if the value exceeds the range of a 32-bit integer. If wtimeout is within the range of a 32-bit integer, we can add it as an integer primitive.

The debug and properties handlers need not ensure compatibility between 32-bit and 64-bit architectures (unlike serialize(), so I think they'd do well to also encode as an integer whenever possible and only fall back to a string on a 32-bit architecture when wtimeout exceeds the range of a 32-bit integer.

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea was to always export as a string, since code might be serialised using var_export on a 64-bit architecture and then read on a 32-bit architecture. In this case, we'd export as an int, but then be unable to read it. That was the main reason for always treating it as if we were on a 32-bit platform.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I wasn't too concerned about var_export being used across platforms since it actually produces code that the user would probably manually edit (unless they're using eval()). I was thinking of serialize() as the more likely way folks might transport between platforms.

In my experience (from GitHub issues), most people using var_export are doing so as an alternative to var_dump and didn't actually rely on __set_state() to rebuild an object.

I'm happy to have var_export produce a string as needed, though.

@jmikola
Copy link
Member

jmikola commented Dec 5, 2019

Note: I will follow up with a PR against your branch when I get a chance.

@jmikola
Copy link
Member

jmikola commented Dec 17, 2019

@alcaeus: Opened alcaeus#2 with proposed changes. This allowed me to revert most of your test changes here, but I also added some new tests.

@alcaeus
Copy link
Member Author

alcaeus commented Dec 18, 2019

Thanks Jeremy! I've added your changes to this PR, so I think this is good for another review 🎉

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

One suggestion for something I overlooked in the PR to your branch.

LGTM!

alcaeus and others added 4 commits December 19, 2019 06:50
For serialization, 64-bit wtimeout values will always be encoded as strings for portability. Debug output and bsonSerialize() can always use integers on 64-bit platforms and can fall back to strings and Int64 objects on 32-bit platforms, respectively.
@alcaeus alcaeus merged commit 366dcbe into mongodb:master Dec 19, 2019
@alcaeus alcaeus deleted the phpc-1411 branch December 19, 2019 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants