From 7264b34825851c5cd4e7233320685e0212dc4ed9 Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Tue, 31 Mar 2020 09:39:33 +0200 Subject: [PATCH 1/2] Docker: Set two Memcached servers --- app/AppKernel.php | 1 + .../cache_pool/cache.multiple.memcached.yml | 14 ++++++++++ composer.json | 1 + docker-compose.bash | 6 ++--- docker-compose.yml | 4 ++- docker/apache/Dockerfile | 4 +-- docker/apache/parameters.yml | 10 +++---- .../Adapter/MultipleMemcachedAdapter.php | 26 +++++++++++++++++++ .../MultipleMemcachedBundle.php | 9 +++++++ 9 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 app/config/cache_pool/cache.multiple.memcached.yml create mode 100644 src/MultipleMemcachedBundle/Component/Cache/Adapter/MultipleMemcachedAdapter.php create mode 100644 src/MultipleMemcachedBundle/MultipleMemcachedBundle.php diff --git a/app/AppKernel.php b/app/AppKernel.php index fd4ef4c2..b9dc1664 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -65,6 +65,7 @@ public function registerBundles() new EzSystems\EzPlatformEnterpriseEditionInstallerBundle\EzPlatformEnterpriseEditionInstallerBundle(), new EzSystems\EzPlatformWorkflowBundle\EzPlatformWorkflowBundle(), // Application + new MultipleMemcachedBundle\MultipleMemcachedBundle(), new AppBundle\AppBundle(), ]; diff --git a/app/config/cache_pool/cache.multiple.memcached.yml b/app/config/cache_pool/cache.multiple.memcached.yml new file mode 100644 index 00000000..163774a4 --- /dev/null +++ b/app/config/cache_pool/cache.multiple.memcached.yml @@ -0,0 +1,14 @@ +services: + cache.multiple.memcached: + parent: cache.adapter.memcached + tags: + - name: cache.pool + clearer: cache.app_clearer + provider: cache.multiple_memcached_provider + namespace: '%cache_namespace%' + + cache.multiple_memcached_provider: + class: \Memcached + factory: ['MultipleMemcachedBundle\Component\Cache\Adapter\MultipleMemcachedAdapter', 'createConnection'] + arguments: + - '%cache_dsn%' diff --git a/composer.json b/composer.json index 21b2d983..a666df9e 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ }, "autoload": { "psr-4": { + "MultipleMemcachedBundle\\": "src/MultipleMemcachedBundle/", "AppBundle\\": "src/AppBundle/" }, "classmap": [ "app/AppKernel.php", "app/AppCache.php" ] diff --git a/docker-compose.bash b/docker-compose.bash index de74c5e6..a36e32cd 100755 --- a/docker-compose.bash +++ b/docker-compose.bash @@ -4,7 +4,7 @@ set -e; # Persistence Cache and Session Handler -CACHE_AND_SESSION_HANDLER=tagaware.filesystem +CACHE_AND_SESSION_HANDLER=memcached while (( "$#" )); do case "$1" in @@ -56,8 +56,8 @@ if [[ 'redis' == "$CACHE_AND_SESSION_HANDLER" ]]; then sed -i '' -e "s/CACHE_AND_SESSION_PORT/6379/" app/config/parameters.yml; UNUSED_CONTAINER_LIST="memcached"; elif [[ 'memcached' == "$CACHE_AND_SESSION_HANDLER" ]]; then - sed -i '' -e "s/CACHE_AND_SESSION_HOST/memcached/" app/config/parameters.yml; - sed -i '' -e "s/CACHE_AND_SESSION_PORT/11211/" app/config/parameters.yml; +# sed -i '' -e "s/CACHE_AND_SESSION_HOST/memcached/" app/config/parameters.yml; +# sed -i '' -e "s/CACHE_AND_SESSION_PORT/11211/" app/config/parameters.yml; UNUSED_CONTAINER_LIST="redis"; else # tagaware.filesystem sed -i '' -e "s/imports://" app/config/parameters.yml; diff --git a/docker-compose.yml b/docker-compose.yml index ddd6abc3..d3245219 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,7 +25,9 @@ services: redis: image: redis:3.2 - memcached: + memcached1: + image: memcached + memcached2: image: memcached mariadb: diff --git a/docker/apache/Dockerfile b/docker/apache/Dockerfile index 0d444072..ace3769a 100644 --- a/docker/apache/Dockerfile +++ b/docker/apache/Dockerfile @@ -3,7 +3,7 @@ FROM debian:10 MAINTAINER adrien.dupuis@ez.no ARG symfony_env=dev -ARG session_save_handler=tagaware.filesystem +ARG session_save_handler=memcached ENV SYMFONY_ENV=$symfony_env ENV DEBIAN_FRONTEND noninteractive @@ -49,7 +49,7 @@ RUN mkdir /run/php && touch /var/log/php7.3-fpm.log RUN if [[ 'redis' == $session_save_handler ]]; then \ echo "\n; Redis as session handler\nphp_value[session.save_handler] = redis\nphp_value[session.save_path] = \"tcp://redis:6379\"" >> /etc/php/7.3/fpm/pool.d/www.conf; \ elif [[ 'memcached' == $session_save_handler ]]; then \ - echo "\n; Memcached as session handler\nphp_value[session.save_handler] = memcached\nphp_value[session.save_path] = \"memcached:11211\"" >> /etc/php/7.3/fpm/pool.d/www.conf; \ + echo "\n; Memcached as session handler\nphp_value[session.save_handler] = memcached\nphp_value[session.save_path] = \"memcached1:11211,memcached2:11211\"" >> /etc/php/7.3/fpm/pool.d/www.conf; \ fi; # Apache: FastCGI Module & PHP-FPM Configuration RUN a2enmod proxy_fcgi setenvif rewrite \ diff --git a/docker/apache/parameters.yml b/docker/apache/parameters.yml index bc85e5f0..fe11a868 100644 --- a/docker/apache/parameters.yml +++ b/docker/apache/parameters.yml @@ -1,5 +1,5 @@ imports: - - { resource: cache_pool/cache.CACHE_AND_SESSION_HOST.yml } + - { resource: cache_pool/cache.multiple.memcached.yml } parameters: #TODO: May move all this to .env file or Dockerfile @@ -11,12 +11,10 @@ parameters: env(HTTPCACHE_PURGE_SERVER): http://varnish #env(HTTPCACHE_VARNISH_INVALIDATE_TOKEN): docker - # https://doc.ezplatform.com/en/2.5/guide/persistence_cache/ - env(CACHE_POOL): cache.CACHE_AND_SESSION_HOST - env(CACHE_DSN): CACHE_AND_SESSION_HOST - # https://doc.ezplatform.com/en/2.5/guide/sessions/ + env(CACHE_POOL): cache.multiple.memcached + env(CACHE_DSN): memcached://memcached1:11211?weight=67,memcached://memcached2:11211?weight=33 ezplatform.session.handler_id: ~ - env(SESSION_SAVE_PATH): tcp://CACHE_AND_SESSION_HOST:CACHE_AND_SESSION_PORT + env(SESSION_SAVE_PATH): memcached1:11211,memcached2:11211 env(DATABASE_HOST): mariadb env(DATABASE_PASSWORD): root diff --git a/src/MultipleMemcachedBundle/Component/Cache/Adapter/MultipleMemcachedAdapter.php b/src/MultipleMemcachedBundle/Component/Cache/Adapter/MultipleMemcachedAdapter.php new file mode 100644 index 00000000..be35f7db --- /dev/null +++ b/src/MultipleMemcachedBundle/Component/Cache/Adapter/MultipleMemcachedAdapter.php @@ -0,0 +1,26 @@ + $server) { + if (0 !== strpos($server, 'memcached://')) { + $servers[$serverIndex] = "memcached://$server"; + } + } + } + if (!\is_array($servers)) { + throw new InvalidArgumentException(sprintf('Unsupported Server/DSN list: %s.', var_export($servers, true))); + } + return MemcachedTrait::createConnection($servers, $options); + } +} diff --git a/src/MultipleMemcachedBundle/MultipleMemcachedBundle.php b/src/MultipleMemcachedBundle/MultipleMemcachedBundle.php new file mode 100644 index 00000000..d3931919 --- /dev/null +++ b/src/MultipleMemcachedBundle/MultipleMemcachedBundle.php @@ -0,0 +1,9 @@ + Date: Tue, 31 Mar 2020 14:57:50 +0200 Subject: [PATCH 2/2] apache/Dockerfile: Add replication in dual Memcached sessions --- docker/apache/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/apache/Dockerfile b/docker/apache/Dockerfile index ace3769a..ac42ff4a 100644 --- a/docker/apache/Dockerfile +++ b/docker/apache/Dockerfile @@ -50,6 +50,7 @@ RUN if [[ 'redis' == $session_save_handler ]]; then \ echo "\n; Redis as session handler\nphp_value[session.save_handler] = redis\nphp_value[session.save_path] = \"tcp://redis:6379\"" >> /etc/php/7.3/fpm/pool.d/www.conf; \ elif [[ 'memcached' == $session_save_handler ]]; then \ echo "\n; Memcached as session handler\nphp_value[session.save_handler] = memcached\nphp_value[session.save_path] = \"memcached1:11211,memcached2:11211\"" >> /etc/php/7.3/fpm/pool.d/www.conf; \ + echo "php_value[memcached.sess_number_of_replicas] = 1\nphp_value[memcached.sess_lock_retries] = 10\nphp_value[memcached.sess_lock_expire] = 900" >> /etc/php/7.3/fpm/pool.d/www.conf \ fi; # Apache: FastCGI Module & PHP-FPM Configuration RUN a2enmod proxy_fcgi setenvif rewrite \