Manual:Maxlag parameter/pt

From Linux Web Expert

Se estiver a executar o MediaWiki num agrupamento de bases de dados replicadas (tal como o da Wikimedia), então frequências de edição elevadas podem causar atrasos nos servidores escravos. One way to mitigate replication lag is to have all bots and maintenance tasks automatically stop whenever lag goes above a certain value. In MediaWiki 1.10, the maxlag parameter was introduced which allows the same thing to be done in client-side scripts. Since 1.27, it only applies to api.php requests.

The maxlag parameter can be passed to api.php through a URL parameter or POST data. It is an integer number of seconds. For example, this link shows metadata about the page "MediaWiki" unless the lag is greater than 1 second while this one (with -1 at the end) shows you the actual lag without metadata.

If the specified lag is exceeded at the time of the request, the API returns an error (with 200 status code, see <translate> task <tvar name=1>T33156</tvar></translate>) like the following:

{
    "error": {
        "code": "maxlag",
        "info": "Waiting for $host: $lag seconds lagged",
        "host": $host,
        "lag": $lag,
        "*": "See https://www.mediawiki.org/w/api.php for API usage"
    }
}

Os seguintes cabeçalhos HTTP são definidos:

  • Retry-After: a recommended minimum number of seconds that the client should wait before retrying
  • X-Database-Lag: The number of seconds of lag of the most lagged replica

Recommended usage for Wikimedia wikis is as follows:

  • Use maxlag=5 (5 seconds). This is an appropriate non-aggressive value, set as default value on Pywikibot. Higher values mean more aggressive behaviour, lower values are nicer.
  • If you get a lag error, pause your script for at least 5 seconds before trying again. Be careful not to go into a busy loop.
  • It's possible that with this value, you may get a low duty cycle at times of high database load. That's OK, just let it wait for off-peak. We give humans priority at times of high load because we don't want to waste their time by rejecting their edits.
  • Unusually high or persistent lag should be reported to <span class="plainlinks" style="font-family: monospace,Courier; white-space: pre-wrap !important; word-wrap: break-word; max-width: 1200px; overflow: auto;" title="<translate nowrap> <tvar name=1>#wikimedia-tech</tvar> on Libera.Chat IRC</translate>">#wikimedia-tech <translate> connect</translate> on IRC.
  • Interactive tasks (where a user is waiting for the result) may omit the maxlag parameter. Noninteractive tasks should always use it.

See also API:Etiquette#The maxlag parameter.

Note that the caching layer (Varnish or squid) may also generate error messages with a 503 status code, due to timeout of an upstream server. Clients should treat these errors differently, because they may occur consistently when you try to perform a long-running expensive operation. Repeating the operation on timeout would use excessive server resources and may leave your client in an infinite loop. You can distinguish between cache-layer errors and MediaWiki lag conditions using any of the following:

  • X-Database-Lag header is distinctive to replication lag errors in MediaWiki
  • No Retry-After in Varnish errors
  • X-Squid-Error header should be present in squid errors
  • The response body in replication lag errors will match the regex /Waiting for [^ ]*: [0-9.-]+ seconds? lagged/

Para fins de teste, poderá intencionalmente fazer com que o software recuse um pedido passando um valor negativo, tal como no seguinte URL: https://www.linuxwebexpert.com/w/api.php?action=query&titles=MediaWiki&format=json&maxlag=-1.