Skip to content
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

Owncloudsql context and connections #2944

Merged
merged 5 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/owncloudsql-context-and-connections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: improve owncloudsql connection management

The owncloudsql storagedriver is now aware of the request context and will close db connections when http connections are closed or time out. We also increased the max number of open connections from 10 to 100 to prevent a corner case where all connections were used but idle connections were not freed.

https://github.com/cs3org/reva/pull/2944
5 changes: 4 additions & 1 deletion pkg/auth/manager/owncloudsql/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ func NewMysql(dsn string, joinUsername, joinUUID, enableMedialSearch bool) (*Acc
if err != nil {
return nil, errors.Wrap(err, "error connecting to the database")
}

// FIXME make configurable
butonic marked this conversation as resolved.
Show resolved Hide resolved
sqldb.SetConnMaxLifetime(time.Minute * 3)
sqldb.SetMaxOpenConns(10)
sqldb.SetConnMaxIdleTime(time.Second * 30)
sqldb.SetMaxOpenConns(100)
sqldb.SetMaxIdleConns(10)

err = sqldb.Ping()
Expand Down
20 changes: 10 additions & 10 deletions pkg/share/manager/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
if err != nil {
return nil, err
}
result, err := stmt.Exec(stmtValues...)
result, err := stmt.ExecContext(ctx, stmtValues...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (m *mgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) er
if err != nil {
return err
}
res, err := stmt.Exec(params...)
res, err := stmt.ExecContext(ctx, params...)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference
if err != nil {
return nil, err
}
if _, err = stmt.Exec(params...); err != nil {
if _, err = stmt.ExecContext(ctx, params...); err != nil {
return nil, err
}

Expand Down Expand Up @@ -309,7 +309,7 @@ func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) (
query = fmt.Sprintf("%s AND (%s)", query, filterQuery)
}

rows, err := m.db.Query(query, params...)
rows, err := m.db.QueryContext(ctx, query, params...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -377,7 +377,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
query = fmt.Sprintf("%s AND (%s)", query, filterQuery)
}
query += ";"
rows, err := m.db.Query(query, params...)
rows, err := m.db.QueryContext(ctx, query, params...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaborat
if err != nil {
return err
}
res, err := stmt.Exec(params...)
res, err := stmt.ExecContext(ctx, params...)
if err != nil {
return err
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (m *mgr) getByID(ctx context.Context, id *collaboration.ShareId) (*collabor
uid := ctxpkg.ContextMustGetUser(ctx).Username
s := DBShare{ID: id.OpaqueId}
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, file_target, stime, permissions, share_type FROM oc_share WHERE id=? AND (uid_owner=? or uid_initiator=?)"
if err := m.db.QueryRow(query, id.OpaqueId, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.STime, &s.Permissions, &s.ShareType); err != nil {
if err := m.db.QueryRowContext(ctx, query, id.OpaqueId, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.STime, &s.Permissions, &s.ShareType); err != nil {
if err == sql.ErrNoRows {
return nil, errtypes.NotFound(id.OpaqueId)
}
Expand All @@ -514,7 +514,7 @@ func (m *mgr) getByKey(ctx context.Context, key *collaboration.ShareKey) (*colla
return nil, err
}
query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, file_target, id, stime, permissions, share_type FROM oc_share WHERE uid_owner=? AND file_source=? AND share_type=? AND share_with=? AND (uid_owner=? or uid_initiator=?)"
if err = m.db.QueryRow(query, owner, key.ResourceId.StorageId, shareType, shareWith, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType); err != nil {
if err = m.db.QueryRowContext(ctx, query, owner, key.ResourceId.StorageId, shareType, shareWith, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType); err != nil {
if err == sql.ErrNoRows {
return nil, errtypes.NotFound(key.String())
}
Expand Down Expand Up @@ -562,7 +562,7 @@ func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (*
`

s := DBShare{}
if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.ItemStorage, &s.Parent); err != nil {
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.ItemStorage, &s.Parent); err != nil {
if err == sql.ErrNoRows {
return nil, errtypes.NotFound(id.OpaqueId)
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey)
query += "AND (share_with=?)"
}

if err := m.db.QueryRow(query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil {
if err == sql.ErrNoRows {
return nil, errtypes.NotFound(key.String())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/owncloudsql/owncloudsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var _ = Describe("SQL manager", func() {
if err != nil {
return -1, err
}
result, err := stmt.Exec(stmtValues...)
result, err := stmt.ExecContext(ctx, stmtValues...)
if err != nil {
return -1, err
}
Expand Down
Loading