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

feature: impl shadow match hint rule #363

Merged
merged 1 commit into from
Aug 19, 2022
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
6 changes: 3 additions & 3 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ data:
port: 3306
username: root
password: "123456"
database: employees_show
database: employees_shadow
weight: r10w10
sharding_rule:
tables:
Expand All @@ -120,7 +120,7 @@ data:

shadow_rule:
tables:
- name: student
- name: employees.student
enable: false
group_node: employees_shadow
match_rules:
Expand All @@ -137,4 +137,4 @@ data:
- operation: [select]
match_type: hint
attributes:
- shadow: true
- value: "shadow"
19 changes: 19 additions & 0 deletions integration_test/config/db_tbl/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ data:
password: "123456"
database: employees_0003
weight: r10w10
- name: employees_shadow
nodes:
- name: node_shadow
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_shadow
weight: r10w10

sharding_rule:
tables:
Expand All @@ -105,3 +114,13 @@ data:
tbl_pattern: student_${0000..0031}
attributes:
sqlMaxLimit: -1
shadow_rule:
tables:
- name: employees.student
enable: true
group_node: employees_shadow
match_rules:
- operation: [select,insert,update,delete]
match_type: hint
attributes:
- value: "shadow"
129 changes: 129 additions & 0 deletions integration_test/config/shadow/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kind: ConfigMap
apiVersion: "1.0"
metadata:
name: arana-config
data:
listeners:
- protocol_type: mysql
server_version: 5.7.0
socket_address:
address: 0.0.0.0
port: 13306

tenants:
- name: arana
users:
- username: root
password: "123456"
- username: arana
password: "123456"

clusters:
- name: employees
type: mysql
sql_max_limit: -1
tenant: arana
parameters:
max_allowed_packet: 256M
groups:
- name: employees_0000
nodes:
- name: node0
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0000
weight: r10w10
parameters:
- name: node0_r_0
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0000_r
weight: r0w0
- name: employees_0001
nodes:
- name: node1
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0001
weight: r10w10
- name: employees_0002
nodes:
- name: node2
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0002
weight: r10w10
- name: employees_0003
nodes:
- name: node3
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0003
weight: r10w10
- name: employees_shadow
nodes:
- name: node_shadow
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_shadow
weight: r10w10
sharding_rule:
tables:
- name: employees.student
allow_full_scan: true
sequence:
type: snowflake
option:
db_rules:
- column: uid
type: scriptExpr
expr: parseInt($value % 32 / 8)
tbl_rules:
- column: uid
type: scriptExpr
expr: $value % 32
step: 32
topology:
db_pattern: employees_${0000..0003}
tbl_pattern: student_${0000..0031}
attributes:
sqlMaxLimit: -1
shadow_rule:
tables:
- name: employees.student
enable: true
group_node: employees_shadow
match_rules:
- operation: [select]
match_type: hint
attributes:
- value: "shadow"
30 changes: 30 additions & 0 deletions integration_test/config/shadow/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kind: DataSet
metadata:
tables:
- name: "order"
columns:
- name: "name"
type: "string"
- name: "value"
type: "string"
data:
- name: "order"
value:
- ["test", "test1"]
30 changes: 30 additions & 0 deletions integration_test/config/shadow/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kind: excepted
metadata:
tables:
- name: "sequence"
columns:
- name: "name"
type: "string"
- name: "value"
type: "string"
data:
- name: "sequence"
value:
- ["1", "2"]
96 changes: 96 additions & 0 deletions integration_test/scene/shadow/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test

import (
"strings"
"testing"
)

import (
_ "github.com/go-sql-driver/mysql" // register mysql

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

import (
"github.com/arana-db/arana/test"
)

type IntegrationSuite struct {
*test.MySuite
}

func TestSuite(t *testing.T) {
su := test.NewMySuite(
test.WithMySQLServerAuth("root", "123456"),
test.WithMySQLDatabase("employees"),
test.WithConfig("../integration_test/config/shadow/config.yaml"),
test.WithScriptPath("../integration_test/scripts/shadow"),
test.WithTestCasePath("../../testcase/casetest.yaml"),
// WithDevMode(), // NOTICE: UNCOMMENT IF YOU WANT TO DEBUG LOCAL ARANA SERVER!!!
)
suite.Run(t, &IntegrationSuite{su})
}

func (s *IntegrationSuite) TestShadowScene() {
var (
db = s.DB()
t = s.T()
)
tx, err := db.Begin()
assert.NoError(t, err, "should begin a new tx")

cases := s.TestCases()
for _, sqlCase := range cases.ExecCases {
for _, sense := range sqlCase.Sense {
if strings.Compare(strings.TrimSpace(sense), "shadow") == 1 {
params := strings.Split(sqlCase.Parameters, ",")
args := make([]interface{}, 0, len(params))
for _, param := range params {
k, _ := test.GetValueByType(param)
args = append(args, k)
}

// Execute sql
result, err := tx.Exec(sqlCase.SQL, args...)
assert.NoError(t, err, "exec not right")
err = sqlCase.ExpectedResult.CompareRow(result)
assert.NoError(t, err, err)
}
}
}

for _, sqlCase := range cases.QueryRowCases {
for _, sense := range sqlCase.Sense {
if strings.Compare(strings.TrimSpace(sense), "shadow") == 1 {
params := strings.Split(sqlCase.Parameters, ",")
args := make([]interface{}, 0, len(params))
for _, param := range params {
k, _ := test.GetValueByType(param)
args = append(args, k)
}

result := tx.QueryRow(sqlCase.SQL, args...)
err = sqlCase.ExpectedResult.CompareRow(result)
assert.NoError(t, err, err)
}
}
}
}
Loading