golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/coordinator/pool/queue/item.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || darwin
     6  
     7  package queue
     8  
     9  import (
    10  	"strings"
    11  	"time"
    12  
    13  	"golang.org/x/build/internal/buildgo"
    14  )
    15  
    16  type BuildletPriority int
    17  
    18  const (
    19  	// PriorityUrgent is reserved for Go releases.
    20  	PriorityUrgent BuildletPriority = iota
    21  	PriorityInteractive
    22  	PriorityAutomated
    23  	PriorityBatch
    24  )
    25  
    26  // SchedItem is a specification of a requested buildlet in its
    27  // exported fields, and internal scheduler state used while waiting
    28  // for that buildlet.
    29  //
    30  // SchedItem is safe for copying.
    31  type SchedItem struct {
    32  	buildgo.BuilderRev // not set for gomote
    33  	HostType           string
    34  	IsRelease          bool
    35  	IsGomote           bool
    36  	IsTry              bool
    37  	IsHelper           bool
    38  	Repo               string
    39  	Branch             string // the Go repository branch
    40  
    41  	// CommitTime is the latest commit date of the relevant repos
    42  	// that make up the work being tested. (For example, x/foo
    43  	// being tested against master can have either x/foo commit
    44  	// being newer, or master being newer).
    45  	CommitTime  time.Time
    46  	RequestTime time.Time
    47  	User        string
    48  }
    49  
    50  // Priority returns the BuildletPriority for a SchedItem.
    51  func (s *SchedItem) Priority() BuildletPriority {
    52  	switch {
    53  	case s.IsRelease:
    54  		return PriorityUrgent
    55  	case s.IsGomote:
    56  		return PriorityInteractive
    57  	case s.IsTry:
    58  		return PriorityAutomated
    59  	default:
    60  		return PriorityBatch
    61  	}
    62  }
    63  
    64  func (s *SchedItem) SortTime() time.Time {
    65  	if s.IsGomote || s.IsTry || s.CommitTime.IsZero() {
    66  		return s.RequestTime
    67  	}
    68  	return s.CommitTime
    69  }
    70  
    71  // Less returns a boolean value of whether SchedItem is more important
    72  // than the provided SchedItem.
    73  func (s *SchedItem) Less(other *SchedItem) bool {
    74  	if s.Priority() != other.Priority() {
    75  		return s.Priority() < other.Priority()
    76  	}
    77  	// Release branch work tends to starve because the Go commits are old.
    78  	// Prioritize release branch work over other branches.
    79  	releaseBranch := func(i *SchedItem) bool { return strings.HasPrefix(s.Branch, "release-branch") }
    80  	if s.Priority() == PriorityAutomated && releaseBranch(s) != releaseBranch(other) {
    81  		return releaseBranch(s)
    82  	}
    83  	if s.Priority() == PriorityBatch {
    84  		// Batch items are completed in LIFO.
    85  		return s.SortTime().After(other.SortTime())
    86  	}
    87  	return other.SortTime().After(s.SortTime())
    88  }