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.

216 lines
4.9 KiB

5 years ago
5 years ago
  1. package tikv
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "fmt"
  7. "io"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/pingcap/tidb/kv"
  12. "github.com/pingcap/tidb/store/tikv"
  13. )
  14. func init() {
  15. filer2.Stores = append(filer2.Stores, &TikvStore{})
  16. }
  17. type TikvStore struct {
  18. store kv.Storage
  19. }
  20. func (store *TikvStore) GetName() string {
  21. return "tikv"
  22. }
  23. func (store *TikvStore) Initialize(configuration weed_util.Configuration) (err error) {
  24. pdAddr := configuration.GetString("pdAddress")
  25. return store.initialize(pdAddr)
  26. }
  27. func (store *TikvStore) initialize(pdAddr string) (err error) {
  28. glog.Infof("filer store tikv pd address: %s", pdAddr)
  29. driver := tikv.Driver{}
  30. store.store, err = driver.Open(fmt.Sprintf("tikv://%s", pdAddr))
  31. if err != nil {
  32. return fmt.Errorf("open tikv %s : %v", pdAddr, err)
  33. }
  34. return
  35. }
  36. func (store *TikvStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  37. tx, err := store.store.Begin()
  38. if err != nil {
  39. return ctx, err
  40. }
  41. return context.WithValue(ctx, "tx", tx), nil
  42. }
  43. func (store *TikvStore) CommitTransaction(ctx context.Context) error {
  44. tx, ok := ctx.Value("tx").(kv.Transaction)
  45. if ok {
  46. return tx.Commit(ctx)
  47. }
  48. return nil
  49. }
  50. func (store *TikvStore) RollbackTransaction(ctx context.Context) error {
  51. tx, ok := ctx.Value("tx").(kv.Transaction)
  52. if ok {
  53. return tx.Rollback()
  54. }
  55. return nil
  56. }
  57. func (store *TikvStore) getTx(ctx context.Context) kv.Transaction {
  58. if tx, ok := ctx.Value("tx").(kv.Transaction); ok {
  59. return tx
  60. }
  61. return nil
  62. }
  63. func (store *TikvStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  64. dir, name := entry.DirAndName()
  65. key := genKey(dir, name)
  66. value, err := entry.EncodeAttributesAndChunks()
  67. if err != nil {
  68. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  69. }
  70. err = store.getTx(ctx).Set(key, value)
  71. if err != nil {
  72. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  73. }
  74. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  75. return nil
  76. }
  77. func (store *TikvStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  78. return store.InsertEntry(ctx, entry)
  79. }
  80. func (store *TikvStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  81. dir, name := fullpath.DirAndName()
  82. key := genKey(dir, name)
  83. data, err := store.getTx(ctx).Get(ctx, key)
  84. if err == kv.ErrNotExist {
  85. return nil, filer2.ErrNotFound
  86. }
  87. if err != nil {
  88. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  89. }
  90. entry = &filer2.Entry{
  91. FullPath: fullpath,
  92. }
  93. err = entry.DecodeAttributesAndChunks(data)
  94. if err != nil {
  95. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  96. }
  97. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  98. return entry, nil
  99. }
  100. func (store *TikvStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  101. dir, name := fullpath.DirAndName()
  102. key := genKey(dir, name)
  103. err = store.getTx(ctx).Delete(key)
  104. if err != nil {
  105. return fmt.Errorf("delete %s : %v", fullpath, err)
  106. }
  107. return nil
  108. }
  109. func (store *TikvStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  110. limit int) (entries []*filer2.Entry, err error) {
  111. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  112. lastFileStart := genDirectoryKeyPrefix(fullpath, startFileName)
  113. iter, err := store.getTx(ctx).Iter(lastFileStart, nil)
  114. if err != nil {
  115. return nil, fmt.Errorf("list %s: %v", fullpath, err)
  116. }
  117. defer iter.Close()
  118. for iter.Valid() {
  119. key := iter.Key()
  120. if !bytes.HasPrefix(key, directoryPrefix) {
  121. break
  122. }
  123. fileName := getNameFromKey(key)
  124. if fileName == "" {
  125. iter.Next()
  126. continue
  127. }
  128. if fileName == startFileName && !inclusive {
  129. iter.Next()
  130. continue
  131. }
  132. limit--
  133. if limit < 0 {
  134. break
  135. }
  136. entry := &filer2.Entry{
  137. FullPath: filer2.NewFullPath(string(fullpath), fileName),
  138. }
  139. // println("list", entry.FullPath, "chunks", len(entry.Chunks))
  140. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  141. err = decodeErr
  142. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  143. break
  144. }
  145. entries = append(entries, entry)
  146. iter.Next()
  147. }
  148. return entries, err
  149. }
  150. func genKey(dirPath, fileName string) (key []byte) {
  151. key = hashToBytes(dirPath)
  152. key = append(key, []byte(fileName)...)
  153. return key
  154. }
  155. func genDirectoryKeyPrefix(fullpath filer2.FullPath, startFileName string) (keyPrefix []byte) {
  156. keyPrefix = hashToBytes(string(fullpath))
  157. if len(startFileName) > 0 {
  158. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  159. }
  160. return keyPrefix
  161. }
  162. func getNameFromKey(key []byte) string {
  163. return string(key[md5.Size:])
  164. }
  165. // hash directory
  166. func hashToBytes(dir string) []byte {
  167. h := md5.New()
  168. io.WriteString(h, dir)
  169. b := h.Sum(nil)
  170. return b
  171. }