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

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

Overview

Parameters:

  • cache_nodes (Hash[String, Hash]) (defaults to: lookup('cache::nodes'))
  • cache_cluster (String) (defaults to: lookup('cache::cluster'))
  • conftool_prefix (String) (defaults to: lookup('conftool_prefix'))
  • fe_vcl_config (Hash[String, Any]) (defaults to: lookup('profile::cache::varnish::frontend::fe_vcl_config'))
  • fe_cache_be_opts (Hash[String, Any]) (defaults to: lookup('profile::cache::varnish::cache_be_opts'))
  • backends_in_etcd (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::backends_in_etcd', {'default_value' => true}))
  • fe_jemalloc_conf (String) (defaults to: lookup('profile::cache::varnish::frontend::fe_jemalloc_conf'))
  • fe_extra_vcl (Array[String]) (defaults to: lookup('profile::cache::varnish::frontend::fe_extra_vcl'))
  • runtime_params (Array[String]) (defaults to: lookup('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' => {}}))
  • packages_component (String) (defaults to: lookup('profile::cache::varnish::frontend::packages_component', {'default_value' => 'component/varnish6'}))
  • separate_vcl (Array[String]) (defaults to: lookup('profile::cache::varnish::separate_vcl', {'default_value' => []}))
  • fe_transient_gb (Integer) (defaults to: lookup('profile::cache::varnish::frontend::transient_gb', {'default_value' => 0}))
  • has_lvs (Boolean) (defaults to: lookup('has_lvs', {'default_value' => true}))
  • listen_uds (Optional[Array[Stdlib::Unixpath]]) (defaults to: lookup('profile::cache::varnish::frontend::listen_uds', {'default_value' => undef}))
  • single_backend_experiment (Optional[Stdlib::Fqdn]) (defaults to: lookup('cache::single_backend_fqdn', {'default_value' => undef}))
  • uds_owner (String) (defaults to: lookup('profile::cache::varnish::frontend::uds_owner', {'default_value' => 'root'}))
  • uds_group (String) (defaults to: lookup('profile::cache::varnish::frontend::uds_group', {'default_value' => 'root'}))
  • uds_mode (Stdlib::Filemode) (defaults to: lookup('profile::cache::varnish::frontend::uds_mode', {'default_value' => '700'}))


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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'modules/profile/manifests/cache/varnish/frontend.pp', line 1

class profile::cache::varnish::frontend (
    Hash[String, Hash] $cache_nodes = lookup('cache::nodes'),
    String $cache_cluster = lookup('cache::cluster'),
    String $conftool_prefix = lookup('conftool_prefix'),
    Hash[String, Any] $fe_vcl_config = lookup('profile::cache::varnish::frontend::fe_vcl_config'),
    Hash[String, Any] $fe_cache_be_opts = lookup('profile::cache::varnish::cache_be_opts'),
    Boolean $backends_in_etcd = lookup('profile::cache::varnish::frontend::backends_in_etcd', {'default_value' => true}),
    String $fe_jemalloc_conf = lookup('profile::cache::varnish::frontend::fe_jemalloc_conf'),
    Array[String] $fe_extra_vcl = lookup('profile::cache::varnish::frontend::fe_extra_vcl'),
    Array[String] $runtime_params = lookup('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' => {}}),
    String $packages_component = lookup('profile::cache::varnish::frontend::packages_component', {'default_value' => 'component/varnish6'}),
    Array[String] $separate_vcl = lookup('profile::cache::varnish::separate_vcl', {'default_value' =>  []}),
    Integer $fe_transient_gb = lookup('profile::cache::varnish::frontend::transient_gb', {'default_value' => 0}),
    Boolean $has_lvs = lookup('has_lvs', {'default_value' => true}),
    Optional[Array[Stdlib::Unixpath]] $listen_uds = lookup('profile::cache::varnish::frontend::listen_uds', {'default_value' => undef}),
    Optional[Stdlib::Fqdn] $single_backend_experiment = lookup('cache::single_backend_fqdn', {'default_value' => undef}),
    String $uds_owner = lookup('profile::cache::varnish::frontend::uds_owner', {'default_value' => 'root'}),
    String $uds_group = lookup('profile::cache::varnish::frontend::uds_group', {'default_value' => 'root'}),
    Stdlib::Filemode $uds_mode = lookup('profile::cache::varnish::frontend::uds_mode', {'default_value' => '700'}),
) {
    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
    }

    $packages = [
        'varnish',
        'varnish-modules',
        'libvmod-netmapper',
        'libvmod-re2',
    ]

    if $packages_component == 'main' {
        package { $packages:
            ensure => installed,
            before => Mount['/var/lib/varnish'],
        }
    } else {
        apt::package_from_component { 'varnish':
            component => $packages_component,
            packages  => $packages,
            before    => Mount['/var/lib/varnish'],
            priority  => 1002, # Take precedence over main
        }
    }

    # Mount /var/lib/varnish as tmpfs to avoid Linux flushing mlocked
    # shm memory to disk
    mount { '/var/lib/varnish':
        ensure  => mounted,
        device  => 'tmpfs',
        fstype  => 'tmpfs',
        options => 'noatime,defaults,size=512M',
        pass    => 0,
        dump    => 0,
    }

    # 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" }

    # Experiment with single backend CDN nodes T288106
    if $single_backend_experiment {
        if $::fqdn == $single_backend_experiment {
            $backend_caches = [ $::fqdn ]
            $etcd_backends = false
        } else {
            $backend_caches = $cache_nodes[$cache_cluster][$::site] - $single_backend_experiment
            $etcd_backends = $backends_in_etcd
        }
        $confd_experiment_fqdn = $single_backend_experiment
    } else {
        $backend_caches = $cache_nodes[$cache_cluster][$::site]
        $etcd_backends = $backends_in_etcd
        $confd_experiment_fqdn = ''
    }

    if $etcd_backends {
        # 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 = [ "${conftool_prefix}/pools/${::site}/cache_${cache_cluster}/ats-be" ]

        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     => $backend_caches,
        backend_options    => $fe_cache_be_opts,
        backends_in_etcd   => $etcd_backends,
        vcl_config         => $vcl_config,
        wikimedia_nets     => $wikimedia_nets,
        wikimedia_trust    => $wikimedia_trust,
        listen_uds         => $listen_uds,
        uds_owner          => $uds_owner,
        uds_group          => $uds_group,
        uds_mode           => $uds_mode,
    }
    # lint:endignore
}