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.

251 lines
6.7 KiB

4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
  1. package leveldb
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "fmt"
  7. "github.com/syndtr/goleveldb/leveldb"
  8. leveldb_errors "github.com/syndtr/goleveldb/leveldb/errors"
  9. "github.com/syndtr/goleveldb/leveldb/opt"
  10. leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
  11. "io"
  12. "os"
  13. "github.com/chrislusf/seaweedfs/weed/filer2"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. weed_util "github.com/chrislusf/seaweedfs/weed/util"
  17. )
  18. func init() {
  19. filer2.Stores = append(filer2.Stores, &LevelDB2Store{})
  20. }
  21. type LevelDB2Store struct {
  22. dbs []*leveldb.DB
  23. dbCount int
  24. }
  25. func (store *LevelDB2Store) GetName() string {
  26. return "leveldb2"
  27. }
  28. func (store *LevelDB2Store) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  29. dir := configuration.GetString(prefix + "dir")
  30. return store.initialize(dir, 8)
  31. }
  32. func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
  33. glog.Infof("filer store leveldb2 dir: %s", dir)
  34. if err := weed_util.TestFolderWritable(dir); err != nil {
  35. return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
  36. }
  37. opts := &opt.Options{
  38. BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
  39. WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
  40. CompactionTableSizeMultiplier: 4,
  41. }
  42. for d := 0; d < dbCount; d++ {
  43. dbFolder := fmt.Sprintf("%s/%02d", dir, d)
  44. os.MkdirAll(dbFolder, 0755)
  45. db, dbErr := leveldb.OpenFile(dbFolder, opts)
  46. if leveldb_errors.IsCorrupted(dbErr) {
  47. db, dbErr = leveldb.RecoverFile(dbFolder, opts)
  48. }
  49. if dbErr != nil {
  50. glog.Errorf("filer store open dir %s: %v", dbFolder, dbErr)
  51. return dbErr
  52. }
  53. store.dbs = append(store.dbs, db)
  54. }
  55. store.dbCount = dbCount
  56. return
  57. }
  58. func (store *LevelDB2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
  59. return ctx, nil
  60. }
  61. func (store *LevelDB2Store) CommitTransaction(ctx context.Context) error {
  62. return nil
  63. }
  64. func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
  65. return nil
  66. }
  67. func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  68. dir, name := entry.DirAndName()
  69. key, partitionId := genKey(dir, name, store.dbCount)
  70. value, err := entry.EncodeAttributesAndChunks()
  71. if err != nil {
  72. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  73. }
  74. err = store.dbs[partitionId].Put(key, value, nil)
  75. if err != nil {
  76. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  77. }
  78. // println("saved", entry.FullPath, "chunks", len(entry.Chunks))
  79. return nil
  80. }
  81. func (store *LevelDB2Store) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  82. return store.InsertEntry(ctx, entry)
  83. }
  84. func (store *LevelDB2Store) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer2.Entry, err error) {
  85. dir, name := fullpath.DirAndName()
  86. key, partitionId := genKey(dir, name, store.dbCount)
  87. data, err := store.dbs[partitionId].Get(key, nil)
  88. if err == leveldb.ErrNotFound {
  89. return nil, filer_pb.ErrNotFound
  90. }
  91. if err != nil {
  92. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  93. }
  94. entry = &filer2.Entry{
  95. FullPath: fullpath,
  96. }
  97. err = entry.DecodeAttributesAndChunks(data)
  98. if err != nil {
  99. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  100. }
  101. // println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
  102. return entry, nil
  103. }
  104. func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  105. dir, name := fullpath.DirAndName()
  106. key, partitionId := genKey(dir, name, store.dbCount)
  107. err = store.dbs[partitionId].Delete(key, nil)
  108. if err != nil {
  109. return fmt.Errorf("delete %s : %v", fullpath, err)
  110. }
  111. return nil
  112. }
  113. func (store *LevelDB2Store) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  114. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  115. batch := new(leveldb.Batch)
  116. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
  117. for iter.Next() {
  118. key := iter.Key()
  119. if !bytes.HasPrefix(key, directoryPrefix) {
  120. break
  121. }
  122. fileName := getNameFromKey(key)
  123. if fileName == "" {
  124. continue
  125. }
  126. batch.Delete(append(directoryPrefix, []byte(fileName)...))
  127. }
  128. iter.Release()
  129. err = store.dbs[partitionId].Write(batch, nil)
  130. if err != nil {
  131. return fmt.Errorf("delete %s : %v", fullpath, err)
  132. }
  133. return nil
  134. }
  135. func (store *LevelDB2Store) ListDirectoryPrefixedEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  136. return nil, filer2.ErrUnsupportedListDirectoryPrefixed
  137. }
  138. func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool,
  139. limit int) (entries []*filer2.Entry, err error) {
  140. directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
  141. lastFileStart, _ := genDirectoryKeyPrefix(fullpath, startFileName, store.dbCount)
  142. iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: lastFileStart}, nil)
  143. for iter.Next() {
  144. key := iter.Key()
  145. if !bytes.HasPrefix(key, directoryPrefix) {
  146. break
  147. }
  148. fileName := getNameFromKey(key)
  149. if fileName == "" {
  150. continue
  151. }
  152. if fileName == startFileName && !inclusive {
  153. continue
  154. }
  155. limit--
  156. if limit < 0 {
  157. break
  158. }
  159. entry := &filer2.Entry{
  160. FullPath: weed_util.NewFullPath(string(fullpath), fileName),
  161. }
  162. // println("list", entry.FullPath, "chunks", len(entry.Chunks))
  163. if decodeErr := entry.DecodeAttributesAndChunks(iter.Value()); decodeErr != nil {
  164. err = decodeErr
  165. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  166. break
  167. }
  168. entries = append(entries, entry)
  169. }
  170. iter.Release()
  171. return entries, err
  172. }
  173. func genKey(dirPath, fileName string, dbCount int) (key []byte, partitionId int) {
  174. key, partitionId = hashToBytes(dirPath, dbCount)
  175. key = append(key, []byte(fileName)...)
  176. return key, partitionId
  177. }
  178. func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string, dbCount int) (keyPrefix []byte, partitionId int) {
  179. keyPrefix, partitionId = hashToBytes(string(fullpath), dbCount)
  180. if len(startFileName) > 0 {
  181. keyPrefix = append(keyPrefix, []byte(startFileName)...)
  182. }
  183. return keyPrefix, partitionId
  184. }
  185. func getNameFromKey(key []byte) string {
  186. return string(key[md5.Size:])
  187. }
  188. // hash directory, and use last byte for partitioning
  189. func hashToBytes(dir string, dbCount int) ([]byte, int) {
  190. h := md5.New()
  191. io.WriteString(h, dir)
  192. b := h.Sum(nil)
  193. x := b[len(b)-1]
  194. return b, int(x) % dbCount
  195. }
  196. func (store *LevelDB2Store) Shutdown() {
  197. for d := 0; d < store.dbCount; d++ {
  198. store.dbs[d].Close()
  199. }
  200. }