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

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

Summary

profile to configure frontend varnis cache

Overview

Parameters:

  • cache_nodes (Hash[String, Hash]) (defaults to: lookup('cache::nodes'))

    list of all cache nodes

  • cache_cluster (String) (defaults to: lookup('cache::cluster'))

    name of cache cluster e.g. upload or text

  • conftool_prefix (String) (defaults to: lookup('conftool_prefix'))

    the prefix to use for conftool

  • fe_vcl_config (Hash[String, Any]) (defaults to: lookup('profile::cache::varnish::frontend::fe_vcl_config'))

    A hash if vcl config

  • runtime_params (Array[String]) (defaults to: lookup('profile::cache::varnish::frontend::runtime_params'))

    A hash of runtime parameters

  • fe_cache_be_opts (Hash[String, Any]) (defaults to: lookup('profile::cache::varnish::frontend::cache_be_opts'))

    hash of backend configs

  • backends_in_etcd (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::backends_in_etcd'))

    indicate if backends are in etcd

  • fe_extra_vcl (Array[String]) (defaults to: lookup('profile::cache::varnish::frontend::fe_extra_vcl'))

    list of extra VCLs

  • req_handling (Profile::Cache::Sites) (defaults to: lookup('cache::req_handling'))

    hash of domains request handling config

  • alternate_domains (Profile::Cache::Sites) (defaults to: lookup('cache::alternate_domains', {'default_value' => {}}))

    List of domains handled by misc

  • packages_component (String) (defaults to: lookup('profile::cache::varnish::frontend::packages_component'))

    The package component to use for apt

  • fe_transient_gb (Integer) (defaults to: lookup('profile::cache::varnish::frontend::transient_gb'))

    Amount of Transient=malloc to configure in GB

  • separate_vcl (Array[String]) (defaults to: lookup('profile::cache::varnish::frontend::separate_vcl'))

    list of addtional VCLs

  • has_lvs (Boolean) (defaults to: lookup('has_lvs', {'default_value' => true}))

    Indicate of cache is behind LVS

  • single_backend (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::single_backend', {'default_value' => false}))

    Feature flag to use only the host-local ats-be

  • listen_uds (Array[Stdlib::Unixpath]) (defaults to: lookup('profile::cache::varnish::frontend::listen_uds'))

    list of uds for varnish

  • uds_owner (String) (defaults to: lookup('profile::cache::varnish::frontend::uds_owner'))

    The owner of the uds sockets

  • uds_group (String) (defaults to: lookup('profile::cache::varnish::frontend::uds_group'))

    The group of the uds sockets

  • uds_mode (Stdlib::Filemode) (defaults to: lookup('profile::cache::varnish::frontend::uds_mode'))

    The mode of the uds sockets

  • use_etcd_req_filters (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::use_etcd_req_filters'))

    use confd dynamically generated rules

  • do_esitest (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::do_esitest', {'default_value' => false}))

    temporary for testing ESI

  • fe_jemalloc_conf (Optional[String]) (defaults to: lookup('profile::cache::varnish::frontend::fe_jemalloc_conf', {'default_value' => undef}))

    jemalloc configuration

  • enable_monitoring (Boolean) (defaults to: lookup('profile::cache::varnish::frontend::enable_monitoring'))


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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'modules/profile/manifests/cache/varnish/frontend.pp', line 24

class profile::cache::varnish::frontend (
    # Globals
    String                  $conftool_prefix      = lookup('conftool_prefix'),
    Boolean                 $has_lvs              = lookup('has_lvs', {'default_value'                                             => true}),
    # TODO: fix theses so they re under the profile namespace
    Hash[String, Hash]      $cache_nodes          = lookup('cache::nodes'),
    String                  $cache_cluster        = lookup('cache::cluster'),
    Profile::Cache::Sites   $req_handling         = lookup('cache::req_handling'),
    Profile::Cache::Sites   $alternate_domains    = lookup('cache::alternate_domains', {'default_value'                            => {}}),
    Boolean                 $single_backend       = lookup('profile::cache::varnish::frontend::single_backend', {'default_value'   => false}),
    # locals
    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::frontend::cache_be_opts'),
    Boolean                 $backends_in_etcd     = lookup('profile::cache::varnish::frontend::backends_in_etcd'),
    Array[String]           $fe_extra_vcl         = lookup('profile::cache::varnish::frontend::fe_extra_vcl'),
    Array[String]           $runtime_params       = lookup('profile::cache::varnish::frontend::runtime_params'),
    String                  $packages_component   = lookup('profile::cache::varnish::frontend::packages_component'),
    Array[String]           $separate_vcl         = lookup('profile::cache::varnish::frontend::separate_vcl'),
    Integer                 $fe_transient_gb      = lookup('profile::cache::varnish::frontend::transient_gb'),
    Array[Stdlib::Unixpath] $listen_uds           = lookup('profile::cache::varnish::frontend::listen_uds'),
    String                  $uds_owner            = lookup('profile::cache::varnish::frontend::uds_owner'),
    String                  $uds_group            = lookup('profile::cache::varnish::frontend::uds_group'),
    Stdlib::Filemode        $uds_mode             = lookup('profile::cache::varnish::frontend::uds_mode'),
    Boolean                 $use_etcd_req_filters = lookup('profile::cache::varnish::frontend::use_etcd_req_filters'),
    Boolean                 $do_esitest           = lookup('profile::cache::varnish::frontend::do_esitest', {'default_value'       => false}),
    Boolean                 $enable_monitoring    = lookup('profile::cache::varnish::frontend::enable_monitoring'),
    Optional[String]        $fe_jemalloc_conf     = lookup('profile::cache::varnish::frontend::fe_jemalloc_conf', {'default_value' => undef}),
) {
    include profile::cache::base
    $wikimedia_nets = $profile::cache::base::wikimedia_nets
    $wikimedia_trust = $profile::cache::base::wikimedia_trust
    $wikimedia_domains = $profile::cache::base::wikimedia_domains
    $wmcs_domains = $profile::cache::base::wmcs_domains

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

    # Defaults for the vcl files
    # TODO: pass it down once we've refactored the varnish classes.
    Varnish::Wikimedia_vcl {
        etcd_filters  => $use_etcd_req_filters,
    }
    $packages = [
        'varnish',
        'varnish-modules',
        'libvmod-netmapper',
        'libvmod-re2',
        'libvmod-querysort',  # T138093
    ]

    # We need these two services disabled as we don't use them.
    systemd::mask { 'varnishncsa.service': }
    systemd::mask { 'varnishlog.service': }

    if $packages_component == 'main' {
        package { $packages:
            ensure  => installed,
            before  => Mount['/var/lib/varnish'],
            require => [
                Systemd::Mask['varnishncsa.service'],
                Systemd::Mask['varnishlog.service'],
            ],
        }
    } else {
        apt::package_from_component { 'varnish':
            component => $packages_component,
            packages  => $packages,
            before    => Mount['/var/lib/varnish'],
            require   => [
                Systemd::Mask['varnishncsa.service'],
                Systemd::Mask['varnishlog.service'],
            ],
            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
    # TODO: possibly convert this to facts['memory']['system']['total_bytes']
    $mem_gb = $facts['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,
        do_esitest           => $do_esitest,
    }

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

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

    # Single-backend nodes (only those with the new 6.4TB NVMe)
    if $single_backend {
        $backend_caches = [ $facts['networking']['fqdn'] ]
        $etcd_backends = false
    } else {
        $backend_caches = $cache_nodes[$cache_cluster][$::site]
        $etcd_backends = $backends_in_etcd
    }

    # Dynamic configuration sourced from etcd.
    $reload_vcl_opts = varnish::reload_vcl_opts($vcl_config['varnish_probe_ms'],
        $separate_vcl_frontend, 'frontend', "${cache_cluster}-frontend")

    $directors_keyspaces = [ "${conftool_prefix}/pools/${::site}/cache_${cache_cluster}/ats-be" ]

    # This is the etcd-driven list of backends for this frontend for chashing,
    # but deployment-prep and single-backend cases will have a false
    # $etcd_backends and thus hit ensure => absent below
    confd::file {
        '/etc/varnish/directors.frontend.vcl':
            ensure     => bool2str($etcd_backends, 'present', 'absent'),
            reload     => "/usr/local/bin/confd-reload-vcl varnish-frontend ${reload_vcl_opts}",
            before     => Service['varnish-frontend'],
            watch_keys => $directors_keyspaces,
            content    => template('profile/cache/varnish-frontend.directors.vcl.tpl.erb');
    }

    if $use_etcd_req_filters {
        confd::file {
            default:
                ensure => present,
                reload => "/usr/local/bin/confd-reload-vcl varnish-frontend ${reload_vcl_opts}",
                before => Service['varnish-frontend'];
            '/etc/varnish/blocked-nets.inc.vcl':
                watch_keys => ['/request-ipblocks/abuse'],
                content    => template('profile/cache/blocked-nets.inc.vcl.tpl.erb'),
                prefix     => $conftool_prefix;
            # request filter actions based on the content of the /request-vcl
            # tree in conftool.
            '/etc/varnish/requestctl-filters.inc.vcl':
                watch_keys => ["/request-vcl/cache-${cache_cluster}"],
                content    => template('profile/cache/varnish-frontend-requestctl-filters.vcl.tpl.erb'),
                prefix     => $conftool_prefix;
        }
    } else {
        # deployment-prep still uses the old template.
        $abuse_networks = network::parse_abuse_nets('varnish')
        file { '/etc/varnish/blocked-nets.inc.vcl':
            ensure  => present,
            content => template('profile/cache/blocked-nets.inc.vcl.erb'),
            owner   => 'root',
            group   => 'root',
            mode    => '0444',
        }

        file { '/etc/varnish/requestctl-filters.inc.vcl':
            ensure => absent,
        }
    }

    # 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',
    }

    # Monitor the mmap usage of varnish; Make sure it doesn't exceed the system limits
    class { 'prometheus::node_sysctl': }

    # 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',
    }

    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,
        wikimedia_domains => $wikimedia_domains,
        wmcs_domains      => $wmcs_domains,
        listen_uds        => $listen_uds,
        uds_owner         => $uds_owner,
        uds_group         => $uds_group,
        uds_mode          => $uds_mode,
        enable_monitoring => $enable_monitoring,
    }
}