Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.beam.model.fnexecution.v1.BeamFnApi;
import org.apache.beam.model.pipeline.v1.Endpoints;
import org.apache.beam.sdk.fn.stream.OutboundObserverFactory;
import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ManagedChannel;
import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.Status;
import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.stub.StreamObserver;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class BeamFnDataGrpcMultiplexer implements AutoCloseable {
private final Endpoints.@Nullable ApiServiceDescriptor apiServiceDescriptor;
private final StreamObserver<BeamFnApi.Elements> inboundObserver;
private final StreamObserver<BeamFnApi.Elements> outboundObserver;
private final @Nullable ManagedChannel channel;
private final ConcurrentHashMap<
/*instructionId=*/ String, CompletableFuture<CloseableFnDataReceiver<BeamFnApi.Elements>>>
receivers;
Expand All @@ -73,7 +75,17 @@ public BeamFnDataGrpcMultiplexer(
OutboundObserverFactory outboundObserverFactory,
OutboundObserverFactory.BasicFactory<BeamFnApi.Elements, BeamFnApi.Elements>
baseOutboundObserverFactory) {
this(apiServiceDescriptor, outboundObserverFactory, baseOutboundObserverFactory, null);
}

public BeamFnDataGrpcMultiplexer(
Endpoints.@Nullable ApiServiceDescriptor apiServiceDescriptor,
OutboundObserverFactory outboundObserverFactory,
OutboundObserverFactory.BasicFactory<BeamFnApi.Elements, BeamFnApi.Elements>
baseOutboundObserverFactory,
@Nullable ManagedChannel channel) {
this.apiServiceDescriptor = apiServiceDescriptor;
this.channel = channel;
this.receivers = new ConcurrentHashMap<>();
this.poisonedInstructionIds =
CacheBuilder.newBuilder().expireAfterWrite(POISONED_INSTRUCTION_ID_CACHE_TIMEOUT).build();
Expand Down Expand Up @@ -194,6 +206,9 @@ public void close() throws Exception {
outboundObserver.onError(
Status.CANCELLED.withDescription("Multiplexer hanging up").asException());
inboundObserver.onCompleted();
if (channel != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I was hopping the leak could have resolved in #39390, where BeamFnDataGrpcMultiplexer is closed there. Turns out there are still missing piece here.

channel.shutdown();
}
if (exception != null) {
throw exception;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.beam.sdk.util.Sleeper;
import org.apache.beam.sdk.util.construction.Environments;
import org.apache.beam.sdk.util.construction.PipelineOptionsTranslation;
import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ManagedChannel;
import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.stub.StreamObserver;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Sets;
Expand Down Expand Up @@ -78,13 +79,18 @@ public void startWorker(
.withInterceptors(
ImmutableList.of(AddHarnessIdInterceptor.create(request.getWorkerId())));

ProvisionServiceGrpc.ProvisionServiceBlockingStub provisionStub =
ProvisionServiceGrpc.newBlockingStub(
channelFactory.forDescriptor(request.getProvisionEndpoint()));
ProvisionApi.ProvisionInfo provisionInfo =
provisionStub
.getProvisionInfo(ProvisionApi.GetProvisionInfoRequest.newBuilder().build())
.getInfo();
ManagedChannel channel = channelFactory.forDescriptor(request.getProvisionEndpoint());
ProvisionApi.ProvisionInfo provisionInfo;
try {
ProvisionServiceGrpc.ProvisionServiceBlockingStub provisionStub =
ProvisionServiceGrpc.newBlockingStub(channel);
provisionInfo =
provisionStub
.getProvisionInfo(ProvisionApi.GetProvisionInfoRequest.newBuilder().build())
.getInfo();
} finally {
channel.shutdown();
}

runnerCapabilites = Sets.newHashSet(provisionInfo.getRunnerCapabilitiesList());
if (provisionInfo.hasControlEndpoint()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ private BeamFnApi.ProcessBundleDescriptor loadDescriptor(String id) {
beamFnStatusClient.close();
}
processBundleHandler.shutdown();
if (channel != null) {
channel.shutdown();
}
} catch (Exception e) {
LOG.error("Shutting down harness due to exception", e);
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ private BeamFnDataGrpcMultiplexer getMultiplexer(
return multiplexerCache.computeIfAbsent(
key,
k -> {
ManagedChannel channel = channelFactory.apply(apiServiceDescriptor);
OutboundObserverFactory.BasicFactory<Elements, Elements> baseOutboundObserverFactory =
inboundObserver -> {
BeamFnDataGrpc.BeamFnDataStub stub =
BeamFnDataGrpc.newStub(channelFactory.apply(apiServiceDescriptor));
BeamFnDataGrpc.BeamFnDataStub stub = BeamFnDataGrpc.newStub(channel);
if (dataStreamId != null && !dataStreamId.isEmpty()) {
Metadata headers = new Metadata();
headers.put(
Expand All @@ -156,7 +156,7 @@ private BeamFnDataGrpcMultiplexer getMultiplexer(
return stub.data(inboundObserver);
};
return new BeamFnDataGrpcMultiplexer(
apiServiceDescriptor, outboundObserverFactory, baseOutboundObserverFactory);
apiServiceDescriptor, outboundObserverFactory, baseOutboundObserverFactory, channel);
});
}
}