forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedRoundRobinLoadBalancerProvider.java
More file actions
115 lines (103 loc) · 4.17 KB
/
WeightedRoundRobinLoadBalancerProvider.java
File metadata and controls
115 lines (103 loc) · 4.17 KB
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
/*
* Copyright 2023 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.xds;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.Deadline;
import io.grpc.Internal;
import io.grpc.LoadBalancer;
import io.grpc.LoadBalancer.Helper;
import io.grpc.LoadBalancerProvider;
import io.grpc.NameResolver.ConfigOrError;
import io.grpc.Status;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.JsonUtil;
import io.grpc.xds.WeightedRoundRobinLoadBalancer.WeightedRoundRobinLoadBalancerConfig;
import java.util.List;
import java.util.Map;
/**
* Provides a {@link WeightedRoundRobinLoadBalancer}.
* */
@Internal
public final class WeightedRoundRobinLoadBalancerProvider extends LoadBalancerProvider {
@VisibleForTesting
static final long MIN_WEIGHT_UPDATE_PERIOD_NANOS = 100_000_000L; // 100ms
static final String SCHEME = "weighted_round_robin";
@Override
public LoadBalancer newLoadBalancer(Helper helper) {
return new WeightedRoundRobinLoadBalancer(helper, Deadline.getSystemTicker());
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public int getPriority() {
return 5;
}
@Override
public String getPolicyName() {
return SCHEME;
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
try {
return parseLoadBalancingPolicyConfigInternal(rawConfig);
} catch (RuntimeException e) {
return ConfigOrError.fromError(
Status.UNAVAILABLE.withCause(e).withDescription(
"Failed parsing configuration for " + getPolicyName()));
}
}
private ConfigOrError parseLoadBalancingPolicyConfigInternal(Map<String, ?> rawConfig) {
Long blackoutPeriodNanos = JsonUtil.getStringAsDuration(rawConfig, "blackoutPeriod");
Long weightExpirationPeriodNanos =
JsonUtil.getStringAsDuration(rawConfig, "weightExpirationPeriod");
Long oobReportingPeriodNanos = JsonUtil.getStringAsDuration(rawConfig, "oobReportingPeriod");
Boolean enableOobLoadReport = JsonUtil.getBoolean(rawConfig, "enableOobLoadReport");
Long weightUpdatePeriodNanos = JsonUtil.getStringAsDuration(rawConfig, "weightUpdatePeriod");
Float errorUtilizationPenalty = JsonUtil.getNumberAsFloat(rawConfig, "errorUtilizationPenalty");
List<String> metricNamesForComputingUtilization = JsonUtil.getListOfStrings(rawConfig,
"metricNamesForComputingUtilization");
WeightedRoundRobinLoadBalancerConfig.Builder configBuilder =
WeightedRoundRobinLoadBalancerConfig.newBuilder();
if (blackoutPeriodNanos != null) {
configBuilder.setBlackoutPeriodNanos(blackoutPeriodNanos);
}
if (weightExpirationPeriodNanos != null) {
configBuilder.setWeightExpirationPeriodNanos(weightExpirationPeriodNanos);
}
if (enableOobLoadReport != null) {
configBuilder.setEnableOobLoadReport(enableOobLoadReport);
}
if (oobReportingPeriodNanos != null) {
configBuilder.setOobReportingPeriodNanos(oobReportingPeriodNanos);
}
if (weightUpdatePeriodNanos != null) {
configBuilder.setWeightUpdatePeriodNanos(weightUpdatePeriodNanos);
if (weightUpdatePeriodNanos < MIN_WEIGHT_UPDATE_PERIOD_NANOS) {
configBuilder.setWeightUpdatePeriodNanos(MIN_WEIGHT_UPDATE_PERIOD_NANOS);
}
}
if (errorUtilizationPenalty != null) {
configBuilder.setErrorUtilizationPenalty(errorUtilizationPenalty);
}
if (metricNamesForComputingUtilization != null
&& GrpcUtil.getFlag("GRPC_EXPERIMENTAL_WRR_CUSTOM_METRICS", false)) {
configBuilder.setMetricNamesForComputingUtilization(metricNamesForComputingUtilization);
}
return ConfigOrError.fromConfig(configBuilder.build());
}
}