func (s *StateStore) nestedUpsertEval(txn *memdb.Txn, index uint64, eval *structs.Evaluation) error {
	// Lookup the evaluation
	existing, err := txn.First("evals", "id", eval.ID)
	if err != nil {
		return fmt.Errorf("eval lookup failed: %v", err)
	}

	// Update the indexes
	if existing != nil {
		eval.CreateIndex = existing.(*structs.Evaluation).CreateIndex
		eval.ModifyIndex = index
	} else {
		eval.CreateIndex = index
		eval.ModifyIndex = index
	}

	// Update the job summary
	summaryRaw, err := txn.First("job_summary", "id", eval.JobID)
	if err != nil {
		return fmt.Errorf("job summary lookup failed: %v", err)
	}
	if summaryRaw != nil {
		js := summaryRaw.(*structs.JobSummary).Copy()
		hasSummaryChanged := false
		for tg, num := range eval.QueuedAllocations {
			if summary, ok := js.Summary[tg]; ok {
				if summary.Queued != num {
					summary.Queued = num
					js.Summary[tg] = summary
					hasSummaryChanged = true
				}
			} else {
				s.logger.Printf("[ERR] state_store: unable to update queued for job %q and task group %q", eval.JobID, tg)
			}
		}

		// Insert the job summary
		if hasSummaryChanged {
			js.ModifyIndex = index
			if err := txn.Insert("job_summary", js); err != nil {
				return fmt.Errorf("job summary insert failed: %v", err)
			}
			if err := txn.Insert("index", &IndexEntry{"job_summary", index}); err != nil {
				return fmt.Errorf("index update failed: %v", err)
			}
		}
	}

	// Check if the job has any blocked evaluations and cancel them
	if eval.Status == structs.EvalStatusComplete && len(eval.FailedTGAllocs) == 0 {
		// Get the blocked evaluation for a job if it exists
		iter, err := txn.Get("evals", "job", eval.JobID, structs.EvalStatusBlocked)
		if err != nil {
			return fmt.Errorf("failed to get blocked evals for job %q: %v", eval.JobID, err)
		}

		var blocked []*structs.Evaluation
		for {
			raw := iter.Next()
			if raw == nil {
				break
			}
			blocked = append(blocked, raw.(*structs.Evaluation))
		}

		// Go through and update the evals
		for _, eval := range blocked {
			newEval := eval.Copy()
			newEval.Status = structs.EvalStatusCancelled
			newEval.StatusDescription = fmt.Sprintf("evaluation %q successful", newEval.ID)
			newEval.ModifyIndex = index
			if err := txn.Insert("evals", newEval); err != nil {
				return fmt.Errorf("eval insert failed: %v", err)
			}
		}
	}

	// Insert the eval
	if err := txn.Insert("evals", eval); err != nil {
		return fmt.Errorf("eval insert failed: %v", err)
	}
	if err := txn.Insert("index", &IndexEntry{"evals", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	return nil
}
// nestedUpsertEvaluation is used to nest an evaluation upsert within a transaction
nestedUpsertEvaluation is used to nest an evaluation upsert within a transaction
// nestedUpsertEvaluation is used to nest an evaluation upsert within a transaction
func (s *StateStore) nestedUpsertEval(txn *memdb.Txn, index uint64, eval *structs.Evaluation) error {
	existing, err := txn.First("evals", "id", eval.ID)
	if err != nil {
		return fmt.Errorf("eval lookup failed: %v", err)
	}
	if existing != nil {
		eval.CreateIndex = existing.(*structs.Evaluation).CreateIndex
		eval.ModifyIndex = index
	} else {
		eval.CreateIndex = index
		eval.ModifyIndex = index
	}
	summaryRaw, err := txn.First("job_summary", "id", eval.JobID)
	if err != nil {
		return fmt.Errorf("job summary lookup failed: %v", err)
	}
	if summaryRaw != nil {
		js := summaryRaw.(*structs.JobSummary).Copy()
		hasSummaryChanged := false
		for tg, num := range eval.QueuedAllocations {
			if summary, ok := js.Summary[tg]; ok {
				if summary.Queued != num {
					summary.Queued = num
					js.Summary[tg] = summary
					hasSummaryChanged = true
				}
			} else {
				s.logger.Printf("[ERR] state_store: unable to update queued for job %q and task group %q", eval.JobID, tg)
			}
		}
		if hasSummaryChanged {
			js.ModifyIndex = index
			if err := txn.Insert("job_summary", js); err != nil {
				return fmt.Errorf("job summary insert failed: %v", err)
			}
			if err := txn.Insert("index", &IndexEntry{"job_summary", index}); err != nil {
				return fmt.Errorf("index update failed: %v", err)
			}
		}
	}
	if eval.Status == structs.EvalStatusComplete && len(eval.FailedTGAllocs) == 0 {
		iter, err := txn.Get("evals", "job", eval.JobID, structs.EvalStatusBlocked)
		if err != nil {
			return fmt.Errorf("failed to get blocked evals for job %q: %v", eval.JobID, err)
		}
		var blocked []*structs.Evaluation
		for {
			raw := iter.Next()
			if raw == nil {
				break
			}
			blocked = append(blocked, raw.(*structs.Evaluation))
		}
		for _, eval := range blocked {
			newEval := eval.Copy()
			newEval.Status = structs.EvalStatusCancelled
			newEval.StatusDescription = fmt.Sprintf("evaluation %q successful", newEval.ID)
			newEval.ModifyIndex = index
			if err := txn.Insert("evals", newEval); err != nil {
				return fmt.Errorf("eval insert failed: %v", err)
			}
		}
	}
	if err := txn.Insert("evals", eval); err != nil {
		return fmt.Errorf("eval insert failed: %v", err)
	}
	if err := txn.Insert("index", &IndexEntry{"evals", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	return nil
}
func ( s * StateStore ) nestedUpsertEval ( txn * memdb . Txn , index uint64 , eval * structs . Evaluation ) error { existing , err := txn . First ( " " , " " , eval . ID ) if err != nil { return fmt . Errorf ( " " , err ) } if existing != nil { eval . CreateIndex = existing . ( * structs . Evaluation ) . CreateIndex eval . ModifyIndex = index } else { eval . CreateIndex = index eval . ModifyIndex = index } summaryRaw , err := txn . First ( " " , " " , eval . JobID ) if err != nil { return fmt . Errorf ( " " , err ) } if summaryRaw != nil { js := summaryRaw . ( * structs . JobSummary ) . Copy ( ) hasSummaryChanged := false for tg , num := range eval . QueuedAllocations { if summary , ok := js . Summary [ tg ] ; ok { if summary . Queued != num { summary . Queued = num js . Summary [ tg ] = summary hasSummaryChanged = true } } else { s . logger . Printf ( " " , eval . JobID , tg ) } } if hasSummaryChanged { js . ModifyIndex = index if err := txn . Insert ( " " , js ) ; err != nil { return fmt . Errorf ( " " , err ) } if err := txn . Insert ( " " , & IndexEntry { " " , index } ) ; err != nil { return fmt . Errorf ( " " , err ) } } } if eval . Status == structs . EvalStatusComplete && len ( eval . FailedTGAllocs ) == 0 { iter , err := txn . Get ( " " , " " , eval . JobID , structs . EvalStatusBlocked ) if err != nil { return fmt . Errorf ( " " , eval . JobID , err ) } var blocked [ ] * structs . Evaluation for { raw := iter . Next ( ) if raw == nil { break } blocked = append ( blocked , raw . ( * structs . Evaluation ) ) } for _ , eval := range blocked { newEval := eval . Copy ( ) newEval . Status = structs . EvalStatusCancelled newEval . StatusDescription = fmt . Sprintf ( " " , newEval . ID ) newEval . ModifyIndex = index if err := txn . Insert ( " " , newEval ) ; err != nil { return fmt . Errorf ( " " , err ) } } } if err := txn . Insert ( " " , eval ) ; err != nil { return fmt . Errorf ( " " , err ) } if err := txn . Insert ( " " , & IndexEntry { " " , index } ) ; err != nil { return fmt . Errorf ( " " , err ) } return nil }
nestedUpsertEvaluation is used to nest an evaluation upsert within a transaction
nestedUpsertEvaluation is used to nest an evaluation upsert within a transaction
// Lookup the evaluation // Update the indexes // Update the job summary // Insert the job summary // Check if the job has any blocked evaluations and cancel them // Get the blocked evaluation for a job if it exists // Go through and update the evals // Insert the eval