Limit the rate of user commands

You can stop a single user, script, or build agent from overloading P4 Server by limiting how much work each user can start. Rate limits refuse new commands from a user who is already running too many commands or has recently run too many expensive commands, and they stop any single command that accumulates too much cost.

You set rate limits in the Group specification, alongside the other "MaxLimit" fields described in Limit database queries and Server resources . The server tracks each user's activity separately, so one busy user does not use up the allowance of other users.

You must be a user with super access or the operator user type to run p4 admin ratelimit.

Understand the rate limits

The Group specification has three rate-limit fields. They appear after MaxMemory and before Timeout. Each field takes a number, unset, or unlimited. The default is unset, which means no limit.

Field Unit Effect

MaxCost

Cost

Limits the cost that a single command can accumulate. The server terminates a command that exceeds the limit.

CostThreshold

Cost

Limits the average cost of a user's recent commands. A user who exceeds the average cannot start additional commands until the average falls.

MaxCommands

Commands

Limits the number of commands that a user can run at the same time. A user who is already running this many commands cannot start additional commands.

A limit applies to each member of the group individually, not to the group as a whole. If a user belongs to more than one group, the highest value of each limit applies, and unlimited overrides any number.

To see the limits that are set, run p4 groups -v. The output includes the maxCost, costThreshold, and maxCommands values.

Understand how each limit works

Each command that a user runs accrues cost for as long as it runs. The server checks the three limits at different points.

MaxCommands

The server checks MaxCommands when a command starts, and at no other time. A new command counts itself, so with MaxCommands set to 4, the server refuses the fifth command that the user runs at the same time. A refused command can be retried after 1 second.

CostThreshold

The server checks CostThreshold when a command starts, and at no other time. The server refuses the command if the user's current cost plus the user's moving average cost is greater than the threshold.

The moving average uses a fixed window of 60 seconds that you cannot configure. At the end of each window, the server combines the cost of the window with the previous average and halves the result. After more than 16 idle windows, the server resets both values to 0. A refused command can be retried after 61 to 120 seconds.

MaxCost

MaxCost is a ceiling on a single command. No other limit terminates a command that is already running. The server checks the cost periodically while the command runs, so a command can pass the limit by a margin before the server stops it. The server logs the reason as killed by MaxCost.

Like MaxScanRows and the other Max* limits, MaxCost can terminate a command only during the compute phase of the command. After the compute phase ends, the command continues to accrue cost, but the server does not terminate the command for MaxCost.

Set up rate limiting

Rate limits take effect only when the server records per-user activity and enforces limits. Complete the following steps before you rely on limits in a group.

You must set the rt.monitorfile configurable before rate limiting is available. If rt.monitorfile is unset or off, the server does not enforce rate limits, and p4 admin ratelimit returns the error "Couldn't create user memory segment".
  1. To make the server charge cost to commands, set the monitor configurable to a non-zero value. For example:

    $ p4 configure set monitor=1

    When monitor is 0, the server charges no cost to any command, so CostThreshold and MaxCost have no effect. MaxCommands still applies. If you change monitor from 0 to a non-zero value, you must stop and restart the server.

  2. To enable the per-user records that rate limiting uses, set the rt.monitorfile configurable. For example:

    $ p4 configure set rt.monitorfile=monitor.mem

    The server sizes the monitor file from the number of licensed users when the server starts, and does not resize it. If the file is full, the server logs "Monitor file is full" and does not track users it sees for the first time. To increase the size, you must restart the server.

  3. To enforce the limits, set the server.commandlimits configurable to 1 or 2. For example:

    $ p4 configure set server.commandlimits=1
    When server.commandlimits is 0, which is the default, users can bypass their own limits by using the -zmaxCost=, -zcostThreshold=, or -zmaxCommands= global options. Set server.commandlimits to 1 or 2 to enforce the limits.
  4. To set the limits, run p4 group for the group, and set MaxCost, CostThreshold, MaxCommands, or all three.

  5. To check the result, run p4 groups -v.

The server begins to enforce limits after a user authenticates. Commands that are not authenticated, such as a p4 login command waiting at the password prompt, are not counted. Rate limits apply at every security level, including 0.

On a case-insensitive server, user names are case-insensitive. For example, BOB and bob are the same user, with one rate-limit record and one MaxCommands allowance.

Example:  Group specification with rate limits

This Group specification limits each member of the group buildfarm to 8 simultaneous commands, to an average recent cost of 500000, and to a cost of 2000000 for any one command. The values are for illustration only. Choose values that suit your site.

Group:         buildfarm
MaxResults:    unset
MaxScanRows:   unset
MaxLockTime:   unset
MaxOpenFiles:  unset
MaxMemory:     unset
MaxCost:       2000000
CostThreshold: 500000
MaxCommands:   8
Timeout:       43200
Subgroups:
Owners:
Users:
        build1
        build2

Each of build1 and build2 has its own allowance. If build1 reaches 8 running commands, the server refuses the next command from build1 only.

Respond to a refused command

When the server refuses a command because of CostThreshold or MaxCommands, the user sees:

Command terminated by rate limit, try again in seconds seconds.

When the server terminates a running command because of MaxCost, the user sees:

Operation accumulated too much cost (over maxCost); see 'p4 help ratelimit'.

The client does not retry a refused command by default. To retry automatically, choose one of the following options:

  • Run the command with the -r global option and the number of retries. For example, p4 -r 3 sync.

  • In an application that uses the P4API, call ClientApi::SetRPCRetries() with the number of retries.

When a retry is enabled and the server refuses a command, the client disconnects, waits for the number of seconds that the server specifies, reconnects, and sends the same command again. The wait is limited to between 0 and 300 seconds. All applications that use ClientApi can retry in this way.

Know which commands are exempt

The server never refuses the following commands because of a rate limit. The server still counts these commands and adds their cost to the user's total.

  • export, failover, failback, info, login, login2, logout, passwd, ping, protect, serverid, and triggers

  • admin, dbstat, dbverify, cluster, configure, counter, counters, depots, diagnostics, diskspace, heartbeat, journals, journalcopy, journaldbchecksums, lockstat, logappend, logexport, logparse, logrotate, logschema, logstat, logtail, monitor, pull, secrets, servers, user, users, and topology

Understand rate limits across servers

Consider the following behavior when you run more than one server:

  • The server never refuses a remote command (rmt-*) or a command that transmits user data when it starts, and never stops one because of MaxCost. The server still adds the cost of these commands to the user's moving average. The rmt-Push and rmt-Fetch commands remain subject to the limits.

  • The server refuses a p4 submit command that exceeds a limit on the server where the user starts the submit.

  • The transmit connections of a parallel p4 sync or p4 submit count toward MaxCommands. For example, a parallel sync that uses 4 threads counts as 5 running commands.

  • Triggers and extensions that run as the user are ordinary commands, and the server can refuse them.

Understand the effect of a restart

When the server restarts and has sole access to the monitor file, it resets the count of running commands. A command whose process ends abnormally keeps its place in the count until the server restarts or you run p4 admin ratelimit off for the user.

Exempt a user from rate limits

You can exempt a user, such as a service account, from all three limits without changing any group.

  1. To exempt the user, run p4 admin ratelimit off with the username. For example:

    $ p4 admin ratelimit off build1

    The command prints the current rate-limit record of the user, in the form user vversion commands n cost n movingAverage n overrides n/flag.

  2. To restore the limits later, run p4 admin ratelimit on with the username:

    $ p4 admin ratelimit on build1
The exemption applies only on the server where you run the command, and lasts only as long as the monitor file exists. The command does not check for replicas. On a distributed installation, run the command on each server where the user must be exempt.

Monitor rate limits

Use the following tools to see how users are affected by rate limits.

  • To see per-user records, run p4 monitor realtime --user. To see the record of one user, run p4 monitor realtime --user=username. The record includes the tagged fields runningCommands, runningCost, movingAverage, and overrides. The -a option does not show the per-user records. The same fields appear in the output of p4 diagnostics.

  • In the structured server log, the f_cost field of command-end and command-compute events records the cost of a command. The value is -1 when the server has no rate-limit record for the user. For details, see Using structured logging.

  • In the server log and track output, the "completed" line ends with the cost of the command, followed by c.

The output of p4 monitor realtime is ordered by name. If a script parses the output by position, update the script.