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

[Config] Early return for DirectoryResource #21458

Merged
merged 2 commits into from Feb 12, 2017

Conversation

robfrawley
Copy link
Contributor

@robfrawley robfrawley commented Jan 29, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? sure?
BC breaks? no
Deprecations? no
Tests pass? no
Fixed tickets
Related PRs #21440
License MIT
Doc PR n/a

Alternate PR that implements an early return for DirectoryResource to increase the speed on large file sets. We can never return early with true without checking all assets within the resource, as the aforementioned referenced PR did; hence this PR takes the counter approach and returns false early where appropriate.

Conversation about possible bug at #21458 (comment).

return false;
}

$newestMTime = max($fileMTime, $newestMTime);
Copy link
Member

@ogizanagi ogizanagi Jan 29, 2017

Choose a reason for hiding this comment

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

This can be removed, right?

Also, may test directory mtime first (before the foreach), and return false if greater than passed timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, removing that breaks the test ->isFresh() returns false if an existing file is removed. Yes, though, we could add another condition at the very beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added initial conditional before loop.

Copy link
Member

@ogizanagi ogizanagi Jan 29, 2017

Choose a reason for hiding this comment

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

Let me know if I'm wrong, but I think the test is failing in this case because the implementation and the test case are slightly flawed.

Try:

diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php
index dcb5e19..bef0b2b 100644
--- a/src/Symfony/Component/Config/Resource/DirectoryResource.php
+++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php
@@ -95,7 +95,7 @@ class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
             $newestMTime = max($fileMTime, $newestMTime);
         }
 
-        return $newestMTime < $timestamp;
+        return $newestMTime <= $timestamp;
     }
 
     public function serialize()

and the test case should fail. However, I'd expect a resource to be considered fresh if its mtime is the exact same as the provided timestamp (on the contrary, you should revised the new conditions to use $timestamp <= $fileMTime when returning false).

The following for instance should work:

See patch
diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php
index dcb5e19..deb40b0 100644
--- a/src/Symfony/Component/Config/Resource/DirectoryResource.php
+++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php
@@ -74,7 +74,10 @@ class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
             return false;
         }
 
-        $newestMTime = filemtime($this->resource);
+        if($timestamp < $newestMTime = filemtime($this->resource)) {
+            return false;
+        };
+
         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
             // if regex filtering is enabled only check matching files
             if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
@@ -88,14 +91,12 @@ class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
             }
 
             // early return if any file mtime is greater than passed timestamp
-            if (($fileMTime = $file->getMTime()) > $timestamp) {
+            if ($timestamp < $fileMTime = $file->getMTime()) {
                 return false;
             }
-
-            $newestMTime = max($fileMTime, $newestMTime);
         }
 
-        return $newestMTime < $timestamp;
+        return true;
     }
 
     public function serialize()
diff --git a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php
index a22209d..9d8e055 100644
--- a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php
+++ b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php
@@ -110,8 +110,10 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
     public function testIsFreshDeleteFile()
     {
         $resource = new DirectoryResource($this->directory);
+        $time = time();
+        sleep(1);
         unlink($this->directory.'/tmp.xml');
-        $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
+        $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
     }
 
     public function testIsFreshDeleteDirectory()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we consider changing this a BC break, though? That's why I hadn't implemented something similar in my testing leading up to this PR. Assuming the answer is no, I'll happily implement these changes, though @nicolas-grekas @ogizanagi

Copy link
Member

Choose a reason for hiding this comment

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

It's rather a bug fix to me.
For reference, FileResource uses filemtime($this->resource) <= $timestamp too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something like this greatly simplifies the code in this PR, and I think may be a viable option (this diff is against this PR), without changing the tests at all:

diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php
index 9011cfe..501f8a5 100644
--- a/src/Symfony/Component/Config/Resource/DirectoryResource.php
+++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php
@@ -68,7 +68,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
             return false;
         }
 
-        if ($timestamp < $newestMTime = filemtime($this->resource)) {
+        if ($timestamp <= filemtime($this->resource)) {
             return false;
         }
 
@@ -85,14 +85,12 @@ class DirectoryResource implements ResourceInterface, \Serializable
             }
 
             // early return if a file's mtime exceeds the passed timestamp
-            if ($timestamp < $fileMTime = $file->getMTime()) {
+            if ($timestamp <= $file->getMTime()) {
                 return false;
             }
-
-            $newestMTime = max($fileMTime, $newestMTime);
         }
 
-        return $newestMTime < $timestamp;
+        return true;
     }
 
     public function serialize()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's see what the maintainers say; changes pushed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I too think that the same timestamp should be considered fresh. Can you clarify why the behavior exists such that a matching timestamp is not fresh @nicolas-grekas

Copy link
Member

Choose a reason for hiding this comment

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

should be fresh with same timestamp to me also - same as in FileResource

@robfrawley robfrawley force-pushed the feature-earily-directory-resource-return branch 2 times, most recently from cab5763 to fc1726f Compare January 29, 2017 22:25
@robfrawley robfrawley changed the base branch from master to 2.7 January 29, 2017 22:25
@robfrawley robfrawley force-pushed the feature-earily-directory-resource-return branch from fc1726f to d9701bd Compare January 29, 2017 22:31
@robfrawley robfrawley changed the title Early return for DirectoryResource [Config] Early return for DirectoryResource Jan 29, 2017
@@ -68,7 +68,10 @@ public function isFresh($timestamp)
return false;
}

$newestMTime = filemtime($this->resource);
if (($newestMTime = filemtime($this->resource)) > $timestamp) {
Copy link
Member

Choose a reason for hiding this comment

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

You can swap for a <, and remove the brackets as done usually

Copy link
Member

Choose a reason for hiding this comment

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

Same below btw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 done

@robfrawley robfrawley force-pushed the feature-earily-directory-resource-return branch from d9701bd to 97bda48 Compare January 29, 2017 22:52
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍 with minor typo

@@ -81,7 +84,12 @@ public function isFresh($timestamp)
continue;
}

$newestMTime = max($file->getMTime(), $newestMTime);
// early return if a file's mtime is exceeds than passed timestamp
Copy link
Member

Choose a reason for hiding this comment

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

is exceeds =>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed ;-)

@robfrawley robfrawley force-pushed the feature-earily-directory-resource-return branch 4 times, most recently from 8fabc4f to 5eac053 Compare January 30, 2017 00:04
@robfrawley robfrawley force-pushed the feature-earily-directory-resource-return branch from 5eac053 to 96107e2 Compare January 30, 2017 00:05
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

👍

@@ -68,7 +68,10 @@ public function isFresh($timestamp)
return false;
}

$newestMTime = filemtime($this->resource);
if ($timestamp <= filemtime($this->resource)) {
Copy link
Member

Choose a reason for hiding this comment

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

this needs to be turned to a <: the "equals" case should not return false, as in eg FileResource

Copy link
Member

Choose a reason for hiding this comment

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

It returned false before. Do we consider this behaviour change a bug fix?

Copy link
Member

Choose a reason for hiding this comment

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

I guess so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check second commit (can squash if its good)

@@ -81,10 +84,13 @@ public function isFresh($timestamp)
continue;
}

$newestMTime = max($file->getMTime(), $newestMTime);
// early return if a file's mtime exceeds the passed timestamp
if ($timestamp <= $file->getMTime()) {
Copy link
Member

Choose a reason for hiding this comment

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

<

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check second commit (can squash if its good)

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Jan 30, 2017
unlink($this->directory.'/tmp.xml');
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
$this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas This fails with the "bug fix" to not return false when timestamp are the same (unlike the original implementation). This test change fixes failures originating from this behavioral change...

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we move this test into the time-sensitive group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch; I didn't know about that convention. I only see that group at the class-level. Is it okay to use it at the test-level?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that should work the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding that group causes the test to fail. What does that group do and why would it result in it failing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xabbuh We can't use that because the internal implementation only relates to the PHP interpreter. This relies on the filesystem and is external of the interpreter.

An added bonus of using the ClockMock class is that time passes instantly. Using PHP's sleep(10) will make your test wait for 10 actual seconds (more or less). In contrast, the ClockMock class advances the internal clock the given number of seconds without actually waiting that time, so your test will execute 10 seconds faster.
http://symfony.com/blog/new-in-symfony-2-8-clock-mocking-and-time-sensitive-tests

@fabpot
Copy link
Member

fabpot commented Feb 12, 2017

Thank you @robfrawley.

@fabpot fabpot merged commit d5746ec into symfony:2.7 Feb 12, 2017
fabpot added a commit that referenced this pull request Feb 12, 2017
This PR was squashed before being merged into the 2.7 branch (closes #21458).

Discussion
----------

[Config] Early return for DirectoryResource

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | sure?
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets |
| Related PRs | #21440
| License       | MIT
| Doc PR        | n/a

Alternate PR  that implements an early return for `DirectoryResource` to increase the speed on large file sets. We can never return early with `true` without checking all assets within the resource, as the aforementioned referenced PR did; hence this PR takes the counter approach and returns `false` early where appropriate.

_Conversation about possible bug at #21458 (comment)

Commits
-------

d5746ec fix directory resource considers same timestamp not fresh
96107e2 return false early from directory resource
@fabpot fabpot mentioned this pull request Feb 17, 2017
This was referenced Mar 6, 2017
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.

None yet

5 participants