etcd_store.go 6.1 KB

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