Skip to content

Commit 02f5e23

Browse files
authored
br: use online to disable import mode (#70221)
close #63505
1 parent 3e9723f commit 02f5e23

8 files changed

Lines changed: 95 additions & 23 deletions

File tree

br/pkg/restore/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ go_test(
6060
],
6161
embed = [":restore"],
6262
flaky = True,
63-
shard_count = 21,
63+
shard_count = 22,
6464
deps = [
6565
"//br/pkg/conn",
6666
"//br/pkg/mock",

br/pkg/restore/import_mode_switcher.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,17 @@ func RestorePostWork(
242242
ctx context.Context,
243243
switcher *ImportModeSwitcher,
244244
restoreSchedulers pdutil.UndoFunc,
245+
isOnline bool,
245246
) {
246247
if ctx.Err() != nil {
247248
log.Warn("context canceled, try shutdown")
248249
ctx = context.Background()
249250
}
250251

251-
if err := switcher.SwitchToNormalMode(ctx); err != nil {
252-
log.Warn("fail to switch to normal mode", zap.Error(err))
252+
if !isOnline {
253+
if err := switcher.SwitchToNormalMode(ctx); err != nil {
254+
log.Warn("fail to switch to normal mode", zap.Error(err))
255+
}
253256
}
254257
if err := restoreSchedulers(ctx); err != nil {
255258
log.Warn("failed to restore PD schedulers", zap.Error(err))

br/pkg/restore/import_mode_switcher_test.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,29 @@ import (
3535
type mockImportServer struct {
3636
import_sstpb.ImportSSTServer
3737

38+
mu sync.Mutex
39+
modes []import_sstpb.SwitchMode
3840
count int
3941
ch chan struct{}
4042
}
4143

4244
func (s *mockImportServer) SwitchMode(_ context.Context, req *import_sstpb.SwitchModeRequest) (*import_sstpb.SwitchModeResponse, error) {
45+
s.mu.Lock()
46+
defer s.mu.Unlock()
47+
s.modes = append(s.modes, req.GetMode())
4348
s.count -= 1
44-
if s.count == 0 {
49+
if s.count == 0 && s.ch != nil {
4550
s.ch <- struct{}{}
4651
}
4752
return &import_sstpb.SwitchModeResponse{}, nil
4853
}
4954

55+
func (s *mockImportServer) Modes() []import_sstpb.SwitchMode {
56+
s.mu.Lock()
57+
defer s.mu.Unlock()
58+
return append([]import_sstpb.SwitchMode(nil), s.modes...)
59+
}
60+
5061
func TestRestorePreWork(t *testing.T) {
5162
ctx := context.Background()
5263
lis, err := net.Listen("tcp", ":0")
@@ -62,7 +73,9 @@ func TestRestorePreWork(t *testing.T) {
6273
go func() {
6374
defer wg.Done()
6475
err := s.Serve(lis)
65-
require.NoError(t, err)
76+
if err != nil && err != grpc.ErrServerStopped {
77+
require.NoError(t, err)
78+
}
6679
}()
6780

6881
pdClient := split.NewFakePDClient([]*metapb.Store{
@@ -109,7 +122,7 @@ func TestRestorePreWork(t *testing.T) {
109122
}
110123
}
111124
<-ch
112-
restore.RestorePostWork(ctx, switcher, undo)
125+
restore.RestorePostWork(ctx, switcher, undo, false)
113126
// check the cfg done
114127
{
115128
cfgs, err := pdHTTPCli.GetConfig(context.TODO())
@@ -126,4 +139,53 @@ func TestRestorePreWork(t *testing.T) {
126139

127140
s.Stop()
128141
lis.Close()
142+
wg.Wait()
143+
}
144+
145+
func TestRestorePostWorkOnlineSkipsNormalMode(t *testing.T) {
146+
ctx := context.Background()
147+
lis, err := net.Listen("tcp", ":0")
148+
require.NoError(t, err)
149+
addr := lis.Addr().String()
150+
151+
s := grpc.NewServer()
152+
importServer := &mockImportServer{}
153+
import_sstpb.RegisterImportSSTServer(s, importServer)
154+
155+
var wg sync.WaitGroup
156+
wg.Add(1)
157+
go func() {
158+
defer wg.Done()
159+
err := s.Serve(lis)
160+
if err != nil && err != grpc.ErrServerStopped {
161+
require.NoError(t, err)
162+
}
163+
}()
164+
defer func() {
165+
s.Stop()
166+
lis.Close()
167+
wg.Wait()
168+
}()
169+
170+
pdClient := split.NewFakePDClient([]*metapb.Store{
171+
{
172+
Id: 1,
173+
Address: addr,
174+
},
175+
}, false, nil)
176+
switcher := restore.NewImportModeSwitcher(pdClient, time.Hour, nil)
177+
require.NoError(t, switcher.GoSwitchToImportMode(ctx))
178+
defer func() {
179+
require.NoError(t, switcher.SwitchToNormalMode(ctx))
180+
}()
181+
require.Equal(t, []import_sstpb.SwitchMode{import_sstpb.SwitchMode_Import}, importServer.Modes())
182+
183+
restoredSchedulers := false
184+
restore.RestorePostWork(ctx, switcher, func(context.Context) error {
185+
restoredSchedulers = true
186+
return nil
187+
}, true)
188+
189+
require.True(t, restoredSchedulers)
190+
require.Equal(t, []import_sstpb.SwitchMode{import_sstpb.SwitchMode_Import}, importServer.Modes())
129191
}

br/pkg/restore/log_client/client.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ func (rc *LogClient) RestoreSSTFileSets(
372372
ctx context.Context,
373373
backupFileSets restore.BatchBackupFileSet,
374374
importModeSwitcher *restore.ImportModeSwitcher,
375+
online bool,
375376
snapshotRestoreDataSize uint64,
376377
checkpointCompactedSSTSize uint64,
377378
onProgress func(int64),
@@ -390,16 +391,18 @@ func (rc *LogClient) RestoreSSTFileSets(
390391
return errors.Trace(err)
391392
}
392393

393-
err := importModeSwitcher.GoSwitchToImportMode(ctx)
394-
if err != nil {
395-
return errors.Trace(err)
396-
}
397-
defer func() {
398-
switchErr := importModeSwitcher.SwitchToNormalMode(ctx)
399-
if switchErr != nil {
400-
log.Warn("[Compacted SST Restore] Failed to switch back to normal mode after restoration.", zap.Error(switchErr))
394+
if !online {
395+
err := importModeSwitcher.GoSwitchToImportMode(ctx)
396+
if err != nil {
397+
return errors.Trace(err)
401398
}
402-
}()
399+
defer func() {
400+
switchErr := importModeSwitcher.SwitchToNormalMode(ctx)
401+
if switchErr != nil {
402+
log.Warn("[Compacted SST Restore] Failed to switch back to normal mode after restoration.", zap.Error(switchErr))
403+
}
404+
}()
405+
}
403406

404407
log.Info("[Compacted SST Restore] Start to restore SST files",
405408
zap.Int("sst-file-count", len(backupFileSets)))
@@ -413,21 +416,24 @@ func (rc *LogClient) RestoreSSTFileSets(
413416
// where batch processing may lead to increased complexity and potential inefficiencies.
414417
// TODO: Future enhancements may explore the feasibility of reintroducing batch restoration
415418
// while maintaining optimal performance and resource utilization.
416-
err = rc.sstRestoreManager.restorer.GoRestore(onProgress, backupFileSets)
417-
if err != nil {
419+
if err := rc.sstRestoreManager.restorer.GoRestore(onProgress, backupFileSets); err != nil {
418420
return errors.Trace(err)
419421
}
420-
err = rc.sstRestoreManager.restorer.WaitUntilFinish()
422+
err := rc.sstRestoreManager.restorer.WaitUntilFinish()
421423

424+
var totalKvs, totalBytes, totalSize uint64
422425
for _, files := range backupFileSets {
423426
for _, f := range files.SSTFiles {
424-
log.Info("Collected file.", zap.Uint64("total_kv", f.TotalKvs), zap.Uint64("total_bytes", f.TotalBytes), zap.Uint64("size", f.Size_))
427+
totalKvs += f.TotalKvs
428+
totalBytes += f.TotalBytes
429+
totalSize += f.Size_
425430
atomic.AddUint64(&rc.restoreStat.restoreSSTKVCount, f.TotalKvs)
426431
atomic.AddUint64(&rc.restoreStat.restoreSSTKVSize, f.TotalBytes)
427432
atomic.AddUint64(&rc.restoreStat.restoreSSTPhySize, f.Size_)
428433
}
429434
}
430435
atomic.AddUint64(&rc.restoreStat.restoreSSTTakes, uint64(time.Since(begin)))
436+
log.Info("Collected files", zap.Uint64("total_kv", totalKvs), zap.Uint64("total_bytes", totalBytes), zap.Uint64("total_size", totalSize))
431437
return err
432438
}
433439

br/pkg/task/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ func runSnapshotRestore(c context.Context, mgr *conn.Mgr, g glue.Glue, cmdName s
16591659
log.Info("start to restore pd scheduler")
16601660
// run the post-work to avoid being stuck in the import
16611661
// mode or emptied schedulers.
1662-
restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulersFunc)
1662+
restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulersFunc, cfg.Online)
16631663
log.Info("finish restoring pd scheduler")
16641664
}()
16651665

br/pkg/task/restore_raw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func RunRestoreRaw(c context.Context, g glue.Glue, cmdName string, cfg *RestoreR
161161
if err != nil {
162162
return errors.Trace(err)
163163
}
164-
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulers)
164+
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulers, cfg.Online)
165165

166166
start := time.Now()
167167
err = client.GetRestorer(nil).GoRestore(onProgress, restore.CreateUniqueFileSets(files))

br/pkg/task/restore_txn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func RunRestoreTxn(c context.Context, g glue.Glue, cmdName string, cfg *Config)
101101
if err != nil {
102102
return errors.Trace(err)
103103
}
104-
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulers)
104+
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulers, false)
105105

106106
err = client.GetRestorer(nil).GoRestore(onProgress, restore.CreateUniqueFileSets(files))
107107
if err != nil {

br/pkg/task/stream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ func restoreStream(
17421742

17431743
// Always run the post-work even on error, so we don't stuck in the import
17441744
// mode or emptied schedulers
1745-
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulersFunc)
1745+
defer restore.RestorePostWork(ctx, importModeSwitcher, restoreSchedulersFunc, cfg.Online)
17461746

17471747
migs, err := client.GetLockedMigrations(ctx)
17481748
if err != nil {
@@ -1878,6 +1878,7 @@ func restoreStream(
18781878
ctx,
18791879
sstFileSets,
18801880
importModeSwitcher,
1881+
cfg.Online,
18811882
cfg.snapshotRestoreDataSize,
18821883
checkpointSSTSize,
18831884
p.IncBy,

0 commit comments

Comments
 (0)