...

Source file src/google.golang.org/grpc/xds/internal/balancer/clusterresolver/config.go

Documentation: google.golang.org/grpc/xds/internal/balancer/clusterresolver

     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/internal/xds/bootstrap"
    27  	"google.golang.org/grpc/serviceconfig"
    28  	"google.golang.org/grpc/xds/internal/balancer/outlierdetection"
    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  	// TelemetryLabels are the telemetry labels associated with this cluster.
   107  	TelemetryLabels  map[string]string `json:"telemetryLabels,omitempty"`
   108  	outlierDetection outlierdetection.LBConfig
   109  }
   110  
   111  // Equal returns whether the DiscoveryMechanism is the same with the parameter.
   112  func (dm DiscoveryMechanism) Equal(b DiscoveryMechanism) bool {
   113  	od := &dm.outlierDetection
   114  	switch {
   115  	case dm.Cluster != b.Cluster:
   116  		return false
   117  	case !equalUint32P(dm.MaxConcurrentRequests, b.MaxConcurrentRequests):
   118  		return false
   119  	case dm.Type != b.Type:
   120  		return false
   121  	case dm.EDSServiceName != b.EDSServiceName:
   122  		return false
   123  	case dm.DNSHostname != b.DNSHostname:
   124  		return false
   125  	case !od.EqualIgnoringChildPolicy(&b.outlierDetection):
   126  		return false
   127  	}
   128  
   129  	if dm.LoadReportingServer == nil && b.LoadReportingServer == nil {
   130  		return true
   131  	}
   132  	if (dm.LoadReportingServer != nil) != (b.LoadReportingServer != nil) {
   133  		return false
   134  	}
   135  	return dm.LoadReportingServer.String() == b.LoadReportingServer.String()
   136  }
   137  
   138  func equalUint32P(a, b *uint32) bool {
   139  	if a == nil && b == nil {
   140  		return true
   141  	}
   142  	if a == nil || b == nil {
   143  		return false
   144  	}
   145  	return *a == *b
   146  }
   147  
   148  // LBConfig is the config for cluster resolver balancer.
   149  type LBConfig struct {
   150  	serviceconfig.LoadBalancingConfig `json:"-"`
   151  	// DiscoveryMechanisms is an ordered list of discovery mechanisms.
   152  	//
   153  	// Must have at least one element. Results from each discovery mechanism are
   154  	// concatenated together in successive priorities.
   155  	DiscoveryMechanisms []DiscoveryMechanism `json:"discoveryMechanisms,omitempty"`
   156  
   157  	// XDSLBPolicy specifies the policy for locality picking and endpoint picking.
   158  	XDSLBPolicy json.RawMessage `json:"xdsLbPolicy,omitempty"`
   159  	xdsLBPolicy internalserviceconfig.BalancerConfig
   160  }
   161  

View as plain text