Skip to content

Commit

Permalink
Storage layer DB retry handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Sep 16, 2018
1 parent 08b0c26 commit 92fa8ff
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 96 deletions.
222 changes: 126 additions & 96 deletions server/core_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,27 @@ FROM storage
WHERE collection = $1 AND read = 2` + cursorQuery + `
LIMIT $2`

rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
return &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}, nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return nil, err
var objects *api.StorageObjectList
err := ExecuteRetryable(func() error {
rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
objects = &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}
return nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
}
}
defer rows.Close()
defer rows.Close()

objects, err := storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
}
objects, err = storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
return nil
})

return objects, err
}
Expand All @@ -125,21 +131,27 @@ FROM storage
WHERE collection = $1 AND read = 2 AND user_id = $2 ` + cursorQuery + `
LIMIT $3`

rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
return &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}, nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return nil, err
var objects *api.StorageObjectList
err := ExecuteRetryable(func() error {
rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
objects = &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}
return nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
}
}
defer rows.Close()
defer rows.Close()

objects, err := storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
}
objects, err = storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
return nil
})

return objects, err
}
Expand All @@ -166,21 +178,27 @@ WHERE collection = $1 AND user_id = $2 ` + cursorQuery + `
LIMIT $3`
}

rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
return &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}, nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return nil, err
var objects *api.StorageObjectList
err := ExecuteRetryable(func() error {
rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
objects = &api.StorageObjectList{Objects: make([]*api.StorageObject, 0), Cursor: cursor}
return nil
} else {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
}
}
defer rows.Close()
defer rows.Close()

objects, err := storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
}
objects, err = storageListObjects(rows, cursor)
if err != nil {
logger.Error("Could not list storage.", zap.Error(err), zap.String("collection", collection), zap.Int("limit", limit), zap.String("cursor", cursor))
return err
}
return nil
})

return objects, err
}
Expand All @@ -191,38 +209,44 @@ SELECT collection, key, user_id, value, version, read, write, create_time, updat
FROM storage
WHERE user_id = $1`

rows, err := db.Query(query, userID)
if err != nil {
if err == sql.ErrNoRows {
return make([]*api.StorageObject, 0), nil
} else {
logger.Error("Could not read storage objects.", zap.Error(err), zap.String("user_id", userID.String()))
return nil, err
}
}
defer rows.Close()

objects := make([]*api.StorageObject, 0)
for rows.Next() {
o := &api.StorageObject{CreateTime: &timestamp.Timestamp{}, UpdateTime: &timestamp.Timestamp{}}
var createTime pq.NullTime
var updateTime pq.NullTime
var userID sql.NullString
if err := rows.Scan(&o.Collection, &o.Key, &userID, &o.Value, &o.Version, &o.PermissionRead, &o.PermissionWrite, &createTime, &updateTime); err != nil {
return nil, err
var objects []*api.StorageObject
err := ExecuteRetryable(func() error {
rows, err := db.Query(query, userID)
if err != nil {
if err == sql.ErrNoRows {
objects = make([]*api.StorageObject, 0)
return nil
} else {
logger.Error("Could not read storage objects.", zap.Error(err), zap.String("user_id", userID.String()))
return err
}
}
defer rows.Close()

funcObjects := make([]*api.StorageObject, 0)
for rows.Next() {
o := &api.StorageObject{CreateTime: &timestamp.Timestamp{}, UpdateTime: &timestamp.Timestamp{}}
var createTime pq.NullTime
var updateTime pq.NullTime
var userID sql.NullString
if err := rows.Scan(&o.Collection, &o.Key, &userID, &o.Value, &o.Version, &o.PermissionRead, &o.PermissionWrite, &createTime, &updateTime); err != nil {
return err
}

o.CreateTime.Seconds = createTime.Time.Unix()
o.UpdateTime.Seconds = updateTime.Time.Unix()
o.CreateTime.Seconds = createTime.Time.Unix()
o.UpdateTime.Seconds = updateTime.Time.Unix()

o.UserId = userID.String
objects = append(objects, o)
}
o.UserId = userID.String
funcObjects = append(funcObjects, o)
}

if rows.Err() != nil {
logger.Error("Could not read storage objects.", zap.Error(err), zap.String("user_id", userID.String()))
return nil, rows.Err()
}
if rows.Err() != nil {
logger.Error("Could not read storage objects.", zap.Error(err), zap.String("user_id", userID.String()))
return rows.Err()
}
objects = funcObjects
return nil
})

return objects, err
}
Expand Down Expand Up @@ -307,42 +331,48 @@ FROM storage
WHERE
` + whereClause

rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
return &api.StorageObjects{Objects: make([]*api.StorageObject, 0)}, nil
} else {
logger.Error("Could not read storage objects.", zap.Error(err))
return nil, err
var objects *api.StorageObjects
err := ExecuteRetryable(func() error {
rows, err := db.Query(query, params...)
if err != nil {
if err == sql.ErrNoRows {
objects = &api.StorageObjects{Objects: make([]*api.StorageObject, 0)}
return nil
} else {
logger.Error("Could not read storage objects.", zap.Error(err))
return err
}
}
}
defer rows.Close()
defer rows.Close()

objects := &api.StorageObjects{Objects: make([]*api.StorageObject, 0)}
for rows.Next() {
o := &api.StorageObject{CreateTime: &timestamp.Timestamp{}, UpdateTime: &timestamp.Timestamp{}}
var createTime pq.NullTime
var updateTime pq.NullTime
funcObjects := &api.StorageObjects{Objects: make([]*api.StorageObject, 0)}
for rows.Next() {
o := &api.StorageObject{CreateTime: &timestamp.Timestamp{}, UpdateTime: &timestamp.Timestamp{}}
var createTime pq.NullTime
var updateTime pq.NullTime

var userID sql.NullString
if err := rows.Scan(&o.Collection, &o.Key, &userID, &o.Value, &o.Version, &o.PermissionRead, &o.PermissionWrite, &createTime, &updateTime); err != nil {
return nil, err
}
var userID sql.NullString
if err := rows.Scan(&o.Collection, &o.Key, &userID, &o.Value, &o.Version, &o.PermissionRead, &o.PermissionWrite, &createTime, &updateTime); err != nil {
return err
}

o.CreateTime.Seconds = createTime.Time.Unix()
o.UpdateTime.Seconds = updateTime.Time.Unix()
o.CreateTime.Seconds = createTime.Time.Unix()
o.UpdateTime.Seconds = updateTime.Time.Unix()

if !uuid.Equal(uuid.FromStringOrNil(userID.String), uuid.Nil) {
o.UserId = userID.String
if !uuid.Equal(uuid.FromStringOrNil(userID.String), uuid.Nil) {
o.UserId = userID.String
}
funcObjects.Objects = append(funcObjects.Objects, o)
}
objects.Objects = append(objects.Objects, o)
}
if err = rows.Err(); err != nil {
logger.Error("Could not read storage objects.", zap.Error(err))
return nil, err
}
if err = rows.Err(); err != nil {
logger.Error("Could not read storage objects.", zap.Error(err))
return err
}
objects = funcObjects
return nil
})

return objects, nil
return objects, err
}

func StorageWriteObjects(logger *zap.Logger, db *sql.DB, authoritativeWrite bool, objects map[uuid.UUID][]*api.WriteStorageObject) (*api.StorageObjectAcks, codes.Code, error) {
Expand Down
13 changes: 13 additions & 0 deletions server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server

import (
"errors"
"github.com/lib/pq"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -59,3 +60,15 @@ func StatusError(code codes.Code, msg string, cause error) error {
cause: cause,
}
}

// Retry functions that perform non-transactional database operations.
func ExecuteRetryable(fn func() error) error {
if err := fn(); err != nil {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "CR000" || pqErr.Code == "40001" {
// A recognised error type that can be retried.
return ExecuteRetryable(fn)
}
return err
}
return nil
}

0 comments on commit 92fa8ff

Please sign in to comment.