Skip to content

Commit

Permalink
[Bugfix] fix snapshot files stat error in metanode startup process
Browse files Browse the repository at this point in the history
Signed-off-by: liubingxing <[email protected]>
  • Loading branch information
liubingxing authored and leonrayang committed Feb 1, 2023
1 parent a2c9592 commit 6635dde
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions metanode/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (m *metadataManager) loadPartitions() (err error) {
func (m *metadataManager) attachPartition(id uint64, partition MetaPartition) (err error) {
syslog.Println(fmt.Sprintf("start load metaPartition %v", id))
partition.ForceSetMetaPartitionToLoadding()
if err = partition.Start(); err != nil {
if err = partition.Start(false); err != nil {
msg := fmt.Sprintf("load meta partition %v fail: %v", id, err)
log.LogError(msg)
syslog.Println(msg)
Expand Down Expand Up @@ -456,7 +456,7 @@ func (m *metadataManager) createPartition(request *proto.CreateMetaPartitionRequ
return
}

if err = partition.Start(); err != nil {
if err = partition.Start(true); err != nil {
os.RemoveAll(mpc.RootDir)
log.LogErrorf("load meta partition %v fail: %v", request.PartitionID, err)
err = errors.NewErrorf("[createPartition]->%s", err.Error())
Expand Down
18 changes: 12 additions & 6 deletions metanode/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ type OpPartition interface {

// MetaPartition defines the interface for the meta partition operations.
type MetaPartition interface {
Start() error
Start(isCreate bool) error
Stop()
DataSize() uint64
GetFreeListLen() int
Expand Down Expand Up @@ -284,7 +284,7 @@ func (mp *metaPartition) GetFreeListLen() int {
}

// Start starts a meta partition.
func (mp *metaPartition) Start() (err error) {
func (mp *metaPartition) Start(isCreate bool) (err error) {
if atomic.CompareAndSwapUint32(&mp.state, common.StateStandby, common.StateStart) {
defer func() {
var newState uint32
Expand All @@ -298,7 +298,7 @@ func (mp *metaPartition) Start() (err error) {
if mp.config.BeforeStart != nil {
mp.config.BeforeStart()
}
if err = mp.onStart(); err != nil {
if err = mp.onStart(isCreate); err != nil {
err = errors.NewErrorf("[Start]->%s", err.Error())
return
}
Expand All @@ -325,14 +325,14 @@ func (mp *metaPartition) Stop() {
}
}

func (mp *metaPartition) onStart() (err error) {
func (mp *metaPartition) onStart(isCreate bool) (err error) {
defer func() {
if err == nil {
return
}
mp.onStop()
}()
if err = mp.load(); err != nil {
if err = mp.load(isCreate); err != nil {
err = errors.NewErrorf("[onStart] load partition id=%d: %s",
mp.config.PartitionId, err.Error())
return
Expand Down Expand Up @@ -557,10 +557,16 @@ func (mp *metaPartition) LoadSnapshot(snapshotPath string) (err error) {
return
}

func (mp *metaPartition) load() (err error) {
func (mp *metaPartition) load(isCreate bool) (err error) {
if err = mp.loadMetadata(); err != nil {
return
}

// create new metaPartition, no need to load snapshot
if isCreate {
return
}

snapshotPath := path.Join(mp.config.RootDir, snapshotDir)
if err = mp.loadInode(snapshotPath); err != nil {
return
Expand Down
24 changes: 10 additions & 14 deletions metanode/partition_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (mp *metaPartition) loadInode(rootDir string) (err error) {
}()
filename := path.Join(rootDir, inodeFile)
if _, err = os.Stat(filename); err != nil {
err = nil
err = errors.NewErrorf("[loadInode] Stat: %s", err.Error())
return
}
fp, err := os.OpenFile(filename, os.O_RDONLY, 0644)
Expand Down Expand Up @@ -160,15 +160,11 @@ func (mp *metaPartition) loadDentry(rootDir string) (err error) {
}()
filename := path.Join(rootDir, dentryFile)
if _, err = os.Stat(filename); err != nil {
err = nil
err = errors.NewErrorf("[loadDentry] Stat: %s", err.Error())
return
}
fp, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
if err == os.ErrNotExist {
err = nil
return
}
err = errors.NewErrorf("[loadDentry] OpenFile: %s", err.Error())
return
}
Expand Down Expand Up @@ -219,10 +215,12 @@ func (mp *metaPartition) loadExtend(rootDir string) error {
var err error
filename := path.Join(rootDir, extendFile)
if _, err = os.Stat(filename); err != nil {
return nil
err = errors.NewErrorf("[loadExtend] Stat: %s", err.Error())
return err
}
fp, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
err = errors.NewErrorf("[loadExtend] OpenFile: %s", err.Error())
return err
}
defer func() {
Expand Down Expand Up @@ -263,10 +261,12 @@ func (mp *metaPartition) loadMultipart(rootDir string) error {
var err error
filename := path.Join(rootDir, multipartFile)
if _, err = os.Stat(filename); err != nil {
return nil
err = errors.NewErrorf("[loadMultipart] Stat: %s", err.Error())
return err
}
fp, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
err = errors.NewErrorf("[loadMultipart] OpenFile: %s", err.Error())
return err
}
defer func() {
Expand Down Expand Up @@ -303,16 +303,12 @@ func (mp *metaPartition) loadMultipart(rootDir string) error {
func (mp *metaPartition) loadApplyID(rootDir string) (err error) {
filename := path.Join(rootDir, applyIDFile)
if _, err = os.Stat(filename); err != nil {
err = nil
err = errors.NewErrorf("[loadApplyID]: Stat %s", err.Error())
return
}
data, err := ioutil.ReadFile(filename)
if err != nil {
if err == os.ErrNotExist {
err = nil
return
}
err = errors.NewErrorf("[loadApplyID] OpenFile: %s", err.Error())
err = errors.NewErrorf("[loadApplyID] ReadFile: %s", err.Error())
return
}
if len(data) == 0 {
Expand Down

0 comments on commit 6635dde

Please sign in to comment.