diff options
-rw-r--r-- | .github/workflows/build.yml | 22 | ||||
-rwxr-xr-x | .rr.yaml | 3 | ||||
-rwxr-xr-x | Makefile | 12 | ||||
-rw-r--r-- | cmd/cli/root.go | 1 | ||||
-rw-r--r-- | cmd/main.go | 9 | ||||
-rw-r--r-- | plugins/kv/memory/config.go | 3 | ||||
-rw-r--r-- | plugins/kv/memory/plugin.go | 23 | ||||
-rw-r--r-- | plugins/kv/memory/storage_test.go | 17 | ||||
-rw-r--r-- | tests/plugins/kv/memcached/plugin_test.go | 10 | ||||
-rw-r--r-- | tests/plugins/kv/memory/configs/.rr-init.yaml | 5 |
10 files changed, 73 insertions, 32 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4c57125d..9a234ee5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ on: # this job on pull_request events for branches that look like fork # branches. Without this we would end up running this job twice for non # forked PRs, once for the push and then once for opening the PR. - - '**:**' + - "**:**" jobs: golang: @@ -18,9 +18,9 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.4', '8.0' ] - go: [ '1.14', '1.15' ] - os: [ ubuntu-20.04, windows-latest, macos-10.15 ] + php: ["7.4", "8.0"] + go: ["1.14", "1.15"] + os: [ubuntu-20.04, windows-latest, macos-10.15] steps: - name: Set up Go ${{ matrix.go }} uses: actions/setup-go@v2 # action page: <https://github.com/actions/setup-go> @@ -85,6 +85,13 @@ jobs: go test -v -race -cover -tags=debug ./tests/plugins/resetter go test -v -race -cover -tags=debug ./tests/plugins/rpc go test -v -race -cover -tags=debug ./tests/plugins/static + go test -v -race -cover -tags=debug ./plugins/kv/boltdb + go test -v -race -cover -tags=debug ./plugins/kv/memory + go test -v -race -cover -tags=debug ./plugins/kv/memcached + go test -v -race -cover -tags=debug ./tests/plugins/kv/boltdb + go test -v -race -cover -tags=debug ./tests/plugins/kv/memory + go test -v -race -cover -tags=debug ./tests/plugins/kv/memcached + docker-compose -f ./tests/docker-compose.yaml down - name: Run golang tests on Linux and MacOS if: ${{ matrix.os != 'windows-latest' }} @@ -110,6 +117,13 @@ jobs: go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/resetter.txt -covermode=atomic ./tests/plugins/resetter go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/rpc.txt -covermode=atomic ./tests/plugins/rpc go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/static.txt -covermode=atomic ./tests/plugins/static + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/boltdb_unit.txt -covermode=atomic ./plugins/kv/boltdb + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/kv_unit.txt -covermode=atomic ./plugins/kv/memory + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/memcached_unit.txt -covermode=atomic ./plugins/kv/memcached + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/boltdb.txt -covermode=atomic ./tests/plugins/kv/boltdb + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/memory.txt -covermode=atomic ./tests/plugins/kv/memory + go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/memcached.txt -covermode=atomic ./tests/plugins/kv/memcached + docker-compose -f ./tests/docker-compose.yaml down cat ./coverage-ci/*.txt > ./coverage-ci/summary.txt - uses: codecov/codecov-action@v1 # Docs: <https://github.com/codecov/codecov-action> @@ -88,6 +88,7 @@ boltdb: file: "rr" bucket: "test" permissions: 0777 + # keys ttl check interval TTL: 60 # seconds # memcached driver @@ -98,6 +99,8 @@ memcached: # in memory KV driver memory: enabled: true + # keys ttl check interval + interval: 60 metrics: # prometheus client address (path /metrics added automatically) @@ -24,8 +24,8 @@ uninstall: ## Uninstall locally installed RR rm -f /usr/local/bin/rr test: ## Run application tests - #go clean -testcache - docker-compose -f docker-compose.yaml up -d + go clean -testcache + docker-compose -f tests/docker-compose.yaml up -d go test -v -race -cover -tags=debug -covermode=atomic ./utils go test -v -race -cover -tags=debug -covermode=atomic ./pkg/pipe go test -v -race -cover -tags=debug -covermode=atomic ./pkg/pool @@ -47,7 +47,11 @@ test: ## Run application tests go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/static go test -v -race -cover -tags=debug -covermode=atomic ./plugins/kv/boltdb go test -v -race -cover -tags=debug -covermode=atomic ./plugins/kv/memory - docker-compose down + go test -v -race -cover -tags=debug -covermode=atomic ./plugins/kv/memcached + go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/boltdb + go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/memory + go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/memcached + docker-compose -f tests/docker-compose.yaml down lint: ## Run application linters golangci-lint run @@ -59,4 +63,4 @@ kv: go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/boltdb go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/memory go test -v -race -cover -tags=debug -covermode=atomic ./tests/plugins/kv/memcached - docker-compose down + docker-compose -f tests/docker-compose.yaml down diff --git a/cmd/cli/root.go b/cmd/cli/root.go index 10e389f2..06a84a82 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -46,7 +46,6 @@ func init() { cobra.OnInitialize(func() { if CfgFile != "" { if absPath, err := filepath.Abs(CfgFile); err == nil { - println(absPath) CfgFile = absPath // force working absPath related to config file diff --git a/cmd/main.go b/cmd/main.go index f5d4e0ad..0f8c1bd9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,6 +7,9 @@ import ( "github.com/spiral/roadrunner/v2/cmd/cli" "github.com/spiral/roadrunner/v2/plugins/http" "github.com/spiral/roadrunner/v2/plugins/informer" + //"github.com/spiral/roadrunner/v2/plugins/kv/boltdb" + "github.com/spiral/roadrunner/v2/plugins/kv/memcached" + "github.com/spiral/roadrunner/v2/plugins/kv/memory" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" "github.com/spiral/roadrunner/v2/plugins/redis" @@ -42,6 +45,12 @@ func main() { &rpc.Plugin{}, // server plugin (NewWorker, NewWorkerPool) &server.Plugin{}, + // boltdb kv plugin + // &boltdb.Plugin{}, + // memcached kv plugin + &memcached.Plugin{}, + // in-memory kv plugin + &memory.Plugin{}, ) if err != nil { log.Fatal(err) diff --git a/plugins/kv/memory/config.go b/plugins/kv/memory/config.go index 329e7fff..0816f734 100644 --- a/plugins/kv/memory/config.go +++ b/plugins/kv/memory/config.go @@ -4,9 +4,12 @@ package memory type Config struct { // Enabled or disabled (true or false) Enabled bool + // Interval for the check + Interval int } // InitDefaults by default driver is turned off func (c *Config) InitDefaults() { c.Enabled = false + c.Interval = 60 // seconds } diff --git a/plugins/kv/memory/plugin.go b/plugins/kv/memory/plugin.go index 2c65f14c..d2d3721b 100644 --- a/plugins/kv/memory/plugin.go +++ b/plugins/kv/memory/plugin.go @@ -11,27 +11,18 @@ import ( "github.com/spiral/roadrunner/v2/plugins/logger" ) +// PluginName is user friendly name for the plugin const PluginName = "memory" type Plugin struct { - heap *sync.Map + // heap is user map for the key-value pairs + heap sync.Map stop chan struct{} log logger.Logger cfg *Config } -func NewInMemoryStorage() kv.Storage { - p := &Plugin{ - heap: &sync.Map{}, - stop: make(chan struct{}), - } - - go p.gc() - - return p -} - func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error { const op = errors.Op("in-memory storage init") s.cfg = &Config{} @@ -42,8 +33,7 @@ func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error { return errors.E(op, err) } s.log = log - // init in-memory - s.heap = &sync.Map{} + s.stop = make(chan struct{}, 1) return nil } @@ -119,7 +109,7 @@ func (s *Plugin) MGet(keys ...string) (map[string]interface{}, error) { for i := range keys { if value, ok := s.heap.Load(keys[i]); ok { - m[keys[i]] = value + m[keys[i]] = value.(kv.Item).Value } } @@ -242,7 +232,7 @@ func (s *Plugin) Name() string { func (s *Plugin) gc() { // TODO check - ticker := time.NewTicker(time.Millisecond * 500) + ticker := time.NewTicker(time.Duration(s.cfg.Interval) * time.Second) for { select { case <-s.stop: @@ -262,6 +252,7 @@ func (s *Plugin) gc() { } if now.After(t) { + s.log.Debug("key deleted", "key", key) s.heap.Delete(key) } return true diff --git a/plugins/kv/memory/storage_test.go b/plugins/kv/memory/storage_test.go index 4b30460d..d3b24860 100644 --- a/plugins/kv/memory/storage_test.go +++ b/plugins/kv/memory/storage_test.go @@ -7,11 +7,26 @@ import ( "time" "github.com/spiral/roadrunner/v2/plugins/kv" + "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/stretchr/testify/assert" + "go.uber.org/zap" ) func initStorage() kv.Storage { - return NewInMemoryStorage() + p := &Plugin{ + stop: make(chan struct{}), + } + p.cfg = &Config{ + Enabled: true, + Interval: 1, + } + + l, _ := zap.NewDevelopment() + p.log = logger.NewZapAdapter(l) + + go p.gc() + + return p } func cleanup(t *testing.T, s kv.Storage, keys ...string) { diff --git a/tests/plugins/kv/memcached/plugin_test.go b/tests/plugins/kv/memcached/plugin_test.go index d30de3b6..6eff8715 100644 --- a/tests/plugins/kv/memcached/plugin_test.go +++ b/tests/plugins/kv/memcached/plugin_test.go @@ -149,8 +149,8 @@ func testRPCMethods(t *testing.T) { err = client.Call("memcached.MGet", keys, &mGet) assert.NoError(t, err) assert.Len(t, mGet, 2) // c is expired - assert.Equal(t, string("aa"), mGet["a"].(string)) - assert.Equal(t, string("bb"), mGet["b"].(string)) + assert.Equal(t, string("aa"), string(mGet["a"].([]byte))) + assert.Equal(t, string("bb"), string(mGet["b"].([]byte))) mExpKeys := make([]kv.Item, 0, 2) tt2 := time.Now().Add(time.Second * 10).Format(time.RFC3339) @@ -164,12 +164,12 @@ func testRPCMethods(t *testing.T) { assert.NoError(t, err) assert.True(t, mExpRes) - // TTL + // TTL call is not supported for the memcached driver keys = []string{"a", "b", "d"} ttlRes := make(map[string]interface{}) err = client.Call("memcached.TTL", keys, &ttlRes) - assert.NoError(t, err) - assert.Len(t, ttlRes, 3) + assert.Error(t, err) + assert.Len(t, ttlRes, 0) // HAS AFTER TTL time.Sleep(time.Second * 11) diff --git a/tests/plugins/kv/memory/configs/.rr-init.yaml b/tests/plugins/kv/memory/configs/.rr-init.yaml index 3dceea95..dedc6cd2 100644 --- a/tests/plugins/kv/memory/configs/.rr-init.yaml +++ b/tests/plugins/kv/memory/configs/.rr-init.yaml @@ -39,4 +39,7 @@ http: # in memory KV driver memory: - enabled: true
\ No newline at end of file + enabled: + true + # keys ttl check interval + interval: 1 |