2019-08-01 02:16:45 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2021-01-30 21:38:44 +00:00
|
|
|
"bytes"
|
2019-08-01 02:16:45 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-04 06:25:33 +00:00
|
|
|
"go.etcd.io/etcd/client/v3"
|
2020-03-08 01:01:39 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
weed_util "github.com/seaweedfs/seaweedfs/weed/util"
|
2019-08-01 02:16:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DIR_FILE_SEPARATOR = byte(0x00)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 07:21:19 +00:00
|
|
|
filer.Stores = append(filer.Stores, &EtcdStore{})
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EtcdStore struct {
|
|
|
|
client *clientv3.Client
|
2023-05-19 06:08:56 +00:00
|
|
|
etcdKeyPrefix string
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *EtcdStore) GetName() string {
|
|
|
|
return "etcd"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (store *EtcdStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
|
|
|
|
servers := configuration.GetString(prefix + "servers")
|
2019-08-01 02:16:45 +00:00
|
|
|
if servers == "" {
|
|
|
|
servers = "localhost:2379"
|
|
|
|
}
|
|
|
|
|
2023-05-16 00:45:14 +00:00
|
|
|
username := configuration.GetString(prefix + "username")
|
|
|
|
password := configuration.GetString(prefix + "password")
|
2023-05-19 06:08:56 +00:00
|
|
|
store.etcdKeyPrefix = configuration.GetString(prefix + "key_prefix")
|
2023-05-16 00:45:14 +00:00
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
timeout := configuration.GetString(prefix + "timeout")
|
2019-08-01 02:16:45 +00:00
|
|
|
if timeout == "" {
|
|
|
|
timeout = "3s"
|
|
|
|
}
|
|
|
|
|
2023-05-16 00:45:14 +00:00
|
|
|
return store.initialize(servers, username, password, timeout)
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 00:45:14 +00:00
|
|
|
func (store *EtcdStore) initialize(servers string, username string, password string, timeout string) (err error) {
|
2019-08-01 02:16:45 +00:00
|
|
|
glog.Infof("filer store etcd: %s", servers)
|
|
|
|
|
|
|
|
to, err := time.ParseDuration(timeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse timeout %s: %s", timeout, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
store.client, err = clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: strings.Split(servers, ","),
|
2023-05-16 00:45:14 +00:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
2019-08-01 02:16:45 +00:00
|
|
|
DialTimeout: to,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connect to etcd %s: %s", servers, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *EtcdStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
func (store *EtcdStore) CommitTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *EtcdStore) RollbackTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *EtcdStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2019-08-01 02:16:45 +00:00
|
|
|
key := genKey(entry.DirAndName())
|
|
|
|
|
2020-09-03 18:00:20 +00:00
|
|
|
meta, err := entry.EncodeAttributesAndChunks()
|
2019-08-01 02:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:33:36 +00:00
|
|
|
if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
|
2020-09-03 18:00:20 +00:00
|
|
|
meta = weed_util.MaybeGzipData(meta)
|
|
|
|
}
|
|
|
|
|
2023-05-19 06:08:56 +00:00
|
|
|
if _, err := store.client.Put(ctx, store.etcdKeyPrefix + string(key), string(meta)); err != nil {
|
2019-08-01 02:16:45 +00:00
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *EtcdStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2019-08-01 02:16:45 +00:00
|
|
|
return store.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *EtcdStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
|
2019-08-01 02:16:45 +00:00
|
|
|
key := genKey(fullpath.DirAndName())
|
|
|
|
|
2023-05-19 06:08:56 +00:00
|
|
|
resp, err := store.client.Get(ctx, store.etcdKeyPrefix + string(key))
|
2019-08-01 02:16:45 +00:00
|
|
|
if err != nil {
|
2021-01-12 10:28:57 +00:00
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Kvs) == 0 {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
entry = &filer.Entry{
|
2019-08-01 02:16:45 +00:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
err = entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(resp.Kvs[0].Value))
|
2019-08-01 02:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *EtcdStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2019-08-01 02:16:45 +00:00
|
|
|
key := genKey(fullpath.DirAndName())
|
|
|
|
|
2023-05-19 06:08:56 +00:00
|
|
|
if _, err := store.client.Delete(ctx, store.etcdKeyPrefix + string(key)); err != nil {
|
2019-08-01 02:16:45 +00:00
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:20 +00:00
|
|
|
func (store *EtcdStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2019-12-13 08:23:05 +00:00
|
|
|
directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
|
|
|
|
|
2023-05-19 06:08:56 +00:00
|
|
|
if _, err := store.client.Delete(ctx, store.etcdKeyPrefix + string(directoryPrefix), clientv3.WithPrefix()); err != nil {
|
2019-12-13 08:23:05 +00:00
|
|
|
return fmt.Errorf("deleteFolderChildren %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *EtcdStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
|
2020-08-05 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *EtcdStore) ListDirectoryEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2021-01-15 06:35:56 +00:00
|
|
|
directoryPrefix := genDirectoryKeyPrefix(dirPath, "")
|
2021-01-30 21:38:44 +00:00
|
|
|
lastFileStart := directoryPrefix
|
|
|
|
if startFileName != "" {
|
|
|
|
lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
|
|
|
|
}
|
2019-08-01 02:16:45 +00:00
|
|
|
|
2023-05-19 06:08:56 +00:00
|
|
|
resp, err := store.client.Get(ctx, store.etcdKeyPrefix + string(lastFileStart),
|
2022-05-06 09:11:58 +00:00
|
|
|
clientv3.WithFromKey(), clientv3.WithLimit(limit+1))
|
2019-08-01 02:16:45 +00:00
|
|
|
if err != nil {
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range resp.Kvs {
|
2021-01-30 21:38:44 +00:00
|
|
|
if !bytes.HasPrefix(kv.Key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
2019-08-01 02:16:45 +00:00
|
|
|
fileName := getNameFromKey(kv.Key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-15 06:35:56 +00:00
|
|
|
if fileName == startFileName && !includeStartFile {
|
2019-08-01 02:16:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
limit--
|
|
|
|
if limit < 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-09-01 07:21:19 +00:00
|
|
|
entry := &filer.Entry{
|
2021-01-15 06:35:56 +00:00
|
|
|
FullPath: weed_util.NewFullPath(string(dirPath), fileName),
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(kv.Value)); decodeErr != nil {
|
2019-08-01 02:16:45 +00:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
2021-01-16 07:56:24 +00:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2021-01-30 21:38:44 +00:00
|
|
|
lastFileName = fileName
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, err
|
2019-08-01 02:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genKey(dirPath, fileName string) (key []byte) {
|
|
|
|
key = []byte(dirPath)
|
|
|
|
key = append(key, DIR_FILE_SEPARATOR)
|
|
|
|
key = append(key, []byte(fileName)...)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
|
2019-08-01 02:16:45 +00:00
|
|
|
keyPrefix = []byte(string(fullpath))
|
|
|
|
keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
|
|
|
|
if len(startFileName) > 0 {
|
|
|
|
keyPrefix = append(keyPrefix, []byte(startFileName)...)
|
|
|
|
}
|
|
|
|
return keyPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNameFromKey(key []byte) string {
|
|
|
|
sepIndex := len(key) - 1
|
|
|
|
for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
|
|
|
|
sepIndex--
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(key[sepIndex+1:])
|
|
|
|
}
|
2020-03-15 03:30:26 +00:00
|
|
|
|
|
|
|
func (store *EtcdStore) Shutdown() {
|
|
|
|
store.client.Close()
|
|
|
|
}
|