Class: ShardManager

Class for managing connections to the DB shards.

Details

This shard manager allows registering multiple shards and request connections to them. It ensures that only one connection to a shard is used within a request.

Implements

Authors

Oleg Schildt

Package

Source code

See also

 

Properties
protected

Internal array for storing the load balancing groups and its shards.

protected

Internal array for storing the mapping between shard names and connection parameters.

Methods
public

The method provides the DBWorker object for working with the shard.

public

The method provides the DBWorker object for working with the shard, that is chosen randomly for load balancing reason.

public

Registers a new shard.


Property: ShardManager::$load_balancing_groups

protected array $load_balancing_groups = [];

Internal array for storing the load balancing groups and its shards.

Type

array

Authors

Oleg Schildt

Source code


Property: ShardManager::$shard_table

protected array $shard_table = [];

Internal array for storing the mapping between shard names and connection parameters.

Type

array

Authors

Oleg Schildt

Source code


Method: ShardManager::dbshard()

public function dbshard(string $shard_name): ?\SmartFactory\DatabaseWorkers\DBWorker;

The method provides the DBWorker object for working with the shard.

Details

If the parameters are omitted, the system takes the parameters from the configuration settings and reuses the single instance of the DBWorker for all requests. If the user passes the parameters explicitly, a new instance of the DBWorker is created upon each new request.

Currently supported: MySQL, PostgreSQL und MS SQL.

Parameters

Name Pass type Value type Default value Description
$shard_name by value string

The name of the shard.

Returns

?\SmartFactory\DatabaseWorkers\DBWorker

returns DBWorker object or null if the object could not be created.

Throws

\Exception

It might throw an exception in the case of any errors:

  • if the interface or class does not exist.
  • if the shard was not found.
  • if the check of the classes and interfaces fails.
  • if the php extension is not installed.
  • db_missing_type_error - if the database type is not specified.
  • db_conn_data_error - if the connection parameters are incomplete.
  • db_server_conn_error - if the database server cannot be connected.
  • db_not_exists_error - if database does not exist od inaccessible to the user.

Overrides

Authors

Oleg Schildt

Source code


Method: ShardManager::randomDBShard()

public function randomDBShard(string $load_balancing_group): ?\SmartFactory\DatabaseWorkers\DBWorker;

The method provides the DBWorker object for working with the shard, that is chosen randomly for load balancing reason.

Parameters

Name Pass type Value type Default value Description
$load_balancing_group by value string

The name of the load balancing group, from which the shard should be randomly picked.

Returns

?\SmartFactory\DatabaseWorkers\DBWorker

returns DBWorker object or null if the object could not be created.

Throws

\Exception

It might throw an exception in the case of any errors:

  • if the load balancing group was not found.
  • db_server_conn_error - if the database server cannot be connected.
  • db_not_exists_error - if database does not exist od inaccesible to the user.

Overrides

Authors

Oleg Schildt

Source code


Method: ShardManager::registerShard()

public function registerShard(string $shard_name, array $parameters, string $load_balancing_group = ""): bool;

Registers a new shard.

Parameters

Name Pass type Value type Default value Description
$shard_name by value string

Unique shard name.

$parameters by value array

The connection parameters to the shard as an associative array in the form key => value:

  • $parameters["db_type"] - type of the database (MySQL or MSSQL)
  • $parameters["db_server"] - server address
  • $parameters["db_name"] - database name
  • $parameters["db_user"] - user name
  • $parameters["db_password"] - user password
  • $parameters["autoconnect"] - should true if the connection should be established automatically upon creation.
  • $parameters["read_only"] - this parameter sets the connection to the read only mode.
$load_balancing_group by value string ""

The name of the load balancing group, if the shard should be part of it, \SmartFactory\DatabaseWorkers\ShardManager::randomDBShard().

Returns

bool

It should return true if the registering was successful, otherwise false.

Throws

\Exception

It might throw an exception in the case of any errors:

  • if the shard name is not specified.
  • if the shard name has been already registered.

Overrides

Authors

Oleg Schildt

Source code