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

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  
     6  	model "github.com/filecoin-project/bacalhau/pkg/model/v1beta1"
     7  )
     8  
     9  func UpdateShardState(
    10  	nodeID string,
    11  	shardIndex int,
    12  	jobState *model.JobState,
    13  	update model.JobShardState,
    14  ) error {
    15  	nodeState, ok := jobState.Nodes[nodeID]
    16  	if !ok {
    17  		nodeState = model.JobNodeState{
    18  			Shards: map[int]model.JobShardState{},
    19  		}
    20  	}
    21  	shardState, ok := nodeState.Shards[shardIndex]
    22  	if !ok {
    23  		shardState = model.JobShardState{
    24  			NodeID:     nodeID,
    25  			ShardIndex: shardIndex,
    26  		}
    27  	}
    28  
    29  	if update.State < shardState.State {
    30  		return fmt.Errorf("cannot update shard state to %s as current state is %s. [NodeID: %s, ShardID: %d]",
    31  			update.State, shardState.State, nodeID, shardIndex)
    32  	}
    33  
    34  	shardState.State = update.State
    35  	if update.Status != "" {
    36  		shardState.Status = update.Status
    37  	}
    38  
    39  	if update.RunOutput != nil {
    40  		shardState.RunOutput = update.RunOutput
    41  	}
    42  
    43  	if len(update.VerificationProposal) != 0 {
    44  		shardState.VerificationProposal = update.VerificationProposal
    45  	}
    46  
    47  	if update.VerificationResult.Complete {
    48  		shardState.VerificationResult = update.VerificationResult
    49  	}
    50  
    51  	if update.ExecutionID != "" {
    52  		shardState.ExecutionID = update.ExecutionID
    53  	}
    54  
    55  	if model.IsValidStorageSourceType(update.PublishedResult.StorageSource) {
    56  		shardState.PublishedResult = update.PublishedResult
    57  	}
    58  
    59  	nodeState.Shards[shardIndex] = shardState
    60  	jobState.Nodes[nodeID] = nodeState
    61  	return nil
    62  }