fix saving uplink public key on satellite (#1797)

* fix save into cert records

* fix transaction logic

* test shouldn't be flaky anymore

* add missing cause method

* restart jenkins
This commit is contained in:
Egon Elbre 2019-04-23 22:48:57 +03:00 committed by aligeti
parent 9f6b010748
commit fe2bf7d4a6
3 changed files with 43 additions and 35 deletions

View File

@ -111,8 +111,6 @@ func TestDownloadWithSomeNodesOffline(t *testing.T) {
}
func TestUploadDownloadOneUplinksInParallel(t *testing.T) {
t.Skip("flaky")
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {

View File

@ -6,9 +6,10 @@ package satellitedb
import (
"context"
"crypto"
"database/sql"
"github.com/zeebo/errs"
"storj.io/storj/internal/dbutil/pgutil"
"storj.io/storj/internal/dbutil/sqliteutil"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
dbx "storj.io/storj/satellite/satellitedb/dbx"
@ -18,40 +19,47 @@ type certDB struct {
db *dbx.DB
}
func (b *certDB) SavePublicKey(ctx context.Context, nodeID storj.NodeID, publicKey crypto.PublicKey) error {
tx, err := b.db.Open(ctx)
func (certs *certDB) SavePublicKey(ctx context.Context, nodeID storj.NodeID, publicKey crypto.PublicKey) error {
_, err := certs.db.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
if err == sql.ErrNoRows {
return certs.tryAddPublicKey(ctx, nodeID, publicKey)
}
if err != nil {
return Error.Wrap(err)
}
_, err = tx.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
if err != nil {
// no rows err, so create/insert an entry
pubbytes, err := pkcrypto.PublicKeyToPKIX(publicKey)
if err != nil {
return Error.Wrap(errs.Combine(err, tx.Rollback()))
}
_, err = tx.Create_CertRecord(ctx,
dbx.CertRecord_Publickey(pubbytes),
dbx.CertRecord_Id(nodeID.Bytes()),
)
if err != nil {
return Error.Wrap(errs.Combine(err, tx.Rollback()))
}
} else {
// nodeID entry already exists, just return
return Error.Wrap(tx.Rollback())
}
return Error.Wrap(tx.Commit())
// nodeID entry already exists, just return
return nil
}
func (b *certDB) GetPublicKey(ctx context.Context, nodeID storj.NodeID) (crypto.PublicKey, error) {
dbxInfo, err := b.db.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
func (certs *certDB) tryAddPublicKey(ctx context.Context, nodeID storj.NodeID, publicKey crypto.PublicKey) error {
// no rows err, so create/insert an entry
pubbytes, err := pkcrypto.PublicKeyToPKIX(publicKey)
if err != nil {
return Error.Wrap(err)
}
// TODO: use upsert here instead of create
_, err = certs.db.Create_CertRecord(ctx,
dbx.CertRecord_Publickey(pubbytes),
dbx.CertRecord_Id(nodeID.Bytes()),
)
// another goroutine might race to create the cert record, let's ignore that error
if pgutil.IsConstraintError(err) || sqliteutil.IsConstraintError(err) {
return nil
} else if err != nil {
return Error.Wrap(err)
}
return nil
}
func (certs *certDB) GetPublicKey(ctx context.Context, nodeID storj.NodeID) (crypto.PublicKey, error) {
dbxInfo, err := certs.db.Get_CertRecord_By_Id(ctx, dbx.CertRecord_Id(nodeID.Bytes()))
if err != nil {
return nil, err
}
pubkey, err := pkcrypto.PublicKeyFromPKIX(dbxInfo.Publickey)
if err != nil {
return nil, Error.New("Failed to extract Public Key from Order: %+v", err)

View File

@ -28,9 +28,10 @@ func init() {
}
// Unwrap returns the underlying error.
func (e *Error) Unwrap() error {
return e.Err
}
func (e *Error) Unwrap() error { return e.Err }
// Cause returns the underlying error.
func (e *Error) Cause() error { return e.Err }
type constraintError struct {
constraint string
@ -38,9 +39,10 @@ type constraintError struct {
}
// Unwrap returns the underlying error.
func (err *constraintError) Unwrap() error {
return err.err
}
func (err *constraintError) Unwrap() error { return err.err }
// Cause returns the underlying error.
func (err *constraintError) Cause() error { return err.err }
// Error implements the error interface.
func (err *constraintError) Error() string {