Skip to content

Commit

Permalink
refactored according to Luiz's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
pkazmierczak committed Jan 11, 2024
1 parent d6849cc commit e918e3b
Showing 1 changed file with 71 additions and 17 deletions.
88 changes: 71 additions & 17 deletions nomad/state/state_store.go
Expand Up @@ -806,6 +806,18 @@ func (s *StateStore) DeleteDeployment(index uint64, deploymentIDs []string) erro
txn := s.db.WriteTxn(index)
defer txn.Abort()

err := s.DeleteDeploymentTxn(index, deploymentIDs, txn)
if err == nil {
return txn.Commit()
}

return err
}

// DeleteDeploymentTxn is used to delete a set of deployments by ID, like
// DeleteDeployment but in a transaction. Useful when making multiple
// modifications atomically.
func (s *StateStore) DeleteDeploymentTxn(index uint64, deploymentIDs []string, txn Txn) error {
if len(deploymentIDs) == 0 {
return nil
}
Expand All @@ -817,7 +829,7 @@ func (s *StateStore) DeleteDeployment(index uint64, deploymentIDs []string) erro
return fmt.Errorf("deployment lookup failed: %v", err)
}
if existing == nil {
return fmt.Errorf("deployment not found")
continue
}

// Delete the deployment
Expand All @@ -830,7 +842,50 @@ func (s *StateStore) DeleteDeployment(index uint64, deploymentIDs []string) erro
return fmt.Errorf("index update failed: %v", err)
}

return txn.Commit()
return nil
}

// DeleteAlloc is used to delete a set of allocations by ID
func (s *StateStore) DeleteAlloc(index uint64, allocIDs []string) error {
txn := s.db.WriteTxn(index)
defer txn.Abort()

err := s.DeleteAllocTxn(index, allocIDs, txn)
if err == nil {
return txn.Commit()
}

return err
}

// DeleteAllocTxn is used to delete a set of allocs by ID, like DeleteALloc but
// in a transaction. Useful when making multiple modifications atomically.
func (s *StateStore) DeleteAllocTxn(index uint64, allocIDs []string, txn Txn) error {
if len(allocIDs) == 0 {
return nil
}

for _, allocID := range allocIDs {
// Lookup the alloc
existing, err := txn.First("allocs", "id", allocID)
if err != nil {
return fmt.Errorf("alloc lookup failed: %v", err)
}
if existing == nil {
continue
}

// Delete the alloc
if err := txn.Delete("allocs", existing); err != nil {
return fmt.Errorf("alloc delete failed: %v", err)
}
}

if err := txn.Insert("index", &IndexEntry{"allocs", index}); err != nil {
return fmt.Errorf("index update failed: %v", err)
}

return nil
}

// UpsertScalingEvent is used to insert a new scaling event.
Expand Down Expand Up @@ -1900,14 +1955,13 @@ func (s *StateStore) DeleteJobTxn(index uint64, namespace, jobID string, txn Txn
return fmt.Errorf("deployment lookup for job %s failed: %v", job.ID, err)
}

for _, deployment := range deployments {
// Delete the deployment
if err := txn.Delete("deployment", deployment); err != nil {
return fmt.Errorf("deployment delete failed: %v", err)
}
if err := txn.Insert("index", &IndexEntry{"deployment", index}); err != nil {
return fmt.Errorf("index update failed: %v", err)
}
deploymentIDs := []string{}
for _, d := range deployments {
deploymentIDs = append(deploymentIDs, d.ID)
}

if err := s.DeleteDeploymentTxn(index, deploymentIDs, txn); err != nil {
return err
}

// Mark all "pending" evals for this job as "complete"
Expand Down Expand Up @@ -1947,13 +2001,13 @@ func (s *StateStore) DeleteJobTxn(index uint64, namespace, jobID string, txn Txn
return fmt.Errorf("alloc lookup for job %s failed: %v", job.ID, err)
}

for _, alloc := range allocs {
if err := txn.Delete("allocs", alloc); err != nil {
return fmt.Errorf("deployment delete failed: %v", err)
}
if err := txn.Insert("index", &IndexEntry{"allocs", index}); err != nil {
return fmt.Errorf("index update failed: %v", err)
}
allocIDs := []string{}
for _, a := range allocs {
allocIDs = append(allocIDs, a.ID)
}

if err := s.DeleteAllocTxn(index, allocIDs, txn); err != nil {
return err
}

// Cleanup plugins registered by this job, before we delete the summary
Expand Down

0 comments on commit e918e3b

Please sign in to comment.