Ensure serial number is not saved if error during order limit creation (#1602)

This commit is contained in:
Kaloyan Raev 2019-03-29 11:53:53 +02:00 committed by GitHub
parent f9ba935286
commit 9fb99c8484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,15 +78,10 @@ func (service *Service) CreateGetOrderLimits(ctx context.Context, uplink *identi
if err != nil {
return nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
errPubKey := service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
err = errs.Combine(err, errSerial, errPubKey)
}()
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
if err != nil {
return nil, err
return nil, Error.Wrap(err)
}
pieceSize := eestream.CalcPieceSize(pointer.GetSegmentSize(), redundancy)
@ -133,11 +128,20 @@ func (service *Service) CreateGetOrderLimits(ctx context.Context, uplink *identi
}
if len(limits) < redundancy.RequiredCount() {
// TODO: undo serial entry
err = Error.New("not enough nodes available: got %d, required %d", len(limits), redundancy.RequiredCount())
return nil, errs.Combine(err, combinedErrs)
}
err = service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
if err != nil {
return nil, Error.Wrap(err)
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return nil, Error.Wrap(err)
}
return limits, nil
}
@ -154,11 +158,6 @@ func (service *Service) CreatePutOrderLimits(ctx context.Context, uplink *identi
if err != nil {
return storj.PieceID{}, nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
errPubKey := service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
err = errs.Combine(err, errSerial, errPubKey)
}()
rootPieceID := storj.NewPieceID()
limits := make([]*pb.AddressedOrderLimit, len(nodes))
@ -186,6 +185,16 @@ func (service *Service) CreatePutOrderLimits(ctx context.Context, uplink *identi
pieceNum++
}
err = service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
if err != nil {
return storj.PieceID{}, nil, Error.Wrap(err)
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return storj.PieceID{}, nil, Error.Wrap(err)
}
return rootPieceID, limits, nil
}
@ -205,11 +214,6 @@ func (service *Service) CreateDeleteOrderLimits(ctx context.Context, uplink *ide
if err != nil {
return nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
errPubKey := service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
err = errs.Combine(err, errSerial, errPubKey)
}()
var combinedErrs error
var limits []*pb.AddressedOrderLimit
@ -253,11 +257,20 @@ func (service *Service) CreateDeleteOrderLimits(ctx context.Context, uplink *ide
}
if len(limits) == 0 {
// TODO: undo serial entry
err = Error.New("failed creating order limits for all nodes")
return nil, errs.Combine(err, combinedErrs)
}
err = service.certdb.SavePublicKey(ctx, uplink.ID, uplink.Leaf.PublicKey)
if err != nil {
return nil, Error.Wrap(err)
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return nil, Error.Wrap(err)
}
return limits, nil
}
@ -280,10 +293,6 @@ func (service *Service) CreateAuditOrderLimits(ctx context.Context, auditor *ide
if err != nil {
return nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
err = errs.Combine(err, errSerial)
}()
var combinedErrs error
var limitsCount int32
@ -329,11 +338,15 @@ func (service *Service) CreateAuditOrderLimits(ctx context.Context, auditor *ide
}
if limitsCount < redundancy.GetMinReq() {
// TODO: undo serial entry
err = Error.New("not enough nodes available: got %d, required %d", limitsCount, redundancy.GetMinReq())
return nil, errs.Combine(err, combinedErrs)
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return nil, Error.Wrap(err)
}
return limits, nil
}
@ -356,10 +369,6 @@ func (service *Service) CreateGetRepairOrderLimits(ctx context.Context, repairer
if err != nil {
return nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
err = errs.Combine(err, errSerial)
}()
var combinedErrs error
var limitsCount int32
@ -404,11 +413,15 @@ func (service *Service) CreateGetRepairOrderLimits(ctx context.Context, repairer
}
if limitsCount < redundancy.GetMinReq() {
// TODO: undo serial entry
err = Error.New("not enough nodes available: got %d, required %d", limitsCount, redundancy.GetMinReq())
return nil, errs.Combine(err, combinedErrs)
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return nil, Error.Wrap(err)
}
return limits, nil
}
@ -430,10 +443,6 @@ func (service *Service) CreatePutRepairOrderLimits(ctx context.Context, repairer
if err != nil {
return nil, err
}
defer func() {
errSerial := service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
err = errs.Combine(err, errSerial)
}()
limits := make([]*pb.AddressedOrderLimit, totalPieces)
var pieceNum int32
@ -472,5 +481,10 @@ func (service *Service) CreatePutRepairOrderLimits(ctx context.Context, repairer
pieceNum++
}
err = service.saveSerial(ctx, serialNumber, bucketID, orderExpirationTime)
if err != nil {
return nil, Error.Wrap(err)
}
return limits, nil
}