From bec9f79659297ae7f64db40c5d9ed10396de7ea7 Mon Sep 17 00:00:00 2001 From: guol-fnst Date: Tue, 19 Jul 2022 09:20:45 +0800 Subject: [PATCH 1/4] use snapshot --- weed/storage/disk_location.go | 7 +- weed/storage/idx/walk.go | 33 +++++++++ weed/storage/needle_map_leveldb.go | 108 ++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 12 deletions(-) diff --git a/weed/storage/disk_location.go b/weed/storage/disk_location.go index 8af8ea663..d72d83208 100644 --- a/weed/storage/disk_location.go +++ b/weed/storage/disk_location.go @@ -123,7 +123,7 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne if volumeName == "" { return false } - + glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) // skip if ec volumes exists if skipIfEcVolumesExists { if util.FileExists(l.Directory + "/" + volumeName + ".ecx") { @@ -147,7 +147,7 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne glog.Warningf("get volume id failed, %s, err : %s", volumeName, err) return false } - + glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) // avoid loading one volume more than once l.volumesLock.RLock() _, found := l.volumes[vid] @@ -156,6 +156,7 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne glog.V(1).Infof("loaded volume, %v", vid) return true } + glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) // load the volume v, e := NewVolume(l.Directory, l.IdxDirectory, collection, vid, needleMapKind, nil, nil, 0, 0) @@ -222,6 +223,8 @@ func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapKind) { workerNum = 10 } } + workerNum = 10 + l.concurrentLoadingVolumes(needleMapKind, workerNum) glog.V(0).Infof("Store started on dir: %s with %d volumes max %d", l.Directory, len(l.volumes), l.MaxVolumeCount) diff --git a/weed/storage/idx/walk.go b/weed/storage/idx/walk.go index 5215d3c4f..ef177ec2f 100644 --- a/weed/storage/idx/walk.go +++ b/weed/storage/idx/walk.go @@ -42,6 +42,39 @@ func WalkIndexFile(r io.ReaderAt, fn func(key types.NeedleId, offset types.Offse return e } +func WalkIndexFileIncrent(r io.ReaderAt, milestone uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { + var readerOffset = int64(milestone * types.NeedleMapEntrySize) + bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead) + count, e := r.ReadAt(bytes, readerOffset) + if count == 0 && e == io.EOF { + return nil + } + glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e) + readerOffset += int64(count) + var ( + key types.NeedleId + offset types.Offset + size types.Size + i int + ) + + for count > 0 && e == nil || e == io.EOF { + for i = 0; i+types.NeedleMapEntrySize <= count; i += types.NeedleMapEntrySize { + key, offset, size = IdxFileEntry(bytes[i : i+types.NeedleMapEntrySize]) + if e = fn(key, offset, size); e != nil { + return e + } + } + if e == io.EOF { + return nil + } + count, e = r.ReadAt(bytes, readerOffset) + glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e) + readerOffset += int64(count) + } + return e +} + func IdxFileEntry(bytes []byte) (key types.NeedleId, offset types.Offset, size types.Size) { key = types.BytesToNeedleId(bytes[:types.NeedleIdSize]) offset = types.BytesToOffset(bytes[types.NeedleIdSize : types.NeedleIdSize+types.OffsetSize]) diff --git a/weed/storage/needle_map_leveldb.go b/weed/storage/needle_map_leveldb.go index 31c86d124..f014797df 100644 --- a/weed/storage/needle_map_leveldb.go +++ b/weed/storage/needle_map_leveldb.go @@ -4,11 +4,14 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/chrislusf/seaweedfs/weed/storage/idx" + "github.com/chrislusf/seaweedfs/weed/storage/types" + "github.com/chrislusf/seaweedfs/weed/util" "github.com/syndtr/goleveldb/leveldb" @@ -17,26 +20,36 @@ import ( . "github.com/chrislusf/seaweedfs/weed/storage/types" ) +//use "2 >> 16" to reduce cpu cost +const milestoneCnt = 40 +const milestoneKey = 0xffffffffffffffff - 1 + type LevelDbNeedleMap struct { baseNeedleMapper dbFileName string db *leveldb.DB + recordNum uint64 } func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Options) (m *LevelDbNeedleMap, err error) { + glog.V(0).Infof("NewLevelDbNeedleMap pocessing %s...", indexFile.Name()) + db, errd := leveldb.OpenFile(dbFileName, opts) + glog.V(0).Infof("begain %v %s %d", errd, dbFileName, getMileStone(db)) + db.Close() + m = &LevelDbNeedleMap{dbFileName: dbFileName} m.indexFile = indexFile if !isLevelDbFresh(dbFileName, indexFile) { - glog.V(1).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name()) + glog.V(0).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name()) generateLevelDbFile(dbFileName, indexFile) - glog.V(1).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name()) + glog.V(0).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name()) } if stat, err := indexFile.Stat(); err != nil { glog.Fatalf("stat file %s: %v", indexFile.Name(), err) } else { m.indexFileOffset = stat.Size() } - glog.V(1).Infof("Opening %s...", dbFileName) + glog.V(0).Infof("Opening %s...", dbFileName) if m.db, err = leveldb.OpenFile(dbFileName, opts); err != nil { if errors.IsCorrupted(err) { @@ -46,11 +59,19 @@ func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Option return } } - glog.V(1).Infof("Loading %s...", indexFile.Name()) + glog.V(0).Infof("getMileStone %s : %d", dbFileName, getMileStone(m.db)) + m.recordNum = uint64(m.indexFileOffset / types.NeedleMapEntrySize) + milestone := (m.recordNum / milestoneCnt) * milestoneCnt + err = setMileStone(m.db, milestone) + if err != nil { + return + } + glog.V(0).Infof("Loading %s... %d %d", indexFile.Name(), milestone, getMileStone(m.db)) mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile) if indexLoadError != nil { return nil, indexLoadError } + glog.V(0).Infof("finish Loading %s...", indexFile.Name()) m.mapMetric = *mm return } @@ -78,9 +99,21 @@ func generateLevelDbFile(dbFileName string, indexFile *os.File) error { return err } defer db.Close() - return idx.WalkIndexFile(indexFile, func(key NeedleId, offset Offset, size Size) error { + + milestone := getMileStone(db) + if stat, err := indexFile.Stat(); err != nil { + glog.Fatalf("stat file %s: %v", indexFile.Name(), err) + return err + } else { + if milestone*types.NeedleMapEntrySize > uint64(stat.Size()) { + glog.Warningf("wrong milestone %d for filesize %d, set milestone to 0", milestone, stat.Size()) + milestone = 0 + } + glog.V(0).Infof("generateLevelDbFile %s, milestone %d, num of entries:%d", dbFileName, milestone, (uint64(stat.Size())-milestone*types.NeedleMapEntrySize)/types.NeedleMapEntrySize) + } + return idx.WalkIndexFileIncrent(indexFile, milestone, func(key NeedleId, offset Offset, size Size) error { if !offset.IsZero() && size.IsValid() { - levelDbWrite(db, key, offset, size) + levelDbWrite(db, key, offset, size, 0) } else { levelDbDelete(db, key) } @@ -102,6 +135,7 @@ func (m *LevelDbNeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, o func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error { var oldSize Size + var milestone uint64 if oldNeedle, ok := m.Get(key); ok { oldSize = oldNeedle.Size } @@ -110,16 +144,61 @@ func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error { if err := m.appendToIndexFile(key, offset, size); err != nil { return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err) } - return levelDbWrite(m.db, key, offset, size) + //atomic.AddUint64(&m.recordNum, 1) + //milestone = atomic.LoadUint64(&m.recordNum) + m.recordNum++ + if m.recordNum%milestoneCnt != 0 { + milestone = 0 + } else { + milestone = (m.recordNum / milestoneCnt) * milestoneCnt + glog.V(0).Infof("put cnt:%d milestone:%s %d", m.recordNum, m.dbFileName, milestone) + } + return levelDbWrite(m.db, key, offset, size, milestone) } -func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size) error { +func getMileStone(db *leveldb.DB) uint64 { + var mskBytes = make([]byte, 8) + util.Uint64toBytes(mskBytes, milestoneKey) + data, err := db.Get(mskBytes, nil) + if err != nil || len(data) != 8 { + glog.Warningf("get milestone from db error: %v, %d", err, len(data)) + if !strings.Contains(strings.ToLower(err.Error()), "not found") { + err = setMileStone(db, 0) + if err != nil { + glog.Errorf("failed to set milestone: %v", err) + } + } + + return 0 + } + return util.BytesToUint64(data) +} + +func setMileStone(db *leveldb.DB, milestone uint64) error { + glog.V(0).Infof("set milestone %d", milestone) + var mskBytes = make([]byte, 8) + util.Uint64toBytes(mskBytes, milestoneKey) + var msBytes = make([]byte, 8) + util.Uint64toBytes(msBytes, milestone) + if err := db.Put(mskBytes, msBytes, nil); err != nil { + return fmt.Errorf("failed to setMileStone: %v", err) + } + glog.V(0).Infof("ssset milestone %d, %d", milestone, getMileStone(db)) + return nil +} + +func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, milestone uint64) error { bytes := needle_map.ToBytes(key, offset, size) if err := db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil { return fmt.Errorf("failed to write leveldb: %v", err) } + // set milestone + if milestone != 0 { + glog.V(0).Infof("actually set milestone %d", milestone) + return setMileStone(db, milestone) + } return nil } func levelDbDelete(db *leveldb.DB, key NeedleId) error { @@ -129,6 +208,7 @@ func levelDbDelete(db *leveldb.DB, key NeedleId) error { } func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error { + var milestone uint64 oldNeedle, found := m.Get(key) if !found || oldNeedle.Size.IsDeleted() { return nil @@ -139,8 +219,16 @@ func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error { if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil { return err } - - return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size) + //atomic.AddUint64(&m.recordNum, 1) + //milestone = atomic.LoadUint64(&m.recordNum) + m.recordNum++ + if m.recordNum%milestoneCnt != 0 { + milestone = 0 + } else { + milestone = (m.recordNum / milestoneCnt) * milestoneCnt + } + glog.V(0).Infof("delete cnt:%d milestone:%s %d", m.recordNum, m.dbFileName, milestone) + return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size, milestone) } func (m *LevelDbNeedleMap) Close() { From b9256e0b34fcd8ae62b89d1df5b61b7c35e204b4 Mon Sep 17 00:00:00 2001 From: guol-fnst Date: Tue, 19 Jul 2022 13:17:52 +0800 Subject: [PATCH 2/4] optimiz --- weed/storage/disk_location.go | 7 ++--- weed/storage/idx/walk.go | 3 +- weed/storage/needle_map_leveldb.go | 46 +++++++++++------------------- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/weed/storage/disk_location.go b/weed/storage/disk_location.go index d72d83208..8af8ea663 100644 --- a/weed/storage/disk_location.go +++ b/weed/storage/disk_location.go @@ -123,7 +123,7 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne if volumeName == "" { return false } - glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) + // skip if ec volumes exists if skipIfEcVolumesExists { if util.FileExists(l.Directory + "/" + volumeName + ".ecx") { @@ -147,7 +147,7 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne glog.Warningf("get volume id failed, %s, err : %s", volumeName, err) return false } - glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) + // avoid loading one volume more than once l.volumesLock.RLock() _, found := l.volumes[vid] @@ -156,7 +156,6 @@ func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind Ne glog.V(1).Infof("loaded volume, %v", vid) return true } - glog.V(0).Infof("data file %s", l.Directory+"/"+volumeName) // load the volume v, e := NewVolume(l.Directory, l.IdxDirectory, collection, vid, needleMapKind, nil, nil, 0, 0) @@ -223,8 +222,6 @@ func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapKind) { workerNum = 10 } } - workerNum = 10 - l.concurrentLoadingVolumes(needleMapKind, workerNum) glog.V(0).Infof("Store started on dir: %s with %d volumes max %d", l.Directory, len(l.volumes), l.MaxVolumeCount) diff --git a/weed/storage/idx/walk.go b/weed/storage/idx/walk.go index ef177ec2f..74cb83b45 100644 --- a/weed/storage/idx/walk.go +++ b/weed/storage/idx/walk.go @@ -42,7 +42,8 @@ func WalkIndexFile(r io.ReaderAt, fn func(key types.NeedleId, offset types.Offse return e } -func WalkIndexFileIncrent(r io.ReaderAt, milestone uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { +//copied from WalkIndexFile, just init readerOffset from milestone +func WalkIndexFileIncrement(r io.ReaderAt, milestone uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { var readerOffset = int64(milestone * types.NeedleMapEntrySize) bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead) count, e := r.ReadAt(bytes, readerOffset) diff --git a/weed/storage/needle_map_leveldb.go b/weed/storage/needle_map_leveldb.go index f014797df..c26856ba3 100644 --- a/weed/storage/needle_map_leveldb.go +++ b/weed/storage/needle_map_leveldb.go @@ -20,8 +20,8 @@ import ( . "github.com/chrislusf/seaweedfs/weed/storage/types" ) -//use "2 >> 16" to reduce cpu cost -const milestoneCnt = 40 +//mark it every milestoneCnt operations +const milestoneCnt = 10000 const milestoneKey = 0xffffffffffffffff - 1 type LevelDbNeedleMap struct { @@ -32,24 +32,19 @@ type LevelDbNeedleMap struct { } func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Options) (m *LevelDbNeedleMap, err error) { - glog.V(0).Infof("NewLevelDbNeedleMap pocessing %s...", indexFile.Name()) - db, errd := leveldb.OpenFile(dbFileName, opts) - glog.V(0).Infof("begain %v %s %d", errd, dbFileName, getMileStone(db)) - db.Close() - m = &LevelDbNeedleMap{dbFileName: dbFileName} m.indexFile = indexFile if !isLevelDbFresh(dbFileName, indexFile) { - glog.V(0).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name()) + glog.V(1).Infof("Start to Generate %s from %s", dbFileName, indexFile.Name()) generateLevelDbFile(dbFileName, indexFile) - glog.V(0).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name()) + glog.V(1).Infof("Finished Generating %s from %s", dbFileName, indexFile.Name()) } if stat, err := indexFile.Stat(); err != nil { glog.Fatalf("stat file %s: %v", indexFile.Name(), err) } else { m.indexFileOffset = stat.Size() } - glog.V(0).Infof("Opening %s...", dbFileName) + glog.V(1).Infof("Opening %s...", dbFileName) if m.db, err = leveldb.OpenFile(dbFileName, opts); err != nil { if errors.IsCorrupted(err) { @@ -59,19 +54,18 @@ func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Option return } } - glog.V(0).Infof("getMileStone %s : %d", dbFileName, getMileStone(m.db)) + glog.V(1).Infof("Loading %s... , milestone: %d", dbFileName, getMileStone(m.db)) m.recordNum = uint64(m.indexFileOffset / types.NeedleMapEntrySize) milestone := (m.recordNum / milestoneCnt) * milestoneCnt err = setMileStone(m.db, milestone) if err != nil { + glog.Fatalf("set milestone for %s error: %s\n", dbFileName, err) return } - glog.V(0).Infof("Loading %s... %d %d", indexFile.Name(), milestone, getMileStone(m.db)) mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile) if indexLoadError != nil { return nil, indexLoadError } - glog.V(0).Infof("finish Loading %s...", indexFile.Name()) m.mapMetric = *mm return } @@ -106,14 +100,13 @@ func generateLevelDbFile(dbFileName string, indexFile *os.File) error { return err } else { if milestone*types.NeedleMapEntrySize > uint64(stat.Size()) { - glog.Warningf("wrong milestone %d for filesize %d, set milestone to 0", milestone, stat.Size()) - milestone = 0 + glog.Warningf("wrong milestone %d for filesize %d", milestone, stat.Size()) } glog.V(0).Infof("generateLevelDbFile %s, milestone %d, num of entries:%d", dbFileName, milestone, (uint64(stat.Size())-milestone*types.NeedleMapEntrySize)/types.NeedleMapEntrySize) } - return idx.WalkIndexFileIncrent(indexFile, milestone, func(key NeedleId, offset Offset, size Size) error { + return idx.WalkIndexFileIncrement(indexFile, milestone, func(key NeedleId, offset Offset, size Size) error { if !offset.IsZero() && size.IsValid() { - levelDbWrite(db, key, offset, size, 0) + levelDbWrite(db, key, offset, size, false, 0) } else { levelDbDelete(db, key) } @@ -144,16 +137,14 @@ func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error { if err := m.appendToIndexFile(key, offset, size); err != nil { return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err) } - //atomic.AddUint64(&m.recordNum, 1) - //milestone = atomic.LoadUint64(&m.recordNum) m.recordNum++ if m.recordNum%milestoneCnt != 0 { milestone = 0 } else { milestone = (m.recordNum / milestoneCnt) * milestoneCnt - glog.V(0).Infof("put cnt:%d milestone:%s %d", m.recordNum, m.dbFileName, milestone) + glog.V(1).Infof("put cnt:%d for %s,milestone: %d", m.recordNum, m.dbFileName, milestone) } - return levelDbWrite(m.db, key, offset, size, milestone) + return levelDbWrite(m.db, key, offset, size, milestone == 0, milestone) } func getMileStone(db *leveldb.DB) uint64 { @@ -175,7 +166,7 @@ func getMileStone(db *leveldb.DB) uint64 { } func setMileStone(db *leveldb.DB, milestone uint64) error { - glog.V(0).Infof("set milestone %d", milestone) + glog.V(1).Infof("set milestone %d", milestone) var mskBytes = make([]byte, 8) util.Uint64toBytes(mskBytes, milestoneKey) var msBytes = make([]byte, 8) @@ -183,11 +174,10 @@ func setMileStone(db *leveldb.DB, milestone uint64) error { if err := db.Put(mskBytes, msBytes, nil); err != nil { return fmt.Errorf("failed to setMileStone: %v", err) } - glog.V(0).Infof("ssset milestone %d, %d", milestone, getMileStone(db)) return nil } -func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, milestone uint64) error { +func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, upateMilstone bool, milestone uint64) error { bytes := needle_map.ToBytes(key, offset, size) @@ -195,8 +185,7 @@ func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, milest return fmt.Errorf("failed to write leveldb: %v", err) } // set milestone - if milestone != 0 { - glog.V(0).Infof("actually set milestone %d", milestone) + if upateMilstone { return setMileStone(db, milestone) } return nil @@ -219,16 +208,13 @@ func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error { if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil { return err } - //atomic.AddUint64(&m.recordNum, 1) - //milestone = atomic.LoadUint64(&m.recordNum) m.recordNum++ if m.recordNum%milestoneCnt != 0 { milestone = 0 } else { milestone = (m.recordNum / milestoneCnt) * milestoneCnt } - glog.V(0).Infof("delete cnt:%d milestone:%s %d", m.recordNum, m.dbFileName, milestone) - return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size, milestone) + return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size, milestone == 0, milestone) } func (m *LevelDbNeedleMap) Close() { From 91285bb51d3b95befb5aba0104f6ac83e5b07572 Mon Sep 17 00:00:00 2001 From: guol-fnst Date: Tue, 19 Jul 2022 20:00:44 +0800 Subject: [PATCH 3/4] remove uncessary code --- weed/storage/needle_map_leveldb.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/weed/storage/needle_map_leveldb.go b/weed/storage/needle_map_leveldb.go index c26856ba3..f41524a97 100644 --- a/weed/storage/needle_map_leveldb.go +++ b/weed/storage/needle_map_leveldb.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/opt" @@ -153,13 +152,14 @@ func getMileStone(db *leveldb.DB) uint64 { data, err := db.Get(mskBytes, nil) if err != nil || len(data) != 8 { glog.Warningf("get milestone from db error: %v, %d", err, len(data)) - if !strings.Contains(strings.ToLower(err.Error()), "not found") { - err = setMileStone(db, 0) - if err != nil { - glog.Errorf("failed to set milestone: %v", err) + /* + if !strings.Contains(strings.ToLower(err.Error()), "not found") { + err = setMileStone(db, 0) + if err != nil { + glog.Errorf("failed to set milestone: %v", err) + } } - } - + */ return 0 } return util.BytesToUint64(data) From ac694f0c8f2b5456bd175e1288023f0bdb5b7a14 Mon Sep 17 00:00:00 2001 From: guol-fnst Date: Wed, 20 Jul 2022 09:26:06 +0800 Subject: [PATCH 4/4] rename parameter and reuse functions rename milestone to watermark --- .../diff_volume_servers.go | 9 +- unmaintained/see_idx/see_idx.go | 5 +- weed/storage/erasure_coding/ec_encoder.go | 2 +- weed/storage/idx/walk.go | 38 +------- weed/storage/needle_map/memdb.go | 2 +- weed/storage/needle_map_leveldb.go | 93 +++++++++---------- weed/storage/needle_map_memory.go | 2 +- 7 files changed, 58 insertions(+), 93 deletions(-) diff --git a/unmaintained/diff_volume_servers/diff_volume_servers.go b/unmaintained/diff_volume_servers/diff_volume_servers.go index 0188d18d4..815eeae54 100644 --- a/unmaintained/diff_volume_servers/diff_volume_servers.go +++ b/unmaintained/diff_volume_servers/diff_volume_servers.go @@ -6,6 +6,10 @@ import ( "errors" "flag" "fmt" + "io" + "math" + "os" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/operation" "github.com/chrislusf/seaweedfs/weed/pb" @@ -16,9 +20,6 @@ import ( "github.com/chrislusf/seaweedfs/weed/storage/types" "github.com/chrislusf/seaweedfs/weed/util" "google.golang.org/grpc" - "io" - "math" - "os" ) var ( @@ -155,7 +156,7 @@ func getVolumeFiles(v uint32, addr pb.ServerAddress) (map[types.NeedleId]needleS var maxOffset int64 files := map[types.NeedleId]needleState{} - err = idx.WalkIndexFile(idxFile, func(key types.NeedleId, offset types.Offset, size types.Size) error { + err = idx.WalkIndexFile(idxFile, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error { if offset.IsZero() || size.IsDeleted() { files[key] = needleState{ state: stateDeleted, diff --git a/unmaintained/see_idx/see_idx.go b/unmaintained/see_idx/see_idx.go index 22c659351..616263b1c 100644 --- a/unmaintained/see_idx/see_idx.go +++ b/unmaintained/see_idx/see_idx.go @@ -3,11 +3,12 @@ package main import ( "flag" "fmt" - "github.com/chrislusf/seaweedfs/weed/util" "os" "path" "strconv" + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/storage/idx" "github.com/chrislusf/seaweedfs/weed/storage/types" @@ -36,7 +37,7 @@ func main() { } defer indexFile.Close() - idx.WalkIndexFile(indexFile, func(key types.NeedleId, offset types.Offset, size types.Size) error { + idx.WalkIndexFile(indexFile, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error { fmt.Printf("key:%v offset:%v size:%v(%v)\n", key, offset, size, util.BytesToHumanReadable(uint64(size))) return nil }) diff --git a/weed/storage/erasure_coding/ec_encoder.go b/weed/storage/erasure_coding/ec_encoder.go index 157149865..ea331ca39 100644 --- a/weed/storage/erasure_coding/ec_encoder.go +++ b/weed/storage/erasure_coding/ec_encoder.go @@ -294,7 +294,7 @@ func readNeedleMap(baseFileName string) (*needle_map.MemDb, error) { defer indexFile.Close() cm := needle_map.NewMemDb() - err = idx.WalkIndexFile(indexFile, func(key types.NeedleId, offset types.Offset, size types.Size) error { + err = idx.WalkIndexFile(indexFile, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error { if !offset.IsZero() && size != types.TombstoneFileSize { cm.Set(key, offset, size) } else { diff --git a/weed/storage/idx/walk.go b/weed/storage/idx/walk.go index 74cb83b45..70d3855ea 100644 --- a/weed/storage/idx/walk.go +++ b/weed/storage/idx/walk.go @@ -9,42 +9,8 @@ import ( // walks through the index file, calls fn function with each key, offset, size // stops with the error returned by the fn function -func WalkIndexFile(r io.ReaderAt, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { - var readerOffset int64 - bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead) - count, e := r.ReadAt(bytes, readerOffset) - if count == 0 && e == io.EOF { - return nil - } - glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e) - readerOffset += int64(count) - var ( - key types.NeedleId - offset types.Offset - size types.Size - i int - ) - - for count > 0 && e == nil || e == io.EOF { - for i = 0; i+types.NeedleMapEntrySize <= count; i += types.NeedleMapEntrySize { - key, offset, size = IdxFileEntry(bytes[i : i+types.NeedleMapEntrySize]) - if e = fn(key, offset, size); e != nil { - return e - } - } - if e == io.EOF { - return nil - } - count, e = r.ReadAt(bytes, readerOffset) - glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e) - readerOffset += int64(count) - } - return e -} - -//copied from WalkIndexFile, just init readerOffset from milestone -func WalkIndexFileIncrement(r io.ReaderAt, milestone uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { - var readerOffset = int64(milestone * types.NeedleMapEntrySize) +func WalkIndexFile(r io.ReaderAt, startFrom uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error { + readerOffset := int64(startFrom * types.NeedleMapEntrySize) bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead) count, e := r.ReadAt(bytes, readerOffset) if count == 0 && e == io.EOF { diff --git a/weed/storage/needle_map/memdb.go b/weed/storage/needle_map/memdb.go index ba1fd3d1e..a362a85ae 100644 --- a/weed/storage/needle_map/memdb.go +++ b/weed/storage/needle_map/memdb.go @@ -111,7 +111,7 @@ func (cm *MemDb) LoadFromIdx(idxName string) (ret error) { func (cm *MemDb) LoadFromReaderAt(readerAt io.ReaderAt) (ret error) { - return idx.WalkIndexFile(readerAt, func(key NeedleId, offset Offset, size Size) error { + return idx.WalkIndexFile(readerAt, 0, func(key NeedleId, offset Offset, size Size) error { if offset.IsZero() || size.IsDeleted() { return cm.Delete(key) } diff --git a/weed/storage/needle_map_leveldb.go b/weed/storage/needle_map_leveldb.go index f41524a97..a1934b8f1 100644 --- a/weed/storage/needle_map_leveldb.go +++ b/weed/storage/needle_map_leveldb.go @@ -19,15 +19,16 @@ import ( . "github.com/chrislusf/seaweedfs/weed/storage/types" ) -//mark it every milestoneCnt operations -const milestoneCnt = 10000 -const milestoneKey = 0xffffffffffffffff - 1 +//mark it every watermarkBatchSize operations +const watermarkBatchSize = 10000 + +var watermarkKey = []byte("idx_entry_watermark") type LevelDbNeedleMap struct { baseNeedleMapper - dbFileName string - db *leveldb.DB - recordNum uint64 + dbFileName string + db *leveldb.DB + recordCount uint64 } func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Options) (m *LevelDbNeedleMap, err error) { @@ -53,12 +54,12 @@ func NewLevelDbNeedleMap(dbFileName string, indexFile *os.File, opts *opt.Option return } } - glog.V(1).Infof("Loading %s... , milestone: %d", dbFileName, getMileStone(m.db)) - m.recordNum = uint64(m.indexFileOffset / types.NeedleMapEntrySize) - milestone := (m.recordNum / milestoneCnt) * milestoneCnt - err = setMileStone(m.db, milestone) + glog.V(0).Infof("Loading %s... , watermark: %d", dbFileName, getWatermark(m.db)) + m.recordCount = uint64(m.indexFileOffset / types.NeedleMapEntrySize) + watermark := (m.recordCount / watermarkBatchSize) * watermarkBatchSize + err = setWatermark(m.db, watermark) if err != nil { - glog.Fatalf("set milestone for %s error: %s\n", dbFileName, err) + glog.Fatalf("set watermark for %s error: %s\n", dbFileName, err) return } mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile) @@ -93,17 +94,17 @@ func generateLevelDbFile(dbFileName string, indexFile *os.File) error { } defer db.Close() - milestone := getMileStone(db) + watermark := getWatermark(db) if stat, err := indexFile.Stat(); err != nil { glog.Fatalf("stat file %s: %v", indexFile.Name(), err) return err } else { - if milestone*types.NeedleMapEntrySize > uint64(stat.Size()) { - glog.Warningf("wrong milestone %d for filesize %d", milestone, stat.Size()) + if watermark*types.NeedleMapEntrySize > uint64(stat.Size()) { + glog.Warningf("wrong watermark %d for filesize %d", watermark, stat.Size()) } - glog.V(0).Infof("generateLevelDbFile %s, milestone %d, num of entries:%d", dbFileName, milestone, (uint64(stat.Size())-milestone*types.NeedleMapEntrySize)/types.NeedleMapEntrySize) + glog.V(0).Infof("generateLevelDbFile %s, watermark %d, num of entries:%d", dbFileName, watermark, (uint64(stat.Size())-watermark*types.NeedleMapEntrySize)/types.NeedleMapEntrySize) } - return idx.WalkIndexFileIncrement(indexFile, milestone, func(key NeedleId, offset Offset, size Size) error { + return idx.WalkIndexFile(indexFile, watermark, func(key NeedleId, offset Offset, size Size) error { if !offset.IsZero() && size.IsValid() { levelDbWrite(db, key, offset, size, false, 0) } else { @@ -127,7 +128,7 @@ func (m *LevelDbNeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, o func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error { var oldSize Size - var milestone uint64 + var watermark uint64 if oldNeedle, ok := m.Get(key); ok { oldSize = oldNeedle.Size } @@ -136,27 +137,25 @@ func (m *LevelDbNeedleMap) Put(key NeedleId, offset Offset, size Size) error { if err := m.appendToIndexFile(key, offset, size); err != nil { return fmt.Errorf("cannot write to indexfile %s: %v", m.indexFile.Name(), err) } - m.recordNum++ - if m.recordNum%milestoneCnt != 0 { - milestone = 0 + m.recordCount++ + if m.recordCount%watermarkBatchSize != 0 { + watermark = 0 } else { - milestone = (m.recordNum / milestoneCnt) * milestoneCnt - glog.V(1).Infof("put cnt:%d for %s,milestone: %d", m.recordNum, m.dbFileName, milestone) + watermark = (m.recordCount / watermarkBatchSize) * watermarkBatchSize + glog.V(1).Infof("put cnt:%d for %s,watermark: %d", m.recordCount, m.dbFileName, watermark) } - return levelDbWrite(m.db, key, offset, size, milestone == 0, milestone) + return levelDbWrite(m.db, key, offset, size, watermark == 0, watermark) } -func getMileStone(db *leveldb.DB) uint64 { - var mskBytes = make([]byte, 8) - util.Uint64toBytes(mskBytes, milestoneKey) - data, err := db.Get(mskBytes, nil) +func getWatermark(db *leveldb.DB) uint64 { + data, err := db.Get(watermarkKey, nil) if err != nil || len(data) != 8 { - glog.Warningf("get milestone from db error: %v, %d", err, len(data)) + glog.Warningf("get watermark from db error: %v, %d", err, len(data)) /* if !strings.Contains(strings.ToLower(err.Error()), "not found") { - err = setMileStone(db, 0) + err = setWatermark(db, 0) if err != nil { - glog.Errorf("failed to set milestone: %v", err) + glog.Errorf("failed to set watermark: %v", err) } } */ @@ -165,28 +164,26 @@ func getMileStone(db *leveldb.DB) uint64 { return util.BytesToUint64(data) } -func setMileStone(db *leveldb.DB, milestone uint64) error { - glog.V(1).Infof("set milestone %d", milestone) - var mskBytes = make([]byte, 8) - util.Uint64toBytes(mskBytes, milestoneKey) - var msBytes = make([]byte, 8) - util.Uint64toBytes(msBytes, milestone) - if err := db.Put(mskBytes, msBytes, nil); err != nil { - return fmt.Errorf("failed to setMileStone: %v", err) +func setWatermark(db *leveldb.DB, watermark uint64) error { + glog.V(1).Infof("set watermark %d", watermark) + var wmBytes = make([]byte, 8) + util.Uint64toBytes(wmBytes, watermark) + if err := db.Put(watermarkKey, wmBytes, nil); err != nil { + return fmt.Errorf("failed to setWatermark: %v", err) } return nil } -func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, upateMilstone bool, milestone uint64) error { +func levelDbWrite(db *leveldb.DB, key NeedleId, offset Offset, size Size, updateWatermark bool, watermark uint64) error { bytes := needle_map.ToBytes(key, offset, size) if err := db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil { return fmt.Errorf("failed to write leveldb: %v", err) } - // set milestone - if upateMilstone { - return setMileStone(db, milestone) + // set watermark + if updateWatermark { + return setWatermark(db, watermark) } return nil } @@ -197,7 +194,7 @@ func levelDbDelete(db *leveldb.DB, key NeedleId) error { } func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error { - var milestone uint64 + var watermark uint64 oldNeedle, found := m.Get(key) if !found || oldNeedle.Size.IsDeleted() { return nil @@ -208,13 +205,13 @@ func (m *LevelDbNeedleMap) Delete(key NeedleId, offset Offset) error { if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil { return err } - m.recordNum++ - if m.recordNum%milestoneCnt != 0 { - milestone = 0 + m.recordCount++ + if m.recordCount%watermarkBatchSize != 0 { + watermark = 0 } else { - milestone = (m.recordNum / milestoneCnt) * milestoneCnt + watermark = (m.recordCount / watermarkBatchSize) * watermarkBatchSize } - return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size, milestone == 0, milestone) + return levelDbWrite(m.db, key, oldNeedle.Offset, -oldNeedle.Size, watermark == 0, watermark) } func (m *LevelDbNeedleMap) Close() { diff --git a/weed/storage/needle_map_memory.go b/weed/storage/needle_map_memory.go index 1b58708c6..4c7909dbd 100644 --- a/weed/storage/needle_map_memory.go +++ b/weed/storage/needle_map_memory.go @@ -33,7 +33,7 @@ func LoadCompactNeedleMap(file *os.File) (*NeedleMap, error) { } func doLoading(file *os.File, nm *NeedleMap) (*NeedleMap, error) { - e := idx.WalkIndexFile(file, func(key NeedleId, offset Offset, size Size) error { + e := idx.WalkIndexFile(file, 0, func(key NeedleId, offset Offset, size Size) error { nm.MaybeSetMaxFileKey(key) if !offset.IsZero() && size.IsValid() { nm.FileCounter++