@@ -20,16 +20,15 @@ import (
20
20
"encoding/hex"
21
21
"encoding/json"
22
22
"fmt"
23
- "io"
24
23
"math"
25
- "net/http"
26
24
"strconv"
27
25
"strings"
28
26
"time"
29
27
30
28
"github.com/pingcap/errors"
31
29
"github.com/pingcap/failpoint"
32
30
"github.com/pingcap/kvproto/pkg/deadlock"
31
+ "github.com/pingcap/kvproto/pkg/kvrpcpb"
33
32
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
34
33
"github.com/pingcap/tidb/ddl/label"
35
34
"github.com/pingcap/tidb/ddl/placement"
@@ -43,7 +42,6 @@ import (
43
42
"github.com/pingcap/tidb/parser/charset"
44
43
"github.com/pingcap/tidb/parser/model"
45
44
"github.com/pingcap/tidb/parser/mysql"
46
- "github.com/pingcap/tidb/parser/terror"
47
45
plannercore "github.com/pingcap/tidb/planner/core"
48
46
"github.com/pingcap/tidb/privilege"
49
47
"github.com/pingcap/tidb/privilege/privileges"
@@ -73,6 +71,8 @@ import (
73
71
"github.com/pingcap/tidb/util/sqlexec"
74
72
"github.com/pingcap/tidb/util/stringutil"
75
73
"github.com/pingcap/tidb/util/syncutil"
74
+ "github.com/tikv/client-go/v2/tikv"
75
+ "github.com/tikv/client-go/v2/tikvrpc"
76
76
"github.com/tikv/client-go/v2/txnkv/txnlock"
77
77
"go.uber.org/zap"
78
78
"golang.org/x/exp/slices"
@@ -3001,7 +3001,7 @@ type TiFlashSystemTableRetriever struct {
3001
3001
outputCols []* model.ColumnInfo
3002
3002
instanceCount int
3003
3003
instanceIdx int
3004
- instanceInfos [] tiflashInstanceInfo
3004
+ instanceIds [] string
3005
3005
rowIdx int
3006
3006
retrieved bool
3007
3007
initialized bool
@@ -3024,7 +3024,7 @@ func (e *TiFlashSystemTableRetriever) retrieve(ctx context.Context, sctx session
3024
3024
}
3025
3025
3026
3026
for {
3027
- rows , err := e .dataForTiFlashSystemTables (sctx , e .extractor .TiDBDatabases , e .extractor .TiDBTables )
3027
+ rows , err := e .dataForTiFlashSystemTables (ctx , sctx , e .extractor .TiDBDatabases , e .extractor .TiDBTables )
3028
3028
if err != nil {
3029
3029
return nil , err
3030
3030
}
@@ -3034,11 +3034,6 @@ func (e *TiFlashSystemTableRetriever) retrieve(ctx context.Context, sctx session
3034
3034
}
3035
3035
}
3036
3036
3037
- type tiflashInstanceInfo struct {
3038
- id string
3039
- url string
3040
- }
3041
-
3042
3037
func (e * TiFlashSystemTableRetriever ) initialize (sctx sessionctx.Context , tiflashInstances set.StringSet ) error {
3043
3038
storeInfo , err := infoschema .GetStoreServerInfo (sctx )
3044
3039
if err != nil {
@@ -3057,53 +3052,8 @@ func (e *TiFlashSystemTableRetriever) initialize(sctx sessionctx.Context, tiflas
3057
3052
if len (hostAndStatusPort ) != 2 {
3058
3053
return errors .Errorf ("node status addr: %s format illegal" , info .StatusAddr )
3059
3054
}
3060
- // fetch tiflash config
3061
- configURL := fmt .Sprintf ("%s://%s/config" , util .InternalHTTPSchema (), info .StatusAddr )
3062
- resp , err := util .InternalHTTPClient ().Get (configURL )
3063
- if err != nil {
3064
- sctx .GetSessionVars ().StmtCtx .AppendWarning (err )
3065
- continue
3066
- }
3067
- if resp .StatusCode != http .StatusOK {
3068
- return errors .Errorf ("request %s failed: %s" , configURL , resp .Status )
3069
- }
3070
- // parse http_port or https_port from the fetched config
3071
- var nestedConfig map [string ]interface {}
3072
- if err = json .NewDecoder (resp .Body ).Decode (& nestedConfig ); err != nil {
3073
- return err
3074
- }
3075
- if engineStoreConfig , ok := nestedConfig ["engine-store" ]; ok {
3076
- foundPort := false
3077
- var port interface {}
3078
- portProtocol := ""
3079
- if httpPort , ok := engineStoreConfig .(map [string ]interface {})["http_port" ]; ok {
3080
- foundPort = true
3081
- port = httpPort
3082
- portProtocol = "http"
3083
- } else if httpsPort , ok := engineStoreConfig .(map [string ]interface {})["https_port" ]; ok {
3084
- foundPort = true
3085
- port = httpsPort
3086
- portProtocol = "https"
3087
- }
3088
- if ! foundPort {
3089
- return errors .Errorf ("engine-store.http_port/https_port not found in server %s" , info .Address )
3090
- }
3091
- switch portValue := port .(type ) {
3092
- case float64 :
3093
- e .instanceInfos = append (e .instanceInfos , tiflashInstanceInfo {
3094
- id : info .Address ,
3095
- url : fmt .Sprintf ("%s://%s:%d" , portProtocol , hostAndStatusPort [0 ], int (portValue )),
3096
- })
3097
- e .instanceCount += 1
3098
- default :
3099
- return errors .Errorf ("engine-store.http_port value(%p) unexpected in server %s" , port , info .Address )
3100
- }
3101
- } else {
3102
- return errors .Errorf ("engine-store config not found in server %s" , info .Address )
3103
- }
3104
- if err = resp .Body .Close (); err != nil {
3105
- return err
3106
- }
3055
+ e .instanceIds = append (e .instanceIds , info .Address )
3056
+ e .instanceCount += 1
3107
3057
}
3108
3058
e .initialized = true
3109
3059
return nil
@@ -3119,7 +3069,7 @@ type tiFlashSQLExecuteResponse struct {
3119
3069
Data [][]interface {} `json:"data"`
3120
3070
}
3121
3071
3122
- func (e * TiFlashSystemTableRetriever ) dataForTiFlashSystemTables (ctx sessionctx.Context , tidbDatabases string , tidbTables string ) ([][]types.Datum , error ) {
3072
+ func (e * TiFlashSystemTableRetriever ) dataForTiFlashSystemTables (ctx context. Context , sctx sessionctx.Context , tidbDatabases string , tidbTables string ) ([][]types.Datum , error ) {
3123
3073
maxCount := 1024
3124
3074
targetTable := strings .ToLower (strings .Replace (e .table .Name .O , "TIFLASH" , "DT" , 1 ))
3125
3075
var filters []string
@@ -3134,29 +3084,33 @@ func (e *TiFlashSystemTableRetriever) dataForTiFlashSystemTables(ctx sessionctx.
3134
3084
sql = fmt .Sprintf ("%s WHERE %s" , sql , strings .Join (filters , " AND " ))
3135
3085
}
3136
3086
sql = fmt .Sprintf ("%s LIMIT %d, %d" , sql , e .rowIdx , maxCount )
3137
- instanceInfo := e .instanceInfos [e .instanceIdx ]
3138
- url := instanceInfo .url
3139
- req , err := http .NewRequest (http .MethodGet , url , nil )
3140
- if err != nil {
3141
- return nil , errors .Trace (err )
3087
+ request := tikvrpc.Request {
3088
+ Type : tikvrpc .CmdGetTiFlashSystemTable ,
3089
+ StoreTp : tikvrpc .TiFlash ,
3090
+ Req : & kvrpcpb.TiFlashSystemTableRequest {
3091
+ Sql : sql ,
3092
+ },
3142
3093
}
3143
- q := req .URL .Query ()
3144
- q .Add ("query" , sql )
3145
- q .Add ("default_format" , "JSONCompact" )
3146
- req .URL .RawQuery = q .Encode ()
3147
- resp , err := util .InternalHTTPClient ().Do (req )
3148
- if err != nil {
3149
- return nil , errors .Trace (err )
3094
+
3095
+ store := sctx .GetStore ()
3096
+ tikvStore , ok := store .(tikv.Storage )
3097
+ if ! ok {
3098
+ return nil , errors .New ("Get tiflash system tables can only run with tikv compatible storage" )
3150
3099
}
3151
- body , err := io .ReadAll (resp .Body )
3152
- terror .Log (resp .Body .Close ())
3100
+ // send request to tiflash, timeout is 1s
3101
+ instanceID := e .instanceIds [e .instanceIdx ]
3102
+ resp , err := tikvStore .GetTiKVClient ().SendRequest (ctx , instanceID , & request , time .Second )
3153
3103
if err != nil {
3154
3104
return nil , errors .Trace (err )
3155
3105
}
3156
3106
var result tiFlashSQLExecuteResponse
3157
- err = json .Unmarshal (body , & result )
3158
- if err != nil {
3159
- return nil , errors .Wrapf (err , "Failed to decode JSON from TiFlash" )
3107
+ if tiflashResp , ok := resp .Resp .(* kvrpcpb.TiFlashSystemTableResponse ); ok {
3108
+ err = json .Unmarshal (tiflashResp .Data , & result )
3109
+ if err != nil {
3110
+ return nil , errors .Wrapf (err , "Failed to decode JSON from TiFlash" )
3111
+ }
3112
+ } else {
3113
+ return nil , errors .Errorf ("Unexpected response type: %T" , resp .Resp )
3160
3114
}
3161
3115
3162
3116
// Map result columns back to our columns. It is possible that some columns cannot be
@@ -3206,7 +3160,7 @@ func (e *TiFlashSystemTableRetriever) dataForTiFlashSystemTables(ctx sessionctx.
3206
3160
return nil , errors .Errorf ("Meet column of unknown type %v" , column )
3207
3161
}
3208
3162
}
3209
- outputRow [len (e .outputCols )- 1 ].SetString (instanceInfo . id , mysql .DefaultCollationName )
3163
+ outputRow [len (e .outputCols )- 1 ].SetString (instanceID , mysql .DefaultCollationName )
3210
3164
outputRows = append (outputRows , outputRow )
3211
3165
}
3212
3166
e .rowIdx += len (outputRows )
0 commit comments