写点什么

SCTL 涅槃重生:投入 RAL 的怀抱

作者:SphereEx
  • 2021 年 11 月 18 日
  • 本文字数:4900 字

    阅读完需:约 16 分钟

《DistSQL:像数据库一样使用 Apache ShardingSphere》一文中,PMC 孟浩然为大家介绍了 DistSQL 的设计初衷和语法体系,并通过实战操作展示了一条 SQL 创建分布式数据库表的强大能力,展现了 Apache ShardingSphere 在新形态下的交互体验。


近期,ShardingSphere 社区对 SCTL 语法和执行引擎进行了重构,使 SCTL 完全投入 DistSQL 语法体系的怀抱,将原 SCTL 功能融入到 RAL 语法中,使 ShardingSphere 管理语言更加统一。本文将带领读者深入了解这次重构的意义,并通过实例一一解析新的 RAL 语句,感受 ShardingSphere 让用户体验更加美好的追求态度。

作者简介

江龙滔


SphereEx 中间件研发工程师,Apache ShardingSphere Committer。目前主要负责 DistSQL 及权限相关特性的设计和研发。


兰城翔


SphereEx 中间件研发工程师,Apache ShardingSphere Contributor。目前专注于 DistSQL 的设计和研发。

前情回顾:什么是 RAL

RAL 是 DistSQL 语法的一个分类,DistSQL 包含了 RDL、RQL 和 RAL 这三种具体类型。

  • RDL(Resource & Rule Definition Language)负责资源和规则的创建、修改和删除;

  • RQL(Resource & Rule Query Language)负责资源和规则的查询和展现;

  • RAL(Resource & Rule Administration Language)提供对资源和规则的高级控制能力。

什么是 SCTL

SCTL(ShardingSphere Control Language)是 Apache ShardingSphere 的控制语言,负责 Hint、事务类型切换、分片执行计划查询等增量功能的操作。

SCTL 包含以下语法:

为何重构

1、SCTL 功能实现于 v3.1.0 时期[1],彼时 ShardingSphere 还没有提出 DistSQL 的概念。如今,DistSQL 提供了功能更加丰富、概念更加统一的新 API,将 SCTL 的功能融入到 RAL 中,可以降低用户的理解成本,避免为用户带来困惑。


2、SCTL 语法使用了特殊的前缀字符 sctl: 作为标识,在 SQL 执行时没有经过 Parser 引擎,而是通过字符串前缀匹配的方式进行解析的。现在 DistSQL 已经实现了完整的解析流程,可以使用规范的语法解析器来处理输入语句,减少特殊代码。


3、尤其重要的是,原来的 SCTL 语法,有点不像真正的 SQL 语句。随着 5.0.0 版本的发布,ShardingSphere 已经推出了 DistSQL 这样一套完整的资源和规则管理方式,ShardingSphere 控制语句也可以变得更像 SQL,因此,社区将 SCTL 语法重构提上了日程。

重构解析

经过 ShardingSphere 社区的精心设计和细致讨论,SCTL 语法将由新的 RAL 语法代替[2],前后变化如下表所示:

下面让我们对这些语句一一进行解析:


show variable transaction_type


查询当前连接的事务类型

  • 输入命令

mysql> show variable transaction_type;
复制代码
  • 输出效果

+------------------+| TRANSACTION_TYPE |+------------------+| LOCAL            |+------------------+
复制代码


set variable transaction_type


修改当前连接的事务类型,支持的类型有:LOCAL, XA, BASE(不区分大小写)。

  • 输入命令

mysql> set variable transaction_type=XA;
复制代码
  • 输出效果

a. 设置成功,响应 Query OK, 0 rows affected;b. 再次执行 show variable transaction_type; 显示事务类型已经改变为 XA;


show variable cached_connection


查询当前连接中缓存的物理数据库连接个数。

  • 输入命令

mysql> show variable cached_connections;
复制代码
  • 输出效果

+--------------------+| CACHED_CONNECTIONS |+--------------------+| 0                  |+--------------------+
复制代码


preview SQL


预览实际 SQL。此处以读写分离场景为例,preview 语法支持任意 SQL 语句。

  • 输入命令

mysql> preview select * from t_order;
复制代码
  • 输出效果

+-----------------+----------------------------------------------+| datasource_name | sql                                          |+-----------------+----------------------------------------------+| read_ds_0       | select * from t_order ORDER BY order_id ASC  || read_ds_1       | select * from t_order ORDER BY order_id ASC  |+-----------------+----------------------------------------------+
复制代码

*注:以下为读写分离场景 Hint 示例,示例中使用了读写分离 + 分片的规则配置,配置如下:

rules:- !READWRITE_SPLITTING  dataSources:    ds_0:      writeDataSourceName: write_ds_0      readDataSourceNames:         - read_ds_0    ds_1:      writeDataSourceName: write_ds_1      readDataSourceNames:         - read_ds_1- !SHARDING  tables:    t_order:      actualDataNodes: ds_${0..1}.t_order  defaultDatabaseStrategy:    standard:      shardingColumn: user_id      shardingAlgorithmName: database_inline  defaultTableStrategy:    none:  shardingAlgorithms:    database_inline: type: INLINE      props:        algorithm-expression: ds_${user_id % 2}
复制代码


show readwrite_splitting hint status


针对当前连接,查询 readwrite_splitting 的 hint 状态。

  • 输入命令

mysql> show readwrite_splitting hint status;
复制代码
  • 输出效果

+--------+| source |+--------+| auto   |+--------+
复制代码


set readwrite_splitting hint source


针对当前连接,设置读写分离的路由策略(自动路由或强制到写库)。source 支持的类型有:AUTO , WRITE(不区分大小写)

  • AUTO:读写分离自动路由

  • WRITE:强制路由到主库

  • 输入命令

mysql> set readwrite_splitting hint source=write;
复制代码
  • 输出效果

a. 设置成功,响应 Query OK, 0 rows affected;

b. 再次执行 show readwrite_splitting hint status; 显示 source 值已经改变为 write;

c. 执行 preview select * from t_order; 能够看到查询 SQL 将会路由到主库:

mysql> preview select * from t_order;+-----------------+----------------------------------------------+| datasource_name | sql                                          |+-----------------+----------------------------------------------+| write_ds_0      | select * from t_order ORDER BY order_id ASC  || write_ds_1      | select * from t_order ORDER BY order_id ASC  |+-----------------+----------------------------------------------+
复制代码


clear readwrite_splitting hint


针对当前连接,清除 readwrite_splitting 的 hint 设置。

  • 输入命令

mysql> clear readwrite_splitting hint;
复制代码
  • 输出效果

a. 清除成功,响应 Query OK, 0 rows affected

b. 将 readwrite_splitting hint 的所有设置恢复到初始状态,可通过 show readwrite_splitting hint status; 命令查看 clear 的结果。

*注:以下为分片场景 Hint 示例,分库和分表均使用了 Hint 算法,使用的分片配置如下:

rules:- !SHARDING  tables:    t_order_item:      actualDataNodes: ds_${0..1}.t_order_item_${0..1}      databaseStrategy:        hint:          shardingAlgorithmName: database_inline      tableStrategy:        hint:          shardingAlgorithmName: table_inline  shardingAlgorithms:    database_inline: type: HINT_INLINE      props:        algorithm-expression: ds_${Integer.valueOf(value) % 2}    table_inline: type: HINT_INLINE      props:        algorithm-expression: t_order_item_${Integer.valueOf(value) % 2}
复制代码


show sharding hint status


针对当前连接,查询 sharding 的 hint 状态。

  • 输入命令

mysql> show sharding hint status;
复制代码
  • 输出效果

以下是初始状态的输出值:

  • 验证查询路由,输入命令

preview select * from t_order_item;
复制代码
  • 输出效果

此时没有 hint 值,查询使用全路由


set sharding hint database_value;


针对当前连接,设置 hint 仅对数据库分片有效,并添加分片值 1。

  • 输入命令

mysql> set sharding hint database_value = 1;
复制代码
  • 输出效果

a. 设置成功,响应 Query OK, 0 rows affected;

b. 执行 show sharding hint status; 显示 t_order_item 表对应的 database_sharding_values 值为 '1', 且 sharding_type 值更新为 'databases_only';

c. 执行 preview select * from t_order_item; SQL 全部路由到 ds_1:

说明:根据 YAML 配置中的分片规则,当 database_value 设置为奇数时路由到 ds_1,设置为偶数时路由到 ds_0。


add sharding hint database_value;


针对当前连接,为表 t_order_item 添加分片值。

  • 输入命令

mysql> add sharding hint database_value t_order_item = 5;
复制代码
  • 输出效果

a. 设置成功,响应 Query OK, 0 rows affected;

b. 执行 show sharding hint status; 显示 t_order_item 表对应的 database_sharding_values 值为 '5',且 sharding_type 值更新为 'databases_tables';

c. 执行 preview select * from t_order_item; SQL 全部路由到 ds_1:

  • 再次输入 add 命令,添加一个偶数值

mysql> add sharding hint database_value t_order_item = 10;

  • 输出效果:

a. 添加成功,响应 Query OK, 0 rows affected;

b. 执行 show sharding hint status; 显示 t_order_item 表对应的 database_sharding_values 值为 '5,10':

c. 执行 preview select * from t_order_item; SQL 路由包含了 ds_0 和 ds_1:(因为 hint 值包含奇数和偶数,将目标数据源全部包含在内)


add sharding hint table_value;


针对当前连接,为表 t_order_item 添加分片值。

  • 输入命令

mysql> add sharding hint table_value t_order_item = 0;
复制代码
  • 输出效果

a. 设置成功,响应 Query OK, 0 rows affected;

b. 执行 show sharding hint status; 显示 t_order_item 表对应的 database_sharding_values 值为 '5,10',table_sharding_values 的值为 '0':

c. 执行 preview select * from t_order_item; 路由情况如下图,每个库只查询 t_order_item_0:

说明:根据 YAML 配置中的分片规则,当 table_value 设置为奇数时路由到 t_order_item_1,设置为偶数时路由到 t_order_item_0。

add sharding hint database_value 类似,add sharding hint database_value 也可以设置多个 hint 值,以覆盖更多的分片。


clear sharding hint


针对当前连接,清除 sharding 的 hint 设置。

  • 输入命令

mysql> clear sharding hint;
复制代码
  • 输出效果

a. 清除成功,响应 Query OK, 0 rows affected

b. 将 sharding hint 的所有设置恢复到初始状态,可通过 show sharding hint status; 命令查看 clear 的结果。初始状态如下:


clear hint


这是一个特殊的指令,包含 clear readwrite_splitting hintclear sharding hint 的能力,将读写分离和分片的 hint 值全部清除,回归初始状态。

  • 任意设置 hint 值,再执行命令

mysql> clear hint;
复制代码
  • 输出效果

a. 清除成功,响应 Query OK, 0 rows affected

b. 将 readwrite_splitting hint 和 sharding hint 的所有设置恢复到初始状态,可通过 show readwrite_splitting hint status;show sharding hint status; 命令查看 clear 的结果。


*注:特别说明:若需使用 Hint 相关 DistSQL 功能,需要启用 ShardingSphere-Proxy 的配置项 `proxy-hint-enabled`,说明信息请参考:

https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-proxy/props/

更多 RAL 语句

除了包含原有 SCTL 语句的功能,RAL 语句还提供弹性伸缩、实例熔断、读写分离读库禁用这样的实用管理功能,欢迎读者前往官网阅读详细文档,了解 RAL 的更多使用场景:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-proxy/distsql/syntax/ral/

结语

以上就是本次分享的全部内容,如果读者对 Apache ShardingSphere 有任何疑问或建议,欢迎在 GitHub issue 列表提出,也可提交 Pull Request 参与到开源社区,也欢迎在中文社区中讨论。

GitHub issue:

https://github.com/apache/shardingsphere/issues

贡献指南:

https://shardingsphere.apache.org/community/cn/contribute/

中文社区:

https://community.sphere-ex.com/

参考信息

[1] https://github.com/apache/shardingsphere/pull/1586

[2] https://github.com/apache/shardingsphere/issues/11677

欢迎关注公众号第一时间了解咨讯

用户头像

SphereEx

关注

还未添加个人签名 2020.11.09 加入

SphereEx,让数据和应用连接更简单

评论

发布
暂无评论
SCTL 涅槃重生:投入 RAL 的怀抱