Repository: matrixorigin/matrixone
Pull request #12942 (2023-11-24 03:51:40)

[bug] BR: fix bug when restore data
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #12891

## What this PR does / why we need it:
there is no default fileservice instance, so we need to get a fileservice
instance explicitly, otherwise, a "service not found" error will be raised.


Patch:
diff --git a/pkg/logservice/service_bootstrap.go b/pkg/logservice/service_bootstrap.go
index 0384c64b6..33f0e61cb 100644
--- a/pkg/logservice/service_bootstrap.go
+++ b/pkg/logservice/service_bootstrap.go
@@ -51,15 +51,20 @@ func (s *Service) BootstrapHAKeeper(ctx context.Context, cfg Config) error {
 	numOfTNShards := cfg.BootstrapConfig.NumOfTNShards
 	numOfLogReplicas := cfg.BootstrapConfig.NumOfLogShardReplicas
 
+	fs, err := fileservice.Get[fileservice.FileService](s.fileService, defines.LocalFileServiceName)
+	if err != nil {
+		return err
+	}
+
 	var nextID uint64
 	var nextIDByKey map[string]uint64
-	backup, err := s.getBackupData(ctx)
+	backup, err := s.getBackupData(ctx, fs)
 	if err != nil {
 		return err
 	}
 	if backup != nil { // We are trying to restore from a backup.
 		// If a backup has already been issued, ignore this time.
-		_, err := s.fileService.StatFile(ctx, restoredTagFile)
+		_, err := fs.StatFile(ctx, restoredTagFile)
 		if s.cfg.BootstrapConfig.Restore.Force || // force is true, we do restore whatever.
 			(err != nil && moerr.IsMoErrCode(err, moerr.ErrFileNotFound)) {
 			// Restored tag file does not exist, we can do backup.
@@ -67,7 +72,7 @@ func (s *Service) BootstrapHAKeeper(ctx context.Context, cfg Config) error {
 			nextIDByKey = backup.NextIDByKey
 
 			// After backup, create a restore file.
-			if err := s.fileService.Write(ctx, fileservice.IOVector{
+			if err := fs.Write(ctx, fileservice.IOVector{
 				FilePath: restoredTagFile,
 				Entries: []fileservice.IOEntry{
 					{
@@ -102,11 +107,7 @@ func (s *Service) BootstrapHAKeeper(ctx context.Context, cfg Config) error {
 	return nil
 }
 
-func (s *Service) getBackupData(ctx context.Context) (*pb.BackupData, error) {
-	fs, err := fileservice.Get[fileservice.FileService](s.fileService, defines.LocalFileServiceName)
-	if err != nil {
-		return nil, err
-	}
+func (s *Service) getBackupData(ctx context.Context, fs fileservice.FileService) (*pb.BackupData, error) {
 	filePath := s.cfg.BootstrapConfig.Restore.FilePath
 	if filePath == "" {
 		return nil, nil


Test patch:
diff --git a/pkg/logservice/service_bootstrap_test.go b/pkg/logservice/service_bootstrap_test.go
index 2608d975b..44c113f36 100644
--- a/pkg/logservice/service_bootstrap_test.go
+++ b/pkg/logservice/service_bootstrap_test.go
@@ -50,7 +50,7 @@ func TestGetBackupData(t *testing.T) {
 		fileService: fs,
 	}
 	// If the file do not exist, do not return error.
-	restore, err := s.getBackupData(ctx)
+	restore, err := s.getBackupData(ctx, fs)
 	assert.NoError(t, err)
 	assert.Nil(t, restore)
 
@@ -66,12 +66,12 @@ func TestGetBackupData(t *testing.T) {
 	err = fs.Write(ctx, ioVec)
 	assert.NoError(t, err)
 
-	restore, err = s.getBackupData(ctx)
+	restore, err = s.getBackupData(ctx, fs)
 	assert.NoError(t, err)
 	assert.Nil(t, restore)
 
 	s.cfg.BootstrapConfig.Restore.FilePath = path.Join(dir, name)
-	restore, err = s.getBackupData(ctx)
+	restore, err = s.getBackupData(ctx, fs)
 	assert.NoError(t, err)
 	assert.NotNil(t, restore)
 	assert.Equal(t, nextID, restore.NextID)