-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
LoadStatsStoreImpl.java
158 lines (147 loc) · 6.26 KB
/
LoadStatsStoreImpl.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
155
156
157
158
/*
* Copyright 2019 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 static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
import io.grpc.internal.GrpcUtil;
import io.grpc.xds.ClientLoadCounter.ClientLoadSnapshot;
import io.grpc.xds.ClientLoadCounter.MetricValue;
import io.grpc.xds.EnvoyProtoData.ClusterStats;
import io.grpc.xds.EnvoyProtoData.ClusterStats.DroppedRequests;
import io.grpc.xds.EnvoyProtoData.EndpointLoadMetricStats;
import io.grpc.xds.EnvoyProtoData.Locality;
import io.grpc.xds.EnvoyProtoData.UpstreamLocalityStats;
import io.grpc.xds.LoadStatsManager.LoadStatsStore;
import io.grpc.xds.LoadStatsManager.LoadStatsStoreFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* An {@link LoadStatsStoreImpl} instance holds the load stats for a cluster from an gRPC
* client's perspective by maintaining a set of locality counters for each locality it is tracking
* loads for.
*/
// https://github.com/google/error-prone/issues/1767
@SuppressWarnings("ModifyCollectionInEnhancedForLoop")
@NotThreadSafe
final class LoadStatsStoreImpl implements LoadStatsStore {
private final String clusterName;
@Nullable
private final String clusterServiceName;
private final ConcurrentMap<Locality, ReferenceCounted<ClientLoadCounter>> localityLoadCounters
= new ConcurrentHashMap<>();
// Cluster level dropped request counts for each category decision made by xDS load balancer.
private final ConcurrentMap<String, AtomicLong> dropCounters;
private final Stopwatch stopwatch;
LoadStatsStoreImpl(String clusterName, @Nullable String clusterServiceName) {
this(clusterName, clusterServiceName, GrpcUtil.STOPWATCH_SUPPLIER.get(),
new ConcurrentHashMap<String, AtomicLong>());
}
@VisibleForTesting
LoadStatsStoreImpl(
String clusterName,
@Nullable String clusterServiceName,
Stopwatch stopwatch,
ConcurrentMap<String, AtomicLong> dropCounters) {
this.clusterName = checkNotNull(clusterName, "clusterName");
this.clusterServiceName = clusterServiceName;
this.stopwatch = checkNotNull(stopwatch, "stopwatch");
this.dropCounters = checkNotNull(dropCounters, "dropCounters");
stopwatch.reset().start();
}
@Override
public ClusterStats generateLoadReport() {
ClusterStats.Builder statsBuilder = ClusterStats.newBuilder();
statsBuilder.setClusterName(clusterName);
if (clusterServiceName != null) {
statsBuilder.setClusterServiceName(clusterServiceName);
}
for (Map.Entry<Locality, ReferenceCounted<ClientLoadCounter>> entry
: localityLoadCounters.entrySet()) {
ClientLoadSnapshot snapshot = entry.getValue().get().snapshot();
UpstreamLocalityStats.Builder localityStatsBuilder =
UpstreamLocalityStats.newBuilder().setLocality(entry.getKey());
localityStatsBuilder
.setTotalSuccessfulRequests(snapshot.getCallsSucceeded())
.setTotalErrorRequests(snapshot.getCallsFailed())
.setTotalRequestsInProgress(snapshot.getCallsInProgress())
.setTotalIssuedRequests(snapshot.getCallsIssued());
for (Map.Entry<String, MetricValue> metric : snapshot.getMetricValues().entrySet()) {
localityStatsBuilder.addLoadMetricStats(
EndpointLoadMetricStats.newBuilder()
.setMetricName(metric.getKey())
.setNumRequestsFinishedWithMetric(metric.getValue().getNumReports())
.setTotalMetricValue(metric.getValue().getTotalValue())
.build());
}
statsBuilder.addUpstreamLocalityStats(localityStatsBuilder.build());
// Discard counters for localities that are no longer exposed by the remote balancer and
// no RPCs ongoing.
if (entry.getValue().getReferenceCount() == 0 && snapshot.getCallsInProgress() == 0) {
localityLoadCounters.remove(entry.getKey());
}
}
long totalDrops = 0;
for (Map.Entry<String, AtomicLong> entry : dropCounters.entrySet()) {
long drops = entry.getValue().getAndSet(0);
totalDrops += drops;
statsBuilder.addDroppedRequests(new DroppedRequests(entry.getKey(),drops));
}
statsBuilder.setTotalDroppedRequests(totalDrops);
statsBuilder.setLoadReportIntervalNanos(stopwatch.elapsed(TimeUnit.NANOSECONDS));
stopwatch.reset().start();
return statsBuilder.build();
}
@Override
public ClientLoadCounter addLocality(final Locality locality) {
ReferenceCounted<ClientLoadCounter> counter = localityLoadCounters.get(locality);
if (counter == null) {
counter = ReferenceCounted.wrap(new ClientLoadCounter());
localityLoadCounters.put(locality, counter);
}
counter.retain();
return counter.get();
}
@Override
public void removeLocality(final Locality locality) {
ReferenceCounted<ClientLoadCounter> counter = localityLoadCounters.get(locality);
counter.release();
}
@Override
public void recordDroppedRequest(String category) {
AtomicLong counter = dropCounters.get(category);
if (counter == null) {
counter = dropCounters.putIfAbsent(category, new AtomicLong());
if (counter == null) {
counter = dropCounters.get(category);
}
}
counter.getAndIncrement();
}
static LoadStatsStoreFactory getDefaultFactory() {
return new LoadStatsStoreFactory() {
@Override
public LoadStatsStore newLoadStatsStore(String cluster, String clusterService) {
return new LoadStatsStoreImpl(cluster, clusterService);
}
};
}
}