VMOD directors - Varnish Directors Modul·续

Add a backend backend to the director.

ident : Optionally specify an identification string for this backend, which will be hashed by xshard.reconfigure() to construct the consistent hashing ring. The identification string defaults to the backend name.

ident allows to add multiple instances of the same backend.

rampup : Optionally specify a specific rampup time for this backend. Otherwise, the per-director rampup time is used (see xshard.set_rampup()).

weight : Optionally specify a weight to scale the xshard.reconfigure() replicas parameter. weight is limited to at least 1. Values above 10 probably do not make much sense. The effect of weight is also capped such that the total number of replicas does not exceed UINT32_MAX.

BOOL xshard.remove_backend([BACKEND backend], [STRING ident])#

BOOL xshard.remove_backend(
      [BACKEND backend=0],
      [STRING ident=0]
)

Remove backend(s) from the director. Either backend or ident must be specified. ident removes a specific instance. If backend is given without ident , all instances of this backend are removed.

BOOL xshard.clear()#

Remove all backends from the director.

BOOL xshard.reconfigure(INT replicas=67)#

Explicitly reconfigure the consistent hashing ring to reflect backend changes to become effective immediately.

If this method is not called explicitly, reconfiguration happens at the end of the current task (after vcl_init {} or when the current client or backend task is finished).

INT xshard.key(STRING)#

Convenience method to generate a sharding key for use with the key argument to the xshard.backend() method by hashing the given string with SHA256.

To generate sharding keys using other hashes, use a custom vmod like vmod blobdigest with the key_blob argument of the xshard.backend() method.

BACKEND xshard.backend([ENUM by], [INT key], [BLOB key_blob], [INT alt], [REAL warmup], [BOOL rampup], [ENUM healthy], [BLOB param], [ENUM resolve])#

BACKEND xshard.backend(
      [ENUM {HASH, URL, KEY, BLOB} by=HASH],
      [INT key],
      [BLOB key_blob],
      [INT alt=0],
      [REAL warmup=-1],
      [BOOL rampup=1],
      [ENUM {CHOSEN, IGNORE, ALL} healthy=CHOSEN],
      [BLOB param],
      [ENUM {NOW, LAZY} resolve]
)

Lookup a backend on the consistent hashing ring.

This documentation uses the notion of an order of backends for a particular shard key. This order is deterministic but seemingly random as determined by the consistent hashing algorithm and is likely to differ for different keys, depending on the number of backends and the number of replicas. In particular, the backend order referred to here is not the order given when backends are added.

  • by how to determine the sharding key

  • HASH:

    • when called in backend context and in vcl_pipe {}: Use the varnish hash value as set by vcl_hash{}
    • when called in client context other than vcl_pipe {}: hash req.url
    • URL: hash req.url / bereq.url
    • KEY: use the key argument
    • BLOB: use the key_blob argument
  • key lookup key with by=KEY

the xshard.key() method may come handy to generate a sharding key from custom strings.

  • key_blob lookup key with by=BLOB

Currently, this uses the first 4 bytes from the given blob in network byte order (big endian), left-padded with zeros for blobs smaller than 4 bytes.

  • alt alternative backend selection

Select the alt -th alternative backend for the given key.

This is particularly useful for retries / restarts due to backend errors: By setting alt=req.restarts or alt=bereq.retries with healthy=ALL, another server gets selected.

The rampup and warmup features are only active for alt==0

  • rampup slow start for servers which just went healthy

If alt==0 and the chosen backend is in its rampup period, with a probability proportional to the fraction of time since the backup became healthy to the rampup period, return the next alternative backend, unless this is also in its rampup period.

The default rampup interval can be set per shard director using the xshard.set_rampup() method or specifically per backend with the xshard.add_backend() method.

  • warmup probabilistic alternative server selection

possible values: -1, 0..1

-1: use the warmup probability from the director definition

Only used for alt==0: Sets the ratio of requests (0.0 to 1.0) that goes to the next alternate backend to warm it up when the preferred backend is healthy. Not active if any of the preferred or alternative backend are in rampup.

warmup=0.5 is a convenient way to spread the load for each key over two backends under normal operating conditions.

  • healthy

  • CHOSEN: Return a healthy backend if possible.

For alt==0, return the first healthy backend or none.

For alt > 0, ignore the health state of backends skipped for alternative backend selection, then return the next healthy backend. If this does not exist, return the last healthy backend of those skipped or none.

  • IGNORE: Completely ignore backend health state

Just return the first or alt -th alternative backend, ignoring health state, rampup and warmup.

  • ALL: Check health state also for alternative backend selection

For alt > 0, return the alt -th alternative backend of all those healthy, the last healthy backend found or none.

  • resolve

default: LAZY in vcl_init{}, NOW otherwise

  • NOW: look up a backend and return it.

Cannot be used in vcl_init{}.

  • LAZY: return an instance of this director for later backend resolution.

LAZY mode is required for referencing shard director instances, for example as backends for other directors (director layering).

In vcl_init{} and on the client side, LAZY mode cannot be used with any other argument.

On the backend side and in vcl_pipe {}, parameters from arguments or an associated parameter set affect the shard director instance for the backend request irrespective of where it is referenced.

  • param

Use or associate a parameter set. The value of the param argument must be a call to the xshard_param.use() method.

default: as set by xshard.associate() or unset.

Implementation notes for use of parameter sets with resolve=LAZY:

  • A param argument remains associated and any changes to the associated parameter set affect the sharding decision once the director resolves to an actual backend.
    • If other parameter arguments are also given, they have preference and are kept even if the parameter set given by the param argument is subsequently changed within the same backend request.
    • Each call to xshard.backend() overrides any previous call.

VOID xshard.debug(INT)#

intentionally undocumented

new xshard_param = directors.shard_param()#

Create a shard parameter set.

A parameter set allows for re-use of xshard.backend() arguments across many shard director instances and simplifies advanced use cases (e.g. shard director with custom parameters layered below other directors).

Parameter sets have two scopes:

  • per-VCL scope defined in vcl_init{}
  • per backend request scope

The per-VCL scope defines defaults for the per backend scope. Any changes to a parameter set in backend context and in vcl_pipe {} only affect the respective backend request.

Parameter sets cannot be used in client context except for vcl_pipe {}.

The following example is a typical use case: A parameter set is associated with several directors. Director choice happens on the client side and parameters are changed on the backend side to implement retries on alternative backends:

sub vcl_init {
  new shard_param = directors.shard_param();

  new dir_A = directors.shard();
  dir_A.add_backend(...);
  dir_A.reconfigure();
  dir_A.associate(shard_param.use()); # <-- !

  new dir_B = directors.shard();
  dir_B.add_backend(...);
  dir_B.reconfigure();
  dir_B.associate(shard_param.use()); # <-- !
}

sub vcl_recv {
  if (...) {
    set req.backend_hint = dir_A.backend(resolve=LAZY);
  } else {
    set req.backend_hint = dir_B.backend(resolve=LAZY);
  }
}

sub vcl_backend_fetch {
  # changes dir_A and dir_B behaviour
  shard_param.set(alt=bereq.retries, by=URL);
}

VOID xshard_param.clear()#

Reset the parameter set to default values as documented for xshard.backend().

  • in vcl_init{}, resets the parameter set default for this VCL in
  • backend context and in vcl_pipe {}, resets the parameter set for this backend request to the VCL defaults

Restricted to: vcl_pipe, backend, housekeeping.

VOID xshard_param.set([ENUM by], [INT key], [BLOB key_blob], [INT alt], [REAL warmup], [BOOL rampup], [ENUM healthy])#

VOID xshard_param.set(
      [ENUM {HASH, URL, KEY, BLOB} by],
      [INT key],
      [BLOB key_blob],
      [INT alt],
      [REAL warmup],
      [BOOL rampup],
      [ENUM {CHOSEN, IGNORE, ALL} healthy]
)

Change the given parameters of a parameter set as documented for xshard.backend().

  • in vcl_init{}, changes the parameter set default for this VCL
  • in backend context and in vcl_pipe {}, changes the parameter set for this backend request, keeping the defaults set for this VCL for unspecified arguments.

Restricted to: vcl_pipe, backend, housekeeping.

STRING xshard_param.get_by()#

Get a string representation of the by enum argument which denotes how a shard director using this parameter object would derive the shard key. See xshard.backend().

INT xshard_param.get_key()#

Get the key which a shard director using this parameter object would use. See xshard.backend().

INT xshard_param.get_alt()#

Get the alt parameter which a shard director using this parameter object would use. See xshard.backend().

REAL xshard_param.get_warmup()#

Get the warmup parameter which a shard director using this parameter object would use. See xshard.backend().

BOOL xshard_param.get_rampup()#

Get the rampup parameter which a shard director using this parameter object would use. See xshard.backend().

STRING xshard_param.get_healthy()#

Get a string representation of the healthy enum argument which a shard director using this parameter object would use. See xshard.backend().

BLOB xshard_param.use()#

For use with the param argument of xshard.backend() to associate this shard parameter set with a shard director.

Restricted to: vcl_pipe, backend, housekeeping.

BACKEND lookup(STRING)#

Lookup a backend by its name.

Restricted to: housekeeping.

ACKNOWLEDGEMENTS#

Development of a previous version of the shard director was partly sponsored by Deutsche Telekom AG - Products & Innovation.

Development of a previous version of the shard director was partly sponsored by BILD GmbH & Co KG.

This document is licensed under the same licence as Varnish
itself. See LICENCE for details.

SPDX-License-Identifier: BSD-2-Clause

Copyright (c) 2013-2015 Varnish Software AS
Copyright 2009-2020 UPLEX - Nils Goroll Systemoptimierung
All rights reserved.

Authors: Poul-Henning Kamp &lt;phk@FreeBSD.org>
         Julian Wiesener &lt;jw@uplex.de>
         Nils Goroll &lt;slink@uplex.de>
         Geoffrey Simmons &lt;geoff@uplex.de>

SPDX-License-Identifier: BSD-2-Clause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.