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

add feature: multi user #427

Merged
merged 3 commits into from
May 13, 2018
Merged
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
4 changes: 3 additions & 1 deletion backend/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ type Node struct {
SlaveWeights []int

DownAfterNoAlive time.Duration

Online bool
}

func (n *Node) CheckNode() {
//to do
//1 check connection alive
for {
for n.Online {
n.checkMaster()
n.checkSlave()
time.Sleep(16 * time.Second)
Expand Down
9 changes: 9 additions & 0 deletions cmd/kingshard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func main() {
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGPIPE,
syscall.SIGUSR1,
)

go func() {
Expand All @@ -131,6 +132,14 @@ func main() {
svr.Close()
} else if sig == syscall.SIGPIPE {
golog.Info("main", "main", "Ignore broken pipe signal", 0)
} else if sig == syscall.SIGUSR1 {
golog.Info("main", "main", "Got update config signal", 0)
newCfg, err := config.ParseConfigFile(*configFile)
if err != nil {
golog.Error("main", "main", fmt.Sprint("parse config file error:%v", err.Error()), 0)
} else {
svr.UpdateConfig(newCfg)
}
}
}
}()
Expand Down
13 changes: 10 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var configFileName string
//整个config文件对应的结构
type Config struct {
Addr string `yaml:"addr"`
User string `yaml:"user"`
Password string `yaml:"password"`

UserList []UserConfig `yaml:"user_list"`

WebAddr string `yaml:"web_addr"`
WebUser string `yaml:"web_user"`
Expand All @@ -42,7 +42,13 @@ type Config struct {
Charset string `yaml:"proxy_charset"`
Nodes []NodeConfig `yaml:"nodes"`

Schema SchemaConfig `yaml:"schema"`
SchemaList []SchemaConfig `yaml:"schema_list"`
}

//user_list对应的配置
type UserConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
}

//node节点对应的配置
Expand All @@ -60,6 +66,7 @@ type NodeConfig struct {

//schema对应的结构体
type SchemaConfig struct {
User string `yaml:"user"`
Nodes []string `yaml:"nodes"`
Default string `yaml:"default"` //default node
ShardRule []ShardConfig `yaml:"shard"` //route rule
Expand Down
2 changes: 2 additions & 0 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ var (
ErrBlackSqlNotExist = errors.New("black sql has not exist")
ErrInsertTooComplex = errors.New("insert is too complex")
ErrSQLNULL = errors.New("sql is null")

ErrInternalServer = errors.New("internal server error")
)
17 changes: 9 additions & 8 deletions doc/KingDoc/admin_command_introduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mysql> admin server(opt,k,v) values('show','proxy','config');
| Key | Value |
+--------------+----------------+
| Addr | 127.0.0.1:9696 |
| User | kingshard |
| User_List | root,kingshard |
| LogPath | ./ |
| LogLevel | debug |
| LogSql | on |
Expand Down Expand Up @@ -66,13 +66,14 @@ mysql> admin server(opt,k,v) values('show','node','config');
#查看schema配置

mysql> admin server(opt,k,v) values('show','schema','config');
+-----------+------------------+---------+------+--------------+-----------+---------------+
| DB | Table | Type | Key | Nodes_List | Locations | TableRowLimit |
+-----------+------------------+---------+------+--------------+-----------+---------------+
| kingshard | | default | | node1 | | 0 |
| kingshard | test_shard_hash | hash | id | node1, node2 | 4, 4 | 0 |
| kingshard | test_shard_range | range | id | node1, node2 | 4, 4 | 10000 |
+-----------+------------------+---------+------+--------------+-----------+---------------+
+-----------+-----------+------------------+---------+------+--------------+-----------+---------------+
| User | DB | Table | Type | Key | Nodes_List | Locations | TableRowLimit |
+-----------+-----------+------------------+---------+------+--------------+-----------+---------------+
| kingshard | kingshard | | default | | node1 | | 0 |
| kingshard | kingshard | test_shard_hash | hash | id | node1, node2 | 4, 4 | 0 |
| kingshard | kingshard | test_shard_range | range | id | node1, node2 | 4, 4 | 10000 |
| root | kingshard | | default | | node1 | | 0 |
+-----------+-----------+------------------+---------+------+--------------+-----------+---------------+

3 rows in set (0.00 sec)

Expand Down
15 changes: 10 additions & 5 deletions doc/KingDoc/how_to_use_kingshard.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
# kingshard的地址和端口
addr : 0.0.0.0:9696
# 连接kingshard的用户名和密码
user : kingshard
password : kingshard
# 连接kingshard的用户名和密码的用户列表
-user_list:
-
user : kingshard
password : kingshard
#kingshard的web API 端口
web_addr : 0.0.0.0:9797
#调用API的用户名和密码
Expand Down Expand Up @@ -79,8 +81,11 @@ nodes :
slave :
down_after_noalive: 100
# 分表规则
schema :
# 各用户的分表规则
schema_list :
-
#schema的所属用户名
user: kingshard
#分表分布的node名字
nodes: [node1,node2]
#所有未分表的SQL,都会发往默认node。
Expand Down
9 changes: 7 additions & 2 deletions doc/KingDoc/how_to_use_kingshard_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ web_addr : 0.0.0.0:9797
web_user : admin
web_password : admin

# server user and password
# user list with user name and password
user_list:
-
user : kingshard
password : kingshard

Expand Down Expand Up @@ -87,8 +89,11 @@ nodes :
# 0 will no down
down_after_noalive: 32

# schema list include all user's schema
# schema defines which db can be used by client and this db's sql will be executed in which nodes
schema :
schema_list :
-
user: kingshard
nodes: [node1,node2]
default: node1
shard:
Expand Down
20 changes: 20 additions & 0 deletions doc/KingDoc/kingshard_admin_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ curl -X GET \
返回结果:
[
{
"user": "kingshard",
"db": "kingshard",
"Table": "",
"Key": "",
Expand All @@ -212,6 +213,7 @@ curl -X GET \
"DateRange": null
},
{
"user": "kingshard",
"db": "kingshard",
"Table": "test_shard_hash",
"Key": "id",
Expand All @@ -228,6 +230,7 @@ curl -X GET \
"DateRange": null
},
{
"user": "kingshard",
"db": "kingshard",
"Table": "test_shard_range",
"Key": "id",
Expand All @@ -244,6 +247,7 @@ curl -X GET \
"DateRange": null
},
{
"user": "kingshard",
"db": "kingshard",
"Table": "test_shard_time",
"Key": "id",
Expand All @@ -260,6 +264,7 @@ curl -X GET \
"DateRange": null
},
{
"user": "kingshard",
"db": "kingshard",
"Table": "test_shard_month",
"Key": "dtime",
Expand All @@ -276,6 +281,7 @@ curl -X GET \
]
},
{
"user": "kingshard",
"db": "kingshard",
"Table": "test_shard_day",
"Key": "mtime",
Expand All @@ -290,6 +296,20 @@ curl -X GET \
"20160306-20160307",
"20160308-20160309"
]
},
{
"user": "root",
"db": "kingshard",
"Table": "",
"Key": "",
"Nodes": [
"node1",
"node2"
],
"Locations": null,
"Type": "default",
"TableRowLimit": 0,
"DateRange": null
}
]
```
Expand Down
12 changes: 9 additions & 3 deletions doc/KingDoc/kingshard_date_sharding.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ kingshard的配置文件如下所示:
# server listen addr
addr : 0.0.0.0:9696

# server user and password
# user list with user name and password
user_list:
-
user : kingshard
password : kingshard

# the web api server
web_addr : 0.0.0.0:9797
#HTTP Basic Auth
Expand Down Expand Up @@ -98,8 +101,11 @@ nodes :
# 0 will no down
down_after_noalive: 32

# schema defines sharding rules, the db is the sharding table database.
schema :
# schema list include all user's schema
# schema defines which db can be used by client and this db's sql will be executed in which nodes
schema_list :
-
user: kingshard
nodes: [node1,node2]
default: node1
shard:
Expand Down
16 changes: 11 additions & 5 deletions doc/KingDoc/kingshard_install_document.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
# kingshard的地址和端口
addr : 0.0.0.0:9696
# 连接kingshard的用户名和密码
user : kingshard
password : kingshard
# 连接kingshard的用户名和密码的用户列表
user_list:
-
user : kingshard
password : kingshard
#kingshard的web API 端口
web_addr : 0.0.0.0:9797
#调用API的用户名和密码
Expand Down Expand Up @@ -70,8 +72,12 @@ nodes :
slave :
down_after_noalive: 100
# 分表规则
schema :
# 各用户的分表规则
schema_list :
-
#schema的所属用户名
user: kingshard
nodes: [node1,node2]
#分表分布的node名字
nodes: [node1,node2]
#所有未分表的SQL,都会发往默认node。
Expand Down
22 changes: 18 additions & 4 deletions etc/ks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
addr : 0.0.0.0:9696

# server user and password
user : kingshard
password : kingshard
user_list:
-
user : root
password : root
-
user : kingshard
password : kingshard

# the web api server
web_addr : 0.0.0.0:9797
Expand Down Expand Up @@ -75,7 +80,16 @@ nodes :
down_after_noalive: 32

# schema defines sharding rules, the db is the sharding table database.
schema :
schema_list :
-
user: root
nodes: [node1,node2]
default: node1
shard:
-

-
user: kingshard
nodes: [node1,node2]
default: node1
shard:
Expand Down Expand Up @@ -122,4 +136,4 @@ schema :
key: mtime
type: date_day
nodes: [node1,node2]
date_range: [20160306-20160307,20160308-20160309]
date_range: [20160306-20160307,20160308-20160309]
20 changes: 17 additions & 3 deletions etc/unshard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
addr : 0.0.0.0:9696

# server user and password
user : kingshard
password : kingshard
user_list:
-
user : root
password : root
-
user : kingshard
password : kingshard

# the web api server
web_addr : 0.0.0.0:9797
Expand Down Expand Up @@ -57,7 +62,16 @@ nodes :

# schema defines which db can be used by client and this db's sql will be executed in which nodes,
# the db is also the default database
schema :
schema_list :
-
user: root
nodes: [node1]
default: node1
shard:
-

-
user: kingshard
nodes: [node1]
default: node1
shard:
Expand Down
Loading