Skip to content

Commit

Permalink
Merge pull request #726 from zhyass/feature_set
Browse files Browse the repository at this point in the history
*: add config lower-case-table-names to support case insensitive tabl…
  • Loading branch information
BohuTANG authored Jan 28, 2021
2 parents 23eda68 + 1bdd2f4 commit 5ae0093
Show file tree
Hide file tree
Showing 21 changed files with 1,280 additions and 1,011 deletions.
21 changes: 11 additions & 10 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ This document describes the RadonDB REST API, which allows users to achieve most
Path: /v1/radon/config
Method: PUT
Request: {
"max-connections": The maximum permitted number of simultaneous client connections,
"max-result-size": The maximum result size(in bytes) of a query,
"max-join-rows": The maximum number of rows that will be held in memory for join's intermediate results,
"ddl-timeout": The execution timeout(in millisecond) for DDL statements,
"query-timeout": The execution timeout(in millisecond) for DML statements,
"twopc-enable": Enables(true or false) radon two phase commit, for distrubuted transaction,
"allowip": ["allow-ip-1", "allow-ip-2", "allow-ip-regexp"],
"audit-mode": The audit log mode, "N": disabled, "R": read enabled, "W": write enabled, "A": read/write enabled,
"blocks-readonly": The size of a block when create hash tables,
"load-balance": Enables(0 or 1) load balance, for read-write separation.
"max-connections": The maximum permitted number of simultaneous client connections,
"max-result-size": The maximum result size(in bytes) of a query,
"max-join-rows": The maximum number of rows that will be held in memory for join's intermediate results,
"ddl-timeout": The execution timeout(in millisecond) for DDL statements,
"query-timeout": The execution timeout(in millisecond) for DML statements,
"twopc-enable": Enables(true or false) radon two phase commit, for distrubuted transaction,
"allowip": ["allow-ip-1", "allow-ip-2", "allow-ip-regexp"],
"audit-mode": The audit log mode, "N": disabled, "R": read enabled, "W": write enabled, "A": read/write enabled,
"blocks-readonly": The size of a block when create hash tables,
"load-balance": Enables(0 or 1) load balance, for read-write separation,
"lower-case-table-names": If set 0, table names are stored as specified and comparisons are case-sensitive. If set 1, not case-sensitive.
}
```
Expand Down
9 changes: 9 additions & 0 deletions intergration/radon-test/r/select.result
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ sum(a) b
select c from integrate_test.t;
ERROR 1054 (42S22): Unknown column 'c' in 'field list'

select a from integrate_test.T;
ERROR 1146 (42S02): Table 'T' doesn't exist

select a from INTEGRATE_TEST.t;
ERROR 1146 (42S02): Table 'INTEGRATE_TEST.t' doesn't exist

select tt.a from integrate_test.t as TT;
ERROR 1105 (HY000): unsupported: unknown.column.'tt.a'.in.field.list


create table integrate_test.s(a int key, b int) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
3 changes: 3 additions & 0 deletions intergration/radon-test/t/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ select a, b c from integrate_test.t where a in (0,1,2) order by C desc;
select a, b from integrate_test.t where A>0;
select sum(a), b from integrate_test.t where a>=0 group by B;
select c from integrate_test.t;
select a from integrate_test.T;
select a from INTEGRATE_TEST.t;
select tt.a from integrate_test.t as TT;

create table integrate_test.s(a int key, b int) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into integrate_test.s(a, b) values(0,1), (2,2);
Expand Down
36 changes: 19 additions & 17 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ const (

// ProxyConfig tuple.
type ProxyConfig struct {
IPS []string `json:"allowip"`
MetaDir string `json:"meta-dir"`
Endpoint string `json:"endpoint"`
TwopcEnable bool `json:"twopc-enable"`
LoadBalance int `json:"load-balance"` // 0 -- disable balance, 1 -- enable balance to replica
IPS []string `json:"allowip"`
MetaDir string `json:"meta-dir"`
Endpoint string `json:"endpoint"`
TwopcEnable bool `json:"twopc-enable"`
LoadBalance int `json:"load-balance"` // 0 -- disable balance, 1 -- enable balance to replica
LowerCaseTableNames int `json:"lower-case-table-names"` // 0 -- case sensitive, 1 -- case insensitive

MaxConnections int `json:"max-connections"`
MaxResultSize int `json:"max-result-size"`
Expand All @@ -48,18 +49,19 @@ type ProxyConfig struct {
// DefaultProxyConfig returns default proxy config.
func DefaultProxyConfig() *ProxyConfig {
return &ProxyConfig{
MetaDir: "./radon-meta",
Endpoint: "127.0.0.1:3308",
LoadBalance: 0,
MaxConnections: 1024,
MaxResultSize: 1024 * 1024 * 1024, // 1GB
MaxJoinRows: 32768,
DDLTimeout: 10 * 3600 * 1000, // 10hours
QueryTimeout: 5 * 60 * 1000, // 5minutes
PeerAddress: "127.0.0.1:8080",
LongQueryTime: 5, // 5 seconds
StreamBufferSize: 1024 * 1024 * 32, // 32MB
IdleTxnTimeout: 60, // 60 seconds
MetaDir: "./radon-meta",
Endpoint: "127.0.0.1:3308",
LoadBalance: 0,
LowerCaseTableNames: 0,
MaxConnections: 1024,
MaxResultSize: 1024 * 1024 * 1024, // 1GB
MaxJoinRows: 32768,
DDLTimeout: 10 * 3600 * 1000, // 10hours
QueryTimeout: 5 * 60 * 1000, // 5minutes
PeerAddress: "127.0.0.1:8080",
LongQueryTime: 5, // 5 seconds
StreamBufferSize: 1024 * 1024 * 32, // 32MB
IdleTxnTimeout: 60, // 60 seconds
}
}

Expand Down
26 changes: 15 additions & 11 deletions src/ctl/v1/radon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ import (
)

type radonParams struct {
MaxConnections *int `json:"max-connections"`
MaxResultSize *int `json:"max-result-size"`
MaxJoinRows *int `json:"max-join-rows"`
DDLTimeout *int `json:"ddl-timeout"`
QueryTimeout *int `json:"query-timeout"`
TwoPCEnable *bool `json:"twopc-enable"`
LoadBalance *int `json:"load-balance"`
AllowIP []string `json:"allowip,omitempty"`
AuditMode *string `json:"audit-mode"`
StreamBufferSize *int `json:"stream-buffer-size"`
Blocks *int `json:"blocks-readonly"`
MaxConnections *int `json:"max-connections"`
MaxResultSize *int `json:"max-result-size"`
MaxJoinRows *int `json:"max-join-rows"`
DDLTimeout *int `json:"ddl-timeout"`
QueryTimeout *int `json:"query-timeout"`
TwoPCEnable *bool `json:"twopc-enable"`
LoadBalance *int `json:"load-balance"`
AllowIP []string `json:"allowip,omitempty"`
AuditMode *string `json:"audit-mode"`
StreamBufferSize *int `json:"stream-buffer-size"`
Blocks *int `json:"blocks-readonly"`
LowerCaseTableNames *int `json:"lower-case-table-names"`
}

// RadonConfigHandler impl.
Expand Down Expand Up @@ -80,6 +81,9 @@ func radonConfigHandler(log *xlog.Log, proxy *proxy.Proxy, w rest.ResponseWriter
if p.Blocks != nil {
proxy.SetBlocks(*p.Blocks)
}
if p.LowerCaseTableNames != nil {
proxy.SetLowerCaseTableNames(*p.LowerCaseTableNames)
}

// reset the allow ip table list.
proxy.IPTable().Refresh()
Expand Down
45 changes: 24 additions & 21 deletions src/ctl/v1/radon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,35 @@ func TestCtlV1RadonConfig(t *testing.T) {
handler := api.MakeHandler()

type radonParams1 struct {
MaxConnections int `json:"max-connections"`
MaxResultSize int `json:"max-result-size"`
MaxJoinRows int `json:"max-join-rows"`
DDLTimeout int `json:"ddl-timeout"`
QueryTimeout int `json:"query-timeout"`
TwoPCEnable bool `json:"twopc-enable"`
LoadBalance int `json:"load-balance"`
AllowIP []string `json:"allowip,omitempty"`
AuditMode string `json:"audit-mode"`
StreamBufferSize int `json:"stream-buffer-size"`
Blocks int `json:"blocks-readonly"`
MaxConnections int `json:"max-connections"`
MaxResultSize int `json:"max-result-size"`
MaxJoinRows int `json:"max-join-rows"`
DDLTimeout int `json:"ddl-timeout"`
QueryTimeout int `json:"query-timeout"`
TwoPCEnable bool `json:"twopc-enable"`
LoadBalance int `json:"load-balance"`
AllowIP []string `json:"allowip,omitempty"`
AuditMode string `json:"audit-mode"`
StreamBufferSize int `json:"stream-buffer-size"`
Blocks int `json:"blocks-readonly"`
LowerCaseTableNames int `json:"lower-case-table-names"`
}

// 200.
{
// client
p := &radonParams1{
MaxConnections: 1023,
MaxResultSize: 1073741823,
MaxJoinRows: 32767,
QueryTimeout: 33,
TwoPCEnable: true,
LoadBalance: 1,
AllowIP: []string{"127.0.0.1", "127.0.0.2"},
AuditMode: "A",
StreamBufferSize: 16777216,
Blocks: 128,
MaxConnections: 1023,
MaxResultSize: 1073741823,
MaxJoinRows: 32767,
QueryTimeout: 33,
TwoPCEnable: true,
LoadBalance: 1,
AllowIP: []string{"127.0.0.1", "127.0.0.2"},
AuditMode: "A",
StreamBufferSize: 16777216,
Blocks: 128,
LowerCaseTableNames: 1,
}
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("PUT", "http://localhost/v1/radon/config", p))
recorded.CodeIs(200)
Expand All @@ -79,6 +81,7 @@ func TestCtlV1RadonConfig(t *testing.T) {
assert.Equal(t, "A", radonConf.Audit.Mode)
assert.Equal(t, 16777216, radonConf.Proxy.StreamBufferSize)
assert.Equal(t, 128, radonConf.Router.Blocks)
assert.Equal(t, 1, radonConf.Proxy.LowerCaseTableNames)
}

// Unset AllowIP.
Expand Down
5 changes: 5 additions & 0 deletions src/proxy/initdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package proxy

import (
"fmt"
"strings"

"github.com/xelabs/go-mysqlstack/driver"
"github.com/xelabs/go-mysqlstack/sqldb"
Expand All @@ -25,6 +26,10 @@ func (spanner *Spanner) ComInitDB(session *driver.Session, database string) erro
return err
}

if spanner.isLowerCaseTableNames() {
database = strings.ToLower(database)
}

privilegePlug := spanner.plugins.PlugPrivilege()
isSet := privilegePlug.CheckUserPrivilegeIsSet(session.User())
if !isSet {
Expand Down
12 changes: 12 additions & 0 deletions src/proxy/initdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestProxyUseDatabase(t *testing.T) {
assert.Nil(t, err)
}

// lower case.
{
session := proxy.sessions.getSession(1).session
spanner := proxy.Spanner()
spanner.ComInitDB(session, "TEST")
assert.Equal(t, "TEST", session.Schema())

proxy.SetLowerCaseTableNames(1)
spanner.ComInitDB(session, "TEST")
assert.Equal(t, "test", session.Schema())
}

// use db.
{
_, err := driver.NewConn("mock", "mock", address, "test", "utf8")
Expand Down
8 changes: 8 additions & 0 deletions src/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,11 @@ func (p *Proxy) SetBlocks(blocks int) {
p.log.Info("proxy.SetBlocks:[%d->%d]", p.conf.Router.Blocks, blocks)
p.conf.Router.Blocks = blocks
}

// SetLowerCaseTableNames used to set LowerCaseTableNames to false or true.
func (p *Proxy) SetLowerCaseTableNames(lowerCase int) {
p.mu.Lock()
defer p.mu.Unlock()
p.log.Info("proxy.SetLowerCaseTableNames:[%v->%v]", p.conf.Proxy.LowerCaseTableNames, lowerCase)
p.conf.Proxy.LowerCaseTableNames = lowerCase
}
8 changes: 8 additions & 0 deletions src/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func TestProxy1(t *testing.T) {
assert.Equal(t, 0, proxy.conf.Proxy.LoadBalance)
}

// SetLowerCaseTableNames.
{
proxy.SetLowerCaseTableNames(1)
assert.Equal(t, 1, proxy.conf.Proxy.LowerCaseTableNames)
proxy.SetLowerCaseTableNames(0)
assert.Equal(t, 0, proxy.conf.Proxy.LowerCaseTableNames)
}

// FlushConfig.
{
err := proxy.FlushConfig()
Expand Down
5 changes: 5 additions & 0 deletions src/proxy/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (spanner *Spanner) ComQuery(session *driver.Session, query string, bindVari
return sqldb.NewSQLError(sqldb.ER_SYNTAX_ERROR, err.Error())
}
}

if spanner.isLowerCaseTableNames() {
node = sqlparser.LowerCaseTableNames(node).(sqlparser.Statement)
query = sqlparser.String(node)
}
log.Debug("query:%v", query)

// Readonly check.
Expand Down
Loading

0 comments on commit 5ae0093

Please sign in to comment.