diff --git a/core/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java b/core/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java index 66380b9631bb..da1eaa6c409e 100644 --- a/core/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java +++ b/core/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java @@ -199,24 +199,7 @@ protected Helper delegate() { public Subchannel createSubchannel(CreateSubchannelArgs args) { // Subchannels are wrapped so that we can monitor call results and to trigger failures when // we decide to eject the subchannel. - OutlierDetectionSubchannel subchannel = new OutlierDetectionSubchannel( - delegate.createSubchannel(args)); - - // If the subchannel is associated with a single address that is also already in the map - // the subchannel will be added to the map and be included in outlier detection. - List addressGroups = subchannel.getAllAddresses(); - if (hasSingleAddress(addressGroups) - && trackerMap.containsKey(addressGroups.get(0).getAddresses().get(0))) { - AddressTracker tracker = trackerMap.get(addressGroups.get(0).getAddresses().get(0)); - tracker.addSubchannel(subchannel); - - // If this address has already been ejected, we need to immediately eject this Subchannel. - if (tracker.ejectionTimeNanos != null) { - subchannel.eject(); - } - } - - return subchannel; + return new OutlierDetectionSubchannel(delegate.createSubchannel(args)); } @Override @@ -345,6 +328,24 @@ class OutlierDetectionSubchannelStateListener implements SubchannelStateListener @Override public void onSubchannelState(ConnectivityStateInfo newState) { lastSubchannelState = newState; + + if (newState.getState().equals(ConnectivityState.READY)) { + // If the subchannel is associated with a single address that is also already in the map + // the subchannel will be added to the map and be included in outlier detection. + List addressGroups = getAllAddresses(); + if (hasSingleAddress(addressGroups) + && trackerMap.containsKey(addressGroups.get(0).getAddresses().get(0))) { + AddressTracker tracker = trackerMap.get(addressGroups.get(0).getAddresses().get(0)); + tracker.addSubchannel(OutlierDetectionSubchannel.this); + + // If this address has already been ejected, we need to immediately eject this + // Subchannel. + if (tracker.ejectionTimeNanos != null) { + eject(); + } + } + } + if (!ejected) { delegate.onSubchannelState(newState); }