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

VarnishPurger cannot handle alot of entities #1856

Closed
Simperfit opened this issue Apr 12, 2018 · 24 comments
Closed

VarnishPurger cannot handle alot of entities #1856

Simperfit opened this issue Apr 12, 2018 · 24 comments
Labels
Projects

Comments

@Simperfit
Copy link
Contributor

Based on this commit: Simperfit/api-perf@4201cc2#diff-c828d4117439a2216f86c406d522c79c

To reproduce it

docker-compose pull
docker-compose up -d
docker-compose exec php bin/console d:f:l

Results in:


In RequestException.php line 113:
                                                                                   
  Client error: `BAN http://cache-proxy` resulted in a `400 Bad Request` response  
 
@Simperfit Simperfit added the bug label Apr 12, 2018
@teohhanhui
Copy link
Contributor

Would #1776 help? 😄

@Simperfit Simperfit added this to To do in 2.3.x Apr 13, 2018
@BatsaxIV
Copy link

BatsaxIV commented Jul 9, 2018

Any news on this issue?

@dunglas
Copy link
Member

dunglas commented Jul 9, 2018

As a workaround, you can do that: api-platform/demo@0bc6849#diff-628212751f227bfce20484adbd0d4191R1

Maybe can we enable the Varnish subsystem only for the prod env in the default skeleton too?

@teohhanhui
Copy link
Contributor

I'd again argue that there's a need for the developer to verify that the caching (especially the invalidation) by reverse proxy works as expected in the dev env. Surprises are bad.

@teohhanhui
Copy link
Contributor

And it's a bug we need to fix anyway.

@dunglas
Copy link
Member

dunglas commented Jul 9, 2018

The only way to "fix" it is to skip the listener in those specific batch cases. It's up the developper to purge Varnish using another way (probably a full purge) in those cases. But dev commands such as Doctrine Migrations ones must work.

@teohhanhui
Copy link
Contributor

A more efficient cache invalidation method should help. But yes, of course we could never guarantee that there will not be a timeout if a certain higher limit is still hit. That will be up to the developer to improvise. My point of disagreement is mainly on disabling it out of the box in the dev env.

@dunglas
Copy link
Member

dunglas commented Jul 9, 2018

We may at least disable it in CLI + dev to allow such commands to work.

@dunglas
Copy link
Member

dunglas commented Jul 9, 2018

Proposal: if in debug mode + cli : log a warning but disable it.

@teohhanhui
Copy link
Contributor

Perhaps also relevant: #1286 (comment)

@Glideh
Copy link

Glideh commented Nov 29, 2018

I'm also having this issue trying to import data with Doctrine from a command (Which I need to do in production too).
Do we have a workaround ?
For now I've simply reduced my bulk size to avoid this.

@jocel1
Copy link
Contributor

jocel1 commented Apr 25, 2019

I also have the issue in a prod environment when invalidating an object with a lot of relations, so it's definitely not only related to dev commands

@applizem
Copy link

I'm having this issue too when I change my profile picture to my API hosted on GKE. Is there a workaround ?

@applizem
Copy link

Hello,
I managed to fix the problem temporarily by modifying the VarnishPurger class like bellow :

<?php

/*
 * This file is part of the API Platform project.
 *
 * (c) Kévin Dunglas <dunglas@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace ApiPlatform\Core\HttpCache;

use GuzzleHttp\ClientInterface;

/**
 * Purges Varnish.
 *
 * @author Kévin Dunglas <dunglas@gmail.com>
 *
 * @experimental
 */
final class VarnishPurger implements PurgerInterface
{
    private $clients;

    /**
     * @param ClientInterface[] $clients
     */
    public function __construct(array $clients)
    {
        $this->clients = $clients;
    }

    /**
     * {@inheritdoc}
     */
    public function purge(array $iris)
    {
        if (!$iris) {
            return;
        }

        // Create the regex to purge all tags in just one request
        $parts = array_map(function ($iri) {
            return sprintf('(^|\,)%s($|\,)', preg_quote($iri));
        }, $iris);

        $parts = array_chunk($parts,10);
        foreach ($parts as $part) {
            $regex = \count($part) > 1 ? sprintf('(%s)', implode(')|(', $part)) : array_shift($part);
            foreach ($this->clients as $client) {
                $client->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => $regex]]);
            }
        }

    }
}

But I do not know how to extend that class as it is declared final, for now I have modified directly the file on vendor/api-platform/core/src/HttpCache/VarnishPurger.php

@cupsster
Copy link

@applizem above worked for executing fixtures but admin part stopped responding for me :/

@teohhanhui
Copy link
Contributor

I'd really like to encourage doing smaller / more requests, if you could afford to. More granular requests could also help with cache hits, as you'd reduce specialized requests.

@cupsster
Copy link

For me this is happening when I trigger fixtures via docker-compose exec php bin/console doctrine:fixtures:load

@dunglas
Copy link
Member

dunglas commented Aug 18, 2019

To prevent the problem when loading fixtures, you can enable the cache invalidation mechanism only for the prod environment. It's what we've done in the demo: https://github.com/api-platform/demo/blob/master/api/config/packages/prod/api_platform.yaml#L7-L11

@dunglas
Copy link
Member

dunglas commented Aug 18, 2019

I'd again argue that there's a need for the developer to verify that the caching (especially the invalidation) by reverse proxy works as expected in the dev env. Surprises are bad.

I don't agree. We are talking about Symfony environments here.
The dev and test environments are already very different from the prod one, because most caches are disabled and debug tools are loaded. Proper (manual and e2e) testing must be done using the prod environment, even locally.

@teohhanhui
Copy link
Contributor

To prevent the problem when loading fixtures, you can enable the cache invalidation mechanism only for the prod environment. It's what we've done in the demo

@dunglas Should we ship that as the default then?

I'd again argue that there's a need for the developer to verify that the caching (especially the invalidation) by reverse proxy works as expected in the dev env. Surprises are bad.

I don't agree. We are talking about Symfony environments here.
The dev and test environments are already very different from the prod one, because most caches are disabled and debug tools are loaded.

Going by that logic, perhaps we shouldn't ship the varnish ("cache-proxy") container in our Docker Compose setup?

@dunglas
Copy link
Member

dunglas commented Aug 19, 2019

@dunglas Should we ship that as the default then?

Yes I think so.

Going by that logic, perhaps we shouldn't ship the varnish ("cache-proxy") container in our Docker Compose setup?

You mean that we need a docker-compose.prod.yaml file? :D Maybe will we end up with that then...

@teohhanhui
Copy link
Contributor

You mean that we need a docker-compose.prod.yaml file? :D Maybe will we end up with that then...

I have already worked on that, if you'd accept it. 😉 I could open a PR.

@dunglas
Copy link
Member

dunglas commented Aug 19, 2019

Let's do that then

@soyuka
Copy link
Member

soyuka commented Nov 24, 2020

#3843

@soyuka soyuka closed this as completed Nov 24, 2020
rbayet added a commit to Elastic-Suite/gally that referenced this issue Feb 13, 2022
Includes now defunct API Platform Varnish configuration.
From https://github.com/api-platform/api-platform/blob/v2.5.7/api/docker/varnish/conf/default.vcl

Please note that is a BAN based purge (which is kinda meh ?)
Please see also
FriendsOfSymfony/FOSHttpCache#495
api-platform/core#1856
api-platform/api-platform#1947
rbayet added a commit to Elastic-Suite/gally that referenced this issue Feb 14, 2022
Includes now defunct API Platform Varnish configuration.
From https://github.com/api-platform/api-platform/blob/v2.5.7/api/docker/varnish/conf/default.vcl

Please note that is a BAN based purge (which is kinda meh ?)
Please see also
FriendsOfSymfony/FOSHttpCache#495
api-platform/core#1856
api-platform/api-platform#1947
PierreGauthier pushed a commit to Elastic-Suite/gally that referenced this issue Jan 12, 2023
Includes now defunct API Platform Varnish configuration.
From https://github.com/api-platform/api-platform/blob/v2.5.7/api/docker/varnish/conf/default.vcl

Please note that is a BAN based purge (which is kinda meh ?)
Please see also
FriendsOfSymfony/FOSHttpCache#495
api-platform/core#1856
api-platform/api-platform#1947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
2.3.x
  
To do
Development

No branches or pull requests

9 participants