diff options
author | Valery Piashchynski <[email protected]> | 2021-06-24 17:40:49 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-24 17:40:49 +0300 |
commit | e9249c7896331bab97a18a7ee0db17803fdd31fb (patch) | |
tree | 99512001f757eb88614acb9b20dada3200008a5d /plugins/kv | |
parent | ce53a8e149b76f15e8a5dd88ac3b953798d57e8b (diff) | |
parent | 60001dbe15b5ff0fec32239ad18b3d308a4150b5 (diff) |
#736 feat(kv): `clear` RPC method which completely cleans storagev2.3.1-beta.6
#736 feat(kv): `clear` RPC method which completely cleans storage
Diffstat (limited to 'plugins/kv')
-rw-r--r-- | plugins/kv/drivers/boltdb/driver.go | 34 | ||||
-rw-r--r-- | plugins/kv/drivers/memcached/driver.go | 10 | ||||
-rw-r--r-- | plugins/kv/interface.go | 5 | ||||
-rw-r--r-- | plugins/kv/rpc.go | 16 |
4 files changed, 64 insertions, 1 deletions
diff --git a/plugins/kv/drivers/boltdb/driver.go b/plugins/kv/drivers/boltdb/driver.go index 4b675271..47d37cc2 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 a2787d72..14e7c078 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/interface.go b/plugins/kv/interface.go index ffdbbe62..5736a6a7 100644 --- a/plugins/kv/interface.go +++ b/plugins/kv/interface.go @@ -22,9 +22,12 @@ type Storage interface { MExpire(items ...*kvv1.Item) error // TTL return the rest time to live for provided keys - // Not supported for the memcached and boltdb + // Not supported for the memcached TTL(keys ...string) (map[string]string, error) + // Clear clean the entire storage + Clear() error + // Delete one or multiple keys. Delete(keys ...string) error } diff --git a/plugins/kv/rpc.go b/plugins/kv/rpc.go index af763600..3f7ba97c 100644 --- a/plugins/kv/rpc.go +++ b/plugins/kv/rpc.go @@ -161,3 +161,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())) +} |