github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/jobstore/types.go (about)

     1  package jobstore
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/model"
     7  )
     8  
     9  type JobQuery struct {
    10  	ID          string              `json:"id"`
    11  	ClientID    string              `json:"clientID"`
    12  	IncludeTags []model.IncludedTag `json:"include_tags"`
    13  	ExcludeTags []model.ExcludedTag `json:"exclude_tags"`
    14  	Limit       int                 `json:"limit"`
    15  	Offset      int                 `json:"offset"`
    16  	ReturnAll   bool                `json:"return_all"`
    17  	SortBy      string              `json:"sort_by"`
    18  	SortReverse bool                `json:"sort_reverse"`
    19  }
    20  
    21  // A Store will persist jobs and their state to the underlying storage.
    22  // It also gives an efficient way to retrieve jobs using queries.
    23  type Store interface {
    24  	GetJob(ctx context.Context, id string) (model.Job, error)
    25  	GetJobs(ctx context.Context, query JobQuery) ([]model.Job, error)
    26  	GetJobState(ctx context.Context, jobID string) (model.JobState, error)
    27  	GetInProgressJobs(ctx context.Context) ([]model.JobWithInfo, error)
    28  	GetJobHistory(ctx context.Context, jobID string) ([]model.JobHistory, error)
    29  	GetJobsCount(ctx context.Context, query JobQuery) (int, error)
    30  	CreateJob(ctx context.Context, j model.Job) error
    31  	// UpdateJobState updates the Job state
    32  	UpdateJobState(ctx context.Context, request UpdateJobStateRequest) error
    33  	// GetShardState returns the shard for a given id
    34  	GetShardState(ctx context.Context, shardID model.ShardID) (model.ShardState, error)
    35  	// UpdateShardState updates the shard state
    36  	UpdateShardState(ctx context.Context, request UpdateShardStateRequest) error
    37  	// CreateExecution creates a new execution for a given job
    38  	CreateExecution(ctx context.Context, execution model.ExecutionState) error
    39  	// UpdateExecution updates the Job state
    40  	UpdateExecution(ctx context.Context, request UpdateExecutionRequest) error
    41  }
    42  
    43  type UpdateJobStateRequest struct {
    44  	JobID     string
    45  	Condition UpdateJobCondition
    46  	NewState  model.JobStateType
    47  	Comment   string
    48  }
    49  
    50  type UpdateShardStateRequest struct {
    51  	ShardID   model.ShardID
    52  	Condition UpdateShardCondition
    53  	NewState  model.ShardStateType
    54  	Comment   string
    55  }
    56  
    57  type UpdateExecutionRequest struct {
    58  	ExecutionID model.ExecutionID
    59  	Condition   UpdateExecutionCondition
    60  	NewValues   model.ExecutionState
    61  	Comment     string
    62  }
    63  
    64  type UpdateJobCondition struct {
    65  	ExpectedState    model.JobStateType
    66  	UnexpectedStates []model.JobStateType
    67  	ExpectedVersion  int
    68  }
    69  
    70  // Validate checks if the condition matches the given shard
    71  func (condition UpdateJobCondition) Validate(jobState model.JobState) error {
    72  	if condition.ExpectedState != model.JobStateNew && condition.ExpectedState != jobState.State {
    73  		return NewErrInvalidJobState(jobState.JobID, jobState.State, condition.ExpectedState)
    74  	}
    75  	if condition.ExpectedVersion != 0 && condition.ExpectedVersion != jobState.Version {
    76  		return NewErrInvalidJobVersion(jobState.JobID, jobState.Version, condition.ExpectedVersion)
    77  	}
    78  	if len(condition.UnexpectedStates) > 0 {
    79  		for _, s := range condition.UnexpectedStates {
    80  			if s == jobState.State {
    81  				return NewErrInvalidJobState(jobState.JobID, jobState.State, model.JobStateNew)
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  type UpdateShardCondition struct {
    89  	ExpectedState    model.ShardStateType
    90  	UnexpectedStates []model.ShardStateType
    91  	ExpectedVersion  int
    92  }
    93  
    94  // Validate checks if the condition matches the given shard
    95  func (condition UpdateShardCondition) Validate(shard model.ShardState) error {
    96  	if condition.ExpectedState != model.ShardStateNew && condition.ExpectedState != shard.State {
    97  		return NewErrInvalidShardState(shard.ID(), shard.State, condition.ExpectedState)
    98  	}
    99  	if condition.ExpectedVersion != 0 && condition.ExpectedVersion != shard.Version {
   100  		return NewErrInvalidShardVersion(shard.ID(), shard.Version, condition.ExpectedVersion)
   101  	}
   102  	if len(condition.UnexpectedStates) > 0 {
   103  		for _, s := range condition.UnexpectedStates {
   104  			if s == shard.State {
   105  				return NewErrInvalidShardState(shard.ID(), shard.State, model.ShardStateNew)
   106  			}
   107  		}
   108  	}
   109  	return nil
   110  }
   111  
   112  type UpdateExecutionCondition struct {
   113  	ExpectedState    model.ExecutionStateType
   114  	ExpectedVersion  int
   115  	UnexpectedStates []model.ExecutionStateType
   116  }
   117  
   118  // Validate checks if the condition matches the given execution
   119  func (condition UpdateExecutionCondition) Validate(execution model.ExecutionState) error {
   120  	if condition.ExpectedState != model.ExecutionStateNew && condition.ExpectedState != execution.State {
   121  		return NewErrInvalidExecutionState(execution.ID(), execution.State, condition.ExpectedState)
   122  	}
   123  	if condition.ExpectedVersion != 0 && condition.ExpectedVersion != execution.Version {
   124  		return NewErrInvalidExecutionVersion(execution.ID(), execution.Version, condition.ExpectedVersion)
   125  	}
   126  	if len(condition.UnexpectedStates) > 0 {
   127  		for _, s := range condition.UnexpectedStates {
   128  			if s == execution.State {
   129  				return NewErrInvalidExecutionState(execution.ID(), execution.State, model.ExecutionStateNew)
   130  			}
   131  		}
   132  	}
   133  	return nil
   134  }