Skip to content

Commit 9173047

Browse files
authored
tpcc: refresh conn if pingContext fails (#76)
Signed-off-by: mahjonp <[email protected]>
1 parent 79bc60e commit 9173047

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pkg/workload/base.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ import (
1111

1212
// TpcState saves state for each thread
1313
type TpcState struct {
14+
DB *sql.DB
1415
Conn *sql.Conn
1516

1617
R *rand.Rand
1718

1819
Buf *util.BufAllocator
1920
}
2021

22+
func (t *TpcState) RefreshConn(ctx context.Context) error {
23+
conn, err := t.DB.Conn(ctx)
24+
if err != nil {
25+
return err
26+
}
27+
t.Conn = conn
28+
return nil
29+
}
30+
2131
// NewTpcState creates a base TpcState
2232
func NewTpcState(ctx context.Context, db *sql.DB) *TpcState {
2333
var conn *sql.Conn
@@ -32,6 +42,7 @@ func NewTpcState(ctx context.Context, db *sql.DB) *TpcState {
3242
r := rand.New(rand.NewSource(time.Now().UnixNano()))
3343

3444
s := &TpcState{
45+
DB: db,
3546
Conn: conn,
3647
R: r,
3748
Buf: util.NewBufAllocator(),

tpcc/workload.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ func getTPCCState(ctx context.Context) *tpccState {
192192
// Run implements Workloader interface
193193
func (w *Workloader) Run(ctx context.Context, threadID int) error {
194194
s := getTPCCState(ctx)
195-
196-
if s.newOrderStmts == nil {
195+
refreshConn := false
196+
if err := s.Conn.PingContext(ctx); err != nil {
197+
if err := s.RefreshConn(ctx); err != nil {
198+
return err
199+
}
200+
refreshConn = true
201+
}
202+
if s.newOrderStmts == nil || refreshConn {
197203
s.newOrderStmts = map[string]*sql.Stmt{
198204
newOrderSelectCustomer: prepareStmt(ctx, s.Conn, newOrderSelectCustomer),
199205
newOrderSelectDistrict: prepareStmt(ctx, s.Conn, newOrderSelectDistrict),

0 commit comments

Comments
 (0)