From 1cce89cb98690ab54c58a78def4c7155e5078509 Mon Sep 17 00:00:00 2001 From: lidezhu Date: Tue, 21 Dec 2021 11:43:00 +0800 Subject: [PATCH 1/5] get tiflash http(s) port from config instead of etcd --- executor/infoschema_reader.go | 111 ++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 45 deletions(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 1e4fcae3829ba..0c040760ef389 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -33,7 +33,6 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/kvproto/pkg/deadlock" "github.com/pingcap/tidb/ddl/label" - "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/domain/infosync" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/expression" @@ -69,7 +68,6 @@ import ( "github.com/pingcap/tidb/util/sqlexec" "github.com/pingcap/tidb/util/stmtsummary" "github.com/pingcap/tidb/util/stringutil" - "go.etcd.io/etcd/clientv3" "go.uber.org/zap" ) @@ -2615,54 +2613,77 @@ type tiflashInstanceInfo struct { } func (e *TiFlashSystemTableRetriever) initialize(sctx sessionctx.Context, tiflashInstances set.StringSet) error { - store := sctx.GetStore() - if etcd, ok := store.(kv.EtcdBackend); ok { - var addrs []string - var err error - if addrs, err = etcd.EtcdAddrs(); err != nil { + storeInfo, err := infoschema.GetStoreServerInfo(sctx) + if err != nil { + return err + } + + for _, info := range storeInfo { + if info.ServerType != kv.TiFlash.Name() { + continue + } + info.ResolveLoopBackAddr() + if len(tiflashInstances) > 0 && !tiflashInstances.Exist(info.Address) { + continue + } + hostAndStatusPort := strings.Split(info.StatusAddr, ":") + if len(hostAndStatusPort) != 2 { + return errors.Errorf("node status addr %s format unknown %s failed: %s", info.StatusAddr) + } + // fetch tiflash config + configURL := fmt.Sprintf("%s://%s/config", util.InternalHTTPSchema(), info.StatusAddr) + req, err := http.NewRequest(http.MethodGet, configURL, nil) + if err != nil { return err } - if addrs != nil { - domainFromCtx := domain.GetDomain(sctx) - if domainFromCtx != nil { - cli := domainFromCtx.GetEtcdClient() - prefix := "/tiflash/cluster/http_port/" - kv := clientv3.NewKV(cli) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - resp, err := kv.Get(ctx, prefix, clientv3.WithPrefix()) - cancel() - if err != nil { - return errors.Trace(err) - } - for _, ev := range resp.Kvs { - id := string(ev.Key)[len(prefix):] - if len(tiflashInstances) > 0 && !tiflashInstances.Exist(id) { - continue - } - url := fmt.Sprintf("%s://%s", util.InternalHTTPSchema(), ev.Value) - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - return errors.Trace(err) - } - resp, err := util.InternalHTTPClient().Do(req) - if err != nil { - sctx.GetSessionVars().StmtCtx.AppendWarning(err) - continue - } - resp.Body.Close() - e.instanceInfos = append(e.instanceInfos, tiflashInstanceInfo{ - id: id, - url: url, - }) - e.instanceCount += 1 - } - e.initialized = true - return nil + resp, err := util.InternalHTTPClient().Do(req) + if err != nil { + sctx.GetSessionVars().StmtCtx.AppendWarning(err) + continue + } + if resp.StatusCode != http.StatusOK { + return errors.Errorf("request %s failed: %s", configURL, resp.Status) + } + // parse http_port or https_port from the fetched config + var nestedConfig map[string]interface{} + if err = json.NewDecoder(resp.Body).Decode(&nestedConfig); err != nil { + return err + } + if engineStoreConfig, ok := nestedConfig["engine-store"]; ok { + foundPort := false + var port interface{} + portProtocol := "" + if httpPort, ok := engineStoreConfig.(map[string]interface{})["http_port"]; ok { + foundPort = true + port = httpPort + portProtocol = "http" + } else if httpsPort, ok := engineStoreConfig.(map[string]interface{})["https_port"]; ok { + foundPort = true + port = httpsPort + portProtocol = "https" + } + if !foundPort { + return errors.Errorf("engine-store.http_port/https_port not found in server %s", info.Address) + } + switch port.(type) { + case float64: + e.instanceInfos = append(e.instanceInfos, tiflashInstanceInfo{ + id: info.Address, + url: fmt.Sprintf("%s://%s:%d", portProtocol, hostAndStatusPort[0], int(port.(float64))), + }) + e.instanceCount += 1 + default: + return errors.Errorf("engine-store.http_port value(%p) unexpected in server %s", port, info.Address) } + } else { + return errors.Errorf("engine-store config not found in server %s", info.Address) + } + if err = resp.Body.Close(); err != nil { + return err } - return errors.Errorf("Etcd addrs not found") } - return errors.Errorf("%T not an etcd backend", store) + e.initialized = true + return nil } func (e *TiFlashSystemTableRetriever) dataForTiFlashSystemTables(ctx sessionctx.Context, tidbDatabases string, tidbTables string) ([][]types.Datum, error) { From 51e6f5bfe580b8efd673051a2ae88bb38e5d9afb Mon Sep 17 00:00:00 2001 From: lidezhu Date: Tue, 21 Dec 2021 14:57:00 +0800 Subject: [PATCH 2/5] fix check dev --- executor/infoschema_reader.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 0c040760ef389..355e9cb531f01 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -2665,11 +2665,11 @@ func (e *TiFlashSystemTableRetriever) initialize(sctx sessionctx.Context, tiflas if !foundPort { return errors.Errorf("engine-store.http_port/https_port not found in server %s", info.Address) } - switch port.(type) { + switch portValue := port.(type) { case float64: e.instanceInfos = append(e.instanceInfos, tiflashInstanceInfo{ id: info.Address, - url: fmt.Sprintf("%s://%s:%d", portProtocol, hostAndStatusPort[0], int(port.(float64))), + url: fmt.Sprintf("%s://%s:%d", portProtocol, hostAndStatusPort[0], int(portValue)), }) e.instanceCount += 1 default: From 04ba0c06ebc079097f4f9f96146b0533b42543ea Mon Sep 17 00:00:00 2001 From: lidezhu Date: Tue, 21 Dec 2021 15:08:49 +0800 Subject: [PATCH 3/5] fix wrong fmt parameters --- executor/infoschema_reader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 355e9cb531f01..dbaf2dc3f374a 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -2628,7 +2628,7 @@ func (e *TiFlashSystemTableRetriever) initialize(sctx sessionctx.Context, tiflas } hostAndStatusPort := strings.Split(info.StatusAddr, ":") if len(hostAndStatusPort) != 2 { - return errors.Errorf("node status addr %s format unknown %s failed: %s", info.StatusAddr) + return errors.Errorf("node status addr: %s format illegal", info.StatusAddr) } // fetch tiflash config configURL := fmt.Sprintf("%s://%s/config", util.InternalHTTPSchema(), info.StatusAddr) From 80a09983878d19b9c864e1f0ebb5e30911a8d4fc Mon Sep 17 00:00:00 2001 From: lidezhu Date: Tue, 21 Dec 2021 17:35:16 +0800 Subject: [PATCH 4/5] fix unit test --- executor/infoschema_reader_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/executor/infoschema_reader_test.go b/executor/infoschema_reader_test.go index 53fd409f0fd2a..47c16e1a7623c 100644 --- a/executor/infoschema_reader_test.go +++ b/executor/infoschema_reader_test.go @@ -969,9 +969,9 @@ func (s *testInfoschemaTableSuite) TestSequences(c *C) { func (s *testInfoschemaTableSuite) TestTiFlashSystemTables(c *C) { tk := testkit.NewTestKit(c, s.store) err := tk.QueryToErr("select * from information_schema.TIFLASH_TABLES;") - c.Assert(err.Error(), Equals, "Etcd addrs not found") + c.Assert(err, Equals, nil) err = tk.QueryToErr("select * from information_schema.TIFLASH_SEGMENTS;") - c.Assert(err.Error(), Equals, "Etcd addrs not found") + c.Assert(err, Equals, nil) } func (s *testInfoschemaTableSuite) TestTablesPKType(c *C) { From 1837f0212ccc393af5c18fd9c61b42b537f18cfb Mon Sep 17 00:00:00 2001 From: lidezhu Date: Wed, 22 Dec 2021 13:10:43 +0800 Subject: [PATCH 5/5] use Get instead of create a explicit request --- executor/infoschema_reader.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index dbaf2dc3f374a..c6b67803b83f4 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -2632,11 +2632,7 @@ func (e *TiFlashSystemTableRetriever) initialize(sctx sessionctx.Context, tiflas } // fetch tiflash config configURL := fmt.Sprintf("%s://%s/config", util.InternalHTTPSchema(), info.StatusAddr) - req, err := http.NewRequest(http.MethodGet, configURL, nil) - if err != nil { - return err - } - resp, err := util.InternalHTTPClient().Do(req) + resp, err := util.InternalHTTPClient().Get(configURL) if err != nil { sctx.GetSessionVars().StmtCtx.AppendWarning(err) continue