You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.9 KiB

3 years ago
5 years ago
5 years ago
  1. package etcd
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go.etcd.io/etcd/client/v3"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. weed_util "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. const (
  15. DIR_FILE_SEPARATOR = byte(0x00)
  16. )
  17. func init() {
  18. filer.Stores = append(filer.Stores, &EtcdStore{})
  19. }
  20. type EtcdStore struct {
  21. client *clientv3.Client
  22. }
  23. func (store *EtcdStore) GetName() string {
  24. return "etcd"
  25. }
  26. func (store *EtcdStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  27. servers := configuration.GetString(prefix + "servers")
  28. if servers == "" {
  29. servers = "localhost:2379"
  30. }
  31. username := configuration.GetString(prefix + "username")
  32. password := configuration.GetString(prefix + "password")
  33. timeout := configuration.GetString(prefix + "timeout")
  34. if timeout == "" {
  35. timeout = "3s"
  36. }
  37. return store.initialize(servers, username, password, timeout)
  38. }
  39. func (store *EtcdStore) initialize(servers string, username string, password string, timeout string) (err error) {
  40. glog.Infof("filer store etcd: %s", servers)
  41. to, err := time.ParseDuration(timeout)
  42. if err != nil {
  43. return fmt.Errorf("parse timeout %s: %s", timeout, err)
  44. }
  45. store.client, err = clientv3.New(clientv3.Config{
  46. Endpoints: strings.Split(servers, ","),
  47. Username: username,
  48. Password: password,
  49. DialTimeout: to,
  50. })
  51. if err != nil {
  52. return fmt.Errorf("connect to etcd %s: %s", servers, err)
  53. }
  54. return
  55. }
  56. func (store *EtcdStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  57. return ctx, nil
  58. }
  59. func (store *EtcdStore) CommitTransaction(ctx context.Context) error {
  60. return nil
  61. }
  62. func (store *EtcdStore) RollbackTransaction(ctx context.Context) error {
  63. return nil
  64. }
  65. func (store *EtcdStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  66. key := genKey(entry.DirAndName())
  67. meta, err := entry.EncodeAttributesAndChunks()
  68. if err != nil {
  69. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  70. }
  71. if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
  72. meta = weed_util.MaybeGzipData(meta)
  73. }
  74. if _, err := store.client.Put(ctx, string(key), string(meta)); err != nil {
  75. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  76. }
  77. return nil
  78. }
  79. func (store *EtcdStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  80. return store.InsertEntry(ctx, entry)
  81. }
  82. func (store *EtcdStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
  83. key := genKey(fullpath.DirAndName())
  84. resp, err := store.client.Get(ctx, string(key))
  85. if err != nil {
  86. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  87. }
  88. if len(resp.Kvs) == 0 {
  89. return nil, filer_pb.ErrNotFound
  90. }
  91. entry = &filer.Entry{
  92. FullPath: fullpath,
  93. }
  94. err = entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(resp.Kvs[0].Value))
  95. if err != nil {
  96. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  97. }
  98. return entry, nil
  99. }
  100. func (store *EtcdStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  101. key := genKey(fullpath.DirAndName())
  102. if _, err := store.client.Delete(ctx, string(key)); err != nil {
  103. return fmt.Errorf("delete %s : %v", fullpath, err)
  104. }
  105. return nil
  106. }
  107. func (store *EtcdStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  108. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  109. if _, err := store.client.Delete(ctx, string(directoryPrefix), clientv3.WithPrefix()); err != nil {
  110. return fmt.Errorf("deleteFolderChildren %s : %v", fullpath, err)
  111. }
  112. return nil
  113. }
  114. 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) {
  115. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  116. }
  117. func (store *EtcdStore) ListDirectoryEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  118. directoryPrefix := genDirectoryKeyPrefix(dirPath, "")
  119. lastFileStart := directoryPrefix
  120. if startFileName != "" {
  121. lastFileStart = genDirectoryKeyPrefix(dirPath, startFileName)
  122. }
  123. resp, err := store.client.Get(ctx, string(lastFileStart),
  124. clientv3.WithFromKey(), clientv3.WithLimit(limit+1))
  125. if err != nil {
  126. return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
  127. }
  128. for _, kv := range resp.Kvs {
  129. if !bytes.HasPrefix(kv.Key, directoryPrefix) {
  130. break
  131. }
  132. fileName := getNameFromKey(kv.Key)
  133. if fileName == "" {
  134. continue
  135. }
  136. if fileName == startFileName && !includeStartFile {
  137. continue
  138. }
  139. limit--
  140. if limit < 0 {
  141. break
  142. }
  143. entry := &filer.Entry{
  144. FullPath: weed_util.NewFullPath(string(dirPath), fileName),
  145. }
  146. if decodeErr := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(kv.Value)); decodeErr != nil {
  147. err = decodeErr
  148. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  149. break
  150. }
  151. if !eachEntryFunc(entry) {
  152. break
  153. }
  154. lastFileName = fileName
  155. }
  156. return lastFileName, err
  157. }
  158. func genKey(dirPath, fileName string) (key []byte) {
  159. key = []byte(dirPath)
  160. key = append(key, DIR_FILE_SEPARATOR)
  161. key = append(key, []byte(fileName)...)
  162. return key
  163. }
  164. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
  165. keyPrefix = []byte(string(fullpath))
  166. keyPrefix = append(keyPrefix, DIR_FILE_SEPARATOR)
  167. if len(startFileName) > 0 {
  168. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  169. }
  170. return keyPrefix
  171. }
  172. func getNameFromKey(key []byte) string {
  173. sepIndex := len(key) - 1
  174. for sepIndex >= 0 && key[sepIndex] != DIR_FILE_SEPARATOR {
  175. sepIndex--
  176. }
  177. return string(key[sepIndex+1:])
  178. }
  179. func (store *EtcdStore) Shutdown() {
  180. store.client.Close()
  181. }