forked from TencentBlueKing/blueking-dbm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mysql): mysql-monitor移除context timeout TencentBlueKing#8730
- Loading branch information
Showing
52 changed files
with
440 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cmd | ||
cmd/* |
23 changes: 23 additions & 0 deletions
23
dbm-services/common/reverse-api/apis/common/list_nginx_addrs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common | ||
|
||
import ( | ||
"dbm-services/common/reverse-api/config" | ||
"dbm-services/common/reverse-api/internal" | ||
"encoding/json" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func ListNginxAddrs(bkCloudId int) ([]string, error) { | ||
data, err := internal.ReverseCall(config.ReverseApiCommonListNginxAddrs, bkCloudId) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to call ListNginxAddrs") | ||
} | ||
|
||
var addrs []string | ||
if err := json.Unmarshal(data, &addrs); err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal ListNginxAddrs") | ||
} | ||
|
||
return addrs, nil | ||
} |
56 changes: 56 additions & 0 deletions
56
dbm-services/common/reverse-api/apis/mysql/list_instance_info.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mysql | ||
|
||
import ( | ||
"dbm-services/common/reverse-api/config" | ||
"dbm-services/common/reverse-api/internal" | ||
"encoding/json" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
AccessLayerStorage string = "storage" | ||
AccessLayerProxy string = "proxy" | ||
) | ||
|
||
type instanceAddr struct { | ||
Ip string `json:"ip"` | ||
Port int `json:"port"` | ||
} | ||
|
||
type commonInstanceInfo struct { | ||
instanceAddr | ||
ImmuteDomain string `json:"immute_domain"` | ||
Phase string `json:"phase"` | ||
Status string `json:"status"` | ||
AccessLayer string `json:"access_layer"` | ||
MachineType string `json:"machine_type"` | ||
} | ||
|
||
type StorageInstanceInfo struct { | ||
commonInstanceInfo | ||
IsStandBy bool `json:"is_stand_by"` | ||
InstanceRole string `json:"instance_role"` | ||
InstanceInnerRole string `json:"instance_inner_role"` | ||
Receivers []instanceAddr `json:"receivers"` | ||
Ejectors []instanceAddr `json:"ejectors"` | ||
} | ||
|
||
type ProxyInstanceInfo struct { | ||
commonInstanceInfo | ||
StorageInstanceList []instanceAddr `json:"storage_instance_list"` | ||
} | ||
|
||
func ListInstanceInfo(bkCloudId int, ports ...int) ([]byte, string, error) { | ||
data, err := internal.ReverseCall(config.ReverseApiMySQLListInstanceInfo, bkCloudId, ports...) | ||
if err != nil { | ||
return nil, "", errors.Wrap(err, "failed to call ListInstanceInfo") | ||
} | ||
var r []commonInstanceInfo | ||
err = json.Unmarshal(data, &r) | ||
if err != nil { | ||
return nil, "", errors.Wrap(err, "failed to unmarshal ListInstanceInfo") | ||
} | ||
|
||
return data, r[0].AccessLayer, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package config | ||
|
||
const ( | ||
ReverseApiCommonListNginxAddrs = "common/list_nginx_addrs" | ||
ReverseApiMySQLListInstanceInfo = "mysql/list_instance_info" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package config | ||
|
||
const CommonConfigDir = "/home/mysql/common_config" | ||
const NginxProxyAddrsFileName = "nginx_proxy.list" | ||
const ReverseApiBase = "apis/proxypass/reverse_api" | ||
|
||
type ReverseApiName string | ||
|
||
func (c ReverseApiName) String() string { | ||
return string(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module dbm-services/common/reverse-api | ||
|
||
go 1.21.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package reverse_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package internal | ||
|
||
import "encoding/json" | ||
|
||
type apiResponse struct { | ||
Result bool `json:"result"` | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Errors string `json:"errors"` | ||
Data json.RawMessage `json:"data"` | ||
} |
103 changes: 103 additions & 0 deletions
103
dbm-services/common/reverse-api/internal/reverse_call.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"dbm-services/common/reverse-api/config" | ||
"encoding/json" | ||
errs "errors" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func ReverseCall(api config.ReverseApiName, bkCloudId int, ports ...int) (data []byte, err error) { | ||
addrs, err := readNginxProxyAddrs() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to read nginx proxy addresses") | ||
} | ||
|
||
var errCollect []error | ||
for _, addr := range addrs { | ||
apiPath, _ := url.JoinPath(config.ReverseApiBase, api.String(), "/") | ||
ep := url.URL{ | ||
Scheme: "http", | ||
Host: addr, | ||
Path: apiPath, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, ep.String(), nil) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create request") | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("bk_cloud_id", strconv.Itoa(bkCloudId)) | ||
for _, port := range ports { | ||
q.Add("port", strconv.Itoa(port)) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
|
||
data, err = do(req) | ||
if err == nil { | ||
return data, nil | ||
} | ||
errCollect = append(errCollect, err) | ||
} | ||
|
||
return nil, errs.Join(errCollect...) | ||
} | ||
|
||
func do(request *http.Request) (data []byte, err error) { | ||
resp, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to send request") | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to read response body") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(b)) | ||
} | ||
|
||
var r apiResponse | ||
err = json.Unmarshal(b, &r) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal response body") | ||
} | ||
|
||
if !r.Result { | ||
return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, r.Errors) | ||
} | ||
|
||
return r.Data, nil | ||
} | ||
|
||
func readNginxProxyAddrs() (addrs []string, err error) { | ||
f, err := os.Open(filepath.Join(config.CommonConfigDir, config.NginxProxyAddrsFileName)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to open nginx proxy addrs") | ||
} | ||
defer func() { | ||
_ = f.Close() | ||
}() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
addrs = append(addrs, scanner.Text()) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, errors.Wrap(err, "failed to read nginx proxy addrs") | ||
} | ||
return addrs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.