IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> maxscale的causal_reads参数 -> 正文阅读

[大数据]maxscale的causal_reads参数

一 需求描述

maxscale可以实现mariadb读写分离(在主库写,在从库读)。mariadb主从复制是异步的,很有可能存在从库延迟于主库的情况。

有时候,有的业务无法容忍mariadb从库延迟,如一个接口执行完写入操作后,其他接口要判断是否能查到该写入的数据,然后做进一步DML处理,如果查的数据不准确,则会导致数据错乱,影响业务逻辑。

如果maxscale启用了causal_reads,客户端连接修改了数据库,则在从上执行的任何后续读取都将以防止复制延迟影响结果的方式进行。这仅适用于客户端连接自己进行的修改。如果从库在配置的时间内没有赶上主库,则将在主库上重试。

根据具体业务谨慎选型吧,个人不建议用,详细请查阅‘四 实验步骤’

二 参数介绍

2.1 causal_reads

如果从库不延迟,则查从库,如果延迟了,则自动查主库。

以下是官网原文:

Enable causal reads. This parameter is disabled by default and was introduced in MaxScale 2.3.0.

If a client connection modifies the database and?causal_reads?is enabled, any subsequent reads performed on slave servers will be done in a manner that prevents replication lag from affecting the results. This only applies to the modifications done by the client itself.

Note:?This feature requires MariaDB 10.2.16 or newer to function. In addition to this, the?session_track_system_variables?parameter must be set to?last_gtid.

Note:?This feature does not work with prepared statements. Only SQL statements executed individually (inside a COM_QUERY packet) can be handled by the causal read mechanism.

Note:?This feature does not work with Galera or any other non-standard replication mechanisms. As Galera does not update the?gtid_slave_pos?variable when events are replicated via the Galera library, the?MASTER_GTID_WAIT?function used by MaxScale to synchronize reads will wait until the timeout. With Galera this is not a serious issue as it, by nature, is a mostly-synchronous replication mechanism.

A practical example can be given by the following set of SQL commands executed with?autocommit=1.

?As the statements are not executed inside a transaction, from the load balancers point of view, the latter statement can be routed to a slave server. The problem with this is that if the value that was inserted on the master has not yet replicated to the server where the SELECT statement is being performed, it can appear as if the value we just inserted is not there.

By prefixing these types of SELECT statements with a command that guarantees consistent results for the reads, read scalability can be improved without sacrificing consistency.

The set of example SQL above will be translated by MaxScale into the following statements.

?The?SET?command will synchronize the slave to a certain logical point in the replication stream (see?MASTER_GTID_WAIT?for more details).

If the slave has not caught up to the master within the configured time, it will be retried on the master. In MaxScale 2.3.0 an error was returned to the client when the slave timed out.

2.2 causal_reads_timeout

The timeout for the slave synchronization done by?causal_reads. The default value is 10 seconds.

The timeout is specified as documented?here. If no explicit unit is provided, the value is interpreted as seconds in MaxScale 2.4. In subsequent versions a value without a unit may be rejected. Note that since the granularity of the timeout is seconds, a timeout specified in milliseconds will be rejected, even if the duration is longer than a second.

三 解决办法

3.1 修改mariadb里session_track_system_variables变量

修改session_track_system_variables变量,让其包含last_gtid

#在线修改(发现在主库上修改变量不能自动同步到从库,那就在主从上都执行以下sql):

set global session_track_system_variables='autocommit,character_set_client,character_set_connection,character_set_results,time_zone,last_gtid';

退出当前会话,重新登录,发现变量修改成功了。

#永久修改

修改mariadb配置文件,

在[mysqld]下添加一行:

session_track_system_variables='autocommit,character_set_client,character_set_connection,character_set_results,time_zone,last_gtid'

3.2 修改maxscale参数

在读写分离服务模块下添加:

causal_reads=on

示例:

#重启maxscale

systemctl restart maxscale

四 实验步骤

4.1 修改配置前的表现

在从库上加个全局读锁:

flush tables with read lock;

?由于对从库加了读锁,所以没法往从库插入23这条数据,由于读写分离,所以就查不到这条数据。

4.2 修改配置后的表现

在从库上加个全局读锁

?发现虽然从库有延迟,但是等待了10秒后,就去查主库的数据去了。

在10秒内在从库上解锁(unlock tables)了的话,很快就能返回数据了,不用等十秒。

后来实验发现,同一个会话里一次执行多个sql,修改了a表的记录,若从库有延迟,查其他没修改的记录,也需要等待,这样的话,影响范围比较大。

而且等待10秒也太长了吧?可是若将causal_reads_timeout改短点儿,若延迟的话,就都去读主库了,主库上的压力就比较大了。

我后来将插入和查询sql放不同数据库连接里,发现查询没有等待,直接查的是从库,没查主库,所以这个casual_reads是针对的同一个数据库连接里的sql。

根据具体业务谨慎选型吧,个人不建议用。

本篇文章参考了

Readwritesplit - MariaDB Knowledge Base

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-07-17 16:30:00  更:2022-07-17 16:33:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 22:21:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码