Skip to content

Commit

Permalink
contractor: fix leeway for contracts without revision
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Jul 30, 2024
1 parent 10bc351 commit c634fd9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 14 additions & 9 deletions autopilot/contractor/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,15 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur
// stats for later logging
var formed, refreshed, renewed int

// helper to add contracts to the set of contracts we keep for the new set
var filteredContracts []api.ContractMetadata
keepContract := func(c api.ContractMetadata, h api.Host) {
filteredContracts = append(filteredContracts, c)
ipFilter.Add(h)
}

// STEP 2: perform contract maintenance
dropOutReasons := make(map[types.FileContractID]string)
var filteredContracts []api.ContractMetadata
if err := func() error {
// fetch all contracts we already have
logger.Info("fetching existing contracts")
Expand Down Expand Up @@ -442,12 +448,13 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur

// check if revision is available
if c.Revision == nil {
if !inSet || remainingLeeway == 0 {
if inSet && remainingLeeway > 0 {
logger.Debug("keeping contract due to leeway")
keepContract(c.ContractMetadata, host)
remainingLeeway--
} else {
logger.Debug("ignoring contract without revision")
dropOutReasons[c.ID] = errContractNoRevision.Error()
} else if ctx.ShouldFilterRedundantIPs() && ipFilter.HasRedundantIP(host) {
logger.Debug("keeping contract due to leeway")
filteredContracts = append(filteredContracts, c.ContractMetadata)
}
continue // no more checks without revision
}
Expand Down Expand Up @@ -529,8 +536,7 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur

// we keep the contract, add the host to the filter
logger.Debug("contract is usable and is added / stays in set")
ipFilter.Add(host)
filteredContracts = append(filteredContracts, contract)
keepContract(contract, host)
}
return nil
}(); err != nil {
Expand Down Expand Up @@ -656,8 +662,7 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur
}

// add new contract and host
filteredContracts = append(filteredContracts, formedContract)
ipFilter.Add(candidate.host)
keepContract(formedContract, candidate.host)
formed++
}

Expand Down
2 changes: 2 additions & 0 deletions stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func ContractSizes(ctx context.Context, tx sql.Tx) (map[types.FileContractID]api
if err != nil {
return nil, fmt.Errorf("failed to fetch contract sizes: %w", err)
}
defer rows.Close()

sizes := make(map[types.FileContractID]api.ContractSize)
for rows.Next() {
Expand Down Expand Up @@ -2211,6 +2212,7 @@ func Settings(ctx context.Context, tx sql.Tx) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("failed to query settings: %w", err)
}
defer rows.Close()
var settings []string
for rows.Next() {
var setting string
Expand Down

0 comments on commit c634fd9

Please sign in to comment.