github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/compute/store/utils.go (about)

     1  package store
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/rs/zerolog/log"
     7  )
     8  
     9  // GetActiveExecution returns the active execution for a given shard.
    10  // In case of a bug where we have more than a single active execution, the latest one is returned
    11  func GetActiveExecution(ctx context.Context, s ExecutionStore, shardID string) (Execution, error) {
    12  	executions, err := s.GetExecutions(ctx, shardID)
    13  	if err != nil {
    14  		return Execution{}, err
    15  	}
    16  
    17  	var activeExecution Execution
    18  	var activeExecutionsCount int
    19  	for _, execution := range executions {
    20  		if execution.State.IsActive() {
    21  			activeExecutionsCount++
    22  			if activeExecutionsCount != 1 || execution.UpdateTime.After(activeExecution.UpdateTime) {
    23  				activeExecution = execution
    24  			}
    25  		}
    26  	}
    27  
    28  	if activeExecutionsCount > 1 {
    29  		log.Ctx(ctx).Warn().Msgf(
    30  			"Found %d active executions for shard %s. Selecting the latest one", activeExecutionsCount, shardID)
    31  	}
    32  
    33  	return activeExecution, nil
    34  }
    35  
    36  func ValidateNewExecution(_ context.Context, execution Execution) error {
    37  	if execution.State != ExecutionStateCreated {
    38  		return NewErrInvalidExecutionState(execution.ID, execution.State, ExecutionStateCreated)
    39  	}
    40  	if execution.Version != 1 {
    41  		return NewErrInvalidExecutionVersion(execution.ID, execution.Version, 1)
    42  	}
    43  
    44  	return nil
    45  }