-
Notifications
You must be signed in to change notification settings - Fork 115
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
Keep state between processes #195
Conversation
e014b59
to
62dd14b
Compare
is it expected that the test are not passing with hhvm ? |
Good job on this. Looks good. |
I don't expect the tests to fail. I will make sure they pass on HHVM. |
I'm afraid it is an HHVM limitation: https://gist.github.com/tlevi/8503668 |
Here is a workaround: https://github.com/LExpress/symfony1/pull/127/files Smarty: smarty-php/smarty@f9d9ca0#diff-07258ab0fdc58bf99abe45a5f24a8aceR21 |
42dc56f
to
cfa7113
Compare
What's left to be done:
|
Good for me. |
I'm really not sure about Why not just use text file with specific syntax which is easy to parse? The code will be cleaner, easier to read, we won't have to deal with OPCache and HHVM workaround. |
And the files are stored as PHP files to take advantage of PHP opcache which makes the read even faster than other caching strategies. If we go without opcache the code will be cleaner but it will be that much slower than the current approach. It will still be better than nothing though. |
My bad about the And how about checking in assertCacheFileIsValid if HHVM && eval() is enabled? |
You are right, we are missing some edge cases there that we should probably cover, nice catch @JanPetr ❤️ |
770d78e
to
4cea7d7
Compare
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 just noticed some small glitches :)
Good choice with JSON file, much better then PHP file and eval()
for me.
*/ | ||
public function __construct($ttl = null, $file = null) | ||
{ | ||
$this->failingHostsCacheFile = null === $file ? $this->getDefaultCacheFile() : (string) $file; |
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'd rewrite this line to be more explicit? Like that it's not really readable and I have to really focus to get what it does :)
if ($json === false) { | ||
return; | ||
} | ||
@file_put_contents($this->failingHostsCacheFile, $json); |
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.
Is the @
really needed? All conditions are checked when the class is created and by suppressing error here we might lose some valuable info for potential debugging.
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.
file_put_contents
throws warning, not fatal - worth to remove @
|
||
$this->assertCacheFileIsValid($this->failingHostsCacheFile); | ||
|
||
if (null === $ttl) { |
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.
Change to $var === null
because of consistency.
src/AlgoliaSearch/ClientContext.php
Outdated
@@ -129,6 +126,18 @@ public function __construct($applicationID, $apiKey, $hostsArray, $placesEnabled | |||
$this->algoliaUserToken = null; | |||
$this->rateLimitAPIKey = null; | |||
$this->headers = array(); | |||
|
|||
if (null === $failingHostsCache) { |
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.
Change to $var === null
because of consistency
*/ | ||
public function __construct($ttl = null) | ||
{ | ||
if (null === $ttl) { |
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.
Change to $var === null
because of consistency
// Keep a local cache of failed hosts in case the file based strategy doesn't work out. | ||
self::$failingHosts[] = $host; | ||
|
||
if (null === self::$timestamp) { |
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.
Change to $var === null
because of consistency
*/ | ||
public function getFailingHosts() | ||
{ | ||
if (null === self::$timestamp) { |
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.
Change to $var === null
because of consistency
private function loadFailingHostsCacheFromDisk() | ||
{ | ||
$json = @file_get_contents($this->failingHostsCacheFile); | ||
if (false === $json) { |
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.
Change to $var === false
because of consistency
*/ | ||
private function loadFailingHostsCacheFromDisk() | ||
{ | ||
$json = @file_get_contents($this->failingHostsCacheFile); |
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.
file_get_contents
throws warning, not fatal - worth to remove @
in order not to loose debugging information
This PR aims to resolve the issue that PHP shares no state between PHP processes. If the primary DNS server is down, then every new PHP process would have to hit the timeout connection once before falling back to the other DNS provider. For now users have to manually state that they want to use the file based caching strategy. After the robustness of this has been proven, we will make this the default strategy. Note that if you are working in a RO filesystem, it would automatically fallback to in memory.
99253db
to
dabc90c
Compare
Looks like this problem is related to sebastianbergmann/phpunit#1684 |
return array(); | ||
} | ||
|
||
$json = file_get_contents($this->failingHostsCacheFile); |
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 you need the @
here
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.
@JanPetr made a point on this subject. We should not obfuscate errors here. The errors here are only warnings so it makes sense to have them to be able to know what happens.
$fileDirectory = dirname($file); | ||
|
||
if (! is_writable($fileDirectory)) { | ||
throw new \RuntimeException(sprintf('Cache file directory "%s" is not writable.', $fileDirectory)); |
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 am not sure we want to throw non catched exceptions here
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.
Yes we do, this strategy makes non sense if the infra does not allow it. This is exactly what Runtime exceptions are for ;)
If tomorrow we push this strategy by default, we will catch the exception and instantiate a fallback instead.
{ | ||
$json = json_encode($data); | ||
if ($json !== false) { | ||
file_put_contents($this->failingHostsCacheFile, $json); |
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.
Same here we probably need the @
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.
Same as before ;)
Good for me |
Will merge and release on Monday. |
We abstracted an interface for the failed hosts retrieval so that users can easily choose their caching strategy based on what is best according to their environment.
By default we push the new File Based cache strategy, but if the tmp directory is not writable, we fallback to the in memory strategy.
User can provide an implementation of FailingHostsCache as a replacement of this strategy.
Because of the current design of the Client, the FailingHostsCache implementation needs to be provided at Client initialization because that is where the ClientContext is instantiated and during that instantiation we need to rotate the hosts.
Regarding the BC static to non static, given it has only been available in 1 version and was only used internally + wasn't even called statically, I think we are safe doing the change.
TODO: