-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
528 changed files
with
28,385 additions
and
27,812 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
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
1 change: 1 addition & 0 deletions
1
dbm-services/mysql/db-partition/assests/migrations/000008_init.down.sql
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 @@ | ||
SET NAMES utf8; |
9 changes: 9 additions & 0 deletions
9
dbm-services/mysql/db-partition/assests/migrations/000008_init.up.sql
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,9 @@ | ||
SET NAMES utf8; | ||
CREATE TABLE `partition_customization_config` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`bk_biz_id` int(11) NOT NULL, | ||
`immute_domain` varchar(255) NOT NULL DEFAULT '', | ||
`partition_column` varchar(255) NOT NULL DEFAULT '', | ||
PRIMARY KEY (`id`), | ||
KEY `bk_biz_id` (`bk_biz_id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
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
46 changes: 46 additions & 0 deletions
46
dbm-services/mysql/db-partition/model/init_custimazation.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,46 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package model | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
var CustimazationMap map[int64]string | ||
|
||
// Custimazation TODO | ||
type Custimazation struct { | ||
id int `gorm:"column:id"` | ||
BkBizId int64 `json:"bk_biz_id" gorm:"column:bk_biz_id"` | ||
PartitionColumn string `json:"partition_column" gorm:"column:partition_column"` | ||
ImmuteDomain string `json:"immute_domain" gorm:"column:immute_domain"` | ||
} | ||
|
||
func InitCustimazation() { | ||
CustimazationMap = make(map[int64]string) | ||
custimazations := []Custimazation{} | ||
result := DB.Self.Session(&gorm.Session{ | ||
Logger: logger.Default.LogMode(logger.Info), | ||
}).Table("partition_customization_config").Find(&custimazations) | ||
if result.Error != nil { | ||
slog.Error("定制化配置读取失败!", result.Error) | ||
} | ||
for _, cs := range custimazations { | ||
if cs.ImmuteDomain != "" { | ||
CustimazationMap[cs.BkBizId] = cs.ImmuteDomain | ||
} else if cs.PartitionColumn != "" { | ||
CustimazationMap[cs.BkBizId] = cs.PartitionColumn | ||
} | ||
} | ||
} |
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.