Skip to content

Commit

Permalink
[ISSUE arana-db#125arana-db#126] support nacos and import config int…
Browse files Browse the repository at this point in the history
…o nacos、etcd (arana-db#130)

* refactor: 配置中心模块重构

* feat: 添加etcd & file的配置存储实现

* feat: feat issue arana-db#62

* feat: feat issue arana-db#62

* fix: fix pr comment

* fix: fix test fail

* style: fix import style

* fix: fix code style

* feat: 支持nacos

* feat: support use command to persist into config.store

* fix: 修复import逻辑问题

* fix: fix watch error

* fix: rollback modify docker/conf/bootstrap.yaml

* style: fix codestyle error

* style: fix liscense header

* style: fix style

* refactor: 优化bootstrap.yaml文件结构

* refactor: 调整结构

* fix: fix CI error

* style: 移除无效的导入

* style: fix License header

* style: fix pr issue

* refactor: 调整代码结构

Co-authored-by: springliao <[email protected]>
  • Loading branch information
chuntaojun and springliao authored Apr 18, 2022
1 parent 2a9fe4b commit d5e6b7f
Show file tree
Hide file tree
Showing 24 changed files with 1,299 additions and 264 deletions.
2 changes: 1 addition & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ header: # `header` section is configurations for source codes license header.
- '.travis.yml'
- '.gitignore'
- '.gitmodules'
- 'makefile'
- 'Makefile'
- 'justfile'
- 'docker'
- 'pkg/resolver/mysql/constants.go' # with two license: apache and Vitess
Expand Down
6 changes: 3 additions & 3 deletions makefile → Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ unit-test:
dist dist/arana-linux-amd64 dist/arana-darwin-amd64 dist/arana-linux-amd64-sha-256 dist/arana-darwin-amd64-sha-256:
rm -fr ./dist
mkdir -p ./dist
GOOS="linux" GOARCH="amd64" CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana-linux-amd64 ./cmd
GOOS="darwin" GOARCH="amd64" CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana-darwin-amd64 ./cmd
GOOS="linux" GOARCH="amd64" CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana-linux-amd64 ./cmd
GOOS="darwin" GOARCH="amd64" CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana-darwin-amd64 ./cmd
sha256sum ./dist/arana-darwin-amd64 | cut -d ' ' -f 1 > ./dist/arana-darwin-amd64-sha-256
sha256sum ./dist/arana-linux-amd64 | cut -d ' ' -f 1 > ./dist/arana-linux-amd64-sha-256

# Generate binaries for a Cortex release
build dist/arana dist/arana-sha-256:
rm -fr ./dist
mkdir -p ./dist
CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana ./cmd
CGO_ENABLED=0 go build $(GO_FLAGS) -o ./dist/arana ./cmd
sha256sum ./dist/arana | cut -d ' ' -f 1 > ./dist/arana-sha-256

docker-build: build
Expand Down
95 changes: 16 additions & 79 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,22 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"
)

import (
"github.com/pkg/errors"

"github.com/spf13/cobra"
)

import (
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/constants"
"github.com/arana-db/arana/pkg/executor"
"github.com/arana-db/arana/pkg/filters"
"github.com/arana-db/arana/pkg/mysql"
"github.com/arana-db/arana/pkg/server"
"github.com/arana-db/arana/pkg/util/log"
)

var (
Version = "0.1.0"

configPath string
bootstrapConfigPath string
importBootConfPath string
)

var (
Expand All @@ -52,79 +42,26 @@ var (
Short: "arana is a db proxy server",
Version: Version,
}

startCommand = &cobra.Command{
Use: "start",
Short: "start arana",

Run: func(cmd *cobra.Command, args []string) {
provider := boot.NewProvider(configPath)
if err := boot.Boot(context.Background(), provider); err != nil {
log.Fatal("start failed: %v", err)
return
}

filters, err := provider.ListFilters(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
}

for _, filterConf := range filters {
factory := filter.GetFilterFactory(filterConf.Name)
if factory == nil {
panic(errors.Errorf("there is no filter factory for filter: %s", filterConf.Name))
}
f, err := factory.NewFilter(filterConf.Config)
if err != nil {
panic(errors.WithMessagef(err, "failed to create filter: %s", filterConf.Name))
}
filter.RegisterFilter(f.GetName(), f)
}

propeller := server.NewServer()

listenersConf, err := provider.ListListeners(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
}

for _, listenerConf := range listenersConf {
listener, err := mysql.NewListener(listenerConf)
if err != nil {
log.Fatalf("create listener failed: %v", err)
return
}
executor := executor.NewRedirectExecutor()
listener.SetExecutor(executor)
propeller.AddListener(listener)
}
propeller.Start()

ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
select {
case <-ctx.Done():
return
}
},
}
)

// init Init startCmd
func init() {
startCommand.PersistentFlags().StringVarP(&configPath, constants.ConfigPathKey, "c", os.Getenv(constants.EnvAranaConfig), "Load configuration from `FILE`")
startCommand.
PersistentFlags().
StringVarP(&bootstrapConfigPath, constants.ConfigPathKey, "c", os.Getenv(constants.EnvAranaConfig), "bootstrap configuration file path")

confImportCommand.
PersistentFlags().
StringVarP(&importBootConfPath, constants.ConfigPathKey, "c", os.Getenv(constants.EnvAranaConfig), "bootstrap configuration file path")
confImportCommand.
PersistentFlags().
StringVarP(&sourceConfigPath, constants.ImportConfigPathKey, "s", "", "import configuration file path")

rootCommand.AddCommand(startCommand)
rootCommand.AddCommand(confImportCommand)
}

func main() {
// Execute Execute command line analysis
func Execute() {
rootCommand.Execute()
}
22 changes: 22 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 main

func main() {
Execute()
}
107 changes: 107 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 main

import (
"context"
"os"
"os/signal"
"syscall"
)

import (
"github.com/pkg/errors"

"github.com/spf13/cobra"
)

import (
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/executor"
filter "github.com/arana-db/arana/pkg/filters"
"github.com/arana-db/arana/pkg/mysql"
"github.com/arana-db/arana/pkg/server"
"github.com/arana-db/arana/pkg/util/log"
)

var (
startCommand = &cobra.Command{
Use: "start",
Short: "start arana",
Example: "arana start -c bootstrap.yaml",
Run: func(cmd *cobra.Command, args []string) {
provider := boot.NewProvider(bootstrapConfigPath)
if err := boot.Boot(context.Background(), provider); err != nil {
log.Fatal("start failed: %v", err)
return
}

filters, err := provider.ListFilters(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
}

for _, filterConf := range filters {
factory := filter.GetFilterFactory(filterConf.Name)
if factory == nil {
panic(errors.Errorf("there is no filter factory for filter: %s", filterConf.Name))
}
f, err := factory.NewFilter(filterConf.Config)
if err != nil {
panic(errors.WithMessagef(err, "failed to create filter: %s", filterConf.Name))
}
filter.RegisterFilter(f.GetName(), f)
}

propeller := server.NewServer()

listenersConf, err := provider.ListListeners(context.Background())
if err != nil {
log.Fatal("start failed: %v", err)
return
}

for _, listenerConf := range listenersConf {
listener, err := mysql.NewListener(listenerConf)
if err != nil {
log.Fatalf("create listener failed: %v", err)
return
}
executor := executor.NewRedirectExecutor()
listener.SetExecutor(executor)
propeller.AddListener(listener)
}
propeller.Start()

ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
select {
case <-ctx.Done():
return
}
},
}
)
64 changes: 64 additions & 0 deletions cmd/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 main

import (
"context"
)

import (
"github.com/spf13/cobra"
)

import (
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/config"
"github.com/arana-db/arana/pkg/util/log"
)

var (
sourceConfigPath string
)

var (
confImportCommand = &cobra.Command{
Use: "import",
Short: "import arana config",
Example: "./arana import -c ../docker/conf/bootstrap.yaml -s ../docker/conf/config.yaml",
Run: func(*cobra.Command, []string) {
provider := boot.NewProvider(importBootConfPath)
if err := provider.Init(context.Background()); err != nil {
log.Fatal("init failed: %+v", err)
return
}

cfg, err := config.LoadV2(sourceConfigPath)
if err != nil {
log.Fatal("load config from %s failed: %+v", sourceConfigPath, err)
return
}

c := provider.GetConfigCenter()

if err := c.ImportConfiguration(cfg); err != nil {
log.Fatal("persist config to config.store failed: %+v", err)
return
}
},
}
)
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ WORKDIR app

COPY dist/ .

CMD ["./arana", "start", "-c", "config.yaml"]
RUN chmod +x ./arana

CMD ["./arana", "start", "-c", "bootstrap.yaml"]
Loading

0 comments on commit d5e6b7f

Please sign in to comment.