satellite/satellitedb: use transaction helpers in orders

Transactions in our code that might need to work against CockroachDB
need to be retried in the event of a retryable error. The WithTx
helper functions in dbutil and dbx do that automatically. I am changing
this code to use those helpers instead.

Change-Id: Iaf492af35471931125f2b7365aa4338f44154881
This commit is contained in:
paul cannon 2019-12-19 03:56:26 -06:00 committed by Yingrong Zhao
parent 05e2b0ca86
commit 723ed23298

View File

@ -227,19 +227,15 @@ func (db *ordersDB) ProcessOrders(ctx context.Context, requests []*orders.Proces
return requests[i].OrderLimit.SerialNumber.Less(requests[k].OrderLimit.SerialNumber) return requests[i].OrderLimit.SerialNumber.Less(requests[k].OrderLimit.SerialNumber)
}) })
tx, err := db.db.Begin() err = db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
if err != nil { responses, err = db.processOrdersInTx(requests, storageNodeID, time.Now(), tx.Tx)
return nil, errs.Wrap(err) return err
} })
defer func() { return responses, errs.Wrap(err)
if err == nil { }
err = tx.Commit()
} else {
err = errs.Combine(err, tx.Rollback())
}
}()
now := time.Now().UTC() func (db *ordersDB) processOrdersInTx(requests []*orders.ProcessOrderRequest, storageNodeID storj.NodeID, now time.Time, tx *sql.Tx) (responses []*orders.ProcessOrderResponse, err error) {
now = now.UTC()
intervalStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) intervalStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
rejected := make(map[storj.SerialNumber]bool) rejected := make(map[storj.SerialNumber]bool)