SCTL 涅槃重生:投入 RAL 的懷抱
阿新 • • 發佈:2021-11-18
在《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(不區分大小寫)。
- 輸入命令
- 輸出效果
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;
- 輸出效果
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;
- 輸出效果
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;
- 輸出效果
set sharding hint database_value;針對當前連線,設定 hint 僅對資料庫分片有效,並新增分片值 1。
- 輸入命令
mysql> set sharding hint database_value = 1;
- 輸出效果
add sharding hint database_value;針對當前連線,為表 t_order_item 新增分片值。
- 輸入命令
mysql> add sharding hint database_value t_order_item = 5;
- 輸出效果
- 再次輸入 add 命令,新增一個偶數值
mysql> add sharding hint database_value t_order_item = 10;
- 輸出效果:
add sharding hint table_value;針對當前連線,為表 t_order_item 新增分片值。
- 輸入命令
mysql> add sharding hint table_value t_order_item = 0;
- 輸出效果
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 hint 和 clear sharding hint 的能力,將讀寫分離和分片的 hint 值全部清除,迴歸初始狀態。
- 任意設定 hint 值,再執行命令
mysql> clear hint;
- 輸出效果