Skip to content

Commit d54720f

Browse files
committed
recover from panic when scale in replicas
Signed-off-by: mahjonp <[email protected]>
1 parent e0bdaf8 commit d54720f

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tpcc/workload.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,37 @@ func getTPCCState(ctx context.Context) *tpccState {
231231
}
232232

233233
// Run implements Workloader interface
234-
func (w *Workloader) Run(ctx context.Context, threadID int) error {
234+
func (w *Workloader) Run(ctx context.Context, threadID int) (err error) {
235+
defer func() {
236+
if r := recover(); r != nil {
237+
err = fmt.Errorf("panic in TPC-C Run (thread %d): %v", threadID, r)
238+
}
239+
}()
240+
235241
s := getTPCCState(ctx)
236242
refreshConn := false
237243

244+
// Helper function to safely refresh connection with panic recovery
245+
safeRefreshConn := func() error {
246+
defer func() {
247+
if r := recover(); r != nil {
248+
err = fmt.Errorf("panic during connection refresh (thread %d): %v", threadID, r)
249+
}
250+
}()
251+
return s.RefreshConn(ctx)
252+
}
253+
238254
// Check if automatic connection refresh is needed
239255
if w.cfg.ConnRefreshInterval > 0 && time.Since(s.lastConnRefresh) >= w.cfg.ConnRefreshInterval {
240-
if err := s.RefreshConn(ctx); err != nil {
241-
return err
256+
if err := safeRefreshConn(); err != nil {
257+
return fmt.Errorf("automatic connection refresh failed (thread %d): %w", threadID, err)
242258
}
243259
s.lastConnRefresh = time.Now()
244260
refreshConn = true
245261
} else if err := s.Conn.PingContext(ctx); err != nil {
246262
// Fallback to ping-based refresh if automatic refresh didn't happen
247-
if err := s.RefreshConn(ctx); err != nil {
248-
return err
263+
if err := safeRefreshConn(); err != nil {
264+
return fmt.Errorf("ping-based connection refresh failed (thread %d): %w", threadID, err)
249265
}
250266
s.lastConnRefresh = time.Now()
251267
refreshConn = true
@@ -325,7 +341,7 @@ func (w *Workloader) Run(ctx context.Context, threadID int) error {
325341
}
326342

327343
start := time.Now()
328-
err := txn.action(ctx, threadID)
344+
err = txn.action(ctx, threadID)
329345

330346
w.rtMeasurement.Measure(txn.name, time.Now().Sub(start), err)
331347

0 commit comments

Comments
 (0)