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.

302 lines
6.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. // +build rocksdb
  2. package rocksdb
  3. import (
  4. "bytes"
  5. "context"
  6. "crypto/md5"
  7. "fmt"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  12. rocksdb "github.com/tecbot/gorocksdb"
  13. "io"
  14. )
  15. func init() {
  16. filer.Stores = append(filer.Stores, &RocksDBStore{})
  17. }
  18. type options struct {
  19. opt *rocksdb.Options
  20. ro *rocksdb.ReadOptions
  21. wo *rocksdb.WriteOptions
  22. }
  23. func (opt *options) init() {
  24. opt.opt = rocksdb.NewDefaultOptions()
  25. opt.ro = rocksdb.NewDefaultReadOptions()
  26. opt.wo = rocksdb.NewDefaultWriteOptions()
  27. }
  28. func (opt *options) close() {
  29. opt.opt.Destroy()
  30. opt.ro.Destroy()
  31. opt.wo.Destroy()
  32. }
  33. type RocksDBStore struct {
  34. path string
  35. db *rocksdb.DB
  36. options
  37. }
  38. func (store *RocksDBStore) GetName() string {
  39. return "rocksdb"
  40. }
  41. func (store *RocksDBStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  42. dir := configuration.GetString(prefix + "dir")
  43. return store.initialize(dir)
  44. }
  45. func (store *RocksDBStore) initialize(dir string) (err error) {
  46. glog.Infof("filer store rocksdb dir: %s", dir)
  47. if err := weed_util.TestFolderWritable(dir); err != nil {
  48. return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
  49. }
  50. store.options.init()
  51. store.opt.SetCreateIfMissing(true)
  52. store.db, err = rocksdb.OpenDb(store.opt, dir)
  53. return
  54. }
  55. func (store *RocksDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  56. return ctx, nil
  57. }
  58. func (store *RocksDBStore) CommitTransaction(ctx context.Context) error {
  59. return nil
  60. }
  61. func (store *RocksDBStore) RollbackTransaction(ctx context.Context) error {
  62. return nil
  63. }
  64. func (store *RocksDBStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  65. dir, name := entry.DirAndName()
  66. key := genKey(dir, name)
  67. value, err := entry.EncodeAttributesAndChunks()
  68. if err != nil {
  69. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  70. }
  71. err = store.db.Put(store.wo, key, value)
  72. if err != nil {
  73. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  74. }
  75. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  76. return nil
  77. }
  78. func (store *RocksDBStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  79. return store.InsertEntry(ctx, entry)
  80. }
  81. func (store *RocksDBStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
  82. dir, name := fullpath.DirAndName()
  83. key := genKey(dir, name)
  84. data, err := store.db.Get(store.ro, key)
  85. if data == nil {
  86. return nil, filer_pb.ErrNotFound
  87. }
  88. defer data.Free()
  89. if err != nil {
  90. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  91. }
  92. entry = &filer.Entry{
  93. FullPath: fullpath,
  94. }
  95. err = entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(data.Data()))
  96. if err != nil {
  97. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  98. }
  99. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  100. return entry, nil
  101. }
  102. func (store *RocksDBStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  103. dir, name := fullpath.DirAndName()
  104. key := genKey(dir, name)
  105. err = store.db.Delete(store.wo, key)
  106. if err != nil {
  107. return fmt.Errorf("delete %s : %v", fullpath, err)
  108. }
  109. return nil
  110. }
  111. func (store *RocksDBStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  112. directoryPrefix := genDirectoryKeyPrefix(fullpath, "")
  113. batch := rocksdb.NewWriteBatch()
  114. defer batch.Destroy()
  115. ro := rocksdb.NewDefaultReadOptions()
  116. defer ro.Destroy()
  117. ro.SetFillCache(false)
  118. iter := store.db.NewIterator(ro)
  119. defer iter.Close()
  120. err = enumerate(iter, directoryPrefix, nil, false, -1, func(key, value []byte) bool {
  121. batch.Delete(key)
  122. return true
  123. })
  124. if err != nil {
  125. return fmt.Errorf("delete list %s : %v", fullpath, err)
  126. }
  127. err = store.db.Write(store.wo, batch)
  128. if err != nil {
  129. return fmt.Errorf("delete %s : %v", fullpath, err)
  130. }
  131. return nil
  132. }
  133. func enumerate(iter *rocksdb.Iterator, prefix, lastKey []byte, includeLastKey bool, limit int, fn func(key, value []byte) bool) error {
  134. if len(lastKey) == 0 {
  135. iter.Seek(prefix)
  136. } else {
  137. iter.Seek(lastKey)
  138. if !includeLastKey {
  139. key := iter.Key().Data()
  140. if !bytes.HasPrefix(key, prefix) {
  141. return nil
  142. }
  143. if bytes.Equal(key, lastKey) {
  144. iter.Next()
  145. }
  146. }
  147. }
  148. i := 0
  149. for ; iter.Valid(); iter.Next() {
  150. if limit > 0 {
  151. i++
  152. if i > limit {
  153. break
  154. }
  155. }
  156. key := iter.Key().Data()
  157. if !bytes.HasPrefix(key, prefix) {
  158. break
  159. }
  160. ret := fn(key, iter.Value().Data())
  161. if !ret {
  162. break
  163. }
  164. }
  165. if err := iter.Err(); err != nil {
  166. return fmt.Errorf("prefix scan iterator: %v", err)
  167. }
  168. return nil
  169. }
  170. func (store *RocksDBStore) ListDirectoryEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool,
  171. limit int) (entries []*filer.Entry, err error) {
  172. return store.ListDirectoryPrefixedEntries(ctx, fullpath, startFileName, inclusive, limit, "")
  173. }
  174. func (store *RocksDBStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  175. directoryPrefix := genDirectoryKeyPrefix(fullpath, prefix)
  176. lastFileStart := directoryPrefix
  177. if startFileName != "" {
  178. lastFileStart = genDirectoryKeyPrefix(fullpath, startFileName)
  179. }
  180. ro := rocksdb.NewDefaultReadOptions()
  181. defer ro.Destroy()
  182. ro.SetFillCache(false)
  183. iter := store.db.NewIterator(ro)
  184. defer iter.Close()
  185. err = enumerate(iter, directoryPrefix, lastFileStart, inclusive, limit, func(key, value []byte) bool {
  186. fileName := getNameFromKey(key)
  187. if fileName == "" {
  188. return true
  189. }
  190. limit--
  191. if limit < 0 {
  192. return false
  193. }
  194. entry := &filer.Entry{
  195. FullPath: weed_util.NewFullPath(string(fullpath), fileName),
  196. }
  197. // println("list", entry.FullPath, "chunks", len(entry.Chunks))
  198. if decodeErr := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(value)); decodeErr != nil {
  199. err = decodeErr
  200. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  201. return false
  202. }
  203. entries = append(entries, entry)
  204. return true
  205. })
  206. if err != nil {
  207. return entries, fmt.Errorf("prefix list %s : %v", fullpath, err)
  208. }
  209. return entries, err
  210. }
  211. func genKey(dirPath, fileName string) (key []byte) {
  212. key = hashToBytes(dirPath)
  213. key = append(key, []byte(fileName)...)
  214. return key
  215. }
  216. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string) (keyPrefix []byte) {
  217. keyPrefix = hashToBytes(string(fullpath))
  218. if len(startFileName) > 0 {
  219. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  220. }
  221. return keyPrefix
  222. }
  223. func getNameFromKey(key []byte) string {
  224. return string(key[md5.Size:])
  225. }
  226. // hash directory, and use last byte for partitioning
  227. func hashToBytes(dir string) []byte {
  228. h := md5.New()
  229. io.WriteString(h, dir)
  230. b := h.Sum(nil)
  231. return b
  232. }
  233. func (store *RocksDBStore) Shutdown() {
  234. store.db.Close()
  235. store.options.close()
  236. }