Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support range shard table based on default value #190

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DEBIAN/conffiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Package:
Version:
Maintainer:
Description: UC DB Proxy Server.
14 changes: 14 additions & 0 deletions DEBIAN/mkrootfs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
#make root file system for debian pakcage
#EnvirmentVar: DEB_ROOTFS, DEB_CTRLDIR

mkdir -p -m 755 "${DEB_ROOTFS}" || exit 1
mkdir -p -m 755 "${DEB_ROOTFS}"/${UC_ROOT}/sbin || exit 2
install -m 755 "bin/ucdbproxy" "${DEB_ROOTFS}"/${UC_ROOT}/sbin || exit 3
mkdir -p -m 755 "${DEB_ROOTFS}/${UC_ROOT}/etc" || exit 4
mkdir -p -m 755 "${DEB_ROOTFS}/${UC_ROOT}/etc/init.d" || exit 5
mkdir -p -m 755 "${DEB_ROOTFS}/${UC_ROOT}/share" || exit 6
install -m 644 "etc/ks.yaml" "${DEB_ROOTFS}/${UC_ROOT}/etc" || exit 7
install -m 744 "ucdbproxy.sh" "${DEB_ROOTFS}/${UC_ROOT}/etc/init.d" || exit 8
chown -R root:root "${DEB_ROOTFS}/${UC_ROOT}" || exit 9

2 changes: 2 additions & 0 deletions DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh

2 changes: 2 additions & 0 deletions DEBIAN/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh

2 changes: 2 additions & 0 deletions DEBIAN/preinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh

2 changes: 2 additions & 0 deletions DEBIAN/prerm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh

22 changes: 10 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
all: build
TOPDIR=../../../../../
include ${TOPDIR}/build-scripts/Macros.mk

DEB_PACKAGE=ucdbproxy
DEB_VERSION=1.0.0.0
#DEB_MAINTAINER=
#DEB_DESCRIPTION=
DEB_DEPENDS=

include ${TOPDIR}/build-scripts/deb.mk

build: kingshard

kingshard:
go tool yacc -o ./sqlparser/sql.go ./sqlparser/sql.y
gofmt -w ./sqlparser/sql.go
@bash genver.sh
go build -o ./bin/kingshard ./cmd/kingshard
clean:
@rm -rf bin
@rm -f ./sqlparser/y.output ./sqlparser/sql.go

test:
go test ./go/... -race
15 changes: 15 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: build

build: kingshard

kingshard:
go tool yacc -o ./sqlparser/sql.go ./sqlparser/sql.y
gofmt -w ./sqlparser/sql.go
@bash genver.sh
go build -o ./bin/kingshard ./cmd/kingshard
clean:
@rm -rf bin
@rm -f ./sqlparser/y.output ./sqlparser/sql.go

test:
go test ./go/... -race
14 changes: 14 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
export GOBIN=/usr/local/go/bin
export GOARCH=amd64
export GOROOT=/usr/local/go
export GOOS=linux

source ./dev.sh

go tool yacc -o ./sqlparser/sql.go ./sqlparser/sql.y
gofmt -w ./sqlparser/sql.go
source genver.sh
go build -o ./bin/ucdbproxy ./cmd/kingshard


23 changes: 23 additions & 0 deletions cmd/kingshard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ const banner string = `
/____/
`

func writePID(cfg *config.Config) {
pidpath := cfg.PidPath
if pidpath == "" {
fmt.Println("WritePid failed, please confirm that the pidfile configuration item are correctly filed")
os.Exit(-1)
}

fp, err := os.Create(pidpath)
if err != nil {
fmt.Println("WritePID failed, create pidfile failed %s", err.Error())
os.Exit(-1)
}
defer fp.Close()

pid := os.Getpid()
_, err = fp.WriteString(fmt.Sprintf("%d", pid))
if err != nil {
fmt.Println("WritePID failed, %s", err.Error())
os.Exit(-1)
}
}

func main() {
fmt.Print(banner)
runtime.GOMAXPROCS(runtime.NumCPU())
Expand Down Expand Up @@ -117,6 +139,7 @@ func main() {
svr.Close()
}()

writePID(cfg)
svr.Run()
}

Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

//整个config文件对应的结构
type Config struct {
PidPath string `yaml:"pidfile"`
Addr string `yaml:"addr"`
User string `yaml:"user"`
Password string `yaml:"password"`
Expand Down Expand Up @@ -66,6 +67,7 @@ type ShardConfig struct {
Nodes []string `yaml:"nodes"`
Locations []int `yaml:"locations"`
Type string `yaml:"type"`
TableRowBase int `yaml:"table_row_base"`
TableRowLimit int `yaml:"table_row_limit"`
DateRange []string `yaml:"date_range"`
}
Expand Down
4 changes: 0 additions & 4 deletions dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ if [[ "$VTTOP" == "${VTTOP/\/src\/github.com\/flike\/kingshard/}" ]]; then
fi

export GOTOP=$VTTOP

function prepend_path()
{
# $1 path variable
# $2 path to add
if [ -d "$2" ] && [[ ":$1:" != *":$2:"* ]]; then
echo "$2:$1"
else
Expand All @@ -21,4 +18,3 @@ function prepend_path()
}

export GOPATH=$(prepend_path $GOPATH $VTROOT)

79 changes: 23 additions & 56 deletions etc/ks.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
pidfile : /uc/etc/ucdbproxy.pid

# server listen addr
addr : 0.0.0.0:9696

# server user and password
user : kingshard
password : kingshard
user : ucdbproxy
password : ucdbproxy

# if set log_path, the sql log will write into log_path/sql.log,the system log
# will write into log_path/sys.log
#log_path : /Users/flike/log

# log level[debug|info|warn|error],default error
log_level : debug
log_level : info

# if set log_sql(on|off) off,the sql log will not output
log_sql: on
Expand All @@ -27,82 +29,47 @@ log_sql: on

# the charset of kingshard, if you don't set this item
# the default charset of kingshard is utf8.
#proxy_charset: gbk
proxy_charset: utf8mb4

# node is an agenda for real remote mysql server.
nodes :
-
name : node1

# default max conns for mysql server
max_conns_limit : 32
max_conns_limit : 128

# all mysql in a node must have the same user and password
user : kingshard
password : kingshard
user : root
password : quanshi

# master represents a real mysql master server
master : 127.0.0.1:3306

# slave represents a real mysql salve server,and the number after '@' is
# read load weight of this slave.
#slave : 192.168.59.101:3307@2,192.168.59.101:3307@3
down_after_noalive : 32
-
name : node2

# default max conns for mysql server
max_conns_limit : 32

# all mysql in a node must have the same user and password
user : kingshard
password : kingshard

# master represents a real mysql master server
master : 192.168.59.103:3307

# slave represents a real mysql salve server
slave :

# down mysql after N seconds noalive
# 0 will no down
down_after_noalive: 32
#down_after_noalive : 32

# schema defines sharding rules, the db is the sharding table database.
schema :
db : kingshard
nodes: [node1,node2]
db : statusnet
nodes: [node1]
default: node1
shard:
-
table: test_shard_hash
key: id
nodes: [node1, node2]
type: hash
locations: [4,4]

-
table: test_shard_range
table: user_group
key: id
type: range
nodes: [node1, node2]
locations: [4,4]
nodes: [node1]
locations: [4]
table_row_base: 2000
table_row_limit: 10000
-
table: test_shard_year
key: ctime
type: date_year
nodes: [node1,node2]
date_range: [2015-2016,2017-2018]
-
table: test_shard_month
key: dtime
type: date_month
nodes: [node1,node2]
date_range: [201603-201605,201609-201612]
-
table: test_shard_day
key: mtime
type: date_day
nodes: [node1,node2]
date_range: [20160306-20160307,20160308-20160309]
table: group_member
key: group_id
type: range
nodes: [node1]
locations: [4]
table_row_base: 2000
table_row_limit: 10000
2 changes: 2 additions & 0 deletions etc/unshard.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pidfile : /uc/etc/ucdbsql.pid

# server listen addr
addr : 0.0.0.0:9696

Expand Down
6 changes: 3 additions & 3 deletions proxy/router/numkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (kr NumKeyRange) String() string {
return fmt.Sprintf("{Start: %d, End: %d}", kr.Start, kr.End)
}

func ParseNumSharding(Locations []int, TableRowLimit int) ([]NumKeyRange, error) {
func ParseNumSharding(Locations []int, TableRowLimit int, TableRowBase int) ([]NumKeyRange, error) {
tableCount := 0
length := len(Locations)

Expand All @@ -57,8 +57,8 @@ func ParseNumSharding(Locations []int, TableRowLimit int) ([]NumKeyRange, error)

ranges := make([]NumKeyRange, tableCount)
for i := 0; i < tableCount; i++ {
ranges[i].Start = int64(i * TableRowLimit)
ranges[i].End = int64((i + 1) * TableRowLimit)
ranges[i].Start = int64(TableRowBase + i*TableRowLimit)
ranges[i].End = int64(TableRowBase + (i+1)*TableRowLimit)
}
return ranges, nil
}
Expand Down
32 changes: 30 additions & 2 deletions proxy/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func parseShard(r *Rule, cfg *config.ShardConfig) error {
case HashRuleType:
r.Shard = &HashShard{ShardNum: len(r.TableToNode)}
case RangeRuleType:
rs, err := ParseNumSharding(cfg.Locations, cfg.TableRowLimit)
rs, err := ParseNumSharding(cfg.Locations, cfg.TableRowLimit, cfg.TableRowBase)
if err != nil {
return err
}
Expand Down Expand Up @@ -490,6 +490,7 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
var prefix string
//rewrite select expr
for _, expr := range node.SelectExprs {

switch v := expr.(type) {
case *sqlparser.StarExpr:
//for shardTable.*,need replace table into shardTable_xxxx.
Expand Down Expand Up @@ -535,6 +536,7 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
buf.Fprintf("%s%v", prefix, n)
}
}

buf.Fprintf(" from ")
switch v := (node.From[0]).(type) {
case *sqlparser.AliasedTableExpr:
Expand Down Expand Up @@ -570,7 +572,32 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
tableIndex,
)
}
buf.Fprintf(" %s %v", v.Join, v.RightExpr)

if ate_r, ok_r := (v.RightExpr).(*sqlparser.AliasedTableExpr); ok_r {
if len(ate_r.As) != 0 {
fmt.Fprintf(buf, " %s %s_%04d as %s",
sqlparser.AST_LEFT_JOIN,
sqlparser.String(ate_r.Expr),
tableIndex,
string(ate_r.As),
)
} else {
fmt.Fprintf(buf, " %s %s_%04d",
sqlparser.AST_LEFT_JOIN,
sqlparser.String(ate_r.Expr),
tableIndex,
)
}
} else {
fmt.Fprintf(buf, " %s %s_%04d",
sqlparser.AST_LEFT_JOIN,
sqlparser.String(v.RightExpr),
tableIndex,
)
}

//buf.Fprintf(" %s %v", v.Join, v.RightExpr)
//golog.Info("router", "sqlparser.JoinTableExpr and v.Join, v.RightExpr", buf.String(), 0)
if v.On != nil {
buf.Fprintf(" on %v", v.On)
}
Expand All @@ -580,6 +607,7 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
tableIndex,
)
}

//append other tables
prefix = ", "
for i := 1; i < len(node.From); i++ {
Expand Down
4 changes: 2 additions & 2 deletions sqlparser/sql.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//line ./sqlparser/sql.y:20
//line ./sqlparser/sql.y:19
package sqlparser

import __yyfmt__ "fmt"

//line ./sqlparser/sql.y:20
//line ./sqlparser/sql.y:21
import "bytes"

func SetParseTree(yylex interface{}, stmt Statement) {
Expand Down
Loading