forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomBackendMetricsLoadBalancerProvider.java
154 lines (132 loc) · 5.18 KB
/
CustomBackendMetricsLoadBalancerProvider.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2022 The 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 io.grpc.testing.integration;
import static io.grpc.testing.integration.AbstractInteropTest.ORCA_OOB_REPORT_KEY;
import static io.grpc.testing.integration.AbstractInteropTest.ORCA_RPC_REPORT_KEY;
import io.grpc.ConnectivityState;
import io.grpc.LoadBalancer;
import io.grpc.LoadBalancerProvider;
import io.grpc.LoadBalancerRegistry;
import io.grpc.services.MetricReport;
import io.grpc.testing.integration.Messages.TestOrcaReport;
import io.grpc.util.ForwardingLoadBalancer;
import io.grpc.util.ForwardingLoadBalancerHelper;
import io.grpc.xds.orca.OrcaOobUtil;
import io.grpc.xds.orca.OrcaPerRequestUtil;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/**
* Implements a test LB policy that receives ORCA load reports.
*/
final class CustomBackendMetricsLoadBalancerProvider extends LoadBalancerProvider {
static final String TEST_ORCA_LB_POLICY_NAME = "test_backend_metrics_load_balancer";
private volatile TestOrcaReport latestOobReport;
@Override
public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) {
return new CustomBackendMetricsLoadBalancer(helper);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public int getPriority() {
return 0;
}
@Override
public String getPolicyName() {
return TEST_ORCA_LB_POLICY_NAME;
}
private final class CustomBackendMetricsLoadBalancer extends ForwardingLoadBalancer {
private LoadBalancer delegate;
public CustomBackendMetricsLoadBalancer(Helper helper) {
this.delegate = LoadBalancerRegistry.getDefaultRegistry()
.getProvider("pick_first")
.newLoadBalancer(new CustomBackendMetricsLoadBalancerHelper(helper));
}
@Override
public LoadBalancer delegate() {
return delegate;
}
private final class CustomBackendMetricsLoadBalancerHelper
extends ForwardingLoadBalancerHelper {
private final Helper orcaHelper;
public CustomBackendMetricsLoadBalancerHelper(Helper helper) {
this.orcaHelper = OrcaOobUtil.newOrcaReportingHelper(helper);
}
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
Subchannel subchannel = super.createSubchannel(args);
OrcaOobUtil.setListener(subchannel, new OrcaOobUtil.OrcaOobReportListener() {
@Override
public void onLoadReport(MetricReport orcaLoadReport) {
latestOobReport = fromCallMetricReport(orcaLoadReport);
}
},
OrcaOobUtil.OrcaReportingConfig.newBuilder()
.setReportInterval(1, TimeUnit.SECONDS)
.build()
);
return subchannel;
}
@Override
public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
delegate().updateBalancingState(newState, new MayReportLoadPicker(newPicker));
}
@Override
public Helper delegate() {
return orcaHelper;
}
}
private final class MayReportLoadPicker extends SubchannelPicker {
private SubchannelPicker delegate;
public MayReportLoadPicker(SubchannelPicker delegate) {
this.delegate = delegate;
}
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
PickResult result = delegate.pickSubchannel(args);
if (result.getSubchannel() == null) {
return result;
}
AtomicReference<TestOrcaReport> reportRef =
args.getCallOptions().getOption(ORCA_OOB_REPORT_KEY);
if (reportRef != null) {
reportRef.set(latestOobReport);
}
return PickResult.withSubchannel(
result.getSubchannel(),
OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(
new OrcaPerRequestUtil.OrcaPerRequestReportListener() {
@Override
public void onLoadReport(MetricReport callMetricReport) {
AtomicReference<TestOrcaReport> reportRef =
args.getCallOptions().getOption(ORCA_RPC_REPORT_KEY);
reportRef.set(fromCallMetricReport(callMetricReport));
}
}));
}
}
}
private static TestOrcaReport fromCallMetricReport(MetricReport callMetricReport) {
return TestOrcaReport.newBuilder()
.setCpuUtilization(callMetricReport.getCpuUtilization())
.setMemoryUtilization(callMetricReport.getMemoryUtilization())
.putAllRequestCost(callMetricReport.getRequestCostMetrics())
.putAllUtilization(callMetricReport.getUtilizationMetrics())
.build();
}
}