Skip to content

Commit 90d80e0

Browse files
committed
Update ext-proc channel and grpc service on filter config updates.
1 parent 66a57e3 commit 90d80e0

3 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎xds/src/main/java/io/grpc/xds/ExternalProcessorFilter.java‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import io.grpc.ClientInterceptor;
2020
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
2121
import io.grpc.ForwardingClientCallListener;
22+
import io.grpc.ManagedChannel;
2223
import io.grpc.Metadata;
2324
import io.grpc.MethodDescriptor;
2425
import io.grpc.Status;
26+
import io.grpc.xds.internal.grpcservice.GrpcServiceChannelCreator;
27+
import io.grpc.xds.internal.grpcservice.GrpcServiceChannelCreatorImpl;
2528
import java.io.ByteArrayInputStream;
2629
import java.io.IOException;
2730
import java.io.InputStream;
@@ -33,8 +36,16 @@ public class ExternalProcessorFilter implements Filter {
3336
static final String TYPE_URL = "type.googleapis.com/envoy.extensions.filters.http.ext_proc.v3.ExternalProcessor";
3437

3538
final String filterInstanceName;
39+
// TODO: Make final after the need to replace with a mock from unit tests is removed.
40+
GrpcServiceChannelCreator grpcServiceChannelCreator;
41+
ManagedChannel grpcServiceChannel;
42+
ExternalProcessorGrpc.ExternalProcessorStub externalProcessorStub;
43+
private final Object lock = new Object();
44+
private GrpcService lastGrpcServiceConfig;
45+
3646
public ExternalProcessorFilter(String name) {
3747
filterInstanceName = checkNotNull(name, "name");
48+
grpcServiceChannelCreator = new GrpcServiceChannelCreatorImpl();
3849
}
3950

4051
static final class Provider implements Filter.Provider {
@@ -77,7 +88,26 @@ public ConfigOrError<? extends FilterConfig> parseFilterConfigOverride(Message r
7788
@Override
7889
public ClientInterceptor buildClientInterceptor(FilterConfig filterConfig,
7990
@Nullable FilterConfig overrideConfig, ScheduledExecutorService scheduler) {
80-
return new ExternalProcessorInterceptor((ExternalProcessorFilterConfig) filterConfig, overrideConfig, scheduler);
91+
return new ExternalProcessorInterceptor(this, (ExternalProcessorFilterConfig) filterConfig, overrideConfig, scheduler);
92+
}
93+
94+
ExternalProcessorGrpc.ExternalProcessorStub getExternalProcessorStub(ExternalProcessor config) {
95+
GrpcService newServiceConfig = config.getGrpcService();
96+
synchronized (lock) {
97+
// TODO: gRFC only mentions we should recreate channel if target or channel creds changed
98+
// but other fields in grpc service config also do seem relevant to warrant channel
99+
// recreation.
100+
if (grpcServiceChannel == null || !newServiceConfig.equals(lastGrpcServiceConfig)) {
101+
if (grpcServiceChannel != null) {
102+
// Shutdown the old channel if the config has changed
103+
grpcServiceChannel.shutdown();
104+
}
105+
grpcServiceChannel = grpcServiceChannelCreator.create(newServiceConfig);
106+
externalProcessorStub = ExternalProcessorGrpc.newStub(grpcServiceChannel);
107+
lastGrpcServiceConfig = newServiceConfig;
108+
}
109+
return externalProcessorStub;
110+
}
81111
}
82112

83113
static final class ExternalProcessorFilterConfig implements FilterConfig {
@@ -95,12 +125,15 @@ public String typeUrl() {
95125
}
96126

97127
static final class ExternalProcessorInterceptor implements ClientInterceptor {
128+
private final ExternalProcessorFilter filter;
98129
private final ExternalProcessorFilterConfig filterConfig;
99130
private final FilterConfig overrideConfig;
100131
private final ScheduledExecutorService scheduler;
101132

102-
ExternalProcessorInterceptor(ExternalProcessorFilterConfig filterConfig,
133+
ExternalProcessorInterceptor(ExternalProcessorFilter filter,
134+
ExternalProcessorFilterConfig filterConfig,
103135
@Nullable FilterConfig overrideConfig, ScheduledExecutorService scheduler) {
136+
this.filter = filter;
104137
this.filterConfig = filterConfig;
105138
this.overrideConfig = overrideConfig;
106139
this.scheduler = scheduler;
@@ -111,8 +144,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
111144
MethodDescriptor<ReqT, RespT> method,
112145
CallOptions callOptions,
113146
Channel next) {
114-
115-
ExternalProcessorGrpc.ExternalProcessorStub stub = getExternalProcessorStub(filterConfig.externalProcessor.getGrpcService());
147+
ExternalProcessorGrpc.ExternalProcessorStub stub = filter.getExternalProcessorStub(filterConfig.externalProcessor);
116148
ExternalProcessor config = filterConfig.externalProcessor;
117149
// Wrap the outgoing call to intercept client events
118150
return new ExtProcClientCall<>(next.newCall(method, callOptions), stub, method, config);
@@ -539,24 +571,5 @@ void unblockAfterStreamComplete() {
539571
}
540572
}
541573
}
542-
543-
@VisibleForTesting
544-
ExternalProcessorGrpc.ExternalProcessorStub getExternalProcessorStub(GrpcService service) {
545-
// TODO: Implement actual stub creation based on the GrpcService configuration.
546-
// This will likely involve creating a ManagedChannel and then a stub from it.
547-
// For now, returning null as a placeholder.
548-
//
549-
// This method needs to create a ManagedChannel based on the GrpcService configuration.
550-
// The GrpcService contains information like target URI, timeout, and optionally
551-
// a Google gRPC service config.
552-
// For a full implementation, you would typically use a ManagedChannelBuilder
553-
// to construct the channel and then create a stub from it.
554-
// Example (simplified, actual implementation would need more details from GrpcService):
555-
// ManagedChannel channel = ManagedChannelBuilder.forTarget(service.getEnvoyGrpc().getClusterName())
556-
// .usePlaintext() // Or use TLS based on configuration
557-
// .build();
558-
// return ExternalProcessorGrpc.newStub(channel);
559-
return null;
560-
}
561574
}
562575
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.grpc.xds.internal.grpcservice;
2+
3+
import io.envoyproxy.envoy.config.core.v3.GrpcService;
4+
import io.grpc.ManagedChannel;
5+
6+
// Interface exists so that unit tests can mock it.
7+
public interface GrpcServiceChannelCreator {
8+
ManagedChannel create(GrpcService grpcService);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.grpc.xds.internal.grpcservice;
2+
3+
import io.envoyproxy.envoy.config.core.v3.GrpcService;
4+
import io.grpc.ManagedChannel;
5+
6+
public final class GrpcServiceChannelCreatorImpl implements GrpcServiceChannelCreator {
7+
@Override
8+
public ManagedChannel create(GrpcService grpcService) {
9+
// TODO
10+
return null;
11+
}
12+
}

0 commit comments

Comments
 (0)