rocksdb_store.go 7.5 KB

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