Skip to content

Commit

Permalink
bug #36536 [Cache] Allow invalidateTags calls to be traced by data co…
Browse files Browse the repository at this point in the history
…llector (l-vo)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] Allow invalidateTags calls to be traced by data collector

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #34810
| License       | MIT
| Doc PR        |

`TraceableTagAwareAdapter` is not used in the fullstack framework since tag aware pools don't have the `cache.pool` tag (it's the decorated adapter that has it). This PR aims to use `TraceableTagAwareAdapter` when a pool is configured with `tags: true`

Commits
-------

28fdb3a Allow invalidateTags calls to be traced by data collector
  • Loading branch information
nicolas-grekas committed Apr 26, 2020
2 parents 2d7b0b8 + 28fdb3a commit 1bc3ee7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
Expand Up @@ -46,29 +46,38 @@ public function process(ContainerBuilder $container)
return;
}

$collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $attributes) {
$definition = $container->getDefinition($id);
if ($definition->isAbstract()) {
continue;
}
$this->addToCollector($id, $container);

$recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
$recorder->setTags($definition->getTags());
if (!$definition->isPublic() || !$definition->isPrivate()) {
$recorder->setPublic($definition->isPublic());
if (($attributes[0]['name'] ?? $id) !== $id) {
$this->addToCollector($attributes[0]['name'], $container);
}
$recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);

$definition->setTags([]);
$definition->setPublic(false);
}
}

$container->setDefinition($innerId, $definition);
$container->setDefinition($id, $recorder);
private function addToCollector(string $id, ContainerBuilder $container)
{
$definition = $container->getDefinition($id);
if ($definition->isAbstract()) {
return;
}

// Tell the collector to add the new instance
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
$collectorDefinition->setPublic(false);
$collectorDefinition = $container->getDefinition($this->dataCollectorCacheId);
$recorder = new Definition(is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class);
$recorder->setTags($definition->getTags());
if (!$definition->isPublic() || !$definition->isPrivate()) {
$recorder->setPublic($definition->isPublic());
}
$recorder->setArguments([new Reference($innerId = $id.$this->cachePoolRecorderInnerSuffix)]);

$definition->setTags([]);
$definition->setPublic(false);

$container->setDefinition($innerId, $definition);
$container->setDefinition($id, $recorder);

// Tell the collector to add the new instance
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
$collectorDefinition->setPublic(false);
}
}
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
Expand All @@ -34,16 +35,29 @@ public function testProcess()
->addArgument(new Reference('fs'))
->addTag('cache.pool');

$container
->register('.php.inner', PhpArrayAdapter::class)
->addTag('cache.pool', ['name' => 'php']);
$container
->register('php', TagAwareAdapter::class)
->addArgument(new Reference('.php.inner'));

$collector = $container->register('data_collector.cache', CacheDataCollector::class);
(new CacheCollectorPass())->process($container);

$this->assertEquals([
['addInstance', ['fs', new Reference('fs')]],
['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
['addInstance', ['.php.inner', new Reference('.php.inner')]],
['addInstance', ['php', new Reference('php')]],
], $collector->getMethodCalls());

$this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());

$this->assertSame(TraceableAdapter::class, $container->findDefinition('.php.inner')->getClass());
$this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('php')->getClass());

$this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
}
}

0 comments on commit 1bc3ee7

Please sign in to comment.