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

     1  package bidstrategy
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/filecoin-project/bacalhau/pkg/executor"
     8  	"github.com/filecoin-project/bacalhau/pkg/model"
     9  )
    10  
    11  type InputLocalityStrategyParams struct {
    12  	Locality  model.JobSelectionDataLocality
    13  	Executors executor.ExecutorProvider
    14  }
    15  
    16  type InputLocalityStrategy struct {
    17  	locality  model.JobSelectionDataLocality
    18  	executors executor.ExecutorProvider
    19  }
    20  
    21  func NewInputLocalityStrategy(params InputLocalityStrategyParams) *InputLocalityStrategy {
    22  	return &InputLocalityStrategy{
    23  		locality:  params.Locality,
    24  		executors: params.Executors,
    25  	}
    26  }
    27  
    28  func (s *InputLocalityStrategy) ShouldBid(ctx context.Context, request BidStrategyRequest) (BidStrategyResponse, error) {
    29  	// if we have an "anywhere" policy for the data then we accept the job
    30  	if s.locality == model.Anywhere {
    31  		return newShouldBidResponse(), nil
    32  	}
    33  
    34  	// otherwise we are checking that all of the named inputs in the job
    35  	// are local to us
    36  	e, err := s.executors.Get(ctx, request.Job.Spec.Engine)
    37  	if err != nil {
    38  		return BidStrategyResponse{}, fmt.Errorf("InputLocalityStrategy: failed to get executor %s: %w", request.Job.Spec.Engine, err)
    39  	}
    40  
    41  	foundInputs := 0
    42  
    43  	for _, input := range request.Job.Spec.Inputs {
    44  		// see if the storage engine reports that we have the resource locally
    45  		hasStorage, err := e.HasStorageLocally(ctx, input)
    46  		if err != nil {
    47  			return BidStrategyResponse{}, fmt.Errorf("InputLocalityStrategy: failed to check for storage resource locality: %w", err)
    48  		}
    49  		if hasStorage {
    50  			foundInputs++
    51  		}
    52  	}
    53  
    54  	if foundInputs >= len(request.Job.Spec.Inputs) {
    55  		return newShouldBidResponse(), nil
    56  	}
    57  	return BidStrategyResponse{ShouldBid: false, Reason: "not all inputs are local"}, nil
    58  }
    59  
    60  func (s *InputLocalityStrategy) ShouldBidBasedOnUsage(
    61  	_ context.Context, _ BidStrategyRequest, _ model.ResourceUsageData) (BidStrategyResponse, error) {
    62  	return newShouldBidResponse(), nil
    63  }