Skip to content

Commit 797d5ef

Browse files
authored
Dev local (#8900)
2 parents 9760ec6 + e8fa3fa commit 797d5ef

File tree

528 files changed

+28385
-27812
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

528 files changed

+28385
-27812
lines changed

dbm-services/common/go-pubpkg/errno/code.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ package errno
1313
var (
1414
// OK TODO
1515
OK = Errno{Code: 0, Message: "", CNMessage: ""}
16+
// SimulationTaskFailed 语义检查失败
17+
SimulationTaskFailed = Errno{Code: 1, Message: "simulation failed", CNMessage: "模拟执行失败"}
1618

1719
// InternalServerError TODO
1820
InternalServerError = Errno{Code: 10001, Message: "Internal server error", CNMessage: "服务器内部错误。"}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cmd
2+
cmd/*
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package common
2+
3+
import (
4+
"dbm-services/common/reverse-api/config"
5+
"dbm-services/common/reverse-api/internal"
6+
"encoding/json"
7+
8+
"github.com/pkg/errors"
9+
)
10+
11+
func ListNginxAddrs(bkCloudId int) ([]string, error) {
12+
data, err := internal.ReverseCall(config.ReverseApiCommonListNginxAddrs, bkCloudId)
13+
if err != nil {
14+
return nil, errors.Wrap(err, "failed to call ListNginxAddrs")
15+
}
16+
17+
var addrs []string
18+
if err := json.Unmarshal(data, &addrs); err != nil {
19+
return nil, errors.Wrap(err, "failed to unmarshal ListNginxAddrs")
20+
}
21+
22+
return addrs, nil
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mysql
2+
3+
import (
4+
"dbm-services/common/reverse-api/config"
5+
"dbm-services/common/reverse-api/internal"
6+
"encoding/json"
7+
8+
"github.com/pkg/errors"
9+
)
10+
11+
const (
12+
AccessLayerStorage string = "storage"
13+
AccessLayerProxy string = "proxy"
14+
)
15+
16+
type instanceAddr struct {
17+
Ip string `json:"ip"`
18+
Port int `json:"port"`
19+
}
20+
21+
type commonInstanceInfo struct {
22+
instanceAddr
23+
ImmuteDomain string `json:"immute_domain"`
24+
Phase string `json:"phase"`
25+
Status string `json:"status"`
26+
AccessLayer string `json:"access_layer"`
27+
MachineType string `json:"machine_type"`
28+
}
29+
30+
type StorageInstanceInfo struct {
31+
commonInstanceInfo
32+
IsStandBy bool `json:"is_stand_by"`
33+
InstanceRole string `json:"instance_role"`
34+
InstanceInnerRole string `json:"instance_inner_role"`
35+
Receivers []instanceAddr `json:"receivers"`
36+
Ejectors []instanceAddr `json:"ejectors"`
37+
}
38+
39+
type ProxyInstanceInfo struct {
40+
commonInstanceInfo
41+
StorageInstanceList []instanceAddr `json:"storage_instance_list"`
42+
}
43+
44+
func ListInstanceInfo(bkCloudId int, ports ...int) ([]byte, string, error) {
45+
data, err := internal.ReverseCall(config.ReverseApiMySQLListInstanceInfo, bkCloudId, ports...)
46+
if err != nil {
47+
return nil, "", errors.Wrap(err, "failed to call ListInstanceInfo")
48+
}
49+
var r []commonInstanceInfo
50+
err = json.Unmarshal(data, &r)
51+
if err != nil {
52+
return nil, "", errors.Wrap(err, "failed to unmarshal ListInstanceInfo")
53+
}
54+
55+
return data, r[0].AccessLayer, nil
56+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
const (
4+
ReverseApiCommonListNginxAddrs = "common/list_nginx_addrs"
5+
ReverseApiMySQLListInstanceInfo = "mysql/list_instance_info"
6+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package config
2+
3+
const CommonConfigDir = "/home/mysql/common_config"
4+
const NginxProxyAddrsFileName = "nginx_proxy.list"
5+
const ReverseApiBase = "apis/proxypass/reverse_api"
6+
7+
type ReverseApiName string
8+
9+
func (c ReverseApiName) String() string {
10+
return string(c)
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module dbm-services/common/reverse-api
2+
3+
go 1.21.11
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package reverse_api
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package internal
2+
3+
import "encoding/json"
4+
5+
type apiResponse struct {
6+
Result bool `json:"result"`
7+
Code int `json:"code"`
8+
Message string `json:"message"`
9+
Errors string `json:"errors"`
10+
Data json.RawMessage `json:"data"`
11+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package internal
2+
3+
import (
4+
"bufio"
5+
"dbm-services/common/reverse-api/config"
6+
"encoding/json"
7+
errs "errors"
8+
"io"
9+
"net/http"
10+
"net/url"
11+
"os"
12+
"path/filepath"
13+
"strconv"
14+
15+
"github.com/pkg/errors"
16+
)
17+
18+
func ReverseCall(api config.ReverseApiName, bkCloudId int, ports ...int) (data []byte, err error) {
19+
addrs, err := readNginxProxyAddrs()
20+
if err != nil {
21+
return nil, errors.Wrap(err, "failed to read nginx proxy addresses")
22+
}
23+
24+
var errCollect []error
25+
for _, addr := range addrs {
26+
apiPath, _ := url.JoinPath(config.ReverseApiBase, api.String(), "/")
27+
ep := url.URL{
28+
Scheme: "http",
29+
Host: addr,
30+
Path: apiPath,
31+
}
32+
33+
req, err := http.NewRequest(http.MethodGet, ep.String(), nil)
34+
if err != nil {
35+
return nil, errors.Wrap(err, "failed to create request")
36+
}
37+
38+
q := req.URL.Query()
39+
q.Add("bk_cloud_id", strconv.Itoa(bkCloudId))
40+
for _, port := range ports {
41+
q.Add("port", strconv.Itoa(port))
42+
}
43+
req.URL.RawQuery = q.Encode()
44+
45+
data, err = do(req)
46+
if err == nil {
47+
return data, nil
48+
}
49+
errCollect = append(errCollect, err)
50+
}
51+
52+
return nil, errs.Join(errCollect...)
53+
}
54+
55+
func do(request *http.Request) (data []byte, err error) {
56+
resp, err := http.DefaultClient.Do(request)
57+
if err != nil {
58+
return nil, errors.Wrap(err, "failed to send request")
59+
}
60+
defer func() {
61+
_ = resp.Body.Close()
62+
}()
63+
64+
b, err := io.ReadAll(resp.Body)
65+
if err != nil {
66+
return nil, errors.Wrap(err, "failed to read response body")
67+
}
68+
69+
if resp.StatusCode != http.StatusOK {
70+
return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(b))
71+
}
72+
73+
var r apiResponse
74+
err = json.Unmarshal(b, &r)
75+
if err != nil {
76+
return nil, errors.Wrap(err, "failed to unmarshal response body")
77+
}
78+
79+
if !r.Result {
80+
return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, r.Errors)
81+
}
82+
83+
return r.Data, nil
84+
}
85+
86+
func readNginxProxyAddrs() (addrs []string, err error) {
87+
f, err := os.Open(filepath.Join(config.CommonConfigDir, config.NginxProxyAddrsFileName))
88+
if err != nil {
89+
return nil, errors.Wrap(err, "failed to open nginx proxy addrs")
90+
}
91+
defer func() {
92+
_ = f.Close()
93+
}()
94+
95+
scanner := bufio.NewScanner(f)
96+
for scanner.Scan() {
97+
addrs = append(addrs, scanner.Text())
98+
}
99+
if err := scanner.Err(); err != nil {
100+
return nil, errors.Wrap(err, "failed to read nginx proxy addrs")
101+
}
102+
return addrs, nil
103+
}

0 commit comments

Comments
 (0)