2021-04-19 11:13:52 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2020-12-07 15:22:20 +08:00
|
|
|
package etcdkv
|
2020-09-07 21:07:36 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"fmt"
|
2020-11-02 10:26:33 +08:00
|
|
|
"path"
|
|
|
|
"time"
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-03-11 14:15:18 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-11-03 14:53:36 +08:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
2020-09-07 21:07:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-16 21:10:43 +08:00
|
|
|
RequestTimeout = 10 * time.Second
|
2020-09-07 21:07:36 +08:00
|
|
|
)
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
type EtcdKV struct {
|
2020-09-07 21:07:36 +08:00
|
|
|
client *clientv3.Client
|
|
|
|
rootPath string
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
// NewEtcdKV creates a new etcd kv.
|
|
|
|
func NewEtcdKV(client *clientv3.Client, rootPath string) *EtcdKV {
|
2021-03-23 14:35:30 +08:00
|
|
|
kv := &EtcdKV{
|
2020-09-07 21:07:36 +08:00
|
|
|
client: client,
|
|
|
|
rootPath: rootPath,
|
|
|
|
}
|
2021-03-23 14:35:30 +08:00
|
|
|
return kv
|
2020-09-07 21:07:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) Close() {
|
2020-09-21 15:10:54 +08:00
|
|
|
kv.client.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
func (kv *EtcdKV) GetPath(key string) string {
|
|
|
|
return path.Join(kv.rootPath, key)
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:26:33 +08:00
|
|
|
func (kv *EtcdKV) LoadWithPrefix(key string) ([]string, []string, error) {
|
2020-09-18 15:44:09 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("LoadWithPrefix ", zap.String("prefix", key))
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
2020-11-02 10:26:33 +08:00
|
|
|
resp, err := kv.client.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
|
2020-09-18 15:44:09 +08:00
|
|
|
if err != nil {
|
2020-11-02 10:26:33 +08:00
|
|
|
return nil, nil, err
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
2020-11-02 10:26:33 +08:00
|
|
|
keys := make([]string, 0, resp.Count)
|
|
|
|
values := make([]string, 0, resp.Count)
|
|
|
|
for _, kv := range resp.Kvs {
|
|
|
|
keys = append(keys, string(kv.Key))
|
|
|
|
values = append(values, string(kv.Value))
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
2020-11-02 10:26:33 +08:00
|
|
|
return keys, values, nil
|
2020-09-18 15:44:09 +08:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) Load(key string) (string, error) {
|
2020-09-07 21:07:36 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
2020-11-02 10:26:33 +08:00
|
|
|
resp, err := kv.client.Get(ctx, key)
|
2020-09-07 21:07:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-11-02 10:26:33 +08:00
|
|
|
if resp.Count <= 0 {
|
2021-03-05 10:15:27 +08:00
|
|
|
return "", fmt.Errorf("there is no value on key = %s", key)
|
2020-09-07 21:07:36 +08:00
|
|
|
}
|
2020-11-02 10:26:33 +08:00
|
|
|
|
2020-09-07 21:07:36 +08:00
|
|
|
return string(resp.Kvs[0].Value), nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
func (kv *EtcdKV) MultiLoad(keys []string) ([]string, error) {
|
|
|
|
ops := make([]clientv3.Op, 0, len(keys))
|
2020-11-12 12:04:12 +08:00
|
|
|
for _, keyLoad := range keys {
|
|
|
|
ops = append(ops, clientv3.OpGet(path.Join(kv.rootPath, keyLoad)))
|
2020-11-03 15:08:50 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
2020-11-03 15:08:50 +08:00
|
|
|
resp, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]string, 0, len(keys))
|
|
|
|
invalid := make([]string, 0, len(keys))
|
|
|
|
for index, rp := range resp.Responses {
|
|
|
|
if rp.GetResponseRange().Kvs == nil {
|
|
|
|
invalid = append(invalid, keys[index])
|
|
|
|
result = append(result, "")
|
|
|
|
}
|
|
|
|
for _, ev := range rp.GetResponseRange().Kvs {
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("MultiLoad", zap.ByteString("key", ev.Key), zap.ByteString("value", ev.Value))
|
2020-11-03 15:08:50 +08:00
|
|
|
result = append(result, string(ev.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(invalid) != 0 {
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("MultiLoad: there are invalid keys", zap.Strings("keys", invalid))
|
2021-03-05 10:15:27 +08:00
|
|
|
err = fmt.Errorf("there are invalid keys: %s", invalid)
|
2020-11-03 15:08:50 +08:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) Save(key, value string) error {
|
2020-09-07 21:07:36 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
2020-11-02 10:26:33 +08:00
|
|
|
_, err := kv.client.Put(ctx, key, value)
|
|
|
|
return err
|
2020-09-07 21:07:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
func (kv *EtcdKV) MultiSave(kvs map[string]string) error {
|
|
|
|
ops := make([]clientv3.Op, 0, len(kvs))
|
|
|
|
for key, value := range kvs {
|
|
|
|
ops = append(ops, clientv3.OpPut(path.Join(kv.rootPath, key), value))
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
_, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
func (kv *EtcdKV) RemoveWithPrefix(prefix string) error {
|
|
|
|
key := path.Join(kv.rootPath, prefix)
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := kv.client.Delete(ctx, key, clientv3.WithPrefix())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) Remove(key string) error {
|
2020-09-07 21:07:36 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2020-11-02 10:26:33 +08:00
|
|
|
_, err := kv.client.Delete(ctx, key)
|
|
|
|
return err
|
2020-09-07 21:07:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
func (kv *EtcdKV) MultiRemove(keys []string) error {
|
|
|
|
ops := make([]clientv3.Op, 0, len(keys))
|
|
|
|
for _, key := range keys {
|
|
|
|
ops = append(ops, clientv3.OpDelete(path.Join(kv.rootPath, key)))
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
_, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *EtcdKV) MultiSaveAndRemove(saves map[string]string, removals []string) error {
|
|
|
|
ops := make([]clientv3.Op, 0, len(saves))
|
|
|
|
for key, value := range saves {
|
|
|
|
ops = append(ops, clientv3.OpPut(path.Join(kv.rootPath, key), value))
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:04:12 +08:00
|
|
|
for _, keyDelete := range removals {
|
|
|
|
ops = append(ops, clientv3.OpDelete(path.Join(kv.rootPath, keyDelete)))
|
2020-11-03 15:08:50 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("MultiSaveAndRemove")
|
2020-11-16 21:10:43 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
2020-11-12 12:04:12 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2020-11-03 15:08:50 +08:00
|
|
|
_, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) Watch(key string) clientv3.WatchChan {
|
2020-09-09 15:24:07 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2021-04-21 18:45:35 +08:00
|
|
|
rch := kv.client.Watch(context.Background(), key, clientv3.WithCreatedNotify())
|
2020-09-09 15:24:07 +08:00
|
|
|
return rch
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:46:34 +08:00
|
|
|
func (kv *EtcdKV) WatchWithPrefix(key string) clientv3.WatchChan {
|
2020-09-18 15:44:09 +08:00
|
|
|
key = path.Join(kv.rootPath, key)
|
2021-04-21 18:45:35 +08:00
|
|
|
rch := kv.client.Watch(context.Background(), key, clientv3.WithPrefix(), clientv3.WithCreatedNotify())
|
2020-09-18 15:44:09 +08:00
|
|
|
return rch
|
|
|
|
}
|
2021-03-06 16:00:41 +08:00
|
|
|
|
|
|
|
func (kv *EtcdKV) MultiRemoveWithPrefix(keys []string) error {
|
|
|
|
ops := make([]clientv3.Op, 0, len(keys))
|
|
|
|
for _, k := range keys {
|
|
|
|
op := clientv3.OpDelete(path.Join(kv.rootPath, k), clientv3.WithPrefix())
|
|
|
|
ops = append(ops, op)
|
|
|
|
}
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("MultiRemoveWithPrefix")
|
2021-03-06 16:00:41 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kv *EtcdKV) MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error {
|
|
|
|
ops := make([]clientv3.Op, 0, len(saves))
|
|
|
|
for key, value := range saves {
|
|
|
|
ops = append(ops, clientv3.OpPut(path.Join(kv.rootPath, key), value))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyDelete := range removals {
|
|
|
|
ops = append(ops, clientv3.OpDelete(path.Join(kv.rootPath, keyDelete), clientv3.WithPrefix()))
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("MultiSaveAndRemove")
|
2021-03-06 16:00:41 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), RequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := kv.client.Txn(ctx).If().Then(ops...).Commit()
|
|
|
|
return err
|
|
|
|
}
|