Skip to content

Commit

Permalink
feat: use etcd in docker compose by default (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii authored Aug 30, 2022
1 parent bec907d commit 897d35c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 10 deletions.
38 changes: 34 additions & 4 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
import (
"github.com/arana-db/arana/cmd/cmds"
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/config"
"github.com/arana-db/arana/pkg/constants"
"github.com/arana-db/arana/pkg/executor"
"github.com/arana-db/arana/pkg/mysql"
Expand All @@ -51,6 +52,11 @@ _____________________________________________
`

const (
_keyBootstrap = "config"
_keyImport = "import"
)

func init() {
cmd := &cobra.Command{
Use: "start",
Expand All @@ -59,18 +65,37 @@ func init() {
Run: run,
}
cmd.PersistentFlags().
StringP(constants.ConfigPathKey, "c", os.Getenv(constants.EnvBootstrapPath), "bootstrap configuration file path")
StringP(_keyBootstrap, "c", os.Getenv(constants.EnvBootstrapPath), "bootstrap configuration file path")
cmd.PersistentFlags().
String(_keyImport, "", "import configuration yaml file path")

cmds.Handle(func(root *cobra.Command) {
root.AddCommand(cmd)
})
}

func Run(bootstrapConfigPath string) {
func Run(bootstrapConfigPath string, importPath string) {
// print slogan
fmt.Printf("\033[92m%s\033[0m\n", slogan) // 92m: light green

provider := boot.NewProvider(bootstrapConfigPath)
if err := provider.Init(context.Background()); err != nil {
log.Fatal("start failed: %v", err)
return
}

if len(importPath) > 0 {
c, err := config.Load(importPath)
if err != nil {
log.Fatal("failed to import configuration from %s: %v", importPath, err)
return
}
if err := provider.GetConfigCenter().ImportConfiguration(c); err != nil {
log.Fatal("failed to import configuration from %s: %v", importPath, err)
return
}
}

if err := boot.Boot(context.Background(), provider); err != nil {
log.Fatal("start failed: %v", err)
return
Expand Down Expand Up @@ -112,7 +137,12 @@ func Run(bootstrapConfigPath string) {

func run(cmd *cobra.Command, args []string) {
_ = args
bootstrapConfigPath, _ := cmd.PersistentFlags().GetString(constants.ConfigPathKey)

var (
bootstrapConfigPath, _ = cmd.PersistentFlags().GetString(_keyBootstrap)
importPath, _ = cmd.PersistentFlags().GetString(_keyImport)
)

if len(bootstrapConfigPath) < 1 {
// search bootstrap yaml
for _, path := range constants.GetConfigSearchPathList() {
Expand All @@ -127,5 +157,5 @@ func run(cmd *cobra.Command, args []string) {
}
}

Run(bootstrapConfigPath)
Run(bootstrapConfigPath, importPath)
}
21 changes: 21 additions & 0 deletions conf/bootstrap.docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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.
#

config:
name: etcd
options:
endpoints: "http://etcd:2379"
23 changes: 20 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@

version: "3"
services:
etcd:
image: docker.io/bitnami/etcd:3.5
container_name: arana-etcd
networks:
- local
environment:
- ALLOW_NONE_AUTHENTICATION=yes
volumes:
- etcd_data:/bitnami/etcd
ports:
- "2379:2379"
- "2380:2380"

mysql:
image: mysql:5.7
container_name: arana-mysql
Expand All @@ -31,9 +44,9 @@ services:
- ./scripts/sequence.sql:/docker-entrypoint-initdb.d/1.sql:ro
- ./scripts/sharding.sql:/docker-entrypoint-initdb.d/2.sql:ro
- mysql_data:/var/lib/mysql
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
command: [ "mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci" ]
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "127.0.0.1"]
test: [ "CMD", "mysqladmin" ,"ping", "-h", "127.0.0.1" ]
interval: 2s
timeout: 1s
retries: 5
Expand All @@ -42,19 +55,23 @@ services:
build: .
container_name: arana
image: aranadb/arana:master
entrypoint: [ "arana", "start", "-c", "/etc/arana/bootstrap.yaml", "--import", "/etc/arana/config.yaml" ]
networks:
- local
ports:
- "3307:13306"
volumes:
- ./conf/config.yaml:/etc/arana/config.yaml:ro
- ./conf/bootstrap.yaml:/etc/arana/bootstrap.yaml:ro
- ./conf/bootstrap.docker.yaml:/etc/arana/bootstrap.yaml:ro
depends_on:
- arana-admin
- mysql
- etcd

networks:
local:
external: false

volumes:
mysql_data:
etcd_data:
2 changes: 1 addition & 1 deletion example/local_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import (

func main() {
bootstrap := testdata.Path("../conf/bootstrap.yaml")
start.Run(bootstrap)
start.Run(bootstrap, "")
}
9 changes: 8 additions & 1 deletion pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
import (
"github.com/pkg/errors"

uatomic "go.uber.org/atomic"

"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -75,7 +77,7 @@ type Cluster struct {
}

type Discovery interface {
// Init init discovery with context
// Init initializes discovery with context
Init(ctx context.Context) error
// ListTenants list tenants name
ListTenants(ctx context.Context) ([]string, error)
Expand Down Expand Up @@ -109,12 +111,17 @@ type Discovery interface {
}

type discovery struct {
inited uatomic.Bool
path string
options *BootOptions
c *config.Center
}

func (fp *discovery) Init(ctx context.Context) error {
if !fp.inited.CAS(false, true) {
return nil
}

if err := fp.loadBootOptions(); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (ms *MySuite) SetupSuite() {
}
go func() {
_ = os.Setenv(constants.EnvConfigPath, ms.tmpFile)
start.Run(testdata.Path("../conf/bootstrap.yaml"))
start.Run(testdata.Path("../conf/bootstrap.yaml"), "")
}()

// waiting for arana server started
Expand Down

0 comments on commit 897d35c

Please sign in to comment.