-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When a SQL error occurs, clean up all prepared statement. #2936
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,25 @@ func expandPlaceholderSQL(sql string, num int, first, rest string) string { | |
return strings.Replace(sql, placeholderSQL, parameters, 1) | ||
} | ||
|
||
// clearAllStmt clean up all sql.Stmt in cache | ||
func (m *mySQLTreeStorage) cleanAllStmt() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
m.statementMutex.Lock() | ||
defer m.statementMutex.Unlock() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some kind of logging for this would be really useful. This should be a rare event in an otherwise healthy system:
Actually, for bonus points I think it's worth incrementing a counter whenever we clear statements, which should appear on monitoring dashboards and allow correlation with potential impacts such as increased latency or errors. Take a look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
klog.Info("Clearing all prepared statements") | ||
|
||
for _, ns := range m.statements { | ||
for _, s := range ns { | ||
if err := s.Close(); err != nil { | ||
klog.Warningf("Failed to close stmt: %s", err) | ||
} | ||
} | ||
} | ||
|
||
m.statements = make(map[string]map[int]*sql.Stmt) | ||
clearedAllStmtCounter.Inc() | ||
} | ||
|
||
// getStmt creates and caches sql.Stmt structs based on the passed in statement | ||
// and number of bound arguments. | ||
// TODO(al,martin): consider pulling this all out as a separate unit for reuse | ||
|
@@ -114,8 +133,6 @@ func (m *mySQLTreeStorage) getStmt(ctx context.Context, statement string, num in | |
|
||
if m.statements[statement] != nil { | ||
if m.statements[statement][num] != nil { | ||
// TODO(al,martin): we'll possibly need to expire Stmts from the cache, | ||
// e.g. when DB connections break etc. | ||
return m.statements[statement][num], nil | ||
} | ||
} else { | ||
|
@@ -200,6 +217,7 @@ func (t *treeTX) getSubtrees(ctx context.Context, treeRevision int64, ids [][]by | |
|
||
rows, err := stx.QueryContext(ctx, args...) | ||
if err != nil { | ||
t.ts.cleanAllStmt() | ||
klog.Warningf("Failed to get merkle subtrees: %s", err) | ||
return nil, err | ||
} | ||
|
@@ -296,6 +314,7 @@ func (t *treeTX) storeSubtrees(ctx context.Context, subtrees []*storagepb.Subtre | |
|
||
r, err := stx.ExecContext(ctx, args...) | ||
if err != nil { | ||
t.ts.cleanAllStmt() | ||
klog.Warningf("Failed to set merkle subtrees: %s", err) | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: -> "number of times the prepared-statement cache has been cleared"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed