Skip to content

Commit

Permalink
feat: implement simple hint backbone (arana-db#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii authored Jul 9, 2022
1 parent 6807233 commit 38e879a
Show file tree
Hide file tree
Showing 64 changed files with 2,313 additions and 1,297 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ docker-build:
integration-test:
@go clean -testcache
go test -tags integration -v ./test/...
go test -tags integration-all -v ./integration_test/...
go test -tags integration-db_tbl -v ./integration_test/scene/db_tbl/...
go test -tags integration-db -v ./integration_test/scene/db/...
go test -tags integration-tbl -v ./integration_test/scene/tbl/...

clean:
@rm -rf coverage.txt
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ when using `arana`, user doesn't need to care about the `sharding` details of da

[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/arana-db/arana/blob/master/LICENSE)
[![codecov](https://codecov.io/gh/arana-db/arana/branch/master/graph/badge.svg)](https://codecov.io/gh/arana-db/arana)
[![Go Report Card](https://goreportcard.com/badge/github.com/arana-db/arana)](https://goreportcard.com/report/github.com/arana-db/arana)
[![Release](https://img.shields.io/github/v/release/arana-db/arana)](https://img.shields.io/github/v/release/arana-db/arana)
[![Docker Pulls](https://img.shields.io/docker/pulls/aranadb/arana)](https://img.shields.io/docker/pulls/aranadb/arana)

Expand All @@ -21,14 +22,14 @@ when using `arana`, user doesn't need to care about the `sharding` details of da

## Introduction | [中文](https://github.com/arana-db/arana/blob/master/README_CN.md)

First, `Arana` is a Cloud Native Database Proxy. It provides transparent data access capabilities, when using `arana`,
First, `Arana` is a Cloud Native Database Proxy. It provides transparent data access capabilities, when using `arana`,
user doesn't need to care about the `sharding` details of database, they can use it just like a single `MySQL` database.
`Arana` also provide abilities of `Multi Tenant`, `Distributed transaction`, `Shadow database`, `SQL Audit`, `Data encrypt / decrypt`
`Arana` also provide abilities of `Multi Tenant`, `Distributed transaction`, `Shadow database`, `SQL Audit`, `Data encrypt / decrypt`
and so on. Through simple config, user can use these abilities provided by `arana` directly.

Second, `Arana` can also be deployed as a Database mesh sidecar. As a Database mesh sidecar, arana switches data access from
client mode to proxy mode, which greatly optimizes the startup speed of applications. It provides the ability to manage database
traffic, it takes up very little container resources, doesn't affect the performance of application services in the container, but
Second, `Arana` can also be deployed as a Database mesh sidecar. As a Database mesh sidecar, arana switches data access from
client mode to proxy mode, which greatly optimizes the startup speed of applications. It provides the ability to manage database
traffic, it takes up very little container resources, doesn't affect the performance of application services in the container, but
provides all the capabilities of proxy.

## Architecture
Expand Down Expand Up @@ -74,7 +75,7 @@ arana start -c ${configFilePath}

## Contact

Arana Chinese Community Meeting Time: **Every Saturday At 9:00PM GMT+8**
Arana Chinese Community Meeting Time: **Every Saturday At 9:00PM GMT+8**

<img src="https://raw.githubusercontent.com/arana-db/arana/master/docs/pics/dingtalk-group.jpeg" width="300px"/>

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/arana-db/arana
go 1.18

require (
github.com/arana-db/parser v0.2.2
github.com/arana-db/parser v0.2.3
github.com/bwmarrin/snowflake v0.3.0
github.com/cespare/xxhash/v2 v2.1.2
github.com/dop251/goja v0.0.0-20220422102209-3faab1d8f20e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/aliyun/alibaba-cloud-sdk-go v1.61.18/go.mod h1:v8ESoHo4SyHmuB4b1tJqDH
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/arana-db/parser v0.2.2 h1:0ndNzQn6Q82s04V2Lp5+cBXSgO592srsISLjhxZ7vQE=
github.com/arana-db/parser v0.2.2/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/arana-db/parser v0.2.3 h1:zLZcx0/oidlHnw/GZYE78NuvwQkHUv2Xtrm2IwyZasA=
github.com/arana-db/parser v0.2.3/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down
3 changes: 1 addition & 2 deletions pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/arana-db/arana/pkg/proto/rule"
"github.com/arana-db/arana/pkg/runtime"
"github.com/arana-db/arana/pkg/runtime/namespace"
"github.com/arana-db/arana/pkg/runtime/optimize"
"github.com/arana-db/arana/pkg/security"
"github.com/arana-db/arana/pkg/util/log"
)
Expand Down Expand Up @@ -129,5 +128,5 @@ func buildNamespace(ctx context.Context, provider Discovery, cluster string) (*n
}
initCmds = append(initCmds, namespace.UpdateRule(&ru))

return namespace.New(cluster, optimize.GetOptimizer(), initCmds...), nil
return namespace.New(cluster, initCmds...), nil
}
9 changes: 6 additions & 3 deletions pkg/dataset/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ func TestParallelDataset_SortBy(t *testing.T) {
}

var (
d = generateFakeParallelDataset(ctrl, pairs...)
pojo fakePojo
ids sort.IntSlice
d = generateFakeParallelDataset(ctrl, pairs...)

ids sort.IntSlice
)

for {
Expand All @@ -214,6 +214,7 @@ func TestParallelDataset_SortBy(t *testing.T) {
return
}

var pojo fakePojo
err = scanPojo(r, &pojo)
assert.NoError(t, err)

Expand All @@ -235,6 +236,8 @@ func TestParallelDataset_SortBy(t *testing.T) {
err := d.SetNextN(bingo)
assert.NoError(t, err)

var pojo fakePojo

r, err := d.Next()
assert.NoError(t, err)
err = scanPojo(r, &pojo)
Expand Down
14 changes: 13 additions & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/arana-db/arana/pkg/metrics"
mysqlErrors "github.com/arana-db/arana/pkg/mysql/errors"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/proto/hint"
"github.com/arana-db/arana/pkg/resultx"
"github.com/arana-db/arana/pkg/runtime"
rcontext "github.com/arana-db/arana/pkg/runtime/context"
Expand Down Expand Up @@ -153,10 +154,20 @@ func (executor *RedirectExecutor) ExecutorComQuery(ctx *proto.Context) (proto.Re
p := parser.New()
query := ctx.GetQuery()
start := time.Now()
act, err := p.ParseOneStmt(query, "", "")
act, hts, err := p.ParseOneStmtHints(query, "", "")
if err != nil {
return nil, 0, errors.WithStack(err)
}

var hints []*hint.Hint
for _, next := range hts {
var h *hint.Hint
if h, err = hint.Parse(next); err != nil {
return nil, 0, err
}
hints = append(hints, h)
}

metrics.ParserDuration.Observe(time.Since(start).Seconds())
log.Debugf("ComQuery: %s", query)

Expand All @@ -172,6 +183,7 @@ func (executor *RedirectExecutor) ExecutorComQuery(ctx *proto.Context) (proto.Re
}

ctx.Stmt = &proto.Stmt{
Hints: hints,
StmtNode: act,
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/mysql/execute_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/arana-db/arana/pkg/constants/mysql"
"github.com/arana-db/arana/pkg/mysql/errors"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/proto/hint"
"github.com/arana-db/arana/pkg/security"
"github.com/arana-db/arana/pkg/util/log"
)
Expand Down Expand Up @@ -263,14 +264,26 @@ func (l *Listener) handlePrepare(c *Conn, ctx *proto.Context) error {
PrepareStmt: query,
}
p := parser.New()
act, err := p.ParseOneStmt(stmt.PrepareStmt, "", "")
act, hts, err := p.ParseOneStmtHints(stmt.PrepareStmt, "", "")
if err != nil {
log.Errorf("Conn %v: Error parsing prepared statement: %v", c, err)
if wErr := c.writeErrorPacketFromError(err); wErr != nil {
log.Errorf("Conn %v: Error writing prepared statement error: %v", c, wErr)
return wErr
}
}

for _, it := range hts {
var h *hint.Hint
if h, err = hint.Parse(it); err != nil {
if wErr := c.writeErrorPacketFromError(err); wErr != nil {
log.Errorf("Conn %v: Error writing prepared statement error: %v", c, wErr)
return wErr
}
}
stmt.Hints = append(stmt.Hints, h)
}

stmt.StmtNode = act

paramsCount := uint16(strings.Count(query, "?"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/mysql/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (bi BinaryRow) Scan(dest []proto.Value) error {
case 1, 2, 3, 4, 5, 6:
dstlen = 8 + 1 + decimals
default:
return errors.Errorf("protocol error, illegal decimals architecture.Value %d", field.decimals)
return errors.Errorf("protocol error, illegal decimals architecture.V %d", field.decimals)
}
val, err = formatBinaryTime(bi.raw[pos:pos+int(num)], dstlen)
dest[i] = val
Expand All @@ -238,7 +238,7 @@ func (bi BinaryRow) Scan(dest []proto.Value) error {
case 1, 2, 3, 4, 5, 6:
dstlen = 19 + 1 + decimals
default:
return errors.Errorf("protocol error, illegal decimals architecture.Value %d", field.decimals)
return errors.Errorf("protocol error, illegal decimals architecture.V %d", field.decimals)
}
}
val, err = formatBinaryDateTime(bi.raw[pos:pos+int(num)], dstlen)
Expand Down
8 changes: 4 additions & 4 deletions pkg/mysql/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ func convertAssignRows(dest, src interface{}) error {
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
return fmt.Errorf("converting driver.V type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetInt(i64)
return nil
Expand All @@ -1096,7 +1096,7 @@ func convertAssignRows(dest, src interface{}) error {
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
return fmt.Errorf("converting driver.V type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetUint(u64)
return nil
Expand All @@ -1108,7 +1108,7 @@ func convertAssignRows(dest, src interface{}) error {
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
return fmt.Errorf("converting driver.V type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetFloat(f64)
return nil
Expand All @@ -1126,7 +1126,7 @@ func convertAssignRows(dest, src interface{}) error {
}
}

return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
return fmt.Errorf("unsupported Scan, storing driver.V type %T into type %T", src, dest)
}

func cloneBytes(b []byte) []byte {
Expand Down
Loading

0 comments on commit 38e879a

Please sign in to comment.