istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/loadbalancersim/loadbalancer/weight.go (about)

     1  //  Copyright Istio 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 loadbalancer
    16  
    17  import (
    18  	mesh2 "istio.io/istio/pkg/test/loadbalancersim/mesh"
    19  	network2 "istio.io/istio/pkg/test/loadbalancersim/network"
    20  	"istio.io/istio/pkg/test/loadbalancersim/timeseries"
    21  )
    22  
    23  type WeightedConnection struct {
    24  	network2.Connection
    25  	Weight uint32
    26  }
    27  
    28  type weightedConnections struct {
    29  	conns  []*WeightedConnection
    30  	helper *network2.ConnectionHelper
    31  }
    32  
    33  func newLBConnection(name string, conns []*WeightedConnection) *weightedConnections {
    34  	return &weightedConnections{
    35  		conns:  conns,
    36  		helper: network2.NewConnectionHelper(name),
    37  	}
    38  }
    39  
    40  func (lb *weightedConnections) AllWeightsEqual() bool {
    41  	if len(lb.conns) == 0 {
    42  		return true
    43  	}
    44  
    45  	weight := lb.conns[0].Weight
    46  	for _, conn := range lb.conns {
    47  		if conn.Weight != weight {
    48  			return false
    49  		}
    50  	}
    51  	return true
    52  }
    53  
    54  func (lb *weightedConnections) get(index int) *WeightedConnection {
    55  	return lb.conns[index]
    56  }
    57  
    58  func (lb *weightedConnections) doRequest(c *WeightedConnection, onDone func()) {
    59  	lb.helper.Request(c.Request, onDone)
    60  }
    61  
    62  func (lb *weightedConnections) Name() string {
    63  	return lb.helper.Name()
    64  }
    65  
    66  func (lb *weightedConnections) TotalRequests() uint64 {
    67  	return lb.helper.TotalRequests()
    68  }
    69  
    70  func (lb *weightedConnections) ActiveRequests() uint64 {
    71  	return lb.helper.ActiveRequests()
    72  }
    73  
    74  func (lb *weightedConnections) Latency() *timeseries.Instance {
    75  	return lb.helper.Latency()
    76  }
    77  
    78  type WeightedConnectionFactory func(src *mesh2.Client, n *mesh2.Node) *WeightedConnection
    79  
    80  func EquallyWeightedConnectionFactory() WeightedConnectionFactory {
    81  	return func(src *mesh2.Client, dest *mesh2.Node) *WeightedConnection {
    82  		return &WeightedConnection{
    83  			Connection: src.Mesh().NewConnection(src, dest),
    84  			Weight:     1,
    85  		}
    86  	}
    87  }
    88  
    89  func PriorityWeightedConnectionFactory(selectPriority PrioritySelector, priorityWeightMap map[uint32]uint32) WeightedConnectionFactory {
    90  	return func(src *mesh2.Client, dest *mesh2.Node) *WeightedConnection {
    91  		// Select the priority for this node.
    92  		priority := selectPriority(src, dest)
    93  
    94  		// Get the weight for the priority.
    95  		weight := uint32(1)
    96  		if priorityWeightMap != nil {
    97  			if w := priorityWeightMap[priority]; w > 0 {
    98  				weight = w
    99  			}
   100  		}
   101  
   102  		return &WeightedConnection{
   103  			Connection: src.Mesh().NewConnection(src, dest),
   104  			Weight:     weight,
   105  		}
   106  	}
   107  }