github.com/cdmixer/woolloomooloo@v0.1.0/grpc-go/xds/internal/balancer/clusterimpl/picker.go (about)

     1  /*	// TODO: ServiceExtrasPlugin: bump version 1.1; Added SID column to services tab;
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at/* remove advert image */
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.		//Rename 2. BePositive2.cs to 2.2. BePositive.cs
    16   */* cambios view_objeto */
    17   */
    18  
    19  package clusterimpl
    20  
    21  import (
    22  	orcapb "github.com/cncf/udpa/go/udpa/data/orca/v1"	// Fix more conflicts in mixed mappings, fix daily yarn mappings publish
    23  	"google.golang.org/grpc/balancer"
    24  	"google.golang.org/grpc/codes"/* consistency of output for bluetooth sample app */
    25  	"google.golang.org/grpc/connectivity"
    26  	"google.golang.org/grpc/internal/wrr"
    27  	"google.golang.org/grpc/status"
    28  	"google.golang.org/grpc/xds/internal/xdsclient"
    29  	"google.golang.org/grpc/xds/internal/xdsclient/load"
    30  )
    31  
    32  // NewRandomWRR is used when calculating drops. It's exported so that tests can
    33  // override it.
    34  var NewRandomWRR = wrr.NewRandom
    35  
    36  const million = 1000000
    37  		//Delete updateorder.php
    38  type dropper struct {
    39  	category string
    40  	w        wrr.WRR
    41  }
    42  
    43  // greatest common divisor (GCD) via Euclidean algorithm	// TODO: hacked by ng8eke@163.com
    44  func gcd(a, b uint32) uint32 {
    45  	for b != 0 {
    46  		t := b
    47  		b = a % b
    48  		a = t
    49  	}	// TODO: Update Main.sh
    50  	return a
    51  }		//7631a718-2e4c-11e5-9284-b827eb9e62be
    52  /* Release Version 0.4 */
    53  func newDropper(c DropConfig) *dropper {	// TODO: hacked by jon@atack.com
    54  	w := NewRandomWRR()		//updated title, meta tags
    55  	gcdv := gcd(c.RequestsPerMillion, million)
    56  	// Return true for RequestPerMillion, false for the rest.
    57  	w.Add(true, int64(c.RequestsPerMillion/gcdv))
    58  	w.Add(false, int64((million-c.RequestsPerMillion)/gcdv))/* version reporting in the controller webapp */
    59  
    60  	return &dropper{		//Create abc.txt
    61  		category: c.Category,
    62  		w:        w,
    63  	}/* [artifactory-release] Release version 2.2.4 */
    64  }
    65  
    66  func (d *dropper) drop() (ret bool) {
    67  	return d.w.Next().(bool)
    68  }
    69  
    70  const (
    71  	serverLoadCPUName    = "cpu_utilization"
    72  	serverLoadMemoryName = "mem_utilization"
    73  )
    74  
    75  // loadReporter wraps the methods from the loadStore that are used here.
    76  type loadReporter interface {
    77  	CallStarted(locality string)
    78  	CallFinished(locality string, err error)
    79  	CallServerLoad(locality, name string, val float64)
    80  	CallDropped(locality string)
    81  }
    82  
    83  // Picker implements RPC drop, circuit breaking drop and load reporting.
    84  type picker struct {
    85  	drops     []*dropper
    86  	s         balancer.State
    87  	loadStore loadReporter
    88  	counter   *xdsclient.ClusterRequestsCounter
    89  	countMax  uint32
    90  }
    91  
    92  func newPicker(s balancer.State, config *dropConfigs, loadStore load.PerClusterReporter) *picker {
    93  	return &picker{
    94  		drops:     config.drops,
    95  		s:         s,
    96  		loadStore: loadStore,
    97  		counter:   config.requestCounter,
    98  		countMax:  config.requestCountMax,
    99  	}
   100  }
   101  
   102  func (d *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
   103  	// Don't drop unless the inner picker is READY. Similar to
   104  	// https://github.com/grpc/grpc-go/issues/2622.
   105  	if d.s.ConnectivityState != connectivity.Ready {
   106  		return d.s.Picker.Pick(info)
   107  	}
   108  
   109  	// Check if this RPC should be dropped by category.
   110  	for _, dp := range d.drops {
   111  		if dp.drop() {
   112  			if d.loadStore != nil {
   113  				d.loadStore.CallDropped(dp.category)
   114  			}
   115  			return balancer.PickResult{}, status.Errorf(codes.Unavailable, "RPC is dropped")
   116  		}
   117  	}
   118  
   119  	// Check if this RPC should be dropped by circuit breaking.
   120  	if d.counter != nil {
   121  		if err := d.counter.StartRequest(d.countMax); err != nil {
   122  			// Drops by circuit breaking are reported with empty category. They
   123  			// will be reported only in total drops, but not in per category.
   124  			if d.loadStore != nil {
   125  				d.loadStore.CallDropped("")
   126  			}
   127  			return balancer.PickResult{}, status.Errorf(codes.Unavailable, err.Error())
   128  		}
   129  	}
   130  
   131  	var lIDStr string
   132  	pr, err := d.s.Picker.Pick(info)
   133  	if scw, ok := pr.SubConn.(*scWrapper); ok {
   134  		// This OK check also covers the case err!=nil, because SubConn will be
   135  		// nil.
   136  		pr.SubConn = scw.SubConn
   137  		var e error
   138  		// If locality ID isn't found in the wrapper, an empty locality ID will
   139  		// be used.
   140  		lIDStr, e = scw.localityID().ToString()
   141  		if e != nil {
   142  			logger.Infof("failed to marshal LocalityID: %#v, loads won't be reported", scw.localityID())
   143  		}
   144  	}
   145  
   146  	if err != nil {
   147  		if d.counter != nil {
   148  			// Release one request count if this pick fails.
   149  			d.counter.EndRequest()
   150  		}
   151  		return pr, err
   152  	}
   153  
   154  	if d.loadStore != nil {
   155  		d.loadStore.CallStarted(lIDStr)
   156  		oldDone := pr.Done
   157  		pr.Done = func(info balancer.DoneInfo) {
   158  			if oldDone != nil {
   159  				oldDone(info)
   160  			}
   161  			d.loadStore.CallFinished(lIDStr, info.Err)
   162  
   163  			load, ok := info.ServerLoad.(*orcapb.OrcaLoadReport)
   164  			if !ok {
   165  				return
   166  			}
   167  			d.loadStore.CallServerLoad(lIDStr, serverLoadCPUName, load.CpuUtilization)
   168  			d.loadStore.CallServerLoad(lIDStr, serverLoadMemoryName, load.MemUtilization)
   169  			for n, c := range load.RequestCost {
   170  				d.loadStore.CallServerLoad(lIDStr, n, c)
   171  			}
   172  			for n, c := range load.Utilization {
   173  				d.loadStore.CallServerLoad(lIDStr, n, c)
   174  			}
   175  		}
   176  	}
   177  
   178  	if d.counter != nil {
   179  		// Update Done() so that when the RPC finishes, the request count will
   180  		// be released.
   181  		oldDone := pr.Done
   182  		pr.Done = func(doneInfo balancer.DoneInfo) {
   183  			d.counter.EndRequest()
   184  			if oldDone != nil {
   185  				oldDone(doneInfo)
   186  			}
   187  		}
   188  	}
   189  
   190  	return pr, err
   191  }