go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/engine/policy/newest_first.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package policy
    16  
    17  import (
    18  	"sort"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/auth/identity"
    22  	"go.chromium.org/luci/common/errors"
    23  
    24  	"go.chromium.org/luci/scheduler/appengine/internal"
    25  )
    26  
    27  // NewestFirstPolicy instantiates new NEWEST_FIRST policy function.
    28  //
    29  // It takes the most newly added pending triggers and creates invocations
    30  // for them. It also discards triggers that have been pending for a long time.
    31  func NewestFirstPolicy(maxConcurrentInvs int, pendingTimeout time.Duration) (Func, error) {
    32  	switch {
    33  	case maxConcurrentInvs <= 0:
    34  		return nil, errors.Reason("max_concurrent_invocations should be positive").Err()
    35  	case pendingTimeout <= 0:
    36  		return nil, errors.Reason("pending_timeout should be positive").Err()
    37  	}
    38  
    39  	return func(env Environment, in In) (out Out) {
    40  		// Split the triggers list into what we're going to discard and what's going to be invoked.
    41  		// Every trigger that's exceeded the pending timeout is going to be discarded.
    42  		triggers := in.Triggers
    43  		i := sort.Search(len(triggers), func(i int) bool {
    44  			pendingTime := in.Now.Sub(triggers[i].Created.AsTime())
    45  			return pendingTime < pendingTimeout
    46  		})
    47  		out.Discard = triggers[:i]
    48  		invoke := triggers[i:]
    49  
    50  		// Determine how many available concurrent invocations we have.
    51  		slots := maxConcurrentInvs - len(in.ActiveInvocations)
    52  		if slots <= 0 {
    53  			// Exit early since we can't fill any slots anyway.
    54  			env.DebugLog(
    55  				"Max concurrent invocations is %d and there's %d running => refusing to launch more",
    56  				maxConcurrentInvs, len(in.ActiveInvocations))
    57  			return // maxed all available slots
    58  		}
    59  
    60  		// Prune the invoke list down to the most recent triggers that will fit in the
    61  		// available invocation slots.
    62  		if len(invoke) > slots {
    63  			invoke = invoke[len(invoke)-slots:]
    64  		}
    65  
    66  		// Create requests for everything left on the invoke list.
    67  		for _, t := range invoke {
    68  			// One trigger maps to one request. There's no batching here.
    69  			req := RequestBuilder{env: env}
    70  			req.FromTrigger(t)
    71  			req.IncomingTriggers = []*internal.Trigger{t}
    72  			req.TriggeredBy = identity.Identity(t.EmittedByUser)
    73  
    74  			// Add the request to the request list.
    75  			out.Requests = append(out.Requests, req.Request)
    76  		}
    77  
    78  		return
    79  	}, nil
    80  }