This is an automated email from the ASF dual-hosted git repository.
torwig pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks-controller.git
The following commit(s) were added to refs/heads/unstable by this push:
new 997d1f3 chore(engine): Extract mocked Engine to a separate file (#205)
997d1f3 is described below
commit 997d1f3930dddef4e8f8ac5d47fa844288daba44
Author: Yaroslav <[email protected]>
AuthorDate: Tue Sep 24 07:03:57 2024 +0300
chore(engine): Extract mocked Engine to a separate file (#205)
* Exctract mocked engine to a separate file
* Increase the Go version in the README->Requirements
---
README.md | 2 +-
store/engine/engine.go | 95 --------------------------
store/engine/{engine.go => engine_inmemory.go} | 34 ++-------
store/engine/etcd/etcd.go | 3 +-
4 files changed, 10 insertions(+), 124 deletions(-)
diff --git a/README.md b/README.md
index 0d23fba..4f10ccc 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ Apache Kvrocks Controller is a cluster management tool for
[Apache Kvrocks](http
### Requirements
-* Go >= 1.16
+* Go >= 1.19
### Build binaries
diff --git a/store/engine/engine.go b/store/engine/engine.go
index 7a264af..9b95594 100644
--- a/store/engine/engine.go
+++ b/store/engine/engine.go
@@ -21,10 +21,6 @@ package engine
import (
"context"
- "strings"
- "sync"
-
- "github.com/apache/kvrocks-controller/consts"
)
type Entry struct {
@@ -46,94 +42,3 @@ type Engine interface {
Close() error
}
-
-type Mock struct {
- mu sync.Mutex
- values map[string]string
-}
-
-func NewMock() *Mock {
- return &Mock{
- values: make(map[string]string),
- }
-}
-
-func (m *Mock) Get(ctx context.Context, key string) ([]byte, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- v, ok := m.values[key]
- if !ok {
- return nil, consts.ErrNotFound
- }
- return []byte(v), nil
-}
-
-func (m *Mock) Exists(ctx context.Context, key string) (bool, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- _, ok := m.values[key]
- return ok, nil
-}
-
-func (m *Mock) Set(ctx context.Context, key string, value []byte) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- m.values[key] = string(value)
- return nil
-}
-
-func (m *Mock) Delete(ctx context.Context, key string) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- delete(m.values, key)
- return nil
-}
-
-func (m *Mock) List(ctx context.Context, prefix string) ([]Entry, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
-
- exists := make(map[string]bool, 0)
- var entries []Entry
- for k, v := range m.values {
- if strings.HasPrefix(k, prefix) {
- k = strings.Trim(strings.TrimPrefix(k, prefix), "/")
- fields := strings.SplitN(k, "/", 2)
- if len(fields) == 2 {
- // only list the first level
- k = fields[0]
- }
- if _, ok := exists[k]; ok {
- continue
- }
- exists[k] = true
- entries = append(entries, Entry{
- Key: k,
- Value: []byte(v),
- })
- }
- }
- return entries, nil
-}
-
-func (m *Mock) Close() error {
- return nil
-}
-
-func (m *Mock) ID() string {
- return "mock_store_engine"
-}
-
-func (m *Mock) Leader() string {
- return "mock_store_engine"
-}
-
-func (m *Mock) LeaderChange() <-chan bool {
- return make(chan bool)
-}
-
-func (m *Mock) IsReady(ctx context.Context) bool {
- return true
-}
-
-var _ Engine = (*Mock)(nil)
diff --git a/store/engine/engine.go b/store/engine/engine_inmemory.go
similarity index 71%
copy from store/engine/engine.go
copy to store/engine/engine_inmemory.go
index 7a264af..00c17dd 100644
--- a/store/engine/engine.go
+++ b/store/engine/engine_inmemory.go
@@ -27,25 +27,7 @@ import (
"github.com/apache/kvrocks-controller/consts"
)
-type Entry struct {
- Key string
- Value []byte
-}
-
-type Engine interface {
- ID() string
- Leader() string
- LeaderChange() <-chan bool
- IsReady(ctx context.Context) bool
-
- Get(ctx context.Context, key string) ([]byte, error)
- Exists(ctx context.Context, key string) (bool, error)
- Set(ctx context.Context, key string, value []byte) error
- Delete(ctx context.Context, key string) error
- List(ctx context.Context, prefix string) ([]Entry, error)
-
- Close() error
-}
+var _ Engine = (*Mock)(nil)
type Mock struct {
mu sync.Mutex
@@ -58,7 +40,7 @@ func NewMock() *Mock {
}
}
-func (m *Mock) Get(ctx context.Context, key string) ([]byte, error) {
+func (m *Mock) Get(_ context.Context, key string) ([]byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.values[key]
@@ -68,28 +50,28 @@ func (m *Mock) Get(ctx context.Context, key string)
([]byte, error) {
return []byte(v), nil
}
-func (m *Mock) Exists(ctx context.Context, key string) (bool, error) {
+func (m *Mock) Exists(_ context.Context, key string) (bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
_, ok := m.values[key]
return ok, nil
}
-func (m *Mock) Set(ctx context.Context, key string, value []byte) error {
+func (m *Mock) Set(_ context.Context, key string, value []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
m.values[key] = string(value)
return nil
}
-func (m *Mock) Delete(ctx context.Context, key string) error {
+func (m *Mock) Delete(_ context.Context, key string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.values, key)
return nil
}
-func (m *Mock) List(ctx context.Context, prefix string) ([]Entry, error) {
+func (m *Mock) List(_ context.Context, prefix string) ([]Entry, error) {
m.mu.Lock()
defer m.mu.Unlock()
@@ -132,8 +114,6 @@ func (m *Mock) LeaderChange() <-chan bool {
return make(chan bool)
}
-func (m *Mock) IsReady(ctx context.Context) bool {
+func (m *Mock) IsReady(_ context.Context) bool {
return true
}
-
-var _ Engine = (*Mock)(nil)
diff --git a/store/engine/etcd/etcd.go b/store/engine/etcd/etcd.go
index 4920cfa..372ccf8 100644
--- a/store/engine/etcd/etcd.go
+++ b/store/engine/etcd/etcd.go
@@ -22,11 +22,12 @@ package etcd
import (
"context"
"errors"
- "github.com/apache/kvrocks-controller/consts"
"strings"
"sync"
"time"
+ "github.com/apache/kvrocks-controller/consts"
+
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
"go.etcd.io/etcd/pkg/transport"