Skip to content

lb: pick first metrics #12193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions core/src/main/java/io/grpc/internal/PickFirstLeafLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.grpc.ConnectivityStateInfo;
import io.grpc.EquivalentAddressGroup;
import io.grpc.LoadBalancer;
import io.grpc.LongCounterMetricInstrument;
import io.grpc.MetricInstrumentRegistry;
import io.grpc.Status;
import io.grpc.SynchronizationContext.ScheduledHandle;
import java.net.Inet4Address;
Expand All @@ -57,6 +59,9 @@
* list and sticking to the first that works.
*/
final class PickFirstLeafLoadBalancer extends LoadBalancer {
private static final LongCounterMetricInstrument DISCONNECTIONS;
private static final LongCounterMetricInstrument CONNECTION_ATTEMPTS_SUCCEEDED;
private static final LongCounterMetricInstrument CONNECTION_ATTEMPTS_FAILED;
private static final Logger log = Logger.getLogger(PickFirstLeafLoadBalancer.class.getName());
@VisibleForTesting
static final int CONNECTION_DELAY_INTERVAL_MS = 250;
Expand All @@ -78,6 +83,33 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
private ScheduledHandle reconnectTask = null;
private final boolean serializingRetries = isSerializingRetries();

// The metric instruments are only registered once and shared by all instances of this LB.
static {
MetricInstrumentRegistry metricInstrumentRegistry
= MetricInstrumentRegistry.getDefaultRegistry();
DISCONNECTIONS = metricInstrumentRegistry.registerLongCounter(
"grpc.lb.pick_first.disconnections",
"EXPERIMENTAL. Number of times the selected subchannel becomes disconnected",
"{disconnection}",
Lists.newArrayList("grpc.target"),
Lists.newArrayList(),
false);
CONNECTION_ATTEMPTS_SUCCEEDED = metricInstrumentRegistry.registerLongCounter(
"grpc.lb.pick_first.connection_attempts_succeeded",
"EXPERIMENTAL. Number of successful connection attempts",
"{attempt}",
Lists.newArrayList("grpc.target"),
Lists.newArrayList(),
false);
CONNECTION_ATTEMPTS_FAILED = metricInstrumentRegistry.registerLongCounter(
"grpc.lb.pick_first.connection_attempts_failed",
"EXPERIMENTAL. Number of failed connection attempts",
"{attempt}",
Lists.newArrayList("grpc.target"),
Lists.newArrayList(),
false);
}

PickFirstLeafLoadBalancer(Helper helper) {
this.helper = checkNotNull(helper, "helper");
}
Expand Down Expand Up @@ -276,6 +308,13 @@ void processSubchannelState(SubchannelData subchannelData, ConnectivityStateInfo
}
}

// if the previous state was ready, count it as a disconnection
if (rawConnectivityState == READY || concludedState == READY) {
helper.getMetricRecorder().addLongCounter(DISCONNECTIONS, 1,
Collections.singletonList(helper.getChannelTarget()),
Collections.emptyList());
}

switch (newState) {
case IDLE:
// Shutdown when ready: connect from beginning when prompted
Expand All @@ -293,11 +332,17 @@ void processSubchannelState(SubchannelData subchannelData, ConnectivityStateInfo
shutdownRemaining(subchannelData);
addressIndex.seekTo(getAddress(subchannelData.subchannel));
rawConnectivityState = READY;
helper.getMetricRecorder().addLongCounter(CONNECTION_ATTEMPTS_SUCCEEDED, 1,
Collections.singletonList(helper.getChannelTarget()),
Collections.emptyList());
updateHealthCheckedState(subchannelData);
break;

case TRANSIENT_FAILURE:
// If we are looking at current channel, request a connection if possible
helper.getMetricRecorder().addLongCounter(CONNECTION_ATTEMPTS_FAILED, 1,
Collections.singletonList(helper.getChannelTarget()),
Collections.emptyList());
if (addressIndex.isValid()
&& subchannels.get(addressIndex.getCurrentAddress()) == subchannelData) {
if (addressIndex.increment()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ public void healthCheckFlow() {

verify(mockHelper, atLeast(0)).getSynchronizationContext();
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verify(mockHelper, atLeast(0)).getChannelTarget();
verifyNoMoreInteractions(mockHelper);

// subchannel | state | health
Expand All @@ -525,6 +527,8 @@ public void healthCheckFlow() {
.getSubchannel()).isSameInstanceAs(mockSubchannel1);
verify(mockHelper, atLeast(0)).getSynchronizationContext();
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getChannelTarget();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verifyNoMoreInteractions(mockHelper);

healthListener2.onSubchannelState(ConnectivityStateInfo.forNonError(READY));
Expand Down Expand Up @@ -618,6 +622,8 @@ public void pickAfterResolutionAfterTransientValue() {
stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(CONNECTING));
verify(mockHelper, atLeast(0)).getSynchronizationContext();
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verify(mockHelper, atLeast(0)).getChannelTarget();
verifyNoMoreInteractions(mockHelper);
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());
}
Expand Down Expand Up @@ -650,6 +656,8 @@ public void pickWithDupAddressesUpDownUp() {
stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(CONNECTING));
verify(mockHelper, atLeast(0)).getSynchronizationContext();
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verify(mockHelper, atLeast(0)).getChannelTarget();
verifyNoMoreInteractions(mockHelper);
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());

Expand Down Expand Up @@ -684,6 +692,8 @@ public void pickWithDupEagsUpDownUp() {
stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(CONNECTING));
verify(mockHelper, atLeast(0)).getSynchronizationContext();
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verify(mockHelper, atLeast(0)).getChannelTarget();
verifyNoMoreInteractions(mockHelper);
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());

Expand Down Expand Up @@ -1859,6 +1869,8 @@ public void updateAddresses_identical_transient_failure() {
// No new connections are requested, subchannels responsible for completing their own backoff
verify(mockHelper, atLeast(0)).getSynchronizationContext(); // Don't care
verify(mockHelper, atLeast(0)).getScheduledExecutorService();
verify(mockHelper, atLeast(0)).getMetricRecorder();
verify(mockHelper, atLeast(0)).getChannelTarget();
verifyNoMoreInteractions(mockHelper);

// First connection attempt is successful
Expand Down Expand Up @@ -2923,6 +2935,11 @@ public String getAuthority() {
return null;
}

@Override
public String getChannelTarget() {
return null;
}

@Override
public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
// ignore
Expand Down
Loading