Skip to content
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

grpclb: support "pick_first" child policy #5438

Merged
merged 17 commits into from
Mar 6, 2019
Merged
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
9 changes: 9 additions & 0 deletions grpclb/src/main/java/io/grpc/grpclb/GrpclbConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.grpc.grpclb;

import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.ExperimentalApi;
import io.grpc.Metadata;

Expand All @@ -32,5 +34,12 @@ public final class GrpclbConstants {
public static final Metadata.Key<String> TOKEN_METADATA_KEY =
Metadata.Key.of("lb-token", Metadata.ASCII_STRING_MARSHALLER);

/**
* For passing LB tokens via the EAG attributes.
*/
@EquivalentAddressGroup.Attr
static final Attributes.Key<String> TOKEN_ATTRIBUTE_KEY =
Attributes.Key.create("lb-token");

private GrpclbConstants() { }
}
26 changes: 21 additions & 5 deletions grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.grpclb;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.Attributes;
Expand Down Expand Up @@ -51,7 +52,11 @@ class GrpclbLoadBalancer extends LoadBalancer {
private static final Logger logger = Logger.getLogger(GrpclbLoadBalancer.class.getName());

private final Helper helper;
private final TimeProvider time;
private final SubchannelPool subchannelPool;
private final BackoffPolicy.Provider backoffPolicyProvider;

private Mode mode = Mode.ROUND_ROBIN;

// All mutable states in this class are mutated ONLY from Channel Executor
@Nullable
Expand All @@ -63,12 +68,12 @@ class GrpclbLoadBalancer extends LoadBalancer {
TimeProvider time,
BackoffPolicy.Provider backoffPolicyProvider) {
this.helper = checkNotNull(helper, "helper");
checkNotNull(time, "time provider");
checkNotNull(backoffPolicyProvider, "backoffPolicyProvider");
this.time = checkNotNull(time, "time provider");
this.backoffPolicyProvider = checkNotNull(backoffPolicyProvider, "backoffPolicyProvider");
this.subchannelPool = checkNotNull(subchannelPool, "subchannelPool");
this.subchannelPool.init(helper);
grpclbState =
new GrpclbState(helper, subchannelPool, time, backoffPolicyProvider);
recreateStates();
checkNotNull(grpclbState, "grpclbState");
}

@Override
Expand Down Expand Up @@ -97,7 +102,12 @@ public void handleResolvedAddressGroups(
newBackendServers = Collections.unmodifiableList(newBackendServers);
Map<String, Object> rawLbConfigValue = attributes.get(ATTR_LOAD_BALANCING_CONFIG);
Mode newMode = retrieveModeFromLbConfig(rawLbConfigValue, helper.getChannelLogger());
grpclbState.handleAddresses(newLbAddressGroups, newBackendServers, newMode);
if (!mode.equals(newMode)) {
mode = newMode;
helper.getChannelLogger().log(ChannelLogLevel.INFO, "Mode: " + newMode);
recreateStates();
}
grpclbState.handleAddresses(newLbAddressGroups, newBackendServers);
}

@VisibleForTesting
Expand Down Expand Up @@ -141,6 +151,12 @@ private void resetStates() {
}
}

private void recreateStates() {
resetStates();
checkState(grpclbState == null, "Should've been cleared");
grpclbState = new GrpclbState(mode, helper, subchannelPool, time, backoffPolicyProvider);
}

@Override
public void shutdown() {
resetStates();
Expand Down
Loading