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

Prevent result cache key collisions when sharing caches across different connections #713

Closed

Conversation

vilartoni
Copy link
Contributor

There's an issue when using the default cache key generation for the result cache and using the same cache system across different connections as the generated key will be the same regardless of the connection used.

We can solve this just by using the connection params in the key generation. This issue is quite similar to the one fixed in #1075.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-1030

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius
Copy link
Member

Ocramius commented Nov 5, 2014

I'm not sure if this needs fixing at all: there seems to be the base mistake to use the same cache for different data-sources here.

Obviously, results from one database or the other are different if they are not replicas, so the cache should be namespaced accordingly.

Did you consider that first?

@vilartoni
Copy link
Contributor Author

Hi!

You're absolutely right about setting the namespace appropriately. That solves this problem. But why not making the most of the automatic cache id generation and prevent a potential issue (with no extra cost nor complexity) which can save pain to some users? I would expect a safe key generation mechanism to be smart enough to take that into account and generate a different key for a different connection.

What are your thoughts?

@Ocramius
Copy link
Member

Ocramius commented Nov 5, 2014

I agree with that, but we'd need to check all our codebases instead of fixing the issue only locally.

@vilartoni
Copy link
Contributor Author

Sorry, don't get what you mean.

@Ocramius
Copy link
Member

Ocramius commented Nov 5, 2014

I mean that we need to find every location where a cache key is generated and fix it.

@vilartoni
Copy link
Contributor Author

Do you mean within the DBAL codebase? The only location calling Doctrine\DBAL\Cache\QueryCacheProfile::generateCacheKeys() has been adapted to pass the connection params in as an argument. And having defaulted connectionParams to an empty array should guarantee none of the possible external usages of it breaks. So there shouldn't be the need to fix anything. Maybe I'm missing something, sorry.

@vilartoni
Copy link
Contributor Author

So... do you think it doesn't add value to add this modification?

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2014

@vilartoni yes, seems valuable to me, though it still doesn't solve cases where different machines access the same cache with same localhost (example) connection. As you can see, the problem is more conceptual than an actual bug: may want to also patch documentation about that, what do you think?

@vilartoni
Copy link
Contributor Author

@Ocramius Sure, I didn't consider the current behaviour was a bug at all. The case you mention wouldn't be solved with this PR changes, as you correctly said, but many times we should aim for the 'better' and not for the 'best' ;)

WRT the documentation, I considered updating the phpdoc was enough.

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2014

WRT the documentation, I considered updating the phpdoc was enough.

Can you check if any chapter mentions caching at all? Those need to be changed indeed.

@vilartoni
Copy link
Contributor Author

@Ocramius Haven't found any mention in the docs to the cache performed by DBAL/Cache/QueryCacheProfile but here (DBAL) and here (ORM) and it doesn't mention anything about its internals.

The ID used to store the result set cache is a hash which is automatically generated for you if you don’t set a custom ID yourself with the setResultCacheId() method.

It keeps being automatically generated, which is the only thing I'd say the end user needs to know.

@Ocramius
Copy link
Member

Ocramius commented Nov 8, 2014

Fair enuff. Last thing before merging: we need tests for the changed units.

@vilartoni vilartoni force-pushed the prevent-result-cache-key-collisions branch from 7b7539e to 90bc19d Compare November 8, 2014 09:10
@vilartoni
Copy link
Contributor Author

@Ocramius Done. The failed tests have nothing to do with the new test I've added.

/**
* @test
*/
public function it_should_use_the_given_cache_key_if_present()
Copy link
Member

Choose a reason for hiding this comment

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

Our convention is still the good 'ol testShouldUseTheGivenCacheKeyIfPresent()

@vilartoni vilartoni force-pushed the prevent-result-cache-key-collisions branch from 9d343e3 to dfbe772 Compare November 8, 2014 20:04
@vilartoni
Copy link
Contributor Author

@Ocramius Done! ;-)

*
* @return array
*/
public function generateCacheKeys($query, $params, $types)
public function generateCacheKeys($query, $params, $types, $connectionParams = array())
Copy link
Member

Choose a reason for hiding this comment

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

May want to type-hint this as array

@Ocramius Ocramius self-assigned this Nov 10, 2014
@vilartoni vilartoni force-pushed the prevent-result-cache-key-collisions branch from 6c93950 to bb143d6 Compare November 10, 2014 15:24
@vilartoni vilartoni force-pushed the prevent-result-cache-key-collisions branch from bb143d6 to 73c8278 Compare November 10, 2014 15:32
@vilartoni
Copy link
Contributor Author

@Ocramius Are you happy with the changes?

@Ocramius
Copy link
Member

Looks good, though I'll have to investigate why you needed to change the .xml files.

@stof
Copy link
Member

stof commented Dec 19, 2014

@Ocramius the change is because the PHPUnit config files used by Travis were outdated (they were missing the bootstrap configuration added in the main config file 2 or 3 years ago)

@vilartoni
Copy link
Contributor Author

@stof @Ocramius yes, that's why the autoloading wasn't working properly.

@Ocramius
Copy link
Member

Ocramius commented May 3, 2017

Urgh, this took aaaages, sorry @vilartoni :-(

I merged this into 2.6 via 232d585

@vilartoni
Copy link
Contributor Author

It's okay ;-)

lcobucci added a commit to lcobucci/doctrine2 that referenced this pull request May 10, 2017
That argument was added to don't have key colisions for different
connections.

More info: doctrine/dbal#713
lcobucci added a commit to lcobucci/doctrine2 that referenced this pull request May 10, 2017
That argument was added to don't have key colision for different
connections.

More info: doctrine/dbal#713
lcobucci added a commit to lcobucci/doctrine2 that referenced this pull request May 10, 2017
That argument was added to not have key collisions for different
connections.

More info: doctrine/dbal#713
@Ocramius Ocramius changed the title Prevent result cache key collisions when sharing cache across different connections Prevent result cache key collisions when sharing caches across different connections Jul 22, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants