-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
GrpclbClientLoadRecorder.java
157 lines (138 loc) · 5.42 KB
/
GrpclbClientLoadRecorder.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
/*
* Copyright 2017 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.grpclb;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.protobuf.util.Timestamps;
import io.grpc.ClientStreamTracer;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.internal.TimeProvider;
import io.grpc.lb.v1.ClientStats;
import io.grpc.lb.v1.ClientStatsPerToken;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
/**
* Record and aggregate client-side load data for GRPCLB. This records load occurred during the
* span of an LB stream with the remote load-balancer.
*/
@ThreadSafe
final class GrpclbClientLoadRecorder extends ClientStreamTracer.Factory {
private static final AtomicLongFieldUpdater<GrpclbClientLoadRecorder> callsStartedUpdater =
AtomicLongFieldUpdater.newUpdater(GrpclbClientLoadRecorder.class, "callsStarted");
private static final AtomicLongFieldUpdater<GrpclbClientLoadRecorder> callsFinishedUpdater =
AtomicLongFieldUpdater.newUpdater(GrpclbClientLoadRecorder.class, "callsFinished");
private static final AtomicLongFieldUpdater<GrpclbClientLoadRecorder> callsFailedToSendUpdater =
AtomicLongFieldUpdater.newUpdater(GrpclbClientLoadRecorder.class, "callsFailedToSend");
private static final AtomicLongFieldUpdater<GrpclbClientLoadRecorder>
callsFinishedKnownReceivedUpdater =
AtomicLongFieldUpdater.newUpdater(
GrpclbClientLoadRecorder.class, "callsFinishedKnownReceived");
private final TimeProvider time;
@SuppressWarnings("unused")
private volatile long callsStarted;
@SuppressWarnings("unused")
private volatile long callsFinished;
private static final class LongHolder {
long num;
}
// Specific finish types
@GuardedBy("this")
private Map<String, LongHolder> callsDroppedPerToken = new HashMap<>(1);
@SuppressWarnings("unused")
private volatile long callsFailedToSend;
@SuppressWarnings("unused")
private volatile long callsFinishedKnownReceived;
GrpclbClientLoadRecorder(TimeProvider time) {
this.time = checkNotNull(time, "time provider");
}
@Override
public ClientStreamTracer newClientStreamTracer(
ClientStreamTracer.StreamInfo info, Metadata headers) {
callsStartedUpdater.getAndIncrement(this);
return new StreamTracer();
}
/**
* Records that a request has been dropped as instructed by the remote balancer.
*/
void recordDroppedRequest(String token) {
callsStartedUpdater.getAndIncrement(this);
callsFinishedUpdater.getAndIncrement(this);
synchronized (this) {
LongHolder holder;
if ((holder = callsDroppedPerToken.get(token)) == null) {
callsDroppedPerToken.put(token, (holder = new LongHolder()));
}
holder.num++;
}
}
/**
* Generate the report with the data recorded this LB stream since the last report.
*/
ClientStats generateLoadReport() {
ClientStats.Builder statsBuilder =
ClientStats.newBuilder()
.setTimestamp(Timestamps.fromNanos(time.currentTimeNanos()))
.setNumCallsStarted(callsStartedUpdater.getAndSet(this, 0))
.setNumCallsFinished(callsFinishedUpdater.getAndSet(this, 0))
.setNumCallsFinishedWithClientFailedToSend(callsFailedToSendUpdater.getAndSet(this, 0))
.setNumCallsFinishedKnownReceived(callsFinishedKnownReceivedUpdater.getAndSet(this, 0));
Map<String, LongHolder> localCallsDroppedPerToken = Collections.emptyMap();
synchronized (this) {
if (!callsDroppedPerToken.isEmpty()) {
localCallsDroppedPerToken = callsDroppedPerToken;
callsDroppedPerToken = new HashMap<>(localCallsDroppedPerToken.size());
}
}
for (Map.Entry<String, LongHolder> entry : localCallsDroppedPerToken.entrySet()) {
statsBuilder.addCallsFinishedWithDrop(
ClientStatsPerToken.newBuilder()
.setLoadBalanceToken(entry.getKey())
.setNumCalls(entry.getValue().num)
.build());
}
return statsBuilder.build();
}
private class StreamTracer extends ClientStreamTracer {
private volatile boolean headersSent;
private volatile boolean anythingReceived;
@Override
public void outboundHeaders() {
headersSent = true;
}
@Override
public void inboundHeaders() {
anythingReceived = true;
}
@Override
public void inboundMessage(int seqNo) {
anythingReceived = true;
}
@Override
public void streamClosed(Status status) {
callsFinishedUpdater.getAndIncrement(GrpclbClientLoadRecorder.this);
if (!headersSent) {
callsFailedToSendUpdater.getAndIncrement(GrpclbClientLoadRecorder.this);
}
if (anythingReceived) {
callsFinishedKnownReceivedUpdater.getAndIncrement(GrpclbClientLoadRecorder.this);
}
}
}
}