Repository: hashicorp/nomad
File: scheduler/reconciler/reconcile_node.go (Go)

Code before:
	// programming error
	if dstate == nil {
		return
	}

	updatingSpec := updates != 0

	hadRunning := false
	hadRunningCondition := func(a *structs.Allocation) bool {
		return a.Job.ID == job.ID && a.Job.Version == job.Version && a.Job.CreateIndex == job.CreateIndex
	}

	for _, alloc := range allocs {
		if hadRunningCondition(alloc) {
			nr.compatHasSameVersionAllocs = true
			hadRunning = true
		}
	}

	// if there's a terminal allocation it means we're doing a reschedule.
	// We don't create deployments for reschedules.
	for _, alloc := range terminal {
		if hadRunningCondition(alloc) {
			hadRunning = true
		}
	}

	// Don't create a deployment if it's not the first time running the job
	// and there are no updates to the spec.
	if hadRunning && !updatingSpec {
		return
	}

	// A previous group may have made the deployment already. If not create one.
	if nr.DeploymentCurrent == nil {
		nr.DeploymentCurrent = structs.NewDeployment(job, job.Priority, time.Now().UnixNano())
		nr.DeploymentUpdates = append(
			nr.DeploymentUpdates, &structs.DeploymentStatusUpdate{
				DeploymentID:      nr.DeploymentCurrent.ID,
				Status:            structs.DeploymentStatusRunning,
				StatusDescription: structs.DeploymentStatusDescriptionRunning,
			})
	}

	// Attach the groups deployment state to the deployment
	if nr.DeploymentCurrent.TaskGroups == nil {
		nr.DeploymentCurrent.TaskGroups = make(map[string]*structs.DeploymentState)
	}

	nr.DeploymentCurrent.TaskGroups[tg.Name] = dstate
}

Review comment (performance):
If the terminal allocation map is large, we iterate through the entire map even if the first entry had the running condition. Does it make sense and give us a tiny performance gain if we break after setting `hadRunning`?

Code after:
	// programming error
	if dstate == nil {
		return
	}

	updatingSpec := updates != 0

	hadRunning := false
	hadRunningCondition := func(a *structs.Allocation) bool {
		return a.Job.ID == job.ID && a.Job.Version == job.Version && a.Job.CreateIndex == job.CreateIndex
	}

	for _, alloc := range allocs {
		if hadRunningCondition(alloc) {
			nr.compatHasSameVersionAllocs = true
			hadRunning = true
			break
		}
	}

	// if there's a terminal allocation it means we're doing a reschedule.
	// We don't create deployments for reschedules.
	for _, alloc := range terminal {
		if hadRunningCondition(alloc) {
			hadRunning = true
			break
		}
	}

	// Don't create a deployment if it's not the first time running the job
	// and there are no updates to the spec.
	if hadRunning && !updatingSpec {
		return
	}

	// A previous group may have made the deployment already. If not create one.
	if nr.DeploymentCurrent == nil {
		nr.DeploymentCurrent = structs.NewDeployment(job, job.Priority, time.Now().UnixNano())
		nr.DeploymentUpdates = append(
			nr.DeploymentUpdates, &structs.DeploymentStatusUpdate{
				DeploymentID:      nr.DeploymentCurrent.ID,
				Status:            structs.DeploymentStatusRunning,
				StatusDescription: structs.DeploymentStatusDescriptionRunning,
			})
	}

	// Attach the groups deployment state to the deployment
	if nr.DeploymentCurrent.TaskGroups == nil {
		nr.DeploymentCurrent.TaskGroups = make(map[string]*structs.DeploymentState)
	}