mysqlnd_ms_set_qos
(PECL mysqlnd_ms < 1.2.0)
mysqlnd_ms_set_qos — Sets the quality of service needed from the cluster
Описание
$connection
, int $service_level
[, int $service_level_option
[, mixed $option_value
]] )Sets the quality of service needed from the cluster. A database cluster delivers a certain quality of service to the user depending on its architecture. A major aspect of the quality of service is the consistency level the cluster can offer. An asynchronous MySQL replication cluster defaults to eventual consistency for slave reads: a slave may serve stale data, current data, or it may have not the requested data at all, because it is not synchronous to the master. In a MySQL replication cluster, only master accesses can give strong consistency, which promises that all clients see each others changes.
PECL/mysqlnd_ms hides the complexity of choosing appropriate nodes to achieve a certain level of service from the cluster. The "Quality of Service" filter implements the necessary logic. The filter can either be configured in the plugins configuration file, or at runtime using mysqlnd_ms_set_qos().
Similar results can be achieved with PECL mysqlnd_ms < 1.2.0, if using SQL hints to force the use of a certain type of node or using the master_on_write plugin configuration option. The first requires more code and causes more work on the application side. The latter is less refined than using the quality of service filter. Settings made through the function call can be reversed, as shown in the example below. The example temporarily switches to a higher service level (session consistency, read your writes) and returns back to the clusters default after it has performed all operations that require the better service. This way, read load on the master can be minimized compared to using master_on_write, which would continue using the master after the first write.
Since 1.5.0 calls will fail when done in the middle of a transaction if transaction stickiness is enabled and transaction boundaries have been detected. properly.
Список параметров
-
connection
-
A PECL/mysqlnd_ms connection handle to a MySQL server of the type PDO_MYSQL, mysqli or ext/mysql for which a service level is to be set. The connection handle is obtained when opening a connection with a host name that matches a mysqlnd_ms configuration file entry using any of the above three MySQL driver extensions.
-
service_level
-
The requested service level:
MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL
,MYSQLND_MS_QOS_CONSISTENCY_SESSION
orMYSQLND_MS_QOS_CONSISTENCY_STRONG
. -
service_level_option
-
An option to parameterize the requested service level. The option can either be
MYSQLND_MS_QOS_OPTION_GTID
orMYSQLND_MS_QOS_OPTION_AGE
.The option
MYSQLND_MS_QOS_OPTION_GTID
can be used to refine the service levelMYSQLND_MS_QOS_CONSISTENCY_SESSION
. It must be combined with a fourth function parameter, theoption_value
. Theoption_value
shall be a global transaction ID obtained from mysqlnd_ms_get_last_gtid(). If set, the plugin considers both master servers and asynchronous slaves for session consistency (read your writes). Otherwise, only masters are used to achieve session consistency. A slave is considered up-to-date and checked if it has already replicated the global transaction ID fromoption_value
. Please note, searching appropriate slaves is an expensive and slow operation. Use the feature sparsely, if the master cannot handle the read load alone.The
MYSQLND_MS_QOS_OPTION_AGE
option can be combined with theMYSQLND_MS_QOS_CONSISTENCY_EVENTUAL
service level, to filter out asynchronous slaves that lag more seconds behind the master thanoption_value
. If set, the plugin will only consider slaves for reading if SHOW SLAVE STATUS reports Slave_IO_Running=Yes, Slave_SQL_Running=Yes and Seconds_Behind_Master <= option_value. Please note, searching appropriate slaves is an expensive and slow operation. Use the feature sparsely in version 1.2.0. Future versions may improve the algorithm used to identify candidates. Please, see the MySQL reference manual about the precision, accuracy and limitations of the MySQL administrative command SHOW SLAVE STATUS. -
option_value
-
Parameter value for the service level option. See also the
service_level_option
parameter.
Возвращаемые значения
Returns TRUE
if the connections service level
has been switched to the requested. Otherwise, returns FALSE
Примечания
Замечание:
mysqlnd_ms_set_qos() requires PHP >= 5.4.0 and PECL mysqlnd_ms >= 1.2.0. Internally, it is using a mysqlnd library C functionality not available with PHP 5.3.
Please note, all MySQL 5.6 production versions do not provide clients with enough information to use GTIDs for enforcing session consistency. In the worst case, the plugin will choose the master only.
Примеры
Пример #1 mysqlnd_ms_set_qos() example
<?php
/* Open mysqlnd_ms connection using mysqli, PDO_MySQL or mysql extension */
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* Session consistency: read your writes */
$ret = mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_SESSION);
if (!$ret)
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Will use master and return fresh data, client can see his last write */
if (!$res = $mysqli->query("SELECT item, price FROM orders WHERE order_id = 1"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Back to default: use of all slaves and masters permitted, stale data can happen */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
?>
Смотрите также
- mysqlnd_ms_get_last_gtid() - Returns the latest global transaction ID
- Service level and consistency concept
- Filter concept
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Mysqlnd replication and load balancing plugin
- mysqlnd_ms_dump_servers
- mysqlnd_ms_fabric_select_global
- mysqlnd_ms_fabric_select_shard
- mysqlnd_ms_get_last_gtid
- mysqlnd_ms_get_last_used_connection
- mysqlnd_ms_get_stats
- mysqlnd_ms_match_wild
- mysqlnd_ms_query_is_select
- mysqlnd_ms_set_qos
- mysqlnd_ms_set_user_pick_server
- mysqlnd_ms_xa_begin
- mysqlnd_ms_xa_commit
- mysqlnd_ms_xa_gc
- mysqlnd_ms_xa_rollback
Коментарии
404 Not Found