Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Multi-shard DDL support v2 #892

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions router/relay/relay.go
Original file line number Diff line number Diff line change
@@ -437,14 +437,20 @@ func (rst *RelayStateImpl) Reroute() error {
}
rst.routingState = routingState
switch v := routingState.(type) {
case routingstate.MultiMatchState, routingstate.DDLState:
case routingstate.MultiMatchState:
if rst.TxActive() {
return fmt.Errorf("ddl is forbidden inside multi-shard transition")
return fmt.Errorf("cannot route in an active transaction")
}
spqrlog.Zero.Debug().
Uint("client", rst.Client().ID()).
Err(err).
Msgf("parsed multi-shard routing state")
Msgf("parsed MultiMatchState")
return rst.procRoutes(rst.Qr.DataShardsRoutes())
case routingstate.DDLState:
spqrlog.Zero.Debug().
Uint("client", rst.Client().ID()).
Err(err).
Msgf("parsed DDLState")
return rst.procRoutes(rst.Qr.DataShardsRoutes())
case routingstate.ShardMatchState:
// TBD: do it better
13 changes: 11 additions & 2 deletions router/server/multishard.go
Original file line number Diff line number Diff line change
@@ -75,7 +75,12 @@ func (m *MultiShardServer) RequestData() {

// DataPending implements Server.
func (m *MultiShardServer) DataPending() bool {
panic("MultiShardServer.DataPending not implemented")
for _, shard := range m.activeShards {
if shard.DataPending() {
return true
}
}
return false
}

func (m *MultiShardServer) Reset() error {
@@ -453,7 +458,11 @@ func (m *MultiShardServer) Cleanup(rule config.FrontendRule) error {
}

func (m *MultiShardServer) Sync() int64 {
panic("MultiShardServer.Sync not implemented")
var syncCount int64
for _, shard := range m.activeShards {
syncCount += shard.Sync()
}
return syncCount
}

func (m *MultiShardServer) Cancel() error {
1 change: 1 addition & 0 deletions test/regress/schedule/router
Original file line number Diff line number Diff line change
@@ -17,3 +17,4 @@ test: show_processing
test: copy_multishard
test: copy_reference_table
test: part_table
test: ddl
74 changes: 74 additions & 0 deletions test/regress/tests/router/expected/ddl.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
\c spqr-console

SPQR router admin console
Here you can configure your routing rules
------------------------------------------------
You can find documentation here
https://github.com/pg-sharding/spqr/tree/master/docs

CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
add distribution
------------------------
distribution id -> ds1
(1 row)

CREATE KEY RANGE krid1 FROM 1 ROUTE TO sh1 FOR DISTRIBUTION ds1;
add key range
---------------
bound -> 1
(1 row)

CREATE KEY RANGE krid2 FROM 101 ROUTE TO sh2 FOR DISTRIBUTION ds1;
add key range
---------------
bound -> 101
(1 row)

ALTER DISTRIBUTION ds1 ATTACH RELATION table_1 DISTRIBUTION KEY id;
attach table
----------------------------
relation name -> table_1
distribution id -> ds1
(2 rows)

ALTER DISTRIBUTION ds1 ATTACH RELATION table_2 DISTRIBUTION KEY id;
attach table
----------------------------
relation name -> table_2
distribution id -> ds1
(2 rows)

\c regress
CREATE TABLE table_1(id INT PRIMARY KEY);
NOTICE: send query to shard(s) : sh1,sh2
CREATE TABLE table_2(id INT PRIMARY KEY);
NOTICE: send query to shard(s) : sh1,sh2
BEGIN;
ALTER TABLE "table_1" RENAME TO "tmp";
NOTICE: send query to shard(s) : sh1,sh2
ALTER TABLE "table_2" RENAME TO "table_1";
ALTER TABLE "tmp" RENAME TO "table_2";
COMMIT;
DROP TABLE table_1 CASCADE;
NOTICE: send query to shard(s) : sh1,sh2
DROP TABLE table_2;
NOTICE: send query to shard(s) : sh1,sh2
\c spqr-console

SPQR router admin console
Here you can configure your routing rules
------------------------------------------------
You can find documentation here
https://github.com/pg-sharding/spqr/tree/master/docs

DROP DISTRIBUTION ALL CASCADE;
drop distribution
------------------------
distribution id -> ds1
(1 row)

DROP KEY RANGE ALL;
drop key range
----------------
(0 rows)

27 changes: 27 additions & 0 deletions test/regress/tests/router/sql/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
\c spqr-console

CREATE DISTRIBUTION ds1 COLUMN TYPES integer;

CREATE KEY RANGE krid1 FROM 1 ROUTE TO sh1 FOR DISTRIBUTION ds1;
CREATE KEY RANGE krid2 FROM 101 ROUTE TO sh2 FOR DISTRIBUTION ds1;

ALTER DISTRIBUTION ds1 ATTACH RELATION table_1 DISTRIBUTION KEY id;
ALTER DISTRIBUTION ds1 ATTACH RELATION table_2 DISTRIBUTION KEY id;

\c regress

CREATE TABLE table_1(id INT PRIMARY KEY);
CREATE TABLE table_2(id INT PRIMARY KEY);

BEGIN;
ALTER TABLE "table_1" RENAME TO "tmp";
ALTER TABLE "table_2" RENAME TO "table_1";
ALTER TABLE "tmp" RENAME TO "table_2";
COMMIT;

DROP TABLE table_1 CASCADE;
DROP TABLE table_2;

\c spqr-console
DROP DISTRIBUTION ALL CASCADE;
DROP KEY RANGE ALL;
Loading