Skip to content

Commit 1a514b5

Browse files
djshow832zeminzhou
authored andcommitted
executor: support showing session_states when signing cert is unavailable (pingcap#59337)
close pingcap#59336
1 parent be98b19 commit 1a514b5

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

pkg/executor/show.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ import (
7676
"github.com/pingcap/tidb/pkg/util/format"
7777
"github.com/pingcap/tidb/pkg/util/hack"
7878
"github.com/pingcap/tidb/pkg/util/hint"
79+
"github.com/pingcap/tidb/pkg/util/logutil"
7980
"github.com/pingcap/tidb/pkg/util/memory"
8081
"github.com/pingcap/tidb/pkg/util/sem"
8182
"github.com/pingcap/tidb/pkg/util/set"
8283
"github.com/pingcap/tidb/pkg/util/sqlexec"
8384
"github.com/pingcap/tidb/pkg/util/stringutil"
85+
"go.uber.org/zap"
8486
)
8587

8688
var etcdDialTimeout = 5 * time.Second
@@ -2285,18 +2287,25 @@ func (e *ShowExec) fetchShowSessionStates(ctx context.Context) error {
22852287
// The token may be leaked without secure transport, but the cloud can ensure security in some situations,
22862288
// so we don't enforce secure connections.
22872289
if token, err = sessionstates.CreateSessionToken(user.Username); err != nil {
2288-
return err
2290+
// Some users deploy TiProxy after the cluster is running and configuring signing certs will restart TiDB.
2291+
// The users may don't need connection migration, e.g. they only want traffic replay, which requires session states
2292+
// but not session tokens. So we don't return errors, just log it.
2293+
logutil.Logger(ctx).Warn("create session token failed", zap.Error(err))
22892294
}
22902295
}
2291-
tokenBytes, err := gjson.Marshal(token)
2292-
if err != nil {
2293-
return errors.Trace(err)
2294-
}
2295-
tokenJSON := types.BinaryJSON{}
2296-
if err = tokenJSON.UnmarshalJSON(tokenBytes); err != nil {
2297-
return err
2296+
if token != nil {
2297+
tokenBytes, err := gjson.Marshal(token)
2298+
if err != nil {
2299+
return errors.Trace(err)
2300+
}
2301+
tokenJSON := types.BinaryJSON{}
2302+
if err = tokenJSON.UnmarshalJSON(tokenBytes); err != nil {
2303+
return err
2304+
}
2305+
e.appendRow([]any{stateJSON, tokenJSON})
2306+
} else {
2307+
e.appendRow([]any{stateJSON, nil})
22982308
}
2299-
e.appendRow([]any{stateJSON, tokenJSON})
23002309
return nil
23012310
}
23022311

pkg/executor/show_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/pingcap/tidb/pkg/errno"
2222
"github.com/pingcap/tidb/pkg/executor"
2323
"github.com/pingcap/tidb/pkg/executor/importer"
24+
"github.com/pingcap/tidb/pkg/parser/auth"
2425
"github.com/pingcap/tidb/pkg/parser/mysql"
2526
"github.com/pingcap/tidb/pkg/testkit"
2627
"github.com/pingcap/tidb/pkg/types"
@@ -124,3 +125,13 @@ func TestShowIndexWithGlobalIndex(t *testing.T) {
124125
tk.MustExec("alter table test_t1 add unique index p_a (a) GLOBAL;")
125126
tk.MustQuery("show index from test_t1").Check(testkit.Rows("test_t1 0 p_a 1 a A 0 <nil> <nil> YES BTREE YES <nil> NO YES"))
126127
}
128+
129+
func TestShowSessionStates(t *testing.T) {
130+
store := testkit.CreateMockStore(t)
131+
tk := testkit.NewTestKit(t, store)
132+
tk.MustQuery("show session_states").CheckAt([]int{1}, testkit.Rows("<nil>"))
133+
134+
tk1 := testkit.NewTestKit(t, store)
135+
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
136+
tk1.MustQuery("show session_states").CheckAt([]int{1}, testkit.Rows("<nil>"))
137+
}

0 commit comments

Comments
 (0)