Skip to content

Commit

Permalink
fix restore dropped account (#21094)
Browse files Browse the repository at this point in the history
fix restore dropped account

Approved by: @daviszhen, @zhangxu19830126, @aressu1985, @heni02
  • Loading branch information
YANGGMM authored Jan 6, 2025
1 parent f3352c2 commit 34d86cc
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 21 deletions.
6 changes: 3 additions & 3 deletions pkg/frontend/pitr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2943,7 +2943,7 @@ func Test_restoreViews(t *testing.T) {
bh.sql2result["rollback;"] = nil

viewMap := map[string]*tableInfo{}
err := restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
err := restoreViews(ctx, ses, bh, "sp01", viewMap, 0, 0)
assert.Error(t, err)

sql := "select * from mo_catalog.mo_snapshots where sname = 'sp01'"
Expand All @@ -2955,7 +2955,7 @@ func Test_restoreViews(t *testing.T) {
mrs = newMrsForPitrRecord([][]interface{}{{uint64(0), "sys", "open", uint64(1), ""}})
bh.sql2result[sql] = mrs

err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0, 0)
assert.NoError(t, err)

viewMap = map[string]*tableInfo{
Expand All @@ -2966,7 +2966,7 @@ func Test_restoreViews(t *testing.T) {
createSql: "create view view01",
},
}
err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0)
err = restoreViews(ctx, ses, bh, "sp01", viewMap, 0, 0)
assert.Error(t, err)
})
}
Expand Down
20 changes: 8 additions & 12 deletions pkg/frontend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ func doCheckCreateSnapshotPriv(ctx context.Context, ses *Session, stmt *tree.Cre
}
case tree.SNAPSHOTLEVELACCOUNT:
snapshotForAccount := string(stmt.Object.ObjName)
if len(snapshotForAccount) == 0 {
snapshotForAccount = currentAccount
}
if currentAccount != sysAccountName && currentAccount != snapshotForAccount {
return moerr.NewInternalError(ctx, "only sys tenant can create tenant level snapshot for other tenant")
}
Expand Down Expand Up @@ -635,7 +638,7 @@ func doRestoreSnapshot(ctx context.Context, ses *Session, stmt *tree.RestoreSnap
}

if len(viewMap) > 0 {
if err = restoreViews(ctx, ses, bh, snapshotName, viewMap, toAccountId); err != nil {
if err = restoreViews(ctx, ses, bh, snapshotName, viewMap, restoreAccount, toAccountId); err != nil {
return
}
}
Expand Down Expand Up @@ -1220,6 +1223,7 @@ func restoreViews(
bh BackgroundExec,
snapshotName string,
viewMap map[string]*tableInfo,
fromAccountId uint32,
toAccountId uint32) error {
getLogger(ses.GetService()).Info("start to restore views")
var (
Expand All @@ -1229,7 +1233,7 @@ func restoreViews(
sortedViews []string
oldSnapshot *plan.Snapshot
)
snapshot, err = getSnapshotPlanWithSharedBh(ctx, bh, snapshotName)
snapshot, err = getSnapshotPlanWithSharedBh(ctx, bh, fromAccountId, snapshotName)
if err != nil {
return err
}
Expand Down Expand Up @@ -2274,7 +2278,7 @@ func getPastExistsAccounts(
return
}

func getSnapshotPlanWithSharedBh(ctx context.Context, bh BackgroundExec, snapshotName string) (snapshot *pbplan.Snapshot, err error) {
func getSnapshotPlanWithSharedBh(ctx context.Context, bh BackgroundExec, fromAccountId uint32, snapshotName string) (snapshot *pbplan.Snapshot, err error) {
var record *snapshotRecord
if record, err = getSnapshotByName(ctx, bh, snapshotName); err != nil {
return
Expand All @@ -2285,19 +2289,11 @@ func getSnapshotPlanWithSharedBh(ctx context.Context, bh BackgroundExec, snapsho
return
}

var accountId uint32
// cluster level record has no accountName, so accountId is 0
if record.accountName != "" {
if accountId, err = getAccountId(ctx, bh, record.accountName); err != nil {
return
}
}

return &pbplan.Snapshot{
TS: &timestamp.Timestamp{PhysicalTime: record.ts},
Tenant: &pbplan.SnapshotTenant{
TenantName: record.accountName,
TenantID: accountId,
TenantID: fromAccountId,
},
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/txn/trace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (s *service) watch(ctx context.Context) {
err := s.executor.ExecTxn(
ctx,
func(txn executor.TxnExecutor) error {
sql := fmt.Sprintf("select name, state from %s", FeaturesTables)
sql := fmt.Sprintf("select name, state from %s.%s", DebugDB, FeaturesTables)
res, err := txn.Exec(sql, executor.StatementOption{})
if err != nil {
return err
Expand Down
137 changes: 132 additions & 5 deletions test/distributed/cases/snapshot/snapshot_database_level.result
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ create database if not exists db1;
create snapshot sn2 for database db1;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn2 2024-12-31 06:16:57.71952 database sys db1
sn2 2025-01-06 06:45:05.622281 database sys db1
drop database if exists db1;
drop snapshot if exists sn2;
drop snapshot if exists sn1;
Expand All @@ -26,7 +26,7 @@ create database if not exists db1;
create snapshot sn2 for database db1;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn2 2024-12-31 06:16:58.293388 database acc01 db1
sn2 2025-01-06 06:45:06.222457 database acc01 db1
drop database if exists db1;
drop snapshot if exists sn2;
drop snapshot if exists sn1;
Expand All @@ -43,7 +43,7 @@ insert into db1.tbl1 values (1), (2), (3);
create snapshot sn2 for table db1 tbl1;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn2 2024-12-31 06:16:58.616386 table sys db1 tbl1
sn2 2025-01-06 06:45:06.289816 table sys db1 tbl1
drop database if exists db1;
drop snapshot if exists sn2;
drop snapshot if exists sn1;
Expand All @@ -63,7 +63,7 @@ insert into db1.tbl1 values (1), (2), (3);
create snapshot sn2 for table db1 tbl1;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn2 2024-12-31 06:16:59.522949 table acc01 db1 tbl1
sn2 2025-01-06 06:45:06.89758 table acc01 db1 tbl1
drop database if exists db1;
drop snapshot if exists sn2;
drop snapshot if exists sn1;
Expand All @@ -82,5 +82,132 @@ drop snapshot if exists sn1;
create snapshot sn1 for account;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn1 2024-12-31 06:16:59.861163 account sys
sn1 2025-01-06 06:45:06.954975 account sys
drop snapshot if exists sn1;
drop account if exists acc01;
create account acc01 admin_name = 'test_account' identified by '111';
drop snapshot if exists sn1;
create snapshot sn1 for account;
show snapshots;
SNAPSHOT_NAME TIMESTAMP SNAPSHOT_LEVEL ACCOUNT_NAME DATABASE_NAME TABLE_NAME
sn1 2025-01-06 06:45:07.474446 account acc01
drop snapshot if exists sn1;
drop account if exists acc1;
drop account if exists acc02;
create account acc02 admin_name = 'test_account' identified by '111';
drop database if exists test01;
create database test01;
use test01;
drop table if exists rs01;
create table rs01 (col1 int, col2 decimal(6), col3 varchar(30));
insert into rs01 values (1, null, 'database');
insert into rs01 values (2, 38291.32132, 'database');
insert into rs01 values (3, null, 'database management system');
insert into rs01 values (4, 10, null);
insert into rs01 values (1, -321.321, null);
insert into rs01 values (2, -1, null);
select count(*) from rs01;
count(*)
6
drop database if exists test03;
create database test03;
use test03;
drop table if exists pri01;
create table pri01(
deptno int unsigned comment '部门编号',
dname varchar(15) comment '部门名称',
loc varchar(50) comment '部门所在位置',
primary key(deptno)
) comment='部门表';
insert into pri01 values (10,'ACCOUNTING','NEW YORK');
insert into pri01 values (20,'RESEARCH','DALLAS');
insert into pri01 values (30,'SALES','CHICAGO');
insert into pri01 values (40,'OPERATIONS','BOSTON');
drop table if exists aff01;
create table aff01(
empno int unsigned auto_increment COMMENT '雇员编号',
ename varchar(15) comment '雇员姓名',
job varchar(10) comment '雇员职位',
mgr int unsigned comment '雇员对应的领导的编号',
hiredate date comment '雇员的雇佣日期',
sal decimal(7,2) comment '雇员的基本工资',
comm decimal(7,2) comment '奖金',
deptno int unsigned comment '所在部门',
primary key(empno),
constraint `c1` foreign key (deptno) references pri01 (deptno)
);
insert into aff01 values (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
insert into aff01 values (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
insert into aff01 values (7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
insert into aff01 values (7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
drop database if exists test04;
create database test04;
use test04;
drop table if exists partition01;
create table partition01 (
emp_no int not null,
birth_date date not null,
first_name varchar(14) not null,
last_name varchar(16) not null,
gender varchar(5) not null,
hire_date date not null,
primary key (emp_no)
) partition by range columns (emp_no)(
partition p01 values less than (100001),
partition p02 values less than (200001),
partition p03 values less than (300001),
partition p04 values less than (400001)
);
insert into partition01 values (9001,'1980-12-17', 'SMITH', 'CLERK', 'F', '2008-12-17'),
(9002,'1981-02-20', 'ALLEN', 'SALESMAN', 'F', '2008-02-20');
drop database if exists test06;
create database test06;
use test06;
set experimental_fulltext_index=1;
create table src (id bigint primary key, body varchar, title text);
insert into src values (0, 'color is red', 't1'), (1, 'car is yellow', 'crazy car'), (2, 'sky is blue', 'no limit'), (3, 'blue is not red', 'colorful'),
(4, '遠東兒童中文是針對6到9歲的小朋友精心設計的中文學習教材,共三冊,目前已出版一、二冊。', '遠東兒童中文'),
(5, '每冊均採用近百張全幅彩圖及照片,生動活潑、自然真實,加深兒童學習印象,洋溢學習樂趣。', '遠東兒童中文'),
(6, '各個單元主題內容涵蓋中華文化及生活應用的介紹。本套教材含課本、教學指引、生字卡、學生作業本與CD,中英對照,精美大字版。本系列有繁體字及簡體字兩種版本印行。', '中文短篇小說'),
(7, '59個簡單的英文和中文短篇小說', '適合初學者'),
(8, NULL, 'NOT INCLUDED'),
(9, 'NOT INCLUDED BODY', NULL),
(10, NULL, NULL);
create fulltext index ftidx on src (body, title);
select * from src;
id body title
0 color is red t1
1 car is yellow crazy car
2 sky is blue no limit
3 blue is not red colorful
4 遠東兒童中文是針對6到9歲的小朋友精心設計的中文學習教材,共三冊,目前已出版一、二冊。 遠東兒童中文
5 每冊均採用近百張全幅彩圖及照片,生動活潑、自然真實,加深兒童學習印象,洋溢學習樂趣。 遠東兒童中文
6 各個單元主題內容涵蓋中華文化及生活應用的介紹。本套教材含課本、教學指引、生字卡、學生作業本與CD,中英對照,精美大字版。本系列有繁體字及簡體字兩種版本印行。 中文短篇小說
7 59個簡單的英文和中文短篇小說 適合初學者
8 null NOT INCLUDED
9 NOT INCLUDED BODY null
10 null null
drop view if exists view01;
create view v01 as select * from src;
select * from v01;
id body title
0 color is red t1
1 car is yellow crazy car
2 sky is blue no limit
3 blue is not red colorful
4 遠東兒童中文是針對6到9歲的小朋友精心設計的中文學習教材,共三冊,目前已出版一、二冊。 遠東兒童中文
5 每冊均採用近百張全幅彩圖及照片,生動活潑、自然真實,加深兒童學習印象,洋溢學習樂趣。 遠東兒童中文
6 各個單元主題內容涵蓋中華文化及生活應用的介紹。本套教材含課本、教學指引、生字卡、學生作業本與CD,中英對照,精美大字版。本系列有繁體字及簡體字兩種版本印行。 中文短篇小說
7 59個簡單的英文和中文短篇小說 適合初學者
8 null NOT INCLUDED
9 NOT INCLUDED BODY null
10 null null
show create table v01;
View Create View character_set_client collation_connection
v01 create view v01 as select * from src; utf8mb4 utf8mb4_general_ci
drop snapshot if exists spsp02;
create snapshot spsp02 for account acc02;
drop account acc02;
restore account acc02 from snapshot spsp02;
drop snapshot if exists spsp02;
drop account if exists acc02;
116 changes: 116 additions & 0 deletions test/distributed/cases/snapshot/snapshot_database_level.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,119 @@ create snapshot sn1 for account;
-- @ignore:1
show snapshots;
drop snapshot if exists sn1;


drop account if exists acc01;
create account acc01 admin_name = 'test_account' identified by '111';
-- @session:id=3&user=acc01:test_account&password=111
drop snapshot if exists sn1;
create snapshot sn1 for account;
-- @ignore:1
show snapshots;
drop snapshot if exists sn1;
-- @session

drop account if exists acc1;

drop account if exists acc02;
create account acc02 admin_name = 'test_account' identified by '111';
-- @session:id=4&user=acc02:test_account&password=111
drop database if exists test01;
create database test01;
use test01;
drop table if exists rs01;
create table rs01 (col1 int, col2 decimal(6), col3 varchar(30));
insert into rs01 values (1, null, 'database');
insert into rs01 values (2, 38291.32132, 'database');
insert into rs01 values (3, null, 'database management system');
insert into rs01 values (4, 10, null);
insert into rs01 values (1, -321.321, null);
insert into rs01 values (2, -1, null);
select count(*) from rs01;

drop database if exists test03;
create database test03;
use test03;
drop table if exists pri01;
create table pri01(
deptno int unsigned comment '部门编号',
dname varchar(15) comment '部门名称',
loc varchar(50) comment '部门所在位置',
primary key(deptno)
) comment='部门表';

insert into pri01 values (10,'ACCOUNTING','NEW YORK');
insert into pri01 values (20,'RESEARCH','DALLAS');
insert into pri01 values (30,'SALES','CHICAGO');
insert into pri01 values (40,'OPERATIONS','BOSTON');

drop table if exists aff01;
create table aff01(
empno int unsigned auto_increment COMMENT '雇员编号',
ename varchar(15) comment '雇员姓名',
job varchar(10) comment '雇员职位',
mgr int unsigned comment '雇员对应的领导的编号',
hiredate date comment '雇员的雇佣日期',
sal decimal(7,2) comment '雇员的基本工资',
comm decimal(7,2) comment '奖金',
deptno int unsigned comment '所在部门',
primary key(empno),
constraint `c1` foreign key (deptno) references pri01 (deptno)
);

insert into aff01 values (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
insert into aff01 values (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
insert into aff01 values (7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
insert into aff01 values (7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);

drop database if exists test04;
create database test04;
use test04;
drop table if exists partition01;
create table partition01 (
emp_no int not null,
birth_date date not null,
first_name varchar(14) not null,
last_name varchar(16) not null,
gender varchar(5) not null,
hire_date date not null,
primary key (emp_no)
) partition by range columns (emp_no)(
partition p01 values less than (100001),
partition p02 values less than (200001),
partition p03 values less than (300001),
partition p04 values less than (400001)
);

insert into partition01 values (9001,'1980-12-17', 'SMITH', 'CLERK', 'F', '2008-12-17'),
(9002,'1981-02-20', 'ALLEN', 'SALESMAN', 'F', '2008-02-20');

drop database if exists test06;
create database test06;
use test06;
set experimental_fulltext_index=1;
create table src (id bigint primary key, body varchar, title text);
insert into src values (0, 'color is red', 't1'), (1, 'car is yellow', 'crazy car'), (2, 'sky is blue', 'no limit'), (3, 'blue is not red', 'colorful'),
(4, '遠東兒童中文是針對6到9歲的小朋友精心設計的中文學習教材,共三冊,目前已出版一、二冊。', '遠東兒童中文'),
(5, '每冊均採用近百張全幅彩圖及照片,生動活潑、自然真實,加深兒童學習印象,洋溢學習樂趣。', '遠東兒童中文'),
(6, '各個單元主題內容涵蓋中華文化及生活應用的介紹。本套教材含課本、教學指引、生字卡、學生作業本與CD,中英對照,精美大字版。本系列有繁體字及簡體字兩種版本印行。', '中文短篇小說'),
(7, '59個簡單的英文和中文短篇小說', '適合初學者'),
(8, NULL, 'NOT INCLUDED'),
(9, 'NOT INCLUDED BODY', NULL),
(10, NULL, NULL);

create fulltext index ftidx on src (body, title);
select * from src;
drop view if exists view01;
create view v01 as select * from src;
select * from v01;
show create table v01;
-- @session

drop snapshot if exists spsp02;
create snapshot spsp02 for account acc02;
drop account acc02;
restore account acc02 from snapshot spsp02;

drop snapshot if exists spsp02;
drop account if exists acc02;

0 comments on commit 34d86cc

Please sign in to comment.