Skip to content

Commit 8b60a36

Browse files
authored
restore: fix failed to restore due to no such policy (#42795)
close #42796
1 parent a991ae5 commit 8b60a36

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

br/pkg/gluetidb/glue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func (gs *tidbSession) ExecuteInternal(ctx context.Context, sql string, args ...
163163
if err != nil {
164164
return errors.Trace(err)
165165
}
166+
defer func() {
167+
// We need to manually clean the TxnCtx here.
168+
// Or we may get stale information schema in the consequent calls.
169+
gs.se.GetSessionVars().TxnCtx.InfoSchema = nil
170+
}()
166171
// Some of SQLs (like ADMIN RECOVER INDEX) may lazily take effect
167172
// when we polling the result set.
168173
// At least call `next` once for triggering theirs side effect.

br/pkg/gluetidb/glue_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2023 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gluetidb
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/pingcap/tidb/br/pkg/glue"
22+
"github.com/pingcap/tidb/parser/model"
23+
"github.com/pingcap/tidb/testkit"
24+
"github.com/pingcap/tidb/types"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestTheSessionIsoation(t *testing.T) {
29+
req := require.New(t)
30+
store, _, clean := testkit.CreateMockStoreAndDomain(t)
31+
defer clean()
32+
ctx := context.Background()
33+
34+
g := Glue{}
35+
session, err := g.CreateSession(store)
36+
req.NoError(err)
37+
38+
req.NoError(session.ExecuteInternal(ctx, "use test;"))
39+
infos := []*model.TableInfo{}
40+
infos = append(infos, &model.TableInfo{
41+
Name: model.NewCIStr("tables_1"),
42+
Columns: []*model.ColumnInfo{
43+
{Name: model.NewCIStr("foo"), FieldType: *types.NewFieldType(types.KindBinaryLiteral), State: model.StatePublic},
44+
},
45+
})
46+
infos = append(infos, &model.TableInfo{
47+
Name: model.NewCIStr("tables_2"),
48+
PlacementPolicyRef: &model.PolicyRefInfo{
49+
Name: model.NewCIStr("threereplication"),
50+
},
51+
Columns: []*model.ColumnInfo{
52+
{Name: model.NewCIStr("foo"), FieldType: *types.NewFieldType(types.KindBinaryLiteral), State: model.StatePublic},
53+
},
54+
})
55+
infos = append(infos, &model.TableInfo{
56+
Name: model.NewCIStr("tables_3"),
57+
PlacementPolicyRef: &model.PolicyRefInfo{
58+
Name: model.NewCIStr("fivereplication"),
59+
},
60+
Columns: []*model.ColumnInfo{
61+
{Name: model.NewCIStr("foo"), FieldType: *types.NewFieldType(types.KindBinaryLiteral), State: model.StatePublic},
62+
},
63+
})
64+
polices := []*model.PolicyInfo{
65+
{
66+
PlacementSettings: &model.PlacementSettings{
67+
Followers: 4,
68+
},
69+
Name: model.NewCIStr("fivereplication"),
70+
},
71+
{
72+
PlacementSettings: &model.PlacementSettings{
73+
Followers: 2,
74+
},
75+
Name: model.NewCIStr("threereplication"),
76+
},
77+
}
78+
for _, pinfo := range polices {
79+
before := session.(*tidbSession).se.GetInfoSchema().SchemaMetaVersion()
80+
req.NoError(session.CreatePlacementPolicy(ctx, pinfo))
81+
after := session.(*tidbSession).se.GetInfoSchema().SchemaMetaVersion()
82+
req.Greater(after, before)
83+
}
84+
req.NoError(session.(glue.BatchCreateTableSession).CreateTables(ctx, map[string][]*model.TableInfo{
85+
"test": infos,
86+
}))
87+
}

0 commit comments

Comments
 (0)