1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package xds contains various xds interop helpers for usage in interop tests.
package xds
import (
"encoding/json"
"fmt"
"sync"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/internal/pretty"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/serviceconfig"
)
func init() {
balancer.Register(rpcBehaviorBB{})
}
const name = "test.RpcBehaviorLoadBalancer"
type rpcBehaviorBB struct{}
func (rpcBehaviorBB) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Balancer {
b := &rpcBehaviorLB{
ClientConn: cc,
}
// round_robin child to complete balancer tree with a usable leaf policy and
// have RPCs actually work.
builder := balancer.Get(roundrobin.Name)
if builder == nil {
// Shouldn't happen, defensive programming. Registered from import of
// roundrobin package.
return nil
}
rr := builder.Build(b, bOpts)
if rr == nil {
// Shouldn't happen, defensive programming.
return nil
}
b.Balancer = rr
return b
}
func (rpcBehaviorBB) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
lbCfg := &lbConfig{}
if err := json.Unmarshal(s, lbCfg); err != nil {
return nil, fmt.Errorf("rpc-behavior-lb: unable to marshal lbConfig: %s, error: %v", string(s), err)
}
return lbCfg, nil
}
func (rpcBehaviorBB) Name() string {
return name
}
type lbConfig struct {
serviceconfig.LoadBalancingConfig `json:"-"`
RPCBehavior string `json:"rpcBehavior,omitempty"`
}
// rpcBehaviorLB is a load balancer that wraps a round robin balancer and
// appends the rpc-behavior metadata field to any metadata in pick results based
// on what is specified in configuration.
type rpcBehaviorLB struct {
// embed a ClientConn to wrap only UpdateState() operation
balancer.ClientConn
// embed a Balancer to wrap only UpdateClientConnState() operation
balancer.Balancer
mu sync.Mutex
cfg *lbConfig
}
func (b *rpcBehaviorLB) UpdateClientConnState(s balancer.ClientConnState) error {
lbCfg, ok := s.BalancerConfig.(*lbConfig)
if !ok {
return fmt.Errorf("test.RpcBehaviorLoadBalancer:received config with unexpected type %T: %s", s.BalancerConfig, pretty.ToJSON(s.BalancerConfig))
}
b.mu.Lock()
b.cfg = lbCfg
b.mu.Unlock()
return b.Balancer.UpdateClientConnState(balancer.ClientConnState{
ResolverState: s.ResolverState,
})
}
func (b *rpcBehaviorLB) UpdateState(state balancer.State) {
b.mu.Lock()
rpcBehavior := b.cfg.RPCBehavior
b.mu.Unlock()
b.ClientConn.UpdateState(balancer.State{
ConnectivityState: state.ConnectivityState,
Picker: newRPCBehaviorPicker(state.Picker, rpcBehavior),
})
}
// rpcBehaviorPicker wraps a picker and adds the rpc-behavior metadata field
// into the child pick result's metadata.
type rpcBehaviorPicker struct {
childPicker balancer.Picker
rpcBehavior string
}
// Pick appends the rpc-behavior metadata entry to the pick result of the child.
func (p *rpcBehaviorPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
pr, err := p.childPicker.Pick(info)
if err != nil {
return balancer.PickResult{}, err
}
pr.Metadata = metadata.Join(pr.Metadata, metadata.Pairs("rpc-behavior", p.rpcBehavior))
return pr, nil
}
func newRPCBehaviorPicker(childPicker balancer.Picker, rpcBehavior string) *rpcBehaviorPicker {
return &rpcBehaviorPicker{
childPicker: childPicker,
rpcBehavior: rpcBehavior,
}
}
|