Perforce Public Knowledge Base - Using P4Broker to redirect read-only commands
× PRODUCTS SOLUTIONS CUSTOMERS LEARN SUPPORT
Downloads Company Partners Careers Contact Free Trials
Menu Search
Perforce
Reset Search
 

 

Article

Using P4Broker to redirect read-only commands

« Go Back

Information

 
Problem

It is possible to use P4Broker to redirect read-only commands to a Perforce replica server (instead of processing those commands on the master Perforce server).

The idea is to  reduce the main server load by processing many read-only commands on the replica server. The P4Broker enables Perforce administrators to configure a single server and host address for their users and transparently force commands to use the appropriate Perforce server (master or replica).

Redirecting commands to a replica server is most useful in older versions of the Perforce Server (2010.2 or 2011.1) when read-only replica servers were first introduced. The advent of Forwarding and Build Farm replicas (in 2012.1) and Edge Servers (2013.2) arguably reduces the need for P4Broker redirection to read-only replicas for reducing master server load.

Solution

Below we provide step by step instructions for configuring the broker to redirect read-only commands to a replica. For this example, we use the following environment settings:

  • The broker is at port "broker:33333"
  • The production server is at port "master:11111"
  • The read only replica is at port "replica:22222"

Installing and configuring P4Broker

  1. Install the appropriate P4Broker software version for the selected platform.
     
  2. Create a blank broker configuration file:
    p4broker -C > /p4broker/root/p4broker.conf
    
    
  3. Edit the P4Broker configuration file to add the target, listen port, and other broker information:
    target      = master:11111;
    listen      = 33333;
    directory   = /p4broker/root/;
    logfile     = broker.log;
    debug-level = server=1;
    admin-name  = "your name";
    admin-phone = x1234;
    admin-email = your.name@yourcompany.dom
    redirection = selective;
    
  4. Add an "altserver" for the replica:

    altserver: replica1
    {
        target  = replica:22222;
    }
    
  5. Add command handlers to re-direct read-only metadata commands to the replica:

    If running P4Broker 2012.1 or later:
    command: ^(branches|changes|clients|counters|depots|dirs \
                    |filelog|files|fstat|groups|interchanges|jobs|labels|opened \ 
                    |sizes|fixes|where|workspaces|users)$
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: ^describe$
    {
        flags   = -s;
        action  = redirect;
        destination = replica1;
    }
    
    # Specifications using the -o flag to output to STDOUT also do not
    # write to the DB except "p4 resolve -o", "p4 integrate -o" and "p4
    # print -o" which should pass to the production server:
    
    command: ^(resolve|integ|integrate|print)$
    {
        flags   = -o;
        action  = pass;
    }
    
    command: .*
    {
        flags   = -o;
        action  = redirect;
        destination = replica1;
    }
    
    # If the replica server also replicates depot files, the following command handlers can be added:
    
    command: ^(annotate|describe|diff2|grep|print|verify)$
    {
        action  = redirect;
        destination = replica1;
    }
    
    # Read/Write Commands with Read-Only flags:
    #
    # sync -p does not write anything to db.have, so it goes to the replica:
    
    command: ^sync$
    {
        flags   = -p;
        action  = redirect;
        destination = replica1;
    }
    
    

    For P4Broker release prior to 2012.1, regular expressions are not supported in command handlers. For these older releases, the broker configuration needs to be expanded for each command as follows:

    command: branches
    		
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: changes
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: clients
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: counters
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: depots
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: describe
    {
        flags   = -s;
        action  = redirect;
        destination = replica1;
    }
    
    command: dirs
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: filelog
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: files
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: fstat
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: groups
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: jobs
    {
    
        action  = redirect;
        destination = replica1;
    }
    
    command: labels
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: opened
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: sizes
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: fixes
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: where
    {
        action  = redirect;
        destination = replica1;
    } 
    
    command: workspaces
    {
        action  = redirect;
        destination = replica1;
    } 
    
    command: users
    {
        action  = redirect;
        destination = replica1;
    }
    
    
    # Specifications using the -o flag to output to STDOUT also do not
    # write to the DB except "p4 resolve -o", "p4 integrate -o" and "p4
    # print -o" which should pass to the production server:
    
    command: resolve
    {
        flags   = -o;
        action  = pass;
    }
    
    command: integrate
    {
        flags   = -o;
        action  = pass;
    }
    
    command: integ
    {
        flags   = -o;
        action  = pass;
    }
    
    command: print
    {
        flags   = -o;
        action  = pass;
    }
    
    command: *
    {
        flags   = -o;
        action  = redirect;
        destination = replica1;
    }
    
    # If the replica server also replicates depot files, 
    # the following command handlers can be added:
    
    command: annotate
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: describe
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: diff2
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: grep
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: print
    {
        action  = redirect;
        destination = replica1;
    }
    
    command: verify
    {
        action  = redirect;
        destination = replica1;
    }
    
    # Read/Write Commands with Read-Only flags:
    #
    # sync -p does not write anything to db.have, so it goes to the replica:
    
    command: sync
    {
        flags   = -p;
        action  = redirect;
        destination = replica1;
    }
    
    
  6. Start the broker normally, e.g. for Unix based systems:
    
    p4broker -c /p4broker/root/p4broker.conf -d
    
    
    Or Windows:
    
    svcinst start -n P4Broker

Notes:

  • Any read-only commands directed to the P4Broker at port "broker:33333" are directed to the replica at port "replica:22222" as configured above.  The user needs access to this replica server.  Use a central authorization server (P4AUTH) as recommended in section of the 'Multi-Site Deployment' guide that covers the P4Broker. As a test, try authorizing using an IP-less login ticket as follows:
    
    p4 -p broker:port login -a
    
    
  • In general, any commands not matching the above handlers are re-directed to the master server "master:11111". However, the default selective mode redirection will make all subsequent commands over the same network session run against the default master server if the first command in that session is redirected to the default master server. This is done to improve GUI experiences with products like P4V.  Redirection mode can be set to 'pedantic', so that all redirection requests are honoured.
     
  • Details on how to use P4Broker can be found in the P4Broker release notes.
     
  • The "Multi-Site Deployment" administrator's guide covers replication, and provides instructions for setting up a replica server. 

 

Related Links

Feedback

 

Was this article helpful?


   

Feedback

Please tell us how we can make this article more useful.

Characters Remaining: 255