2019-05-18 00:33:49 +00:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2021-07-23 20:05:59 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2019-05-18 00:33:49 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2020-08-31 16:35:16 +00:00
|
|
|
leveldb_errors "github.com/syndtr/goleveldb/leveldb/errors"
|
2021-07-23 20:05:59 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/filter"
|
2019-05-18 00:33:49 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
leveldb_util "github.com/syndtr/goleveldb/leveldb/util"
|
2019-12-13 08:23:05 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
weed_util "github.com/seaweedfs/seaweedfs/weed/util"
|
2019-05-18 00:33:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 07:21:19 +00:00
|
|
|
filer.Stores = append(filer.Stores, &LevelDB2Store{})
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDB2Store struct {
|
2019-07-22 04:51:38 +00:00
|
|
|
dbs []*leveldb.DB
|
2019-07-03 04:25:53 +00:00
|
|
|
dbCount int
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) GetName() string {
|
|
|
|
return "leveldb2"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (store *LevelDB2Store) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
|
|
|
|
dir := configuration.GetString(prefix + "dir")
|
2019-07-03 04:25:53 +00:00
|
|
|
return store.initialize(dir, 8)
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
func (store *LevelDB2Store) initialize(dir string, dbCount int) (err error) {
|
2019-05-18 00:33:49 +00:00
|
|
|
glog.Infof("filer store leveldb2 dir: %s", dir)
|
2021-03-14 20:20:14 +00:00
|
|
|
os.MkdirAll(dir, 0755)
|
2019-05-18 00:33:49 +00:00
|
|
|
if err := weed_util.TestFolderWritable(dir); err != nil {
|
|
|
|
return fmt.Errorf("Check Level Folder %s Writable: %s", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &opt.Options{
|
2021-09-09 02:42:34 +00:00
|
|
|
BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
|
|
|
|
WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
|
|
|
|
Filter: filter.NewBloomFilter(8), // false positive rate 0.02
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 04:51:38 +00:00
|
|
|
for d := 0; d < dbCount; d++ {
|
2019-07-03 04:25:53 +00:00
|
|
|
dbFolder := fmt.Sprintf("%s/%02d", dir, d)
|
|
|
|
os.MkdirAll(dbFolder, 0755)
|
2019-07-03 04:28:51 +00:00
|
|
|
db, dbErr := leveldb.OpenFile(dbFolder, opts)
|
2020-08-31 16:35:16 +00:00
|
|
|
if leveldb_errors.IsCorrupted(dbErr) {
|
2020-05-22 17:54:42 +00:00
|
|
|
db, dbErr = leveldb.RecoverFile(dbFolder, opts)
|
|
|
|
}
|
2019-07-03 04:28:51 +00:00
|
|
|
if dbErr != nil {
|
|
|
|
glog.Errorf("filer store open dir %s: %v", dbFolder, dbErr)
|
2020-05-22 17:54:42 +00:00
|
|
|
return dbErr
|
2019-07-03 04:25:53 +00:00
|
|
|
}
|
|
|
|
store.dbs = append(store.dbs, db)
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
2019-07-03 04:25:53 +00:00
|
|
|
store.dbCount = dbCount
|
|
|
|
|
2019-05-18 00:33:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LevelDB2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
func (store *LevelDB2Store) CommitTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *LevelDB2Store) RollbackTransaction(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDB2Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2019-07-03 04:25:53 +00:00
|
|
|
dir, name := entry.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
value, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:28:55 +00:00
|
|
|
if len(entry.Chunks) > filer.CountEntryChunksForGzip {
|
2020-09-03 18:00:20 +00:00
|
|
|
value = weed_util.MaybeGzipData(value)
|
|
|
|
}
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
err = store.dbs[partitionId].Put(key, value, nil)
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// println("saved", entry.FullPath, "chunks", len(entry.Chunks))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDB2Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
return store.InsertEntry(ctx, entry)
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *LevelDB2Store) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
|
2019-07-03 04:25:53 +00:00
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 00:33:49 +00:00
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
data, err := store.dbs[partitionId].Get(key, nil)
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
if err == leveldb.ErrNotFound {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-01-12 10:28:57 +00:00
|
|
|
return nil, fmt.Errorf("get %s : %v", fullpath, err)
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
entry = &filer.Entry{
|
2019-05-18 00:33:49 +00:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
err = entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(data))
|
2019-05-18 00:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2019-07-03 04:33:23 +00:00
|
|
|
// println("read", entry.FullPath, "chunks", len(entry.Chunks), "data", len(data), string(data))
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *LevelDB2Store) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2019-07-03 04:25:53 +00:00
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
key, partitionId := genKey(dir, name, store.dbCount)
|
2019-05-18 00:33:49 +00:00
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
err = store.dbs[partitionId].Delete(key, nil)
|
2019-05-18 00:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:23:20 +00:00
|
|
|
func (store *LevelDB2Store) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
|
2019-12-13 08:23:05 +00:00
|
|
|
directoryPrefix, partitionId := genDirectoryKeyPrefix(fullpath, "", store.dbCount)
|
|
|
|
|
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
|
|
|
|
iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: directoryPrefix}, nil)
|
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := getNameFromKey(key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
batch.Delete(append(directoryPrefix, []byte(fileName)...))
|
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
|
|
|
|
err = store.dbs[partitionId].Write(batch, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *LevelDB2Store) ListDirectoryEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
|
|
|
return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", eachEntryFunc)
|
2020-11-23 05:10:41 +00:00
|
|
|
}
|
2019-05-18 00:33:49 +00:00
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
func (store *LevelDB2Store) ListDirectoryPrefixedEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2020-11-23 05:10:41 +00:00
|
|
|
|
2021-01-15 07:10:37 +00:00
|
|
|
directoryPrefix, partitionId := genDirectoryKeyPrefix(dirPath, prefix, store.dbCount)
|
2021-01-02 04:23:23 +00:00
|
|
|
lastFileStart := directoryPrefix
|
|
|
|
if startFileName != "" {
|
2021-01-15 07:10:37 +00:00
|
|
|
lastFileStart, _ = genDirectoryKeyPrefix(dirPath, startFileName, store.dbCount)
|
2021-01-02 04:23:23 +00:00
|
|
|
}
|
2019-05-18 00:33:49 +00:00
|
|
|
|
2019-07-08 03:37:48 +00:00
|
|
|
iter := store.dbs[partitionId].NewIterator(&leveldb_util.Range{Start: lastFileStart}, nil)
|
2019-05-18 00:33:49 +00:00
|
|
|
for iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, directoryPrefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := getNameFromKey(key)
|
|
|
|
if fileName == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-15 07:10:37 +00:00
|
|
|
if fileName == startFileName && !includeStartFile {
|
2019-05-18 00:33:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
limit--
|
|
|
|
if limit < 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-02-01 04:11:44 +00:00
|
|
|
lastFileName = fileName
|
2020-09-01 07:21:19 +00:00
|
|
|
entry := &filer.Entry{
|
2021-01-15 07:10:37 +00:00
|
|
|
FullPath: weed_util.NewFullPath(string(dirPath), fileName),
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
2019-07-03 04:25:53 +00:00
|
|
|
|
2019-07-03 04:33:23 +00:00
|
|
|
// println("list", entry.FullPath, "chunks", len(entry.Chunks))
|
2020-09-03 18:00:20 +00:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(weed_util.MaybeDecompressData(iter.Value())); decodeErr != nil {
|
2019-05-18 00:33:49 +00:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
2021-01-16 07:56:24 +00:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
|
2021-01-16 07:56:24 +00:00
|
|
|
return lastFileName, err
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
func genKey(dirPath, fileName string, dbCount int) (key []byte, partitionId int) {
|
|
|
|
key, partitionId = hashToBytes(dirPath, dbCount)
|
2019-05-18 00:33:49 +00:00
|
|
|
key = append(key, []byte(fileName)...)
|
2019-07-03 04:25:53 +00:00
|
|
|
return key, partitionId
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func genDirectoryKeyPrefix(fullpath weed_util.FullPath, startFileName string, dbCount int) (keyPrefix []byte, partitionId int) {
|
2019-07-03 04:25:53 +00:00
|
|
|
keyPrefix, partitionId = hashToBytes(string(fullpath), dbCount)
|
2019-05-18 00:33:49 +00:00
|
|
|
if len(startFileName) > 0 {
|
|
|
|
keyPrefix = append(keyPrefix, []byte(startFileName)...)
|
|
|
|
}
|
2019-07-03 04:25:53 +00:00
|
|
|
return keyPrefix, partitionId
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNameFromKey(key []byte) string {
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
return string(key[md5.Size:])
|
2019-05-18 00:33:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
// hash directory, and use last byte for partitioning
|
|
|
|
func hashToBytes(dir string, dbCount int) ([]byte, int) {
|
2019-05-18 00:33:49 +00:00
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, dir)
|
|
|
|
|
|
|
|
b := h.Sum(nil)
|
|
|
|
|
2019-07-03 04:25:53 +00:00
|
|
|
x := b[len(b)-1]
|
|
|
|
|
2019-07-22 04:51:38 +00:00
|
|
|
return b, int(x) % dbCount
|
2019-05-18 00:33:49 +00:00
|
|
|
}
|
2020-03-15 03:30:26 +00:00
|
|
|
|
|
|
|
func (store *LevelDB2Store) Shutdown() {
|
|
|
|
for d := 0; d < store.dbCount; d++ {
|
|
|
|
store.dbs[d].Close()
|
|
|
|
}
|
|
|
|
}
|