summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-06-25 11:47:24 +0300
committerValery Piashchynski <[email protected]>2021-06-25 11:47:24 +0300
commit09041fe7499be1929aaffaa113f93f6853564c09 (patch)
treea608df4250439cf9a1511f9e00ff7af6a033579d /plugins
parent64d41f6d87bb8002e700cdcddb5a3a241e4a2a7d (diff)
parente9249c7896331bab97a18a7ee0db17803fdd31fb (diff)
Merge remote-tracking branch 'origin/master' into feature/jobs_plugin
Diffstat (limited to 'plugins')
-rw-r--r--plugins/kv/drivers/boltdb/driver.go34
-rw-r--r--plugins/kv/drivers/memcached/driver.go10
-rw-r--r--plugins/kv/rpc.go16
-rw-r--r--plugins/memory/kv.go16
-rw-r--r--plugins/redis/kv.go9
5 files changed, 84 insertions, 1 deletions
diff --git a/plugins/kv/drivers/boltdb/driver.go b/plugins/kv/drivers/boltdb/driver.go
index e5aac290..0f737fbd 100644
--- a/plugins/kv/drivers/boltdb/driver.go
+++ b/plugins/kv/drivers/boltdb/driver.go
@@ -18,6 +18,7 @@ import (
)
type Driver struct {
+ clearMu sync.RWMutex
// db instance
DB *bolt.DB
// name should be UTF-8
@@ -373,6 +374,35 @@ func (d *Driver) TTL(keys ...string) (map[string]string, error) {
return m, nil
}
+func (d *Driver) Clear() error {
+ err := d.DB.Update(func(tx *bolt.Tx) error {
+ err := tx.DeleteBucket(d.bucket)
+ if err != nil {
+ d.log.Error("boltdb delete bucket", "error", err)
+ return err
+ }
+
+ _, err = tx.CreateBucket(d.bucket)
+ if err != nil {
+ d.log.Error("boltdb create bucket", "error", err)
+ return err
+ }
+
+ return nil
+ })
+
+ if err != nil {
+ d.log.Error("clear transaction failed", "error", err)
+ return err
+ }
+
+ d.clearMu.Lock()
+ d.gc = sync.Map{}
+ d.clearMu.Unlock()
+
+ return nil
+}
+
// ========================= PRIVATE =================================
func (d *Driver) startGCLoop() { //nolint:gocognit
@@ -382,6 +412,8 @@ func (d *Driver) startGCLoop() { //nolint:gocognit
for {
select {
case <-t.C:
+ d.clearMu.RLock()
+
// calculate current time before loop started to be fair
now := time.Now()
d.gc.Range(func(key, value interface{}) bool {
@@ -414,6 +446,8 @@ func (d *Driver) startGCLoop() { //nolint:gocognit
}
return true
})
+
+ d.clearMu.RUnlock()
case <-d.stop:
err := d.DB.Close()
if err != nil {
diff --git a/plugins/kv/drivers/memcached/driver.go b/plugins/kv/drivers/memcached/driver.go
index 520ec7d5..42e342ac 100644
--- a/plugins/kv/drivers/memcached/driver.go
+++ b/plugins/kv/drivers/memcached/driver.go
@@ -237,3 +237,13 @@ func (d *Driver) Delete(keys ...string) error {
}
return nil
}
+
+func (d *Driver) Clear() error {
+ err := d.client.DeleteAll()
+ if err != nil {
+ d.log.Error("flush_all operation failed", "error", err)
+ return err
+ }
+
+ return nil
+}
diff --git a/plugins/kv/rpc.go b/plugins/kv/rpc.go
index b9b302fe..ad4aefa9 100644
--- a/plugins/kv/rpc.go
+++ b/plugins/kv/rpc.go
@@ -162,3 +162,19 @@ func (r *rpc) Delete(in *kvv1.Request, _ *kvv1.Response) error {
return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}
+
+// Clear clean the storage
+func (r *rpc) Clear(in *kvv1.Request, _ *kvv1.Response) error {
+ const op = errors.Op("rcp_delete")
+
+ if st, exists := r.storages[in.GetStorage()]; exists {
+ err := st.Clear()
+ if err != nil {
+ return errors.E(op, err)
+ }
+
+ return nil
+ }
+
+ return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
+}
diff --git a/plugins/memory/kv.go b/plugins/memory/kv.go
index 1906e4fd..3cec1f97 100644
--- a/plugins/memory/kv.go
+++ b/plugins/memory/kv.go
@@ -13,7 +13,8 @@ import (
)
type Driver struct {
- heap sync.Map
+ clearMu sync.RWMutex
+ heap sync.Map
// stop is used to stop keys GC and close boltdb connection
stop chan struct{}
log logger.Logger
@@ -203,6 +204,14 @@ func (s *Driver) Delete(keys ...string) error {
return nil
}
+func (s *Driver) Clear() error {
+ s.clearMu.Lock()
+ s.heap = sync.Map{}
+ s.clearMu.Unlock()
+
+ return nil
+}
+
// ================================== PRIVATE ======================================
func (s *Driver) gc() {
@@ -213,6 +222,9 @@ func (s *Driver) gc() {
ticker.Stop()
return
case now := <-ticker.C:
+ // mutes needed to clear the map
+ s.clearMu.RLock()
+
// check every second
s.heap.Range(func(key, value interface{}) bool {
v := value.(*kvv1.Item)
@@ -231,6 +243,8 @@ func (s *Driver) gc() {
}
return true
})
+
+ s.clearMu.RUnlock()
}
}
}
diff --git a/plugins/redis/kv.go b/plugins/redis/kv.go
index 378d7630..5bf03af1 100644
--- a/plugins/redis/kv.go
+++ b/plugins/redis/kv.go
@@ -240,3 +240,12 @@ func (d *Driver) TTL(keys ...string) (map[string]string, error) {
}
return m, nil
}
+
+func (d *Driver) Clear() error {
+ fdb := d.universalClient.FlushDB(context.Background())
+ if fdb.Err() != nil {
+ return fdb.Err()
+ }
+
+ return nil
+}