Skip to content

Commit

Permalink
optimize maxAllowedPacket config fix: #292 #281
Browse files Browse the repository at this point in the history
  • Loading branch information
Penglq committed Jul 18, 2022
1 parent 71ece68 commit c8ba392
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 28 deletions.
7 changes: 3 additions & 4 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ data:
type: mysql
sql_max_limit: -1
tenant: arana
parameters:
maxAllowedPacket: 256M
groups:
- name: employees_0000
nodes:
Expand All @@ -51,7 +53,6 @@ data:
database: employees_0000
weight: r10w10
parameters:
maxAllowedPacket: 256M
- name: node0_r_0
host: arana-mysql
port: 3306
Expand All @@ -60,7 +61,6 @@ data:
database: employees_0000_r
weight: r0w0
parameters:
maxAllowedPacket: 256M
- name: employees_0001
nodes:
- name: node1
Expand All @@ -71,7 +71,6 @@ data:
database: employees_0001
weight: r10w10
parameters:
maxAllowedPacket: 256M
- name: employees_0002
nodes:
- name: node2
Expand All @@ -93,7 +92,7 @@ data:
database: employees_0003
weight: r10w10
parameters:
maxAllowedPacket: 256M
max_allowed_packet: 256M
sharding_rule:
tables:
- name: employees.student
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
go.uber.org/zap v1.19.1
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

Expand Down Expand Up @@ -110,7 +111,6 @@ require (
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 // indirect
google.golang.org/grpc v1.42.0 // indirect
Expand Down
32 changes: 23 additions & 9 deletions pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,54 @@ func Boot(ctx context.Context, provider Discovery) error {
return nil
}

func buildNamespace(ctx context.Context, provider Discovery, cluster string) (*namespace.Namespace, error) {
func buildNamespace(ctx context.Context, provider Discovery, clusterName string) (*namespace.Namespace, error) {
var (
groups []string
err error
cluster *config.DataSourceCluster
groups []string
err error
)

if groups, err = provider.ListGroups(ctx, cluster); err != nil {
cluster, err = provider.GetDataSourceCluster(ctx, clusterName)
if err != nil {
return nil, err
}
parameters := config.ParametersMap{}
if cluster != nil {
parameters = cluster.Parameters
}

if groups, err = provider.ListGroups(ctx, clusterName); err != nil {
return nil, err
}

var initCmds []namespace.Command
for _, group := range groups {
var nodes []string
if nodes, err = provider.ListNodes(ctx, cluster, group); err != nil {
if nodes, err = provider.ListNodes(ctx, clusterName, group); err != nil {
return nil, err
}
for _, it := range nodes {
var node *config.Node
if node, err = provider.GetNode(ctx, cluster, group, it); err != nil {
if node, err = provider.GetNode(ctx, clusterName, group, it); err != nil {
return nil, errors.WithStack(err)
}
if node.Parameters == nil {
node.Parameters = config.ParametersMap{}
}
node.Parameters.Merge(parameters)
initCmds = append(initCmds, namespace.UpsertDB(group, runtime.NewAtomDB(node)))
}
}

var tables []string
if tables, err = provider.ListTables(ctx, cluster); err != nil {
if tables, err = provider.ListTables(ctx, clusterName); err != nil {
return nil, errors.WithStack(err)
}

var ru rule.Rule
for _, table := range tables {
var vt *rule.VTable
if vt, err = provider.GetTable(ctx, cluster, table); err != nil {
if vt, err = provider.GetTable(ctx, clusterName, table); err != nil {
return nil, err
}
if vt == nil {
Expand All @@ -129,5 +143,5 @@ func buildNamespace(ctx context.Context, provider Discovery, cluster string) (*n
}
initCmds = append(initCmds, namespace.UpdateRule(&ru))

return namespace.New(cluster, initCmds...), nil
return namespace.New(clusterName, initCmds...), nil
}
10 changes: 10 additions & 0 deletions pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type Discovery interface {

// ListClusters lists the cluster names.
ListClusters(ctx context.Context) ([]string, error)
// GetClusterObject returns the dataSourceCluster object
GetDataSourceCluster(ctx context.Context, cluster string) (*config.DataSourceCluster, error)
// GetCluster returns the cluster info
GetCluster(ctx context.Context, cluster string) (*Cluster, error)
// ListGroups lists the group names.
Expand Down Expand Up @@ -163,6 +165,14 @@ func (fp *discovery) GetConfigCenter() *config.Center {
return fp.c
}

func (fp *discovery) GetDataSourceCluster(ctx context.Context, cluster string) (*config.DataSourceCluster, error) {
dataSourceCluster, ok := fp.loadCluster(cluster)
if !ok {
return nil, nil
}
return dataSourceCluster, nil
}

func (fp *discovery) GetCluster(ctx context.Context, cluster string) (*Cluster, error) {
exist, ok := fp.loadCluster(cluster)
if !ok {
Expand Down
27 changes: 26 additions & 1 deletion pkg/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ import (
"strconv"
"strings"
"time"
"unicode"
)

import (
"github.com/go-playground/validator/v10"

"github.com/pkg/errors"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -80,6 +84,7 @@ type (
Type DataSourceType `yaml:"type" json:"type"`
SqlMaxLimit int `default:"-1" yaml:"sql_max_limit" json:"sql_max_limit,omitempty"`
Tenant string `yaml:"tenant" json:"tenant"`
Parameters ParametersMap `yaml:"parameters" json:"parameters"`
Groups []*Group `yaml:"groups" json:"groups"`
}

Expand Down Expand Up @@ -141,17 +146,37 @@ type (

type ParametersMap map[string]string

func (pm *ParametersMap) Merge(parametersMap ParametersMap) {
for key, val := range parametersMap {
if _, ok := (*pm)[key]; !ok {
(*pm)[key] = val
}
}
}

func (pm *ParametersMap) String() string {
sBuff := strings.Builder{}
for k, v := range *pm {
sBuff.WriteString(k)
sBuff.WriteString(pm.LowercaseFirstLetter(pm.Camel(k)))
sBuff.WriteString("=")
sBuff.WriteString(v)
sBuff.WriteString("&")
}
return strings.TrimRight(sBuff.String(), "&")
}

// Camel underline to camel
func (pm *ParametersMap) Camel(name string) string {
name = strings.Replace(name, "_", " ", -1)
name = cases.Title(language.English, cases.NoLower).String(name)
return strings.Replace(name, " ", "", -1)
}

// LowercaseFirstLetter lowercase letter
func (pm *ParametersMap) LowercaseFirstLetter(str string) string {
return string(unicode.ToLower(rune(str[0]))) + str[1:]
}

// Decoder decodes configuration.
type Decoder struct {
reader io.Reader
Expand Down
6 changes: 6 additions & 0 deletions pkg/constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ const (
ConfigPathKey = "config"
ImportConfigPathKey = "source"
)

const (
SQLShowVariables = "SHOW VARIABLES WHERE Variable_name = '%s'"

VariableNameMaxAllowedPacket = "max_allowed_packet"
)
Loading

0 comments on commit c8ba392

Please sign in to comment.