Puppet Class: profile::cache::varnish::frontend

Defined in:
modules/profile/manifests/cache/varnish/frontend.pp

Overview

Parameters:

  • cache_nodes (Any) (defaults to: hiera('cache::nodes'))
  • cache_cluster (Any) (defaults to: hiera('cache::cluster'))
  • conftool_prefix (Any) (defaults to: hiera('conftool_prefix'))
  • fe_vcl_config (Any) (defaults to: hiera('profile::cache::varnish::frontend::fe_vcl_config'))
  • fe_cache_be_opts (Any) (defaults to: hiera('profile::cache::varnish::cache_be_opts'))
  • fe_jemalloc_conf (Any) (defaults to: hiera('profile::cache::varnish::frontend::fe_jemalloc_conf'))
  • fe_extra_vcl (Any) (defaults to: hiera('profile::cache::varnish::frontend::fe_extra_vcl'))
  • runtime_params (Any) (defaults to: hiera('profile::cache::varnish::frontend::runtime_params'))
  • req_handling (Profile::Cache::Sites) (defaults to: lookup('cache::req_handling'))
  • alternate_domains (Profile::Cache::Sites) (defaults to: lookup('cache::alternate_domains', {'default_value' => {}}))
  • separate_vcl (Any) (defaults to: hiera('profile::cache::varnish::separate_vcl', []))
  • fe_transient_gb (Any) (defaults to: hiera('profile::cache::varnish::frontend::transient_gb', 0))
  • backend_services (Any) (defaults to: hiera('profile::cache::varnish::frontend::backend_services', ['ats-be']))
  • has_lvs (Boolean) (defaults to: lookup('has_lvs', {'default_value' => true}))


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'modules/profile/manifests/cache/varnish/frontend.pp', line 1

class profile::cache::varnish::frontend (
    $cache_nodes = hiera('cache::nodes'),
    $cache_cluster = hiera('cache::cluster'),
    $conftool_prefix = hiera('conftool_prefix'),
    $fe_vcl_config = hiera('profile::cache::varnish::frontend::fe_vcl_config'),
    $fe_cache_be_opts = hiera('profile::cache::varnish::cache_be_opts'),
    $fe_jemalloc_conf = hiera('profile::cache::varnish::frontend::fe_jemalloc_conf'),
    $fe_extra_vcl = hiera('profile::cache::varnish::frontend::fe_extra_vcl'),
    $runtime_params = hiera('profile::cache::varnish::frontend::runtime_params'),
    Profile::Cache::Sites $req_handling = lookup('cache::req_handling'),
    Profile::Cache::Sites $alternate_domains = lookup('cache::alternate_domains', {'default_value' => {}}),
    $separate_vcl = hiera('profile::cache::varnish::separate_vcl', []),
    $fe_transient_gb = hiera('profile::cache::varnish::frontend::transient_gb', 0),
    $backend_services = hiera('profile::cache::varnish::frontend::backend_services', ['ats-be']),
    Boolean $has_lvs = lookup('has_lvs', {'default_value' => true}),
) {
    require ::profile::cache::base
    $wikimedia_nets = $profile::cache::base::wikimedia_nets
    $wikimedia_trust = $profile::cache::base::wikimedia_trust

    if $has_lvs {
        require ::profile::lvs::realserver
    }

    # Frontend memory cache sizing
    $mem_gb = $::memorysize_mb / 1024.0
    if ($mem_gb < 90.0) {
        # virtuals, test hosts, etc...
        $fe_mem_gb = 1
    } else {
        # Removing a constant factor before scaling helps with
        # low-memory hosts, as they need more relative space to
        # handle all the non-cache basics.
        $fe_mem_gb = ceiling(0.7 * ($mem_gb - 100.0))
    }

    $vcl_config = $fe_vcl_config + {
        req_handling         => $req_handling,
        alternate_domains    => $alternate_domains,
        fe_mem_gb            => $fe_mem_gb,
    }

    # VCL files common to all instances
    class { 'varnish::common::vcl':
        vcl_config => $vcl_config,
    }

    $separate_vcl_frontend = $separate_vcl.map |$vcl| { "${vcl}-frontend" }

    # Backend caches used by this Frontend from Etcd
    $reload_vcl_opts = varnish::reload_vcl_opts($vcl_config['varnish_probe_ms'],
        $separate_vcl_frontend, 'frontend', "${cache_cluster}-frontend")

    $keyspaces = $backend_services.map |$service| {
        "${conftool_prefix}/pools/${::site}/cache_${cache_cluster}/${service}"
    }
    confd::file { '/etc/varnish/directors.frontend.vcl':
        ensure     => present,
        watch_keys => $keyspaces,
        content    => template('profile/cache/varnish-frontend.directors.vcl.tpl.erb'),
        reload     => "/usr/local/bin/confd-reload-vcl varnish-frontend ${reload_vcl_opts}",
        before     => Service['varnish-frontend'],
    }

    # Transient storage limits T164768
    if $fe_transient_gb > 0 {
        $fe_transient_storage = "-s Transient=malloc,${fe_transient_gb}G"
    } else {
        $fe_transient_storage = ''
    }

    # Raise maximum number of memory map areas per process from 65530 to
    # $vm_max_map_count. See https://www.kernel.org/doc/Documentation/sysctl/vm.txt.
    # Varnish frontend crashes with "Error in munmap(): Cannot allocate
    # memory" are likely due to the varnish child process reaching this limit.
    # https://phabricator.wikimedia.org/T242417
    $vm_max_map_count = 262120

    sysctl::parameters { 'maximum map count':
        values => {
            'vm.max_map_count' => $vm_max_map_count,
        }
    }

    class { 'prometheus::node_varnishd_mmap_count':
        service => 'varnish-frontend.service',
    }

    monitoring::check_prometheus { 'varnishd-mmap-count':
        description     => 'Varnish number of memory map areas',
        query           => "scalar(varnishd_mmap_count{instance=\"${::hostname}:9100\"})",
        method          => 'gt',
        warning         => $vm_max_map_count - 5000,
        critical        => $vm_max_map_count - 1000,
        prometheus_url  => "http://prometheus.svc.${::site}.wmnet/ops",
        notes_link      => 'https://wikitech.wikimedia.org/wiki/Varnish',
        dashboard_links => ["https://grafana.wikimedia.org/dashboard/db/cache-host-drilldown?fullscreen&orgId=1&panelId=76&var-site=${::site} prometheus/ops&var-instance=${::hostname}"],
    }

    # Monitor number of varnish file descriptors. Initially added to track
    # T243634 but generally useful.
    prometheus::node_file_count {'track vcache fds':
        paths   => [ '/proc/$(pgrep -u vcache)/fd' ],
        outfile => '/var/lib/prometheus/node.d/vcache_fds.prom',
        metric  => 'node_varnish_filedescriptors_total',
    }

    # lint:ignore:arrow_alignment
    varnish::instance { "${cache_cluster}-frontend":
        instance_name      => 'frontend',
        vcl                => "${cache_cluster}-frontend",
        separate_vcl       => $separate_vcl_frontend,
        extra_vcl          => $fe_extra_vcl,
        ports              => [ '80', '3120', '3121', '3122', '3123', '3124', '3125', '3126', '3127' ],
        admin_port         => 6082,
        runtime_params     => join(prefix($runtime_params, '-p '), ' '),
        storage            => "-s malloc,${fe_mem_gb}G ${fe_transient_storage}",
        jemalloc_conf      => $fe_jemalloc_conf,
        backend_caches     => $cache_nodes[$cache_cluster]["${::site}_ats"],
        backend_options    => $fe_cache_be_opts,
        vcl_config         => $vcl_config,
        wikimedia_nets     => $wikimedia_nets,
        wikimedia_trust    => $wikimedia_trust,
    }
    # lint:endignore
}