use of io.grpc.grpclb.GrpclbConstants.LbPolicy in project grpc-java by grpc.
the class GrpclbLoadBalancer method handleResolvedAddresses.
@Override
public void handleResolvedAddresses(List<ResolvedServerInfoGroup> updatedServers, Attributes attributes) {
LbPolicy newLbPolicy = attributes.get(GrpclbConstants.ATTR_LB_POLICY);
// LB addresses and backend addresses are treated separately
List<LbAddressGroup> newLbAddressGroups = new ArrayList<LbAddressGroup>();
List<ResolvedServerInfoGroup> newBackendServerInfoGroups = new ArrayList<ResolvedServerInfoGroup>();
for (ResolvedServerInfoGroup serverInfoGroup : updatedServers) {
String lbAddrAuthority = serverInfoGroup.getAttributes().get(GrpclbConstants.ATTR_LB_ADDR_AUTHORITY);
EquivalentAddressGroup eag = serverInfoGroup.toEquivalentAddressGroup();
if (lbAddrAuthority != null) {
newLbAddressGroups.add(new LbAddressGroup(eag, lbAddrAuthority));
} else {
newBackendServerInfoGroups.add(serverInfoGroup);
}
}
if (newBackendServerInfoGroups.isEmpty()) {
// handleResolvedAddresses()'s javadoc has guaranteed updatedServers is never empty.
checkState(!newLbAddressGroups.isEmpty(), "No backend address nor LB address. updatedServers=%s", updatedServers);
if (newLbPolicy != LbPolicy.GRPCLB) {
newLbPolicy = LbPolicy.GRPCLB;
logger.log(Level.FINE, "[{0}] Switching to GRPCLB because all addresses are balancers", logId);
}
}
if (newLbPolicy == null) {
logger.log(Level.FINE, "[{0}] New config missing policy. Using PICK_FIRST", logId);
newLbPolicy = LbPolicy.PICK_FIRST;
}
// Switch LB policy if requested
if (newLbPolicy != lbPolicy) {
shutdownDelegate();
shutdownLbComm();
lbAddressGroups = null;
currentLbIndex = 0;
switch(newLbPolicy) {
case PICK_FIRST:
delegate = checkNotNull(pickFirstBalancerFactory.newLoadBalancer(helper), "pickFirstBalancerFactory.newLoadBalancer()");
break;
case ROUND_ROBIN:
delegate = checkNotNull(roundRobinBalancerFactory.newLoadBalancer(helper), "roundRobinBalancerFactory.newLoadBalancer()");
break;
default:
}
}
lbPolicy = newLbPolicy;
// Consume the new addresses
switch(lbPolicy) {
case PICK_FIRST:
case ROUND_ROBIN:
checkNotNull(delegate, "delegate should not be null. newLbPolicy=" + newLbPolicy);
delegate.handleResolvedAddresses(newBackendServerInfoGroups, attributes);
break;
case GRPCLB:
if (newLbAddressGroups.isEmpty()) {
shutdownLbComm();
lbAddressGroups = null;
handleGrpclbError(Status.UNAVAILABLE.withDescription("NameResolver returned no LB address while asking for GRPCLB"));
} else {
// See if the currently used LB server is in the new list.
int newIndexOfCurrentLb = -1;
if (lbAddressGroups != null) {
LbAddressGroup currentLb = lbAddressGroups.get(currentLbIndex);
newIndexOfCurrentLb = newLbAddressGroups.indexOf(currentLb);
}
lbAddressGroups = newLbAddressGroups;
if (newIndexOfCurrentLb == -1) {
shutdownLbComm();
currentLbIndex = 0;
startLbComm();
} else {
// Current LB is still in the list, calibrate index.
currentLbIndex = newIndexOfCurrentLb;
}
}
break;
default:
}
}
Aggregations