Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Cache] Add DSN, createClient & better error reporting to MemcachedAdapter#21108
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[Cache] Add DSN, createClient & better error reporting to MemcachedAdapter#21108
Uh oh!
There was an error while loading.Please reload this page.
Conversation
70e509a to6d35624Compare95ad534 to6e68d2cCompare| } | ||
| if ($client->getOption(\Memcached::OPT_NO_BLOCK)) { | ||
| thrownewCacheException('MemcachedAdapter: OPT_NO_BLOCK must be disabled.'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I understand the serializer requirement, but why are weforcing binary protocol and no_block=false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Binary because we must support all chars in keys especially spaces, per psr-6.
No block because we need guarantees when reading/writing to the cache. Dealing with async would make the code complex for no benefit.
robfrawleyDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Regardingbinary_protocol requirement: I'm on board with this.
But, regardingno_block requirement: I disagree. The intended use of this option doesn't require any additional code, and, in fact, writing such code would invalidate the entire intended usage and benefits ofno_block. The expectation when it is enabled is that the userdoesn't need guarantees when writing. It is acommonly used option and generally doesn't change expectations; due to the design of memcached, one can never assume a value exists as it may have been purged at any time by the LRU algorithm, and will definitely not exist after a restart of the daemon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
fixed for no_block
| unset($options['persistent_id'],$options['username'],$options['password']); | ||
| // set client's options | ||
| foreach ($optionsas$name =>$value) { |
robfrawleyDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This needs to beforeach (array_reverse($options) as $name => $value) as seen in my PR, otherwise defaul values that affect multiple options (such aslibketama_compatible) will overwrite use-provided values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think reverse or not, it's the responsibility of the user to order correctly. I'd better behave the same as the native setOptions so that expectations don't have to be reversed when using our lib...
robfrawleyDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It's due to our code, though. The default option oflibketama_compatible in combination with$options += static::$defaultClientOptions; means a user cannot change, for example, the hash algorithm, ever...as our default oflibketama_compatible will be appliedafter the user-provided option (for example['hash' => 'something-else']), resetting the hash tomd5. The options need to be applied in reverse order to allow the user to overwrite our defaults.
robfrawleyDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
To clarify: the user should, rightfully, assume that any options they provide will overwrite our defaults. The issue is,some options actually affect multiple options. Without reversing the array, the user options will not always result in a proper assignment; while reversing the options ensures user options always overwrite defaults.
It's not theorder of options that is important, it's the order ofdefault options touser options that is important.Default options must be applied first, so either the assignment needs to change:
# from this:$options +=static::$defaultClientOptions;# to this (or something similar):foreach (static::$defaultClientOptionsas$name =>$option) {if (!array_key_exists($name,$options) {array_unshift($options, [$name =>$option]); }}
OR the order of options needs to be reversed:
# from this:foreach ($optionsas$name =>$value) {}# to this (or something similar):foreach (array_reverse($options)as$name =>$value) {}
Otherwise options will behave unexpectedly for the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
understood, fixed also
| } | ||
| // set client's servers, taking care of persistent connections | ||
| if (!$client->isPristine()) { |
robfrawleyDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We shouldn't add servers after a connection is open (even when the user has added an additional server to their configuration)?
nicolas-grekasDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Not sure to get what you mean. Do you see anything wrong with this logic?
Here it is: if the connection is not pristine (meaning it's a 2nd use persistent connection), then we check if there is any change in the list of server. If there is, we reset the list and set the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Ok. Had trouble following the code, but makes sense now.
robfrawley commentedDec 30, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
A documentation PR for#20863 is already open (see issuesymfony/symfony-docs#7256 and PRsymfony/symfony-docs#7265) and it doesn't require much at all to update it for this PR. I will ensure it is accurate after the implementation details I've asked about are handled/clarified. Thanks! |
d2565af to9e899a4Comparerobfrawley commentedDec 30, 2016
I'd add a test to cover the serialize requirement: /** * @expectedException \Symfony\Component\Cache\Exception\CacheException * @expectedExceptionMessage MemcachedAdapter: PHP or IGBINARY must be used. */publicfunctiontestOptionSerializer() {newMemcachedAdapter(MemcachedAdapter::createConnection(array(),array('serializer' =>'json'))); } |
9e899a4 to4e31feaComparenicolas-grekas commentedDec 31, 2016
@robfrawley test case added, thanks. Note also that considering your comment about no block, and the use case here (a cache layer), I enabled no-block by default. Tests are green which means it's good for PSR-6! |
4e31fea to95c9777Comparerobfrawley commentedDec 31, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Platform.sh is not properly building the documentation for a bunch of PRs ATM, but I think the docs are accurate with regard to this PR (updated to show correct defaults per your latest updates):give it a look here. |
| if (!$container->hasDefinition($name =md5($dsn))) { | ||
| $definition =newDefinition(\Redis::class); | ||
| $definition =newDefinition('_'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What about usingAdapterInterface::class to not make it harder for bundles that do static analyses of the container?
| return MemcachedAdapter::createConnection($dsn,$options); | ||
| } | ||
| thrownewInvalidArgumentException(sprintf('Unsupported DSN: %s.',$dsn)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should theAbstractAdapter really be aware of all adapters that require a connection?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is a stateless static method, no need for yet another class. This has to be somewhere :)
Other comments addressed.
| } | ||
| $opt =$client->getOption(\Memcached::OPT_SERIALIZER); | ||
| if (\Memcached::SERIALIZER_PHP !==$opt && \Memcached::SERIALIZER_IGBINARY !==$opt) { | ||
| thrownewCacheException('MemcachedAdapter: serializer must be PHP or IGBINARY.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should we useOPT_SERIALIZER instead ofserializer here to be consistent with the exception message below?
robfrawleyDec 31, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The way this code is setup and documented, you passserializer, notopt_serializer, so I would argue the inverse: theother exception message should be amend to:
binary_protocol must be used
| if (is_string($servers)) { | ||
| $servers =array($servers); | ||
| }elseif (!is_array($servers)) { | ||
| thrownewInvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, %s given.',get_type($servers))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
gettype()
36fd01e to3eae606Compare3eae606 to87030b4Comparexabbuh commentedJan 3, 2017
👍 |
robfrawley commentedJan 3, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@nicolas-grekas Do we want the RedisAdapter to accept an array of DSNs like the MemcacheAdapter; I'm looking into bringing the RedisAdapter documentation in-line with the new MemcacheAdapter documentation, re:symfony/symfony-docs/#7265@269875795. It appears it does not at this time. |
nicolas-grekas commentedJan 4, 2017
@robfrawley it would be great yes |
nicolas-grekas commentedJan 4, 2017
Thank you@robfrawley. |
… to MemcachedAdapter (nicolas-grekas, robfrawley)This PR was merged into the 3.3-dev branch.Discussion----------[Cache] Add DSN, createClient & better error reporting to MemcachedAdapter| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | -| License | MIT| Doc PR |symfony/symfony-docs#7265Replaces#20863ping@robfrawley: would you mind opening a doc PR for this?Commits-------87030b4 [cache] Add tests for MemcachedAdapter::createClient()e109438 [Cache] Add DSN, createClient & better error reporting to MemcachedAdapter
Uh oh!
There was an error while loading.Please reload this page.
Replaces#20863
ping@robfrawley: would you mind opening a doc PR for this?