forked from arana-db/arana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate config v2 and implement simple tx backbone (arana-db#90)
* feat: integrate config v2 and implement simple tx backbone * add ut
- Loading branch information
Showing
38 changed files
with
2,663 additions
and
1,387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,61 @@ | ||
listeners: | ||
- protocol_type: mysql | ||
socket_address: | ||
address: 0.0.0.0 | ||
port: 13306 | ||
config: | ||
users: | ||
dksl: "123456" | ||
kind: ConfigMap | ||
apiVersion: "1.0" | ||
metadata: | ||
name: arana-config | ||
data: | ||
listeners: | ||
- protocol_type: mysql | ||
server_version: 5.7.0 | ||
executor: redirect | ||
socket_address: | ||
address: 0.0.0.0 | ||
port: 13306 | ||
|
||
tenants: | ||
- name: arana | ||
users: | ||
- username: arana | ||
password: "123456" | ||
- username: dksl | ||
password: "123456" | ||
|
||
executors: | ||
- name: redirect | ||
mode: readwritesplitting | ||
data_sources: | ||
- master: | ||
name: employees | ||
slaves: | ||
- name: employees1 | ||
weight: 50 | ||
- name: employees2 | ||
weight: 50 | ||
clusters: | ||
- name: employees | ||
type: mysql | ||
sql_max_limit: -1 | ||
tenant: arana | ||
conn_props: | ||
capacity: 10 | ||
max_capacity: 20 | ||
idle_timeout: 60 | ||
groups: | ||
- name: employees_0000 | ||
nodes: | ||
- name: arana-node-1 | ||
host: arana-mysql | ||
port: 3306 | ||
username: root | ||
password: "123456" | ||
database: employees | ||
weight: r10w10 | ||
labels: | ||
zone: shanghai | ||
conn_props: | ||
readTimeout: "1s" | ||
writeTimeout: "1s" | ||
parseTime: true | ||
loc: Local | ||
charset: utf8mb4,utf8 | ||
|
||
data_source_cluster: | ||
- role: master | ||
type: mysql | ||
name: employees | ||
capacity: 10 | ||
max_capacity: 20 | ||
idle_timeout: 60s | ||
conf: | ||
dsn: root:123456@tcp(arana-mysql:3306)/employees?timeout=11s&readTimeout=11s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8 | ||
- role: slave | ||
type: mysql | ||
name: employees1 | ||
capacity: 10 | ||
max_capacity: 20 | ||
idle_timeout: 60s | ||
conf: | ||
dsn: root:123456@tcp(arana-mysql:3306)/employees?timeout=11s&readTimeout=1s&writeTimeout=11s&parseTime=true&loc=Local&charset=utf8mb4,utf8 | ||
- role: slave | ||
type: mysql | ||
name: employees2 | ||
capacity: 10 | ||
max_capacity: 20 | ||
idle_timeout: 60s | ||
conf: | ||
dsn: root:123456@tcp(arana-mysql:3306)/employees?timeout=11s&readTimeout=1s&writeTimeout=11s&parseTime=true&loc=Local&charset=utf8mb4,utf8 | ||
sharding_rule: | ||
tables: | ||
- name: employees.student | ||
allow_full_scan: true | ||
db_rules: | ||
tbl_rules: | ||
- column: uid | ||
expr: modShard(32) | ||
topology: | ||
db_pattern: employees_0000 | ||
tbl_Pattern: student_${0000...0031} | ||
attributes: | ||
sqlMaxLimit: -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Licensed to 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. Apache Software Foundation (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 boot | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/arana-db/arana/pkg/config" | ||
"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" | ||
) | ||
|
||
func Boot(ctx context.Context, provider Discovery) error { | ||
if err := provider.Init(ctx); err != nil { | ||
return err | ||
} | ||
|
||
clusters, err := provider.ListClusters(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, cluster := range clusters { | ||
var ( | ||
c *Cluster | ||
ns *namespace.Namespace | ||
) | ||
|
||
if c, err = provider.GetCluster(ctx, cluster); err != nil { | ||
continue | ||
} | ||
if ns, err = buildNamespace(ctx, provider, cluster); err != nil { | ||
log.Errorf("build namespace %s failed: %v", cluster, err) | ||
continue | ||
} | ||
if err = namespace.Register(ns); err != nil { | ||
log.Errorf("register namespace %s failed: %v", cluster, err) | ||
continue | ||
} | ||
log.Infof("register namespace %s successfully", cluster) | ||
security.DefaultTenantManager().PutCluster(c.Tenant, cluster) | ||
} | ||
|
||
var tenants []string | ||
if tenants, err = provider.ListTenants(ctx); err != nil { | ||
return errors.Wrap(err, "no tenants found") | ||
} | ||
|
||
for _, tenant := range tenants { | ||
var t *config.Tenant | ||
if t, err = provider.GetTenant(ctx, tenant); err != nil { | ||
log.Errorf("failed to get tenant %s: %v", tenant, err) | ||
continue | ||
} | ||
for _, it := range t.Users { | ||
security.DefaultTenantManager().PutUser(tenant, it) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildNamespace(ctx context.Context, provider Discovery, cluster string) (*namespace.Namespace, error) { | ||
var ( | ||
groups []string | ||
err error | ||
) | ||
|
||
if groups, err = provider.ListGroups(ctx, cluster); err != nil { | ||
return nil, err | ||
} | ||
|
||
var initCmds []namespace.Command | ||
for _, group := range groups { | ||
var nodes []string | ||
if nodes, err = provider.ListNodes(ctx, cluster, group); err != nil { | ||
return nil, err | ||
} | ||
for _, it := range nodes { | ||
var node *config.Node | ||
if node, err = provider.GetNode(ctx, cluster, group, it); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
initCmds = append(initCmds, namespace.UpsertDB(group, runtime.NewAtomDB(node))) | ||
} | ||
} | ||
|
||
var tables []string | ||
if tables, err = provider.ListTables(ctx, cluster); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
var ru rule.Rule | ||
for _, table := range tables { | ||
var vt *rule.VTable | ||
if vt, err = provider.GetTable(ctx, cluster, table); err != nil { | ||
return nil, err | ||
} | ||
if vt == nil { | ||
log.Warnf("no such table %s", table) | ||
continue | ||
} | ||
ru.SetVTable(table, vt) | ||
} | ||
initCmds = append(initCmds, namespace.UpdateRule(&ru)) | ||
|
||
return namespace.New(cluster, optimize.GetOptimizer(), initCmds...), nil | ||
} |
Oops, something went wrong.