@@ -396,11 +396,20 @@ func (k *connectorClusterService) GetClientID(clusterID string) (string, error)
396
396
// SaveDeployment creates a connector deployment in the database
397
397
func (k * connectorClusterService ) SaveDeployment (ctx context.Context , resource * dbapi.ConnectorDeployment ) * errors.ServiceError {
398
398
dbConn := k .connectionFactory .New ()
399
+ if resource .Version != 0 {
400
+ dbConn = dbConn .Where ("version = ?" , resource .Version )
401
+ }
399
402
400
- if err := dbConn .Save (resource ).Error ; err != nil {
403
+ saveResult := dbConn .Save (resource )
404
+ if err := saveResult .Error ; err != nil {
401
405
return services .HandleCreateError (`Connector deployment` , err )
402
406
}
407
+ if saveResult .RowsAffected == 0 {
408
+ return errors .Conflict ("Optimistic locking: resource version changed from %v" , resource .Version )
409
+ }
403
410
411
+ //refresh version
412
+ dbConn = k .connectionFactory .New ()
404
413
if err := dbConn .Where ("id = ?" , resource .ID ).Select ("version" ).First (& resource ).Error ; err != nil {
405
414
return services .HandleGetError (`Connector deployment` , "id" , resource .ID , err )
406
415
}
@@ -416,11 +425,20 @@ func (k *connectorClusterService) SaveDeployment(ctx context.Context, resource *
416
425
417
426
func (k * connectorClusterService ) UpdateDeployment (resource * dbapi.ConnectorDeployment ) * errors.ServiceError {
418
427
dbConn := k .connectionFactory .New ()
428
+
429
+ if resource .Version != 0 {
430
+ dbConn = dbConn .Where ("version = ?" , resource .Version )
431
+ }
432
+
419
433
updates := dbConn .Where ("id = ?" , resource .ID ).
420
434
Updates (resource )
421
435
if err := updates .Error ; err != nil {
422
- return services .HandleUpdateError (`Connector namespace` , err )
436
+ return services .HandleUpdateError (`Connector deployment` , err )
437
+ }
438
+ if updates .RowsAffected == 0 {
439
+ return errors .Conflict ("Optimistic locking: resource version changed from %v" , resource .Version )
423
440
}
441
+
424
442
return nil
425
443
}
426
444
@@ -494,7 +512,7 @@ func (k *connectorClusterService) ListConnectorDeployments(ctx context.Context,
494
512
func (k * connectorClusterService ) UpdateConnectorDeploymentStatus (ctx context.Context , deploymentStatus dbapi.ConnectorDeploymentStatus ) * errors.ServiceError {
495
513
dbConn := k .connectionFactory .New ()
496
514
497
- // lets get the connector id of the deployment..
515
+ // let's get the connector id of the deployment. ..
498
516
deployment := dbapi.ConnectorDeployment {}
499
517
if err := dbConn .Unscoped ().Select ("connector_id" , "deleted_at" ).
500
518
Where ("id = ?" , deploymentStatus .ID ).
@@ -505,7 +523,7 @@ func (k *connectorClusterService) UpdateConnectorDeploymentStatus(ctx context.Co
505
523
return services .HandleGoneError ("Connector deployment" , "id" , deploymentStatus .ID )
506
524
}
507
525
508
- if err := dbConn .Model ( & deploymentStatus ). Where ("id = ? and version <= ?" , deploymentStatus .ID , deploymentStatus .Version ).Save (& deploymentStatus ).Error ; err != nil {
526
+ if err := dbConn .Where ("id = ? and version <= ?" , deploymentStatus .ID , deploymentStatus .Version ).Save (& deploymentStatus ).Error ; err != nil {
509
527
return errors .Conflict ("failed to update deployment status: %s, probably a stale deployment status version was used: %d" , err .Error (), deploymentStatus .Version )
510
528
}
511
529
@@ -531,8 +549,8 @@ func (k *connectorClusterService) UpdateConnectorDeploymentStatus(ctx context.Co
531
549
}
532
550
}
533
551
534
- // update the connector status
535
- if err := dbConn .Where ("id = ?" , deployment .ConnectorID ).Updates (& connectorStatus ).Error ; err != nil {
552
+ // update the connector status, don't updated deleted statues
553
+ if err := dbConn .Where ("deleted_at IS NULL AND id = ?" , deployment .ConnectorID ).Updates (& connectorStatus ).Error ; err != nil {
536
554
return services .HandleUpdateError ("Connector status" , err )
537
555
}
538
556
@@ -564,7 +582,7 @@ func (k *connectorClusterService) FindAvailableNamespace(owner string, orgID str
564
582
565
583
func (k * connectorClusterService ) GetDeploymentByConnectorId (ctx context.Context , connectorID string ) (resource dbapi.ConnectorDeployment , serr * errors.ServiceError ) {
566
584
567
- if err := k .connectionFactory .New ().Preload ( clause . Associations ).
585
+ if err := k .connectionFactory .New ().
568
586
Joins ("Status" ).Joins ("ConnectorShardMetadata" ).Joins ("Connector" ).
569
587
Where ("connector_id = ?" , connectorID ).First (& resource ).Error ; err != nil {
570
588
return resource , services .HandleGetError ("Connector deployment" , "connector_id" , connectorID , err )
0 commit comments