Skip to content

Commit

Permalink
Add the support of using Apache Zookeeper as an alternative storage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jihuayu authored Dec 2, 2023
1 parent b8882c5 commit d77ea0f
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 5 deletions.
11 changes: 7 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"

"github.com/apache/kvrocks-controller/storage/persistence/etcd"
"github.com/apache/kvrocks-controller/storage/persistence/zookeeper"
"github.com/go-playground/validator/v10"
)

Expand All @@ -48,10 +49,12 @@ type ControllerConfig struct {
const defaultPort = 9379

type Config struct {
Addr string `yaml:"addr"`
Etcd *etcd.Config `yaml:"etcd"`
Admin AdminConfig `yaml:"admin"`
Controller *ControllerConfig `yaml:"controller"`
Addr string `yaml:"addr"`
StorageType string `yaml:"storage_type"`
Etcd *etcd.Config `yaml:"etcd"`
Zookeeper *zookeeper.Config `yaml:"zookeeper"`
Admin AdminConfig `yaml:"admin"`
Controller *ControllerConfig `yaml:"controller"`
}

func Default() *Config {
Expand Down
14 changes: 14 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@

addr: "127.0.0.1:9379"


# Which storage engine should be used by controller
# options: etcd, zookeeper
# default: etcd
storage_type: etcd

etcd:
addrs:
- "127.0.0.1:2379"
username:
password:
elect_path:
tls:
enable: false
cert_file:
key_file:
ca_file:

zookeeper:
addrs:
- "127.0.0.1:2181"
scheme:
auth:
elect_path:

controller:
failover:
gc_interval_seconds: 3600
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-playground/validator/v10 v10.9.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-resty/resty/v2 v2.7.0
github.com/go-zookeeper/zk v1.0.3
github.com/google/uuid v1.3.0
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/prometheus/client_golang v1.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-zookeeper/zk v1.0.3 h1:7M2kwOsc//9VeeFiPtf+uSJlVpU66x9Ba5+8XK7/TDg=
github.com/go-zookeeper/zk v1.0.3/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
6 changes: 6 additions & 0 deletions scripts/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ services:
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd0=http://etcd0:2380
- ETCD_INITIAL_CLUSTER_STATE=new
zookeeper0:
image: "zookeeper:latest"
container_name: zookeeper0
ports:
- "2181:2181"

21 changes: 20 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import (
"fmt"
"net/http"
"net/http/pprof"
"strings"
"time"

"github.com/apache/kvrocks-controller/config"
"github.com/apache/kvrocks-controller/controller"
"github.com/apache/kvrocks-controller/controller/probe"
"github.com/apache/kvrocks-controller/logger"
"github.com/apache/kvrocks-controller/storage"
"github.com/apache/kvrocks-controller/storage/persistence"
"github.com/apache/kvrocks-controller/storage/persistence/etcd"
"github.com/apache/kvrocks-controller/storage/persistence/zookeeper"
"github.com/gin-gonic/gin"
)

Expand All @@ -44,11 +48,26 @@ type Server struct {
}

func NewServer(cfg *config.Config) (*Server, error) {
var persist persistence.Persistence
var err error
switch {
case strings.EqualFold(cfg.StorageType, "etcd"):
logger.Get().Info("Use Etcd as storage")
persist, err = etcd.New(cfg.Addr, cfg.Etcd)
case strings.EqualFold(cfg.StorageType, "zookeeper"):
logger.Get().Info("Use Zookeeper as storage")
persist, err = zookeeper.New(cfg.Addr, cfg.Zookeeper)
default:
logger.Get().Info("Use Etcd as default storage")
persist, err = etcd.New(cfg.Addr, cfg.Etcd)
}

persist, err := etcd.New(cfg.Addr, cfg.Etcd)
if err != nil {
return nil, err
}
if persist == nil {
return nil, fmt.Errorf("no found any storage config")
}
storage, err := storage.NewStorage(persist)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit d77ea0f

Please sign in to comment.