Skip to content

Commit

Permalink
refactor: refact db group (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-lockdown committed Aug 13, 2022
1 parent 7a1a977 commit a7ddbfc
Show file tree
Hide file tree
Showing 17 changed files with 731 additions and 768 deletions.
1 change: 1 addition & 0 deletions docker/conf/config_rws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ app_config:
- mysqlDTFilter

- name: employees-slave
master_name: employees-master
capacity: 10
max_capacity: 20
idle_timeout: 60s
Expand Down
70 changes: 64 additions & 6 deletions pkg/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ package config
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
)

"github.com/cectc/dbpack/pkg/lb"
const (
weightRegex = `^r([\d]+)w([\d]+)$`
)

type (
Expand All @@ -35,10 +40,13 @@ type (

ExecuteMode byte

LoadBalanceAlgorithm int32

// DataSource ...
DataSource struct {
Name string `yaml:"name" json:"name"`
DSN string `yaml:"dsn" json:"dsn"`
MasterName string `yaml:"master_name" json:"master_name"`
Capacity int `yaml:"capacity" json:"capacity"` // connection pool capacity
MaxCapacity int `yaml:"max_capacity" json:"max_capacity"` // max connection pool capacity
IdleTimeout time.Duration `yaml:"idle_timeout" json:"idle_timeout"` // close backend direct connection after idle_timeout,unit: seconds
Expand All @@ -53,14 +61,14 @@ type (
}

ReadWriteSplittingConfig struct {
LoadBalanceAlgorithm lb.LoadBalanceAlgorithm `yaml:"load_balance_algorithm" json:"load_balance_algorithm"`
DataSources []*DataSourceRef `yaml:"data_sources" json:"data_sources"`
LoadBalanceAlgorithm LoadBalanceAlgorithm `yaml:"load_balance_algorithm" json:"load_balance_algorithm"`
DataSources []*DataSourceRef `yaml:"data_sources" json:"data_sources"`
}

DataSourceRefGroup struct {
Name string `yaml:"name" json:"name"`
LBAlgorithm lb.LoadBalanceAlgorithm `yaml:"load_balance_algorithm" json:"load_balance_algorithm"`
DataSources []*DataSourceRef `yaml:"data_sources" json:"data_sources"`
Name string `yaml:"name" json:"name"`
LBAlgorithm LoadBalanceAlgorithm `yaml:"load_balance_algorithm" json:"load_balance_algorithm"`
DataSources []*DataSourceRef `yaml:"data_sources" json:"data_sources"`
}

ShardingRule struct {
Expand Down Expand Up @@ -101,6 +109,12 @@ const (
SHD
)

const (
Random LoadBalanceAlgorithm = iota
RoundRobin
RandomWeight
)

func (r *DataSourceRole) UnmarshalText(text []byte) error {
if r == nil {
return errors.New("can't unmarshal a nil *DataSourceRole")
Expand Down Expand Up @@ -183,3 +197,47 @@ func (m *ExecuteMode) unmarshalText(text []byte) bool {
}
return true
}

func (l *LoadBalanceAlgorithm) UnmarshalText(text []byte) error {
if l == nil {
return errors.New("can't unmarshal a nil *ProtocolType")
}
if !l.unmarshalText(bytes.ToLower(text)) {
return fmt.Errorf("unrecognized protocol type: %s", text)
}
return nil
}

func (l *LoadBalanceAlgorithm) unmarshalText(text []byte) bool {
alg := string(text)
if strings.EqualFold(alg, "Random") {
*l = Random
return true
}
if strings.EqualFold(alg, "RoundRobin") {
*l = RoundRobin
return true
}
if strings.EqualFold(alg, "RandomWeight") {
*l = RandomWeight
return true
}
return false
}

func (dataSource *DataSourceRef) ParseWeight() (readWeight int, writeWeight int, err error) {
weightRegexp := regexp.MustCompile(weightRegex)
params := weightRegexp.FindStringSubmatch(dataSource.Weight)
if len(params) != 3 {
return 0, 0, errors.Errorf("datasource reference '%s' weight invalid: %s", dataSource.Name, dataSource.Weight)
}
rw, err := strconv.Atoi(params[1])
if err != nil {
return 0, 0, errors.Errorf("cast read weight for datasource reference '%s' failed, read weight: %s", dataSource.Name, params[1])
}
ww, err := strconv.Atoi(params[2])
if err != nil {
return 0, 0, errors.Errorf("cast write weight for datasource reference '%s' failed, write weight: %s", dataSource.Name, params[2])
}
return rw, ww, nil
}
97 changes: 0 additions & 97 deletions pkg/executor/data_source.go

This file was deleted.

160 changes: 0 additions & 160 deletions pkg/executor/data_source_test.go

This file was deleted.

Loading

0 comments on commit a7ddbfc

Please sign in to comment.