google.golang.org/grpc@v1.62.1/xds/internal/balancer/clusterresolver/config.go (about)

     1  /*
     2   *
     3   * Copyright 2021 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
     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.
    16   */
    17  
    18  package clusterresolver
    19  
    20  import (
    21  	"bytes"
    22  	"encoding/json"
    23  	"fmt"
    24  
    25  	internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
    26  	"google.golang.org/grpc/serviceconfig"
    27  	"google.golang.org/grpc/xds/internal/balancer/outlierdetection"
    28  	"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
    29  )
    30  
    31  // DiscoveryMechanismType is the type of discovery mechanism.
    32  type DiscoveryMechanismType int
    33  
    34  const (
    35  	// DiscoveryMechanismTypeEDS is eds.
    36  	DiscoveryMechanismTypeEDS DiscoveryMechanismType = iota // `json:"EDS"`
    37  	// DiscoveryMechanismTypeLogicalDNS is DNS.
    38  	DiscoveryMechanismTypeLogicalDNS // `json:"LOGICAL_DNS"`
    39  )
    40  
    41  // MarshalJSON marshals a DiscoveryMechanismType to a quoted json string.
    42  //
    43  // This is necessary to handle enum (as strings) from JSON.
    44  //
    45  // Note that this needs to be defined on the type not pointer, otherwise the
    46  // variables of this type will marshal to int not string.
    47  func (t DiscoveryMechanismType) MarshalJSON() ([]byte, error) {
    48  	buffer := bytes.NewBufferString(`"`)
    49  	switch t {
    50  	case DiscoveryMechanismTypeEDS:
    51  		buffer.WriteString("EDS")
    52  	case DiscoveryMechanismTypeLogicalDNS:
    53  		buffer.WriteString("LOGICAL_DNS")
    54  	}
    55  	buffer.WriteString(`"`)
    56  	return buffer.Bytes(), nil
    57  }
    58  
    59  // UnmarshalJSON unmarshals a quoted json string to the DiscoveryMechanismType.
    60  func (t *DiscoveryMechanismType) UnmarshalJSON(b []byte) error {
    61  	var s string
    62  	err := json.Unmarshal(b, &s)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	switch s {
    67  	case "EDS":
    68  		*t = DiscoveryMechanismTypeEDS
    69  	case "LOGICAL_DNS":
    70  		*t = DiscoveryMechanismTypeLogicalDNS
    71  	default:
    72  		return fmt.Errorf("unable to unmarshal string %q to type DiscoveryMechanismType", s)
    73  	}
    74  	return nil
    75  }
    76  
    77  // DiscoveryMechanism is the discovery mechanism, can be either EDS or DNS.
    78  //
    79  // For DNS, the ClientConn target will be used for name resolution.
    80  //
    81  // For EDS, if EDSServiceName is not empty, it will be used for watching. If
    82  // EDSServiceName is empty, Cluster will be used.
    83  type DiscoveryMechanism struct {
    84  	// Cluster is the cluster name.
    85  	Cluster string `json:"cluster,omitempty"`
    86  	// LoadReportingServer is the LRS server to send load reports to. If not
    87  	// present, load reporting will be disabled.
    88  	LoadReportingServer *bootstrap.ServerConfig `json:"lrsLoadReportingServer,omitempty"`
    89  	// MaxConcurrentRequests is the maximum number of outstanding requests can
    90  	// be made to the upstream cluster. Default is 1024.
    91  	MaxConcurrentRequests *uint32 `json:"maxConcurrentRequests,omitempty"`
    92  	// Type is the discovery mechanism type.
    93  	Type DiscoveryMechanismType `json:"type,omitempty"`
    94  	// EDSServiceName is the EDS service name, as returned in CDS. May be unset
    95  	// if not specified in CDS. For type EDS only.
    96  	//
    97  	// This is used for EDS watch if set. If unset, Cluster is used for EDS
    98  	// watch.
    99  	EDSServiceName string `json:"edsServiceName,omitempty"`
   100  	// DNSHostname is the DNS name to resolve in "host:port" form. For type
   101  	// LOGICAL_DNS only.
   102  	DNSHostname string `json:"dnsHostname,omitempty"`
   103  	// OutlierDetection is the Outlier Detection LB configuration for this
   104  	// priority.
   105  	OutlierDetection json.RawMessage `json:"outlierDetection,omitempty"`
   106  	outlierDetection outlierdetection.LBConfig
   107  }
   108  
   109  // Equal returns whether the DiscoveryMechanism is the same with the parameter.
   110  func (dm DiscoveryMechanism) Equal(b DiscoveryMechanism) bool {
   111  	od := &dm.outlierDetection
   112  	switch {
   113  	case dm.Cluster != b.Cluster:
   114  		return false
   115  	case !equalUint32P(dm.MaxConcurrentRequests, b.MaxConcurrentRequests):
   116  		return false
   117  	case dm.Type != b.Type:
   118  		return false
   119  	case dm.EDSServiceName != b.EDSServiceName:
   120  		return false
   121  	case dm.DNSHostname != b.DNSHostname:
   122  		return false
   123  	case !od.EqualIgnoringChildPolicy(&b.outlierDetection):
   124  		return false
   125  	}
   126  
   127  	if dm.LoadReportingServer == nil && b.LoadReportingServer == nil {
   128  		return true
   129  	}
   130  	if (dm.LoadReportingServer != nil) != (b.LoadReportingServer != nil) {
   131  		return false
   132  	}
   133  	return dm.LoadReportingServer.String() == b.LoadReportingServer.String()
   134  }
   135  
   136  func equalUint32P(a, b *uint32) bool {
   137  	if a == nil && b == nil {
   138  		return true
   139  	}
   140  	if a == nil || b == nil {
   141  		return false
   142  	}
   143  	return *a == *b
   144  }
   145  
   146  // LBConfig is the config for cluster resolver balancer.
   147  type LBConfig struct {
   148  	serviceconfig.LoadBalancingConfig `json:"-"`
   149  	// DiscoveryMechanisms is an ordered list of discovery mechanisms.
   150  	//
   151  	// Must have at least one element. Results from each discovery mechanism are
   152  	// concatenated together in successive priorities.
   153  	DiscoveryMechanisms []DiscoveryMechanism `json:"discoveryMechanisms,omitempty"`
   154  
   155  	// XDSLBPolicy specifies the policy for locality picking and endpoint picking.
   156  	XDSLBPolicy json.RawMessage `json:"xdsLbPolicy,omitempty"`
   157  	xdsLBPolicy internalserviceconfig.BalancerConfig
   158  }