Skip to content

Commit 1bd3500

Browse files
committed
fixes: Queries continuing after timeout in CH benchmark
1 parent c71da30 commit 1bd3500

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

ch/workload.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,14 @@ func (w *Workloader) Run(ctx context.Context, threadID int) error {
207207
defer w.updateState(ctx)
208208

209209
if err := s.Conn.PingContext(ctx); err != nil {
210-
time.Sleep(w.cfg.RefreshConnWait) // I feel it silly to sleep, but don't come up with better idea
210+
select {
211+
case <-time.After(w.cfg.RefreshConnWait):
212+
// Sleep completed normally
213+
case <-ctx.Done():
214+
// Context was cancelled or timed out during sleep
215+
return ctx.Err()
216+
}
217+
211218
if err := s.RefreshConn(ctx); err != nil {
212219
return err
213220
}
@@ -226,8 +233,14 @@ func (w *Workloader) Run(ctx context.Context, threadID int) error {
226233
}
227234
start := time.Now()
228235
rows, err := s.Conn.QueryContext(ctx, query)
229-
defer w.measurement.Measure(queryName, time.Now().Sub(start), err)
236+
defer func() {
237+
w.measurement.Measure(queryName, time.Since(start), err)
238+
}()
230239
if err != nil {
240+
// Check if error is due to context cancellation/timeout
241+
if ctx.Err() != nil {
242+
return fmt.Errorf("query %s cancelled due to timeout: %v", queryName, ctx.Err())
243+
}
231244
return fmt.Errorf("execute query %s failed %v", queryName, err)
232245
}
233246
defer rows.Close()
@@ -237,7 +250,7 @@ func (w *Workloader) Run(ctx context.Context, threadID int) error {
237250
if err != nil {
238251
return err
239252
}
240-
util.StdErrLogger.Printf("explain analyze result of query %s (takes %s):\n%s\n", queryName, time.Now().Sub(start), table)
253+
util.StdErrLogger.Printf("explain analyze result of query %s (takes %s):\n%s\n", queryName, time.Since(start), table)
241254
return nil
242255
}
243256
if err := w.drainQueryResult(queryName, rows); err != nil {

cmd/go-tpc/misc.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ func checkPrepare(ctx context.Context, w workload.Workloader) {
4040
func execute(timeoutCtx context.Context, w workload.Workloader, action string, threads, index int) error {
4141
count := totalCount / threads
4242

43-
ctx := w.InitThread(context.Background(), index)
43+
// For prepare, cleanup and check operations, use background context to avoid timeout constraints
44+
// Only run phases should be limited by timeout
45+
var ctx context.Context
46+
if action == "prepare" || action == "cleanup" || action == "check" {
47+
ctx = w.InitThread(context.Background(), index)
48+
} else {
49+
ctx = w.InitThread(timeoutCtx, index)
50+
}
4451
defer w.CleanupThread(ctx, index)
4552

4653
switch action {
@@ -58,21 +65,37 @@ func execute(timeoutCtx context.Context, w workload.Workloader, action string, t
5865
return w.Check(ctx, index)
5966
}
6067

68+
// This loop is only reached for "run" action since other actions return earlier
6169
for i := 0; i < count || count <= 0; i++ {
70+
// Check if timeout has occurred before starting next query
71+
select {
72+
case <-ctx.Done():
73+
if !silence {
74+
fmt.Printf("[%s] %s worker %d stopped due to timeout after %d iterations\n",
75+
time.Now().Format("2006-01-02 15:04:05"), action, index, i)
76+
}
77+
return nil
78+
default:
79+
}
80+
6281
err := w.Run(ctx, index)
6382
if err != nil {
83+
// Check if the error is due to timeout/cancellation
84+
if ctx.Err() != nil {
85+
if !silence {
86+
fmt.Printf("[%s] %s worker %d stopped due to timeout: %v\n",
87+
time.Now().Format("2006-01-02 15:04:05"), action, index, err)
88+
}
89+
return nil // Don't treat timeout as an error
90+
}
91+
6492
if !silence {
6593
fmt.Printf("[%s] execute %s failed, err %v\n", time.Now().Format("2006-01-02 15:04:05"), action, err)
6694
}
6795
if !ignoreError {
6896
return err
6997
}
7098
}
71-
select {
72-
case <-timeoutCtx.Done():
73-
return nil
74-
default:
75-
}
7699
}
77100

78101
return nil

0 commit comments

Comments
 (0)