func (s *StateStore) updateDeploymentWithAlloc(index uint64, alloc, existing *structs.Allocation, txn *memdb.Txn) error {
	// Nothing to do if the allocation is not associated with a deployment
	if alloc.DeploymentID == "" {
		return nil
	}

	// Get the deployment
	ws := memdb.NewWatchSet()
	deployment, err := s.deploymentByIDImpl(ws, alloc.DeploymentID, txn)
	if err != nil {
		return err
	}
	if deployment == nil {
		return nil
	}

	// Retrieve the deployment state object
	_, ok := deployment.TaskGroups[alloc.TaskGroup]
	if !ok {
		// If the task group isn't part of the deployment, the task group wasn't
		// part of a rolling update so nothing to do
		return nil
	}

	// Do not modify in-place. Instead keep track of what must be done
	placed := 0
	healthy := 0
	unhealthy := 0

	// If there was no existing allocation, this is a placement and we increment
	// the placement
	existingHealthSet := existing != nil && existing.DeploymentStatus != nil && existing.DeploymentStatus.Healthy != nil
	allocHealthSet := alloc.DeploymentStatus != nil && alloc.DeploymentStatus.Healthy != nil
	if existing == nil || existing.DeploymentID != alloc.DeploymentID {
		placed++
	} else if !existingHealthSet && allocHealthSet {
		if *alloc.DeploymentStatus.Healthy {
			healthy++
		} else {
			unhealthy++
		}
	} else if existingHealthSet && allocHealthSet {
		// See if it has gone from healthy to unhealthy
		if *existing.DeploymentStatus.Healthy && !*alloc.DeploymentStatus.Healthy {
			healthy--
			unhealthy++
		}
	}

	// Nothing to do
	if placed == 0 && healthy == 0 && unhealthy == 0 {
		return nil
	}

	// Update the allocation's deployment status modify index
	if alloc.DeploymentStatus != nil && healthy+unhealthy != 0 {
		alloc.DeploymentStatus.ModifyIndex = index
	}

	// Create a copy of the deployment object
	deploymentCopy := deployment.Copy()
	deploymentCopy.ModifyIndex = index

	state := deploymentCopy.TaskGroups[alloc.TaskGroup]
	state.PlacedAllocs += placed
	state.HealthyAllocs += healthy
	state.UnhealthyAllocs += unhealthy

	// Upsert the deployment
	if err := s.upsertDeploymentImpl(index, deploymentCopy, txn); err != nil {
		return err
	}

	return nil
}
// updateDeploymentWithAlloc is used to update the deployment state associated
// with the given allocation. The passed alloc may be updated if the deployment
// status has changed to capture the modify index at which it has changed.
updateDeploymentWithAlloc is used to update the deployment state associated
with the given allocation. The passed alloc may be updated if the deployment
status has changed to capture the modify index at which it has changed.
// updateDeploymentWithAlloc is used to update the deployment state associated // with the given allocation . The passed alloc may be updated if the deployment // status has changed to capture the modify index at which it has changed .
func (s *StateStore) updateDeploymentWithAlloc(index uint64, alloc, existing *structs.Allocation, txn *memdb.Txn) error {
	if alloc.DeploymentID == "" {
		return nil
	}
	ws := memdb.NewWatchSet()
	deployment, err := s.deploymentByIDImpl(ws, alloc.DeploymentID, txn)
	if err != nil {
		return err
	}
	if deployment == nil {
		return nil
	}
	_, ok := deployment.TaskGroups[alloc.TaskGroup]
	if !ok {
		return nil
	}
	placed := 0
	healthy := 0
	unhealthy := 0
	existingHealthSet := existing != nil && existing.DeploymentStatus != nil && existing.DeploymentStatus.Healthy != nil
	allocHealthSet := alloc.DeploymentStatus != nil && alloc.DeploymentStatus.Healthy != nil
	if existing == nil || existing.DeploymentID != alloc.DeploymentID {
		placed++
	} else if !existingHealthSet && allocHealthSet {
		if *alloc.DeploymentStatus.Healthy {
			healthy++
		} else {
			unhealthy++
		}
	} else if existingHealthSet && allocHealthSet {
		if *existing.DeploymentStatus.Healthy && !*alloc.DeploymentStatus.Healthy {
			healthy--
			unhealthy++
		}
	}
	if placed == 0 && healthy == 0 && unhealthy == 0 {
		return nil
	}
	if alloc.DeploymentStatus != nil && healthy+unhealthy != 0 {
		alloc.DeploymentStatus.ModifyIndex = index
	}
	deploymentCopy := deployment.Copy()
	deploymentCopy.ModifyIndex = index
	state := deploymentCopy.TaskGroups[alloc.TaskGroup]
	state.PlacedAllocs += placed
	state.HealthyAllocs += healthy
	state.UnhealthyAllocs += unhealthy
	if err := s.upsertDeploymentImpl(index, deploymentCopy, txn); err != nil {
		return err
	}
	return nil
}
func ( s * StateStore ) updateDeploymentWithAlloc ( index uint64 , alloc , existing * structs . Allocation , txn * memdb . Txn ) error { if alloc . DeploymentID == " " { return nil } ws := memdb . NewWatchSet ( ) deployment , err := s . deploymentByIDImpl ( ws , alloc . DeploymentID , txn ) if err != nil { return err } if deployment == nil { return nil } _ , ok := deployment . TaskGroups [ alloc . TaskGroup ] if ! ok { return nil } placed := 0 healthy := 0 unhealthy := 0 existingHealthSet := existing != nil && existing . DeploymentStatus != nil && existing . DeploymentStatus . Healthy != nil allocHealthSet := alloc . DeploymentStatus != nil && alloc . DeploymentStatus . Healthy != nil if existing == nil || existing . DeploymentID != alloc . DeploymentID { placed ++ } else if ! existingHealthSet && allocHealthSet { if * alloc . DeploymentStatus . Healthy { healthy ++ } else { unhealthy ++ } } else if existingHealthSet && allocHealthSet { if * existing . DeploymentStatus . Healthy && ! * alloc . DeploymentStatus . Healthy { healthy -- unhealthy ++ } } if placed == 0 && healthy == 0 && unhealthy == 0 { return nil } if alloc . DeploymentStatus != nil && healthy + unhealthy != 0 { alloc . DeploymentStatus . ModifyIndex = index } deploymentCopy := deployment . Copy ( ) deploymentCopy . ModifyIndex = index state := deploymentCopy . TaskGroups [ alloc . TaskGroup ] state . PlacedAllocs += placed state . HealthyAllocs += healthy state . UnhealthyAllocs += unhealthy if err := s . upsertDeploymentImpl ( index , deploymentCopy , txn ) ; err != nil { return err } return nil }
updateDeploymentWithAlloc is used to update the deployment state associated
with the given allocation.
updateDeploymentWithAlloc is used to update the deployment state associated with the given allocation .
// Nothing to do if the allocation is not associated with a deployment // Get the deployment // Retrieve the deployment state object // If the task group isn't part of the deployment, the task group wasn't // part of a rolling update so nothing to do // Do not modify in-place. Instead keep track of what must be done // If there was no existing allocation, this is a placement and we increment // the placement // See if it has gone from healthy to unhealthy // Nothing to do // Update the allocation's deployment status modify index // Create a copy of the deployment object // Upsert the deployment