From 897d35ce2fdc89d6d8ecb9e1c3534d1d4f25dde4 Mon Sep 17 00:00:00 2001 From: Jeffsky Date: Tue, 30 Aug 2022 14:11:30 +0800 Subject: [PATCH] feat: use etcd in docker compose by default (#382) --- cmd/start/start.go | 38 ++++++++++++++++++++++++++++++++---- conf/bootstrap.docker.yaml | 21 ++++++++++++++++++++ docker-compose.yaml | 23 +++++++++++++++++++--- example/local_server/main.go | 2 +- pkg/boot/discovery.go | 9 ++++++++- test/suite.go | 2 +- 6 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 conf/bootstrap.docker.yaml diff --git a/cmd/start/start.go b/cmd/start/start.go index dbdb0d17..2b4b0ad8 100644 --- a/cmd/start/start.go +++ b/cmd/start/start.go @@ -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" @@ -51,6 +52,11 @@ _____________________________________________ ` +const ( + _keyBootstrap = "config" + _keyImport = "import" +) + func init() { cmd := &cobra.Command{ Use: "start", @@ -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 @@ -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() { @@ -127,5 +157,5 @@ func run(cmd *cobra.Command, args []string) { } } - Run(bootstrapConfigPath) + Run(bootstrapConfigPath, importPath) } diff --git a/conf/bootstrap.docker.yaml b/conf/bootstrap.docker.yaml new file mode 100644 index 00000000..1ea636ff --- /dev/null +++ b/conf/bootstrap.docker.yaml @@ -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" diff --git a/docker-compose.yaml b/docker-compose.yaml index 7a1548fc..ab8a26c2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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 @@ -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 @@ -42,15 +55,18 @@ 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: @@ -58,3 +74,4 @@ networks: volumes: mysql_data: + etcd_data: diff --git a/example/local_server/main.go b/example/local_server/main.go index 1ebbd2c8..ef5a5316 100644 --- a/example/local_server/main.go +++ b/example/local_server/main.go @@ -24,5 +24,5 @@ import ( func main() { bootstrap := testdata.Path("../conf/bootstrap.yaml") - start.Run(bootstrap) + start.Run(bootstrap, "") } diff --git a/pkg/boot/discovery.go b/pkg/boot/discovery.go index 3fdd3f68..2b409978 100644 --- a/pkg/boot/discovery.go +++ b/pkg/boot/discovery.go @@ -32,6 +32,8 @@ import ( import ( "github.com/pkg/errors" + uatomic "go.uber.org/atomic" + "gopkg.in/yaml.v3" ) @@ -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) @@ -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 } diff --git a/test/suite.go b/test/suite.go index ed201f02..51e3f762 100644 --- a/test/suite.go +++ b/test/suite.go @@ -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