关于java:SCTL-涅槃重生投入-RAL-的怀抱

6次阅读

共计 8021 个字符,预计需要花费 21 分钟才能阅读完成。

在《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 蕴含以下语法:

语句 阐明
sctl:set transaction_type=XX 批改以后连贯的事务类型, 反对 LOCAL,XA,BASE。例:sctl:set transaction_type=XA
sctl:show transaction_type 查问以后连贯的事务类型
sctl:show cached_connections 查问以后连贯中缓存的物理数据库连贯个数
sctl:explain SQL 查看逻辑 SQL 的执行打算,例:sctl:explain select * from t_order;
sctl:hint set PRIMARY_ONLY=true 针对以后连贯,是否将数据库操作强制路由到主库
sctl:hint set DatabaseShardingValue=yy 针对以后连贯,设置 hint 仅对数据库分片无效,并增加分片值,yy:数据库分片值
sctl:hint addDatabaseShardingValue xx=yy 针对以后连贯,为表 xx 增加分片值 yy,xx:逻辑表名称,yy:数据库分片值
sctl:hint addTableShardingValue xx=yy 针对以后连贯,为表 xx 增加分片值 yy,xx:逻辑表名称,yy:表分片值
sctl:hint clear 针对以后连贯,革除 hint 所有设置
sctl:hint show status 针对以后连贯,查问 hint 状态,primary_only:true/false,sharding_type:databases_only/databases_tables
sctl:hint show table status 针对以后连贯,查问逻辑表的 hint 分片值

为何重构

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],前后变动如下表所示:

调整前 调整后
sctl:set transaction_type=XX set variable transaction_type=XX
sctl:show transaction_type show variable transaction_type
sctl:show cached_connections show variable cached_connections
sctl:explain SQL preview SQL
sctl:hint set PRIMARY_ONLY=true set readwrite_splitting hint source = [auto / write]
sctl:hint set DatabaseShardingValue=yy set sharding hint database_value = yy;
sctl:hint addDatabaseShardingValue xx=yy add sharding hint database_value xx= yy;
sctl:hint addTableShardingValue xx=yy add sharding hint table_value xx = yy
sctl:hint clear clear [hint / sharding hint / readwrite_splitting hint]
sctl:hint show status show [sharding / readwrite_splitting] hint status
sctl:hint show table status 纳入【show sharding hint status】

上面让咱们对这些语句一一进行解析:

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 状态。

  • 输入成果
+--------+
| 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 affectedb. 将 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…

更多 RAL 语句

除了蕴含原有 SCTL 语句的性能,RAL 语句还提供弹性伸缩、实例熔断、读写拆散读库禁用这样的实用治理性能,欢送读者返回官网浏览具体文档,理解 RAL 的更多应用场景:https://shardingsphere.apache…

结语

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

GitHub issue:

https://github.com/apache/sha…

奉献指南:

https://shardingsphere.apache…

中文社区:

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

参考信息

[1] https://github.com/apache/sha…

[2] https://github.com/apache/sha…

欢送关注公众号第一工夫理解咨讯

正文完
 0