Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
gfphoenix78 committed Jan 17, 2025
1 parent ba51e4a commit 11a037c
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
-- While a tablespace is being dropped, if any table is created
-- in the same tablespace, the data of that table should not be deleted
-- in the same tablespace, only one query can be successful.
-- The behavior guarantees that the table will never use a
-- dropped or invalid tablespace.

-- start_matchsubs
-- m/ERROR: could not create directory "pg_tblspc.*: No such file or directory/
-- s/ERROR: could not create directory "pg_tblspc.*: No such file or directory/ERROR: could not create directory "pg_tblspc\/XXXX": No such file or directory/
-- end_matchsubs

-- create a tablespace directory
!\retcode rm -rf /tmp/concurrent_tblspace;
Expand All @@ -16,51 +23,112 @@
CREATE TABLESPACE concurrent_tblspace LOCATION '/tmp/concurrent_tblspace';
CREATE

-- suspend execution after TablespaceCreateLock is released
SELECT gp_inject_fault('AfterTablespaceCreateLockRelease', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
-- test 1:
-- when creating a table using a tablespace, after the tuple of tablespace
-- is locked, the tablespace is not allowed to drop
2: begin;
BEGIN
2: CREATE TABLE t_in_tablespace(a int, b int) TABLESPACE concurrent_tblspace;
CREATE

-- drop tablespace will fail: can't acuqire the lock
DROP TABLESPACE concurrent_tblspace;
ERROR: could not lock tablespace "concurrent_tblspace"
2: rollback;
ROLLBACK

-- test 2:
-- if DROP TABLESPACE acquires lock first and rollback, the blocking CREATE
-- TABLE will be successful.

-- suspend execution after tablespace lock is acquired
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)
1&:DROP TABLESPACE concurrent_tblspace; <waiting ...>
1&: DROP TABLESPACE concurrent_tblspace; <waiting ...>

-- wait for the fault to be triggered
SELECT gp_wait_until_triggered_fault('AfterTablespaceCreateLockRelease', 1, dbid) from gp_segment_configuration where content <> -1 and role='p';
SELECT gp_wait_until_triggered_fault('drop_tablespace_after_acquire_lock', 1, dbid) from gp_segment_configuration where content <> -1 and role='p';
gp_wait_until_triggered_fault
-------------------------------
Success:
Success:
Success:
(3 rows)

-- create a table in the same tablespace which is being dropped via a concurrent session
CREATE TABLE drop_tablespace_tbl(a int, b int) TABLESPACE concurrent_tblspace DISTRIBUTED BY (a);
2&: CREATE TABLE t_in_tablespace(a int, b int) TABLESPACE concurrent_tblspace; <waiting ...>
-- inject an error to ensure that the above DROP command will rollback
SELECT gp_inject_fault('after_xlog_tblspc_drop', 'error', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)
-- fail
1<: <... completed>
ERROR: fault triggered, fault name:'after_xlog_tblspc_drop' fault type:'error'
-- success
2<: <... completed>
CREATE
INSERT INTO drop_tablespace_tbl SELECT i, i FROM generate_series(1,100)i;
INSERT 100
-- reset the fault, drop tablespace command will not delete the data files on the tablespace
SELECT gp_inject_fault('AfterTablespaceCreateLockRelease', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
-- drop the above table, so the tablespace is empty.
2: DROP TABLE t_in_tablespace;
DROP
SELECT gp_inject_fault('after_xlog_tblspc_drop', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)

-- test 3:
-- if DROP TABLESPACE acquires lock first and going to drop, any CREATE TABLE
-- will fail

-- suspend execution after tablespace lock is acquired
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)
1&: DROP TABLESPACE concurrent_tblspace; <waiting ...>

-- wait for the fault to be triggered
SELECT gp_wait_until_triggered_fault('drop_tablespace_after_acquire_lock', 1, dbid) from gp_segment_configuration where content <> -1 and role='p';
gp_wait_until_triggered_fault
-------------------------------
Success:
Success:
Success:
(3 rows)

-- create a table in the same tablespace which is being dropped via a concurrent session
2&:CREATE TABLE drop_tablespace_tbl(a int, b int) TABLESPACE concurrent_tblspace DISTRIBUTED BY (a); <waiting ...>
-- reset the fault, drop tablespace command will continue
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
gp_inject_fault
-----------------
Success:
Success:
Success:
(3 rows)
-- success
1<: <... completed>
DROP
-- check data exists
SELECT count(*) FROM drop_tablespace_tbl;
count
-------
100
(1 row)
-- move to another tablespace and check the data.
ALTER TABLE drop_tablespace_tbl SET TABLESPACE pg_default;
ALTER
SELECT count(*) FROM drop_tablespace_tbl;
count
-------
100
(1 row)
-- fail
2<: <... completed>
ERROR: could not create directory "pg_tblspc/33175/GPDB_1_302501601/32799": No such file or directory
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
-- While a tablespace is being dropped, if any table is created
-- in the same tablespace, the data of that table should not be deleted
-- in the same tablespace, only one query can be successful.
-- The behavior guarantees that the table will never use a
-- dropped or invalid tablespace.

-- start_matchsubs
-- m/ERROR: could not create directory "pg_tblspc.*: No such file or directory/
-- s/ERROR: could not create directory "pg_tblspc.*: No such file or directory/ERROR: could not create directory "pg_tblspc\/XXXX": No such file or directory/
-- end_matchsubs

-- create a tablespace directory
!\retcode rm -rf /tmp/concurrent_tblspace;
!\retcode mkdir -p /tmp/concurrent_tblspace;

CREATE TABLESPACE concurrent_tblspace LOCATION '/tmp/concurrent_tblspace';

-- suspend execution after TablespaceCreateLock is released
SELECT gp_inject_fault('AfterTablespaceCreateLockRelease', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
1&:DROP TABLESPACE concurrent_tblspace;
-- test 1:
-- when creating a table using a tablespace, after the tuple of tablespace
-- is locked, the tablespace is not allowed to drop
2: begin;
2: CREATE TABLE t_in_tablespace(a int, b int) TABLESPACE concurrent_tblspace;

-- drop tablespace will fail: can't acuqire the lock
DROP TABLESPACE concurrent_tblspace;
2: rollback;

-- test 2:
-- if DROP TABLESPACE acquires lock first and rollback, the blocking CREATE
-- TABLE will be successful.

-- suspend execution after tablespace lock is acquired
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
1&: DROP TABLESPACE concurrent_tblspace;

-- wait for the fault to be triggered
SELECT gp_wait_until_triggered_fault('drop_tablespace_after_acquire_lock', 1, dbid)
from gp_segment_configuration where content <> -1 and role='p';

2&: CREATE TABLE t_in_tablespace(a int, b int) TABLESPACE concurrent_tblspace;
-- inject an error to ensure that the above DROP command will rollback
SELECT gp_inject_fault('after_xlog_tblspc_drop', 'error', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
-- fail
1<:
-- success
2<:
-- drop the above table, so the tablespace is empty.
2: DROP TABLE t_in_tablespace;
SELECT gp_inject_fault('after_xlog_tblspc_drop', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';

-- test 3:
-- if DROP TABLESPACE acquires lock first and going to drop, any CREATE TABLE
-- will fail

-- suspend execution after tablespace lock is acquired
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'suspend', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
1&: DROP TABLESPACE concurrent_tblspace;

-- wait for the fault to be triggered
SELECT gp_wait_until_triggered_fault('AfterTablespaceCreateLockRelease', 1, dbid)
SELECT gp_wait_until_triggered_fault('drop_tablespace_after_acquire_lock', 1, dbid)
from gp_segment_configuration where content <> -1 and role='p';

-- create a table in the same tablespace which is being dropped via a concurrent session
CREATE TABLE drop_tablespace_tbl(a int, b int) TABLESPACE concurrent_tblspace DISTRIBUTED BY (a);
INSERT INTO drop_tablespace_tbl SELECT i, i FROM generate_series(1,100)i;
-- reset the fault, drop tablespace command will not delete the data files on the tablespace
SELECT gp_inject_fault('AfterTablespaceCreateLockRelease', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
2&:CREATE TABLE drop_tablespace_tbl(a int, b int) TABLESPACE concurrent_tblspace DISTRIBUTED BY (a);
-- reset the fault, drop tablespace command will continue
SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'reset', dbid) FROM gp_segment_configuration WHERE content <> -1 and role='p';
-- success
1<:
-- check data exists
SELECT count(*) FROM drop_tablespace_tbl;
-- move to another tablespace and check the data.
ALTER TABLE drop_tablespace_tbl SET TABLESPACE pg_default;
SELECT count(*) FROM drop_tablespace_tbl;
-- fail
2<:

0 comments on commit 11a037c

Please sign in to comment.