# User's Guide

## Overview

The p_usrloc module is built upon the usrloc module and provides the same usrloc export to the registrar module (for example). The usrloc module must not be loaded at the same time with the p_usrloc module. The parameters and the interface for the p_usrloc module are thus almost the same, so in this document only extra parameters (specific only to p_usrloc module) and differences are noted. For a complete description of the parameters that are inherited from usrloc see the page here. Note that the modparam statement must still refer to the p_usrloc module.

The goal of the p_usrloc module is to provide partitioned user location service to Kamailio/SER. This provides three major benefits:

- Redundancy Failure of a location database does not mean that the location service is down

- Load Balancing DB performance is greatly increased as queries are automatically split on a pool of ID(number configurable at runtime). Furthermore, a read only db handler can be used to further increase the spread of the "select" queries.

- Failover A faulty DB server is detected and taken out. When it recovers, the module makes sure that no stale contacts are returned from it.

Write queries are replicated to two or more databases, depending on some distribution keys, e.g. the username and domain the subscribers. The number of databases used for replication is configurable at compile time by modifying the DB_NUM define. Its main field of application is for storing and retrieving location data. The actual database for this subscriber is chosen from a set that is specified in the master database. Read queries are executed on one database of the set. If this database is not available, the other database would be used. This failure is recorded in the master database.

Connections to the distributed databases are set up prior the actual query is executed. The module maintains a cache of the database handles to provide better performance. This handles are periodically checked if there are still valid and if needed updated from the master database. As default only one key is used for the database lookup, but the additional usage of a second one is possible.

Changes in the master database are autodetected. For better performance it is possible to use one read-only instance, that is locally installed, and write only errors and other status information to a central master database. It is also possible to specify different failover schemas and even use transaction to provide additional data safety. This module could further try to minimize the impact of a database error with the usage of spare databases that are automatically activated. It also supports the manual deactivation of redundant databases for maintenance purposes.

The p_usrloc module still has some missing feature, like automatic expiry of contacts and dumping of all users via the fifo cmd.

## Dependencies

### &kamailio; Modules

The following modules must be loaded before this module:

- a database module necessary to connect to the master database and to the distributed databases.

### External Libraries or Applications

The following libraries or applications must be installed before running &kamailio; with this module loaded:

- None.

## Parameters

### `write_db_url` (string)

The url to the master database where errors are written to.

Default value is &defaultdb;

#### Set `write_db_url` parameter

    ...
    modparam("p_usrloc", "write_db_url", "mysql://username:password@localhost/databasename")
    ...

### `read_db_url` (string)

The url to the master database where the distribution data is read from. It is separated from write access, so for better performance, a local replicate can be used for read access.

Default value is &defaultdb; .

#### Set `read_db_url` parameter

    ...
    modparam("p_usrloc", "read_db_url", "mysql://user:passwd@localhost/db")
    ...

### `reg_db_table` (string)

Specifies the table where the information about the distributed databases is stored.

Default value is locdb.

#### Set `reg_db_table` parameter

    ...
    modparam("p_usrloc", "reg_db_table", "locdb")
    ...

### `id_column` (string)

Specifies the column where the id of a distributed database is stored.

Default value is id.

#### Set `id_column` parameter

    ...
    modparam("p_usrloc", "id_column", "id")
    ...

### `num_column` (string)

Specifies the column where the associated number of the distributed database is stored. For each distributed database ID there must be at least two databases available, the databases above the second are ignored.

Default value is no.

#### Set `num_column` parameter

    ...
    modparam("p_usrloc", "num_column", "nr")
    ...

### `url_column` (string)

Specifies the column where the url of the distributed database is stored.

Default value is url.

#### Set `url_column` parameter

    ...
    modparam("p_usrloc", "url_column", "url")
    ...

### `status_column` (string)

Specifies the column where the status of the distributed database is stored.

Default value is status.

#### Set `status_column` parameter

    ...
    modparam("p_usrloc", "status_column", "status")
    ...

### `failover_time_column` (string)

Specifies the column where the failover time of the location databases is stored. This field is set to the current time when a databases is turned off or turned on.

Default value is failover.

#### Set `failover_time_column` parameter

    ...
    modparam("p_usrloc", "failover_time_column", "fail")
    ...

### `spare_flag_column` (string)

Specifies the column where the spare flag of the location databases is stored. It is possible to declare an entry in the p_usrloc table as spare which is used in a failover. Due to the fact that the data is stored in two databases and it takes the spare the complete expire time to be up to date, it is not very useful.

Default value is spare.

#### Set `spare_flag_column` parameter

    ...
    modparam("p_usrloc", "spare_flag_column", "spare")
    ...

### `error_column` (string)

Specifies the column where the errors of the distributed databases are stored. Each call to db_handle_error increases the error counter. After exceeding the error threshold, the database's status is set to off.

Default value is errors.

#### Set `error_column` parameter

    ...
    modparam("p_usrloc", "error_column", "error")
    ...

### `risk_group_column` (string)

Specifies the column where the risk group of the distributed databases is stored. All databases on one host are in the same risk group. This is only useful when using spares and prevents the module from taking a spare which shares the same risk as the broken database.

Default value is rg.

#### Set `risk_group_column` parameter

    ...
    modparam("p_usrloc", "risk_group_column", "rg")
    ...

### `expire_time` (int)

Specifies the time (in seconds) when a contact expires, used for resetting the failover time of a reactivated database. It should be equal or greater than the contact expiration time of the registrar module.

Default value is 3600.

Can be set at runtime, e.g.:

    			$ &sercmd; cfg.set_now_int p_usrloc expire_time 7200

#### Set `expire_time` parameter

    ...
    modparam("p_usrloc", "expire_time", "3600")
    ...

### `db_err_threshold` (int)

Specifies the error value on which a database shall be turned of.

Default value is 50.

Can be set at runtime, e.g.:

    			$ &sercmd; cfg.set_now_int p_usrloc db_err_threshold 20

#### Set `db_err_threshold` parameter

    ...
    modparam("p_usrloc", "db_err_threshold", "50")
    ...

### `failover_level` (int)

Specifies the manner a failover is done. Following modes are supported:

- 1 - Just turn off the broken database

- 2 - Try to find a spare, if none found, just turn off the broken database

Default value is 1.

Can be set at runtime, e.g.:

    			$ &sercmd; cfg.set_now_int p_usrloc failover_level 2

#### Set `failover_level` parameter

    ...
    modparam("p_usrloc", "failover_level", "1")
    ...

### `db_retry_interval` (int)

Specifies the interval (in seconds) in which a timer process shall check the availability of the databases and try to reconnect to broken ones. It doesn't make sense to choose a lower value as 10. It's necessary to provide a writeable master database, otherwise this check stays disabled.

Default value is 10.

#### Set `db_retry_interval` parameter

    ...
    modparam("p_usrloc", "db_retry_interval", "10")
    ...

### `db_use_transactions` (int)

Specifies if transactions should be used (set to 1) to reach a higher data consistency. Keep in mind that this will probably decrease performance.

Default value is 0.

#### Set `db_use_transactions` parameter

    ...
    modparam("p_usrloc", "db_use_transactions", "0")
    ...

### `db_transaction_level` (string)

Specifies the isolation level on which transactions are performed. Possible values: Modes supported by the database backend. In order to activate transaction the db_use_transactions parameter must be also set.

Default value is READ UNCOMMITTED.

#### Set `db_transaction_level` parameter

    ...
    modparam("p_usrloc", "db_transaction_level", "READ UNCOMMITTED")
    ...

### `write_on_master_db` (int)

Sets the write access to the master database. If set to 0, no write operations are permitted on the master database.

Default value is 0.

#### Set `write_on_master_db` parameter

    ...
    modparam("p_usrloc", "write_on_master_db", "0")
    ...

### `mdb_availability_control`(str)

Checks the master database in certain time intervals specified by the retry_interval parameter. Overwrites the write_on_master_db parameter based on the reachability of master database. If the master db is unavailable then the aforementioned parameter will be deactivated.

Default value is 0.

#### Set `mdb_availability_control` parameter

    ...
    modparam("p_usrloc", "mdb_availability_control", 1)
    ...

### `write_on_db` (int)

Sets the write access to the distributed databases. If set to 0, no write operations are permitted on the databases.

Default value is 0.

#### Set `write_on_db` parameter

    ...
    modparam("p_usrloc", "write_on_db", "0")
    ...

### `connection_expires` (int)

Specifies the period (in seconds) after a database connection expires. Usage of a too small value will probably decrease the performance.

Default value is 300.

#### Set `connection_expires` parameter

    ...
    modparam("p_usrloc", "connection_expires", "300")
    ...

### `alg_location` (int)

Specify the way the distribution of the subscriptions are computed. At the moment the only way is to use the CRC32 algorithm to compute the location ID. Any integer value is fine.

Default value is 0.

#### Set `alg_location` parameter

    ...
    modparam("p_usrloc", "alg_location", 1)
    ...

### `domain_db`(str)

Specify the way the lookup is made. In can be either partitioned or single. For example, if you have a location table that is large and needs to be partitioned, and a smaller table cfa that is ok to be on only the master db(so there is no need to have it distributed), you can set this parameter to location=cluster,cfa=single. This means that a call to

    lookup(location)

will be done via the partition databases configured via the reg_db_table parameter, but a call to

    lookup(cfa)

will be done on only the master database (as with usrloc module)

Default value is location=cluster,cfa=single.

#### Set `domain_db` parameter

    ...
    modparam("p_usrloc", "domain_db", "location=cluster,cfa=single")
    ...

### `default_db_type`(str)

In case a domain (like location,cfa) is not matched by a domain_db definition, the type is configured by using this parameter. Accepted values are single and cluster.

Default value is single.

#### Set `default_db_type` parameter

    ...
    modparam("p_usrloc", "default_db_type", "cluster")
    ...

### `db_ops_ruid` (int)

Defines how database queries are done:

- 0 uses the old style using aor, contact and call-id

- 1 uses ruid value

- 2 uses the new style using aor, contact and call-id

- 3 uses contact uniq value + ruid value

Default value is 0

Can be set at runtime, e.g.:

    		$ &sercmd; cfg.set_now_int p_usrloc db_ops_ruid 1

#### Set `db_ops_ruid` parameter

    ...
    modparam("p_usrloc", "db_ops_ruid", 2)
    ...

### `db_update_as_insert` (string)

Set this parameter if you want to do INSERT DB operations instead of UPDATE DB operations. It is recommended to set this parameter if you use Cassandra as a DB backend.

Default value is 1.

Can be set at runtime, e.g.:

    		$ &sercmd; cfg.set_now_int p_usrloc db_update_as_insert 1

#### Set `db_update_as_insert` parameter

    ...
    modparam("usrloc", "db_update_as_insert", 1)
    ...

### `default_db_url`(str)

The URL of the default database for Location domains (for domains that are single). This must be configured if they are use.

Default value is DEFAULT_DB_URL.

#### Set `default_db_type` parameter

    ...
    modparam("p_usrloc", "default_db_url", "mysql://ser:ser@localhost/ser")
    ...

### `matching_mode`(int)

Defines the matching algorithm. Possible values are:

- 0 (CONTACT_ONLY)

- 1 (CONTACT_CALLID)

- 2 (CONTACT_PATH)

Default value is 0 (CONTACT_ONLY).

#### Set `matching_mode` parameter

    ...
    modparam("p_usrloc", "matching_mode", 1)
    modparam("p_usrloc", "matching_mode", 2)
    ...

    kamctl rpc cfg.set_now_int p_usrloc matching_mode 2
    kamctl rpc cfg.get p_usrloc matching_mode

### `UTC_timestamps`(int)

Enables UTC timestamps for expires and last_modified columns. It can be changed at runtime via rpc command.

Default value is 0 (disabled).

#### Set `UTC_timestamps` parameter

    ...
    modparam("p_usrloc", "UTC_timestamps", 1)
    ...

### `use_domain_crc32`(str)

Enables or disables the crc32 of domain part for user@domain. If disabled, will calculate crc32 only for user part.

Default value is 1 (enabled).

#### Set `use_domain_crc32` parameter

    ...
    modparam("p_usrloc", "use_domain_crc32", "0")
    ...

## Changes from usrloc module

### `db_mode` (integer)

The p_usrloc module must utilize database for persistent contact storage. Only mode 3 is possible at this time. Because of the way other matching mode work, they make no sense on a distributed environment.

- 3 - DB-Only scheme. No memory cache is kept, all operations being directly performed with the database. The timer deletes all expired contacts from database - cleans after clients that didn't un-register or re-register. The mode is useful if you configure more servers sharing the same DB without any replication at SIP level. The mode may be slower due the high number of DB operation. For example NAT pinging is a killer since during each ping cycle all nated contact are loaded from the DB; The lack of memory caching also disable the statistics exports.

Default value is 3.

#### Set `db_mode` parameter

    ...
    modparam("p_usrloc", "db_mode", 2)
    ...

### `db_url`

This parameters is now obsolete, and replaced by specific p_usrloc parameters

## Installation & Running

### Database setup

#### Configuration Table

Before running &kamailio; with p_usrloc, you have to set up the master database table where the module will find data about the distributed databases. For that, if the table was not created by the installation script or you choose to install everything by yourself you can use the p_usrloc-create.sql SQL script in the database directories in the utils/kamctl/mysql/ folder as template. Database and table name can be set with module parameters so they can be changed, but the name of the columns must be as they are in the SQL script. You can also find the complete database documentation on the project webpage, &kamailiodbdocs;.

##### Example database content - reg_table (locdb) table

    ...
    +----+----+------+--------+--------+---------------------+-------+----+
    | id | no | url  | status | errors | failover            | spare | rg |
    +----+----+------+--------+--------+---------------------+-------+----+
    |  1 |  1 | URL1 |      1 |      0 | 1900-01-01 00:00:01 |     0 |  0 |
    |  1 |  2 | URL2 |      1 |      0 | 1900-01-01 00:00:01 |     0 |  0 |
    |  2 |  1 | URL3 |      1 |      0 | 1900-01-01 00:00:01 |     0 |  0 |
    |  2 |  2 | URL4 |      1 |      0 | 1900-01-01 00:00:01 |     0 |  0 |
    +----+----+------+--------+--------+---------------------+-------+----+
    ...

The URLs are omitted for a better overview, but you can use standard &kamailio; database URLs like &defaultdb; Databases don't need to be on different hosts, e.g. for testing purposes.

This table contains two database groups. The first with id one, and the second with the id two.

### Maintenance

The module supports the deactivation of redundant databases for maintenance reasons. This can be done by setting the status column of the respective database in the p_usrloc to the value 2. This setting is autodetected from all modules on the server cluster. Changes in the locdb table are periodically polled with help of a timer process. After one minute all the read and write traffic is removed from the deactivated database, and maintenance can be done.

In order to activate the database again, after the maintenance has been finished, the respective status column needs to be set to 0. This is autodetected as well, the first module that noticed the change will set the status column to 1 and setting the failover column to the current time and date. Write requests are now transferred again to the database, but no reads are done yet.

After the configured expire_time has been passed, i.e. every contact has been inserted at least one time in the database the respective failover time column is set to the default value again. The database is then also used to read contacts from, the cluster is in normal operation with full redundancy again.

### Additional configuration

As this module is only used internally from other modules, there is no additional configuration except for the module parameter setup necessary.